# All files / src/parser index.js

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

    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

    24x
    24x
    24x
    24x
    24x
    24x

    180x
    180x
    180x

    180x

    180x

    180x
    2x

    178x
    2014x
    2012x
    1144x

    176x

     /**
     * 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 from '../utils/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;
      }

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

      return node;
    }