# All files / src/utils stream.js

100% Statements 15/15
88.89% Branches 8/9
100% Functions 7/7
100% Lines 15/15

    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

    115x
    115x
    115x
    115x

    55635x

    20051x

    20051x
    868x

    19183x

    20051x

    983x
    983x

    45780x

    24577x

    11007x

     // @flow

    // 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 (Stream.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
      static eol(char: string): boolean {
        return char === "\n";
      }

      // Is the character an end of file
      static 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;