src/Lexer/Input.js   A
last analyzed

Size

Lines of Code 81

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
nc 1
dl 0
loc 81
rs 10
noi 0

1 Function

Rating   Name   Duplication   Size   Complexity  
A Input.js ➔ ??? 0 22 1
1
'use strict';
2
3
/**
4
 * Representation of the input stream with some util functions. The class is used by the botlang
5
 * lexer which takes an instance of `Input` as a constructor argument and converts it into a
6
 * token stream.
7
 */
8
class Input {
9
  /**
10
   * Create an Input.
11
   * @param {String} input
12
   */
13
  constructor(input) {
14
    /**
15
     * @private
16
     * @type {Number}
17
     */
18
    this.column = 0;
19
    /**
20
     * @private
21
     * @type {String}
22
     */
23
    this.input = `${input}`;
24
    /**
25
     * @private
26
     * @type {Number}
27
     */
28
    this.line = 1;
29
    /**
30
     * @private
31
     * @type {Number}
32
     */
33
    this.position = 0;
34
  }
35
36
  /**
37
   * Determine whether or not there are no more values in the stream.
38
   * @return {Boolean}
39
   */
40
  eof() {
41
    return '' === this.peek();
42
  }
43
44
  /**
45
   * Throw a new Error.
46
   * @param  {String} message
47
   * @throws {Error}
48
   * @return {void}
49
   */
50
  error(message) {
51
    throw new Error(`${message} (Line: ${this.line}, Column: ${this.column})`);
52
  }
53
54
  /**
55
   * Return the next character from the input stream.
56
   * @return {String}
57
   */
58
  next() {
59
    const character = this.input.charAt(this.position += 1);
60
61
    // Keep track of the current column and line number
62
    if ('\n' === character) {
63
      this.column = 0;
64
      this.line += 1;
65
    } else {
66
      this.column += 1;
67
    }
68
69
    return character;
70
  }
71
72
  /**
73
   * Return the character from the current position without "consuming" it.
74
   * @return {String}
75
   */
76
  peek() {
77
    return this.input.charAt(this.position);
78
  }
79
}
80
81
export default Input;
82