# All files / src/parser index.js

100% Statements 19/19
100% Branches 6/6
100% Functions 1/1
100% Lines 19/19

    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

    22x
    22x
    22x
    22x
    22x
    22x

    88x
    88x
    88x

    88x

    88x

    88x
    2x

    86x

    86x
    300x
    298x
    225x

    84x

     /**
     * 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 Stream from "../utils/stream";
    import tokenStream from "../utils/token-stream";

    import type { NodeType } from "../flow/types";

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

      ctx.token = tokens.next();

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

      return node;
    }