# All files / src/parser expression.js

100% Statements 92/92
97.73% Branches 43/44
100% Functions 10/10
100% Lines 90/90

    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
    28x
    28x
    28x
    28x
    28x
    28x
    28x
    28x

    812x

    28x
    33x

    28x
    2854x

    28x
    700x
    700x

    700x
    700x

    700x

    700x
    76x
    76x
    48x
    48x

    700x
    563x
    563x

    125x

    700x
    1011x

    4x
    4x
    4x
    4x
    4x

    4x

    26x
    26x
    26x

    35x
    35x
    35x

    35x
    35x
    35x
    35x

    297x
    297x
    271x

    26x

    26x

    26x

    15x
    15x
    15x

    40x
    40x
    25x

    15x
    15x
    15x

    559x
    559x

    26x

    533x

    559x
    559x

    700x
    2155x

    400x
    400x

    552x

    552x
    552x

    29x
    29x

    162x
    162x

    1011x
    1011x
    300x

    711x

    1303x

    700x
    2155x
    1303x
    1303x

    700x
    390x

    700x

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

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

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

    export const predicate = (token: TokenType, depth: number): boolean =>
      token.value !== ";" && depth > 0;

    // 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 prev = last(operators);
        while (prev && prev.value !== condition) {
          consume();
          prev = last(operators);
        }
      };

      const flushOperators = precedence => {
        let previous = null;
        while (
          (previous = last(operators)) &&
          previous.Type !== Syntax.Sequence &&
          getPrecedence(previous) >= precedence &&
          getAssociativty(previous) === "left"
        ) {
          consume();
        }
      };

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

      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.Punctuator:
            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();
        }
      }

      while (operators.length) {
        consume();
      }

      // Should be a node
      return operands.pop();
    };

    export default expression;