# All files / src/parser expression.js

100% Statements 96/96
97.96% Branches 48/49
100% Functions 10/10
100% Lines 94/94

    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
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220

    26x
    26x
    26x
    26x
    26x
    26x
    26x
    26x

    1379x

    26x
    16x

    26x
    4628x

    26x

    26x
    1094x
    1094x

    1094x
    1094x

    1094x

    1094x
    117x
    117x
    93x
    93x

    1094x
    922x
    922x

    247x
    247x

    1094x
    1660x

    4x
    4x
    4x
    4x
    4x

    4x

    62x
    62x
    62x

    36x
    36x
    36x

    36x
    36x
    36x
    36x

    527x
    527x
    465x

    62x

    62x

    62x

    20x
    20x
    20x

    57x
    57x
    38x

    19x
    19x
    19x

    918x
    918x

    2x

    916x

    918x
    918x

    1094x
    3552x

    569x
    569x

    967x

    967x
    967x

    107x
    107x

    248x
    248x

    1661x

    1x

    1660x
    1660x
    507x

    1153x

    2078x

    1094x
    3552x
    2078x
    2078x

    1094x
    583x

    1094x

     // @flow
    /**
     * The expression parser for generating all parsed nodes, uses a modified Shunting
     * Yard algo.
     *
     * @author Arthur Buldauksas <arthurbuldauskas@gmail.com>
     */
    import Syntax from "../Syntax";
    import operator from "./operator";
    import constant from "./constant";
    import stringLiteral from "./string-literal";
    import builtInType from "./builtin-type";
    import block from "./block";
    import { getAssociativty, getPrecedence } from "./introspection";
    import maybeIdentifier from "./maybe-identifier";
    import type Context from "./context";
    import type { NodeType, TokenType } from "../flow/types";

    export type Predicate = (TokenType, number) => boolean;
    export type OperatorCheck = TokenType => boolean;

    // PLEASE READ BEFORE EDITING:
    //
    // 100% of the program is statements which are made up of expressions. The code
    // below is the "engine" to parsing just about everything(useful) in the syntax.
    // Take great care editing it.
    //
    // * Avoid special cases as much as possible.
    // * Leverage precednece and other Shunting Yard rules.
    // * Simplify whenever possible, avoid adding code.
    //
    // Thanks.

    const last = (list: any[]): any => list[list.length - 1];

    export const isPunctuatorAndNotBracket = (t: ?TokenType) =>
      t && t.type === Syntax.Punctuator && t.value !== "]" && t.value !== ")";

    // Because expressions can be anywhere and likely nested inside another expression
    // this nesting is represented with a depth. If we reach an "exit" like a ) or a }
    // and drop our depth below zero we know we have escaped our intended expression
    // and we bail out.
    export const predicate = (token: TokenType, depth: number): boolean =>
      token.value !== ";" && depth > 0;

    // Exceptions to no-keywords-in-expressions
    export const validKeywordsInExpressions = ["as"];

    // Shunting yard
    const expression = (ctx: Context, check: Predicate = predicate) => {
      const operators: TokenType[] = [];
      const operands: NodeType[] = [];

      // Depth is the nesting level of brackets in this expression. If we find a
      // closing bracket which causes our depth to fall below 1, then we know we
      // should exit the expression.
      let depth: number = 1;
      let previousToken = null;

      const consume = () => operands.push(operator(ctx, operators, operands));

      const eatUntil = condition => {
        let previous = last(operators);
        while (previous && previous.value !== condition) {
          consume();
          previous = last(operators);
        }
      };

      // The rules for consuming punctuators(+ - , etc.)
      const flushOperators = precedence => {
        let previous = last(operators);
        while (
          previous &&
          // A sequence is a special case. Note that this is a check for a Sequence NODE.
          // This is so that math operators don't "eat" already parsed sequences of nodes.
          // To put it plainly a comma separated list should never be added to a number.
          // Examples include code like: 1, 2, 3, 2 + 2.
          previous.Type !== Syntax.Sequence &&
          // The rest of this is Shunting Yard rules
          getPrecedence(previous) >= precedence &&
          getAssociativty(previous) === "left"
        ) {
          consume();
          previous = last(operators);
        }
      };

      // Process individual punctuators, below are the rules for handling things like
      // brackets and code blocks. Other punctuators follow a precedence rule parsing
      // approach.
      const processPunctuator = () => {
        switch (ctx.token.value) {
          case "=>":
            flushOperators(getPrecedence(ctx.token));
            operators.push(ctx.token);
            ctx.next();
            Eif (ctx.token.value === "{") {
              operands.push(block(ctx));
            }
            return false;
          case "(":
            depth++;
            operators.push(ctx.token);
            break;
          case "[":
            depth++;
            operators.push(ctx.token);
            break;
          case "]":
            depth--;
            eatUntil("[");
            consume();
            break;
          case ")": {
            depth--;
            if (depth < 1) {
              return false;
            }
            // If we are not in a group already find the last LBracket,
            // consume everything until that point
            eatUntil("(");
            // Pop left bracket
            operators.pop();

            break;
          }
          case "{":
            depth++;
            operators.push(ctx.token);
            break;
          case "}":
            depth--;
            if (depth < 1) {
              return false;
            }
            eatUntil("{");
            consume();
            break;
          default: {
            const token = (t => {
              if (
                (t.value === "-" && previousToken == null) ||
                (t.value === "-" && isPunctuatorAndNotBracket(previousToken))
              ) {
                return {
                  ...t,
                  value: "--",
                };
              }

              return t;
            })(ctx.token);

            flushOperators(getPrecedence(token));
            operators.push(token);
          }
        }
      };

      // Process individual tokens, this will either push to an operand stack or
      // process an operator.
      const process = () => {
        switch (ctx.token.type) {
          case Syntax.Constant:
            operands.push(constant(ctx));
            break;
          case Syntax.Identifier:
            previousToken = ctx.token;
            // Maybe an Identifier or a function call
            operands.push(maybeIdentifier(ctx));
            return false;
          case Syntax.StringLiteral:
            operands.push(stringLiteral(ctx));
            break;
          case Syntax.Type:
            operands.push(builtInType(ctx));
            break;
          case Syntax.Keyword:
          case Syntax.Punctuator:
            // Some special keywords may show up in expressions, but only a small
            // subset. These keywords are treated as punctuators and processed by
            // the overall punctuator rules
            // EXAMPLE: the 'as' keyword - import statements consist of a sequence of
            // expressions but the as keyword can be used to rename an import within.
            if (
              ctx.token.type === Syntax.Keyword &&
              !validKeywordsInExpressions.includes(ctx.token.value)
            ) {
              break;
            }
            const punctuatorResult = processPunctuator();
            if (punctuatorResult != null) {
              return punctuatorResult;
            }
            break;
        }

        return true;
      };

      while (ctx.token && check(ctx.token, depth)) {
        if (process()) {
          previousToken = ctx.token;
          ctx.next();
        }
      }

      // If we get to the end of our available tokens then proceed to eat any left over
      // operators and finalize the expression.
      while (operators.length) {
        consume();
      }

      // Last operand should be a node that is at the "root" of this expression
      return operands.pop();
    };

    export default expression;