# All files / src/utils stream.js

100% Statements 17/17
100% Branches 8/8
100% Functions 8/8
100% Lines 16/16

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    153276x

    237x
    237x
    237x
    237x

    287264x

    114014x

    114014x
    3377x

    110637x

    114014x

    3614x
    3614x

    114014x

    124366x

    48884x

     // @flow
    export const eol = (char: string) => char === '\n';

    // Base Character stream class
    class Stream {
      input: string;
      pos: number;
      line: number;
      col: number;
      lines: string[];

      constructor(input: string) {
        this.pos = this.line = this.col = 0;
        this.input = input;
        this.lines = input.split('\n');
        this.newLine();
      }

      // Peek at a character at current position
      peek(): string {
        return this.input.charAt(this.pos);
      }

      // Advance to next character in stream
      next(): string {
        const char = this.input.charAt(this.pos++);

        if (this.eol(char)) {
          this.newLine();
        } else {
          this.col++;
        }

        return char;
      }

      // Begin a new line
      newLine() {
        this.line++;
        this.col = 0;
      }

      // Is the character an end of line
      eol(char: string): boolean {
        return char === '\n';
      }

      // Is the character an end of file
      eof(char: string): boolean {
        return char === '';
      }

      // Is the charater a whitespace
      static whitespace(char: string): boolean {
        return (
          char === '\n' ||
          char === ' ' ||
          char === '\t' ||
          char === '\v' ||
          char === '\r' ||
          char === '\f'
        );
      }
    }

    export default Stream;