# All files / src/utils stream.js

97.3% Statements 36/37
100% Branches 10/10
92.86% Functions 13/14
100% Lines 34/34

    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
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    27x
    27x
    27x
    27x

    57776x

    27x
    2x

    2x
    2x
    2x

    4x

    2x

    2x

    2x
    7163x
    19345x

    3348x

    6695x

    9132x

    133x
    133x
    133x
    133x

    95241x

    35588x

    35588x
    1529x

    34059x

    35588x

    1662x
    1662x

    35588x

    43258x

    19446x

     // @flow
    import { link } from "walt-link";
    import path from "path";
    import { stringEncoder } from "./string";
    import {
      mapNode,
      walkNode,
      parser,
      semantics,
      validate,
      emitter,
      generator,
      prettyPrintNode,
    } from "..";

    declare var WebAssembly: any;
    export const eol = (char: string) => char === "\n";

    export const async = (input: string) => {
      const buildStream = link(
        path.resolve(__dirname, "../walt/utils/stream.walt"),
        {},
        {
          mapNode,
          walkNode,
          parser,
          semantics,
          validate,
          emitter,
          generator,
          prettyPrintNode,
        }
      );
      const memory = new WebAssembly.Memory({ initial: 1 });
      const stringStream = stringEncoder(input);
      stringStream.buffer(memory.buffer);

      const withText = text => i => console.log(text, i);

      return buildStream({
        env: {
          memory,
          STRING_BYTE_LENGTH: stringStream.size,
          log: withText("logger - "),
          start: withText("start - "),
        },
      }).then(module => {
        const {
          _next,
          _peek,
          _column,
          _line,
          whitespace,
        } = module.instance.exports;

        return {
          next: () => String.fromCodePoint(_next()),
          peek: () => String.fromCodePoint(_peek()),
          get col() {
            return _column();
          },
          get line() {
            return _line();
          },
          eof(char) {
            return char === "" || char === "\0";
          },
          lines: input.split("\n"),
          whitespace,
        };
      });
    };

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