# 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
    27x
    27x
    27x
    27x
    27x
    27x
    27x
    27x

    1957x

    27x
    22x

    27x
    6124x

    27x
    1400x
    1400x

    1400x
    1400x

    1400x

    1400x
    167x
    167x
    155x
    155x

    1400x
    1260x
    1260x

    375x

    1400x
    2232x

    4x
    4x
    4x
    4x
    4x

    4x

    106x
    106x
    106x

    36x
    36x
    36x

    36x
    36x
    36x
    36x

    689x
    689x
    583x

    106x

    106x

    106x

    28x
    28x
    28x

    77x
    77x
    52x

    25x
    25x
    25x

    1256x
    1256x

    2x

    1254x

    1256x
    1256x

    1400x
    4746x

    669x
    669x

    1385x

    1385x
    1385x

    135x
    135x

    324x
    324x

    2232x
    2232x
    639x

    1593x

    2722x

    1400x
    4746x
    2722x
    2722x

    1400x
    733x

    1400x

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