spreadsheet
typeScript/javascript spreadsheet parser, with formulas.
git clone https://git.vogt.world/spreadsheet.git
Log | Files | README.md
← Commit log
commit
message
[Parser] using ReductionPair in PRODUCTIONS for clarity
author
Ben Vogt <[email protected]>
date
2017-08-26 19:11:50
stats
1 file(s) changed, 80 insertions(+), 59 deletions(-)
files
src/Parser/Parser.ts
  1diff --git a/src/Parser/Parser.ts b/src/Parser/Parser.ts
  2index 2147165..10d49fa 100644
  3--- a/src/Parser/Parser.ts
  4+++ b/src/Parser/Parser.ts
  5@@ -100,55 +100,83 @@ const enum ReduceActions {
  6   REDUCE_LAST_THREE_B
  7 }
  8 
  9+/**
 10+ * Represents the length to reduce the stack by, and the token index value that will replace those tokens in the stack.
 11+ */
 12+class ReductionPair {
 13+  private lengthToReduceStackBy : number;
 14+  private replacementTokenIndex : number;
 15+  constructor(replacementTokenIndex : number, length : number) {
 16+    this.lengthToReduceStackBy = length;
 17+    this.replacementTokenIndex = replacementTokenIndex;
 18+  }
 19+
 20+  /**
 21+   * Get the number representing the length to reduce the stack by.
 22+   * @returns {number}
 23+   */
 24+  getLengthToReduceStackBy() : number {
 25+    return this.lengthToReduceStackBy;
 26+  }
 27+
 28+  /**
 29+   * Get the replacement token index.
 30+   * @returns {number}
 31+   */
 32+  getReplacementTokenIndex() : number {
 33+    return this.replacementTokenIndex;
 34+  }
 35+}
 36+
 37 /**
 38  * Productions is used to look up both the number to use when reducing the stack (productions[x][1]) and the semantic
 39  * value that will replace the tokens in the stack (productions[x][0]).
 40- * @type {Array}
 41+ * @type {Array<ReductionPair>}
 42  */
 43-let productions = [];
 44-productions[ReduceActions.NO_ACTION] = 0;
 45-productions[ReduceActions.RETURN_LAST] = [3, 2];
 46-productions[ReduceActions.CALL_VARIABLE] = [4, 1];
 47-productions[ReduceActions.TIME_CALL_TRUE] = [4, 1];
 48-productions[ReduceActions.TIME_CALL] = [4, 1];
 49-productions[ReduceActions.AS_NUMBER] = [4, 1];
 50-productions[ReduceActions.AS_STRING] = [4, 1];
 51-productions[ReduceActions.AMPERSAND] = [4, 3];
 52-productions[ReduceActions.EQUALS] = [4, 3];
 53-productions[ReduceActions.PLUS] = [4, 3];
 54-productions[ReduceActions.LAST_NUMBER] = [4, 3];
 55-productions[ReduceActions.LTE] = [4, 4];
 56-productions[ReduceActions.GTE] = [4, 4];
 57-productions[ReduceActions.NOT_EQ] = [4, 4];
 58-productions[ReduceActions.NOT] = [4, 3];
 59-productions[ReduceActions.GT] = [4, 3];
 60-productions[ReduceActions.LT] = [4, 3];
 61-productions[ReduceActions.MINUS] = [4, 3];
 62-productions[ReduceActions.MULTIPLY] = [4, 3];
 63-productions[ReduceActions.DIVIDE] = [4, 3];
 64-productions[ReduceActions.TO_POWER] = [4, 3];
 65-productions[ReduceActions.INVERT_NUM] = [4, 2];
 66-productions[ReduceActions.TO_NUMBER_NAN_AS_ZERO] = [4, 2];
 67-productions[ReduceActions.CALL_FUNCTION_LAST_BLANK] = [4, 3];
 68-productions[ReduceActions.CALL_FUNCTION_LAST_TWO_IN_STACK] = [4, 4];
 69-productions[ReduceActions.I25] = [4, 1];
 70-productions[ReduceActions.I26] = [4, 1];
 71-productions[ReduceActions.I27] = [4, 2];
 72-productions[ReduceActions.FIXED_CELL_VAL] = [25, 1];
 73-productions[ReduceActions.FIXED_CELL_RANGE_VAL] = [25, 3];
 74-productions[ReduceActions.CELL_VALUE] = [25, 1];
 75-productions[ReduceActions.CELL_RANGE_VALUE] = [25, 3];
 76-productions[ReduceActions.ENSURE_IS_ARRAY] = [24, 1];
 77-productions[ReduceActions.ENSURE_YYTEXT_ARRAY] = [24, 1];
 78-productions[ReduceActions.REDUCE_INT] = [24, 3];
 79-productions[ReduceActions.REDUCE_PERCENT] = [24, 3];
 80-productions[ReduceActions.WRAP_CURRENT_INDEX_TOKEN_AS_ARRAY] = [6, 1];
 81-productions[ReduceActions.ENSURE_LAST_TWO_IN_ARRAY_AND_PUSH] = [6, 3];
 82-productions[ReduceActions.REFLEXIVE_REDUCE] = [9, 1];
 83-productions[ReduceActions.REDUCE_FLOAT] = [9, 3];
 84-productions[ReduceActions.REDUCE_PREV_AS_PERCENT] = [9, 2];
 85-productions[ReduceActions.REDUCE_LAST_THREE_A] = [2, 3];
 86-productions[ReduceActions.REDUCE_LAST_THREE_B] = [2, 4];
 87+let productions : Array<ReductionPair> = [];
 88+productions[ReduceActions.NO_ACTION] = null;
 89+productions[ReduceActions.RETURN_LAST] = new ReductionPair(3, 2);
 90+productions[ReduceActions.CALL_VARIABLE] = new ReductionPair(4, 1);
 91+productions[ReduceActions.TIME_CALL_TRUE] = new ReductionPair(4, 1);
 92+productions[ReduceActions.TIME_CALL] = new ReductionPair(4, 1);
 93+productions[ReduceActions.AS_NUMBER] = new ReductionPair(4, 1);
 94+productions[ReduceActions.AS_STRING] = new ReductionPair(4, 1);
 95+productions[ReduceActions.AMPERSAND] = new ReductionPair(4, 3);
 96+productions[ReduceActions.EQUALS] = new ReductionPair(4, 3);
 97+productions[ReduceActions.PLUS] = new ReductionPair(4, 3);
 98+productions[ReduceActions.LAST_NUMBER] = new ReductionPair(4, 3);
 99+productions[ReduceActions.LTE] = new ReductionPair(4, 4);
