# All files / src/parser index.js

96.77% Statements 30/31
91.67% Branches 11/12
100% Functions 3/3
96.77% Lines 30/31

    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

    27x
    27x
    27x
    27x
    27x
    27x

    27x
    1x
    1x
    1x
    1x

    1x

    1x

    1x
    55x
    55x
    36x

    1x

    96x
    96x
    96x

    96x

    96x

    96x
    2x

    94x
    434x
    432x
    317x

    92x

     /**
     * Syntax Analysis
     *
     * The parser below creates the "bare" Abstract Syntax Tree.
     */

    // @flow
    import Syntax from "../Syntax";
    import statement from "./statement";
    import Context from "./context";
    import Tokenizer from "../tokenizer";
    import tokenStream from "../utils/token-stream";
    import Stream, { async as asyncStream } from "../utils/stream";
    import type { NodeType } from "../flow/types";

    export const async = (source: string): Promise<NodeType> => {
      return asyncStream(source).then(stream => {
        const tokenizer = new Tokenizer(stream);
        const tokens = tokenStream(tokenizer.parse());
        const ctx = new Context({
          stream: tokens,
          token: tokens.tokens[0],
          lines: stream.lines,
          filename: "unknown.walt",
        });

        const node: NodeType = ctx.makeNode(
          {
            value: "ROOT_NODE",
          },
          Syntax.Program
        );

        // No code, no problem, empty ast equals
        // (module) ; the most basic wasm module
        Iif (!ctx.stream || !ctx.stream.length) {
          return node;
        }

        while (ctx.stream.peek()) {
          const child = statement(ctx);
          if (child) {
            node.params.push(child);
          }
        }

        return node;
      });
    };

    export default function parse(source: string): NodeType {
      const stream = new Stream(source);
      const tokenizer = new Tokenizer(stream);
      const tokens = tokenStream(tokenizer.parse());

      const ctx = new Context({
        stream: tokens,
        token: tokens.tokens[0],
        lines: stream.lines,
        filename: "unknown.walt",
      });

      const node: NodeType = ctx.makeNode(
        {
          value: "ROOT_NODE",
        },
        Syntax.Program
      );

      // No code, no problem, empty ast equals
      // (module) ; the most basic wasm module
      if (!ctx.stream || !ctx.stream.length) {
        return node;
      }

      while (ctx.stream.peek()) {
        const child = statement(ctx);
        if (child) {
          node.params.push(child);
        }
      }

      return node;
    }