# All files / src/utils stream.js

100% Statements 15/15
100% Branches 8/8
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

    117x
    117x
    117x
    117x

    77691x

    28460x

    28460x
    1135x

    27325x

    28460x

    1252x
    1252x

    64964x

    35361x

    13870x

     // @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;