100+productions[ReduceActions.GTE] = new ReductionPair(4, 4);
101+productions[ReduceActions.NOT_EQ] = new ReductionPair(4, 4);
102+productions[ReduceActions.NOT] = new ReductionPair(4, 3);
103+productions[ReduceActions.GT] = new ReductionPair(4, 3);
104+productions[ReduceActions.LT] = new ReductionPair(4, 3);
105+productions[ReduceActions.MINUS] = new ReductionPair(4, 3);
106+productions[ReduceActions.MULTIPLY] = new ReductionPair(4, 3);
107+productions[ReduceActions.DIVIDE] = new ReductionPair(4, 3);
108+productions[ReduceActions.TO_POWER] = new ReductionPair(4, 3);
109+productions[ReduceActions.INVERT_NUM] = new ReductionPair(4, 2);
110+productions[ReduceActions.TO_NUMBER_NAN_AS_ZERO] = new ReductionPair(4, 2);
111+productions[ReduceActions.CALL_FUNCTION_LAST_BLANK] = new ReductionPair(4, 3);
112+productions[ReduceActions.CALL_FUNCTION_LAST_TWO_IN_STACK] = new ReductionPair(4, 4);
113+productions[ReduceActions.I25] = new ReductionPair(4, 1);
114+productions[ReduceActions.I26] = new ReductionPair(4, 1);
115+productions[ReduceActions.I27] = new ReductionPair(4, 2);
116+productions[ReduceActions.FIXED_CELL_VAL] = new ReductionPair(25, 1);
117+productions[ReduceActions.FIXED_CELL_RANGE_VAL] = new ReductionPair(25, 3);
118+productions[ReduceActions.CELL_VALUE] = new ReductionPair(25, 1);
119+productions[ReduceActions.CELL_RANGE_VALUE] = new ReductionPair(25, 3);
120+productions[ReduceActions.ENSURE_IS_ARRAY] = new ReductionPair(24, 1);
121+productions[ReduceActions.ENSURE_YYTEXT_ARRAY] = new ReductionPair(24, 1);
122+productions[ReduceActions.REDUCE_INT] = new ReductionPair(24, 3);
123+productions[ReduceActions.REDUCE_PERCENT] = new ReductionPair(24, 3);
124+productions[ReduceActions.WRAP_CURRENT_INDEX_TOKEN_AS_ARRAY] = new ReductionPair(6, 1);
125+productions[ReduceActions.ENSURE_LAST_TWO_IN_ARRAY_AND_PUSH] = new ReductionPair(6, 3);
126+productions[ReduceActions.REFLEXIVE_REDUCE] = new ReductionPair(9, 1);
127+productions[ReduceActions.REDUCE_FLOAT] = new ReductionPair(9, 3);
128+productions[ReduceActions.REDUCE_PREV_AS_PERCENT] = new ReductionPair(9, 2);
129+productions[ReduceActions.REDUCE_LAST_THREE_A] = new ReductionPair(2, 3);
130+productions[ReduceActions.REDUCE_LAST_THREE_B] = new ReductionPair(2, 4);
131 const PRODUCTIONS = productions;
132 
133 /**
134@@ -1344,9 +1372,9 @@ let Parser = (function () {
135             break;
136 
137           case LexActions.REDUCE: // Reduce
138-            let currentProduction = this.productions[action[1]];
139+            let currentProduction : ReductionPair = this.productions[action[1]];
140 
141-            let lengthToReduceStackBy = currentProduction[1];
142+            let lengthToReduceStackBy = currentProduction.getLengthToReduceStackBy();
143 
144             // perform semantic action
145             yyval.$ = semanticValueStack[semanticValueStack.length - lengthToReduceStackBy]; // default to $$ = $1
146@@ -1374,7 +1402,7 @@ let Parser = (function () {
147             }
148 
149             // push non-terminal (reduce)
150-            stack.push(currentProduction[0]);
151+            stack.push(currentProduction.getReplacementTokenIndex());
152             semanticValueStack.push(yyval.$);
153             locationStack.push(yyval._$);
154             newState = table[stack[stack.length - 2]][stack[stack.length - 1]];
155@@ -1694,30 +1722,12 @@ let Parser = (function () {
156         }
157       },
158 
159-      // return the currently active lexer condition state; when an index argument is provided it produces the N-th previous condition state, if available
160-      topState: function topState(n) {
161-        n = this.conditionStack.length - 1 - Math.abs(n || 0);
162-        if (n >= 0) {
163-          return this.conditionStack[n];
164-        } else {
165-          return "INITIAL";
166-        }
167-      },
168-
169-      // alias for begin(condition)
170-      pushState: function pushState(condition) {
171-        this.begin(condition);
172-      },
173-
174-      // return the number of states currently on the stack
175-      stateStackSize: function stateStackSize() {
176-        return this.conditionStack.length;
177-      },
178       options: {
179         // backtrack_lexer?
180         // ranges?
181         // flex?
182       },
183+
184       mapActionToActionIndex: function (ruleIndex) {
185         switch (ruleIndex) {
186           case WHITE_SPACE_RULE_INDEX: