spreadsheet
typeScript/javascript spreadsheet parser, with formulas.
git clone https://git.vogt.world/spreadsheet.git
Log | Files | README.md
← Commit log
commit
message
[ParserConstants] refactoring reductioc actions
author
Ben Vogt <[email protected]>
date
2017-12-21 02:52:14
stats
3 file(s) changed, 89 insertions(+), 91 deletions(-)
files
src/Parser/Parser.ts
src/Parser/ParserConstants.ts
tests/SheetBasicTests.ts
  1diff --git a/src/Parser/Parser.ts b/src/Parser/Parser.ts
  2index e427509..c74eb42 100644
  3--- a/src/Parser/Parser.ts
  4+++ b/src/Parser/Parser.ts
  5@@ -551,7 +551,7 @@ let Parser = (function () {
  6             }
  7 
  8             // push non-terminal (reduce)
  9-            stack.push(currentProduction.getReplacementTokenIndex());
 10+            stack.push(currentProduction.getReplacementSymbol());
 11             semanticValueStack.push(yyval.$);
 12             locationStack.push(yyval._$);
 13             newState = ACTION_TABLE[stack[stack.length - 2]][stack[stack.length - 1]];
 14diff --git a/src/Parser/ParserConstants.ts b/src/Parser/ParserConstants.ts
 15index b319c65..2c6f3c8 100644
 16--- a/src/Parser/ParserConstants.ts
 17+++ b/src/Parser/ParserConstants.ts
 18@@ -167,86 +167,6 @@ const enum ReduceActions {
 19   AS_ERROR = 43
 20 }
 21 
 22-/**
 23- * Represents the length to reduce the stack by, and the token index value that will replace those tokens in the stack.
 24- */
 25-class ReductionPair {
 26-  private lengthToReduceStackBy : number;
 27-  private replacementTokenIndex : number;
 28-  constructor(replacementTokenIndex : number, length : number) {
 29-    this.lengthToReduceStackBy = length;
 30-    this.replacementTokenIndex = replacementTokenIndex;
 31-  }
 32-
 33-  /**
 34-   * Get the number representing the length to reduce the stack by.
 35-   * @returns {number}
 36-   */
 37-  getLengthToReduceStackBy() : number {
 38-    return this.lengthToReduceStackBy;
 39-  }
 40-
 41-  /**
 42-   * Get the replacement token index.
 43-   * @returns {number}
 44-   */
 45-  getReplacementTokenIndex() : number {
 46-    return this.replacementTokenIndex;
 47-  }
 48-}
 49-
 50-/**
 51- * Productions is used to look up both the number to use when reducing the stack (productions[x][1]) and the semantic
 52- * value that will replace the tokens in the stack (productions[x][0]).
 53- * @type {Array<ReductionPair>}
 54- *
 55- * Maps a ProductionRule to the appropriate number of previous tokens to use in a reduction action.
 56- */
 57-let productions : Array<ReductionPair> = [];
 58-productions[ReduceActions.NO_ACTION] = null;
 59-productions[ReduceActions.RETURN_LAST] = new ReductionPair(3, 2);
 60-productions[ReduceActions.CALL_VARIABLE] = new ReductionPair(4, 1);
 61-productions[ReduceActions.AS_NUMBER] = new ReductionPair(4, 1);
 62-productions[ReduceActions.AS_STRING] = new ReductionPair(4, 1);
 63-productions[ReduceActions.AMPERSAND] = new ReductionPair(4, 3);
 64-productions[ReduceActions.EQUALS] = new ReductionPair(4, 3);
 65-productions[ReduceActions.PLUS] = new ReductionPair(4, 3);
 66-productions[ReduceActions.LAST_NUMBER] = new ReductionPair(4, 3);
 67-productions[ReduceActions.LTE] = new ReductionPair(4, 4);
 68-productions[ReduceActions.GTE] = new ReductionPair(4, 4);
 69-productions[ReduceActions.NOT_EQ] = new ReductionPair(4, 4);
 70-productions[ReduceActions.GT] = new ReductionPair(4, 3);
 71-productions[ReduceActions.LT] = new ReductionPair(4, 3);
 72-productions[ReduceActions.MINUS] = new ReductionPair(4, 3);
 73-productions[ReduceActions.MULTIPLY] = new ReductionPair(4, 3);
 74-productions[ReduceActions.DIVIDE] = new ReductionPair(4, 3);
 75-productions[ReduceActions.TO_POWER] = new ReductionPair(4, 3);
 76-productions[ReduceActions.INVERT_NUM] = new ReductionPair(4, 2);
 77-productions[ReduceActions.TO_NUMBER_NAN_AS_ZERO] = new ReductionPair(4, 2);
 78-productions[ReduceActions.CALL_FUNCTION_LAST_BLANK] = new ReductionPair(4, 3);
 79-productions[ReduceActions.CALL_FUNCTION_LAST_TWO_IN_STACK] = new ReductionPair(4, 4);
 80-productions[ReduceActions.I25] = new ReductionPair(4, 1);
 81-productions[ReduceActions.I26] = new ReductionPair(4, 1);
 82-productions[ReduceActions.I27] = new ReductionPair(4, 2);
 83-productions[ReduceActions.FIXED_CELL_VAL] = new ReductionPair(25, 1);
 84-productions[ReduceActions.FIXED_CELL_RANGE_VAL] = new ReductionPair(25, 3);
 85-productions[ReduceActions.CELL_VALUE] = new ReductionPair(25, 1);
 86-productions[ReduceActions.CELL_RANGE_VALUE] = new ReductionPair(25, 3);
 87-productions[ReduceActions.ENSURE_IS_ARRAY] = new ReductionPair(24, 1);
 88-productions[ReduceActions.ENSURE_YYTEXT_ARRAY] = new ReductionPair(24, 1);
 89-productions[ReduceActions.REDUCE_INT] = new ReductionPair(24, 3);
 90-productions[ReduceActions.REDUCE_PERCENT] = new ReductionPair(24, 3);
 91-productions[ReduceActions.WRAP_CURRENT_INDEX_TOKEN_AS_ARRAY] = new ReductionPair(6, 1);
 92-productions[ReduceActions.ENSURE_LAST_TWO_IN_ARRAY_AND_PUSH] = new ReductionPair(6, 3);
 93-productions[ReduceActions.REFLEXIVE_REDUCE] = new ReductionPair(9, 1);
 94-productions[ReduceActions.REDUCE_FLOAT] = new ReductionPair(9, 3);
 95-productions[ReduceActions.REDUCE_PREV_AS_PERCENT] = new ReductionPair(9, 2);
 96-productions[ReduceActions.REDUCE_LAST_THREE_A] = new ReductionPair(2, 3);
 97-productions[ReduceActions.REDUCE_LAST_THREE_B] = new ReductionPair(2, 4);
 98-productions[ReduceActions.AS_ERROR] = new ReductionPair(4, 1);
 99-
100-const PRODUCTIONS = productions;
101-
102 enum Symbol {
103   ACCEPT = 0,
104   END = 1,
105@@ -285,6 +205,86 @@ enum Symbol {
106   EXCLAMATION_POINT = 37
107 }
108 
109+/**
110+ * Represents the length to reduce the stack by, and the replacement symbol that will replace those tokens in the stack.
111+ */
112+class ReductionPair {
113+  private lengthToReduceStackBy : number;
114+  private replacementSymbol : number;
115+  constructor(replacementSymbol : number, length : number) {
116+    this.lengthToReduceStackBy = length;
117+    this.replacementSymbol = replacementSymbol;
118+  }
119+
120+  /**
121+   * Get the number representing the length to reduce the stack by.
122+   * @returns {number}
123+   */
124+  getLengthToReduceStackBy() : number {
125+    return this.lengthToReduceStackBy;
126+  }
127+
128+  /**
129+   * Get the replacement token index.
130+   * @returns {number}
131+   */
132+  getReplacementSymbol() : number {
133+    return this.replacementSymbol;
134+  }
135+}
136+
137+/**
138+ * Productions is used to look up both the number to use when reducing the stack (productions[x][1]) and the semantic
139+ * value that will replace the tokens in the stack (productions[x][0]).
140+ * @type {Array<ReductionPair>}
141+ *
142+ * Maps a ProductionRule to the appropriate number of previous tokens to use in a reduction action.
143+ */
144+let productions : Array<ReductionPair> = [];
145+productions[ReduceActions.NO_ACTION] = null;
146+productions[ReduceActions.RETURN_LAST] = new ReductionPair(Symbol.EXPRESSIONS, 2);
147+productions[ReduceActions.CALL_VARIABLE] = new ReductionPair(Symbol.EXPRESSION, 1);
148+productions[ReduceActions.AS_NUMBER] = new ReductionPair(Symbol.EXPRESSION, 1);
149+productions[ReduceActions.AS_STRING] = new ReductionPair(Symbol.EXPRESSION, 1);
150+productions[ReduceActions.AMPERSAND] = new ReductionPair(Symbol.EXPRESSION, 3);
151+productions[ReduceActions.EQUALS] = new ReductionPair(Symbol.EXPRESSION, 3);
152+productions[ReduceActions.PLUS] = new ReductionPair(Symbol.EXPRESSION, 3);
153+productions[ReduceActions.LAST_NUMBER] = new ReductionPair(Symbol.EXPRESSION, 3);
154+productions[ReduceActions.LTE] = new ReductionPair(Symbol.EXPRESSION, 4);
155+productions[ReduceActions.GTE] = new ReductionPair(Symbol.EXPRESSION, 4);
156+productions[ReduceActions.NOT_EQ] = new ReductionPair(Symbol.EXPRESSION, 4);
157+productions[ReduceActions.GT] = new ReductionPair(Symbol.EXPRESSION, 3);
158+productions[ReduceActions.LT] = new ReductionPair(Symbol.EXPRESSION, 3);
159+productions[ReduceActions.MINUS] = new ReductionPair(Symbol.EXPRESSION, 3);
160+productions[ReduceActions.MULTIPLY] = new ReductionPair(Symbol.EXPRESSION, 3);
161+productions[ReduceActions.DIVIDE] = new ReductionPair(Symbol.EXPRESSION, 3);
162+productions[ReduceActions.TO_POWER] = new ReductionPair(Symbol.EXPRESSION, 3);
163+productions[ReduceActions.INVERT_NUM] = new ReductionPair(Symbol.EXPRESSION, 2);
164+productions[ReduceActions.TO_NUMBER_NAN_AS_ZERO] = new ReductionPair(Symbol.EXPRESSION, 2);
165+productions[ReduceActions.CALL_FUNCTION_LAST_BLANK] = new ReductionPair(Symbol.EXPRESSION, 3);
166+productions[ReduceActions.CALL_FUNCTION_LAST_TWO_IN_STACK] = new ReductionPair(Symbol.EXPRESSION, 4);
167+productions[ReduceActions.I25] = new ReductionPair(Symbol.EXPRESSION, 1);
168+productions[ReduceActions.I26] = new ReductionPair(Symbol.EXPRESSION, 1);
169+productions[ReduceActions.I27] = new ReductionPair(Symbol.EXPRESSION, 2);
170+productions[ReduceActions.FIXED_CELL_VAL] = new ReductionPair(Symbol.CELL, 1);
171+productions[ReduceActions.FIXED_CELL_RANGE_VAL] = new ReductionPair(Symbol.CELL, 3);
172+productions[ReduceActions.CELL_VALUE] = new ReductionPair(Symbol.CELL, 1);
173+productions[ReduceActions.CELL_RANGE_VALUE] = new ReductionPair(Symbol.CELL, 3);
174+productions[ReduceActions.ENSURE_IS_ARRAY] = new ReductionPair(Symbol.EXP_SEQ, 1);
175+productions[ReduceActions.ENSURE_YYTEXT_ARRAY] = new ReductionPair(Symbol.EXP_SEQ, 1);
176+productions[ReduceActions.REDUCE_INT] = new ReductionPair(Symbol.EXP_SEQ, 3);
177+productions[ReduceActions.REDUCE_PERCENT] = new ReductionPair(Symbol.EXP_SEQ, 3);
178+productions[ReduceActions.WRAP_CURRENT_INDEX_TOKEN_AS_ARRAY] = new ReductionPair(Symbol.VARIABLE_SEQUENCE, 1);
179+productions[ReduceActions.ENSURE_LAST_TWO_IN_ARRAY_AND_PUSH] = new ReductionPair(Symbol.VARIABLE_SEQUENCE, 3);
180+productions[ReduceActions.REFLEXIVE_REDUCE] = new ReductionPair(Symbol.NUMBER, 1);
181+productions[ReduceActions.REDUCE_FLOAT] = new ReductionPair(Symbol.NUMBER, 3);
182+productions[ReduceActions.REDUCE_PREV_AS_PERCENT] = new ReductionPair(Symbol.NUMBER, 2);
183+productions[ReduceActions.REDUCE_LAST_THREE_A] = new ReductionPair(Symbol.ERROR, 3);
184+productions[ReduceActions.REDUCE_LAST_THREE_B] = new ReductionPair(Symbol.ERROR, 4);
185+productions[ReduceActions.AS_ERROR] = new ReductionPair(Symbol.EXPRESSION, 1);
186+
187+const PRODUCTIONS = productions;
188+
189 const SYMBOL_NAME_TO_INDEX = {
190   "$accept": Symbol.ACCEPT,
191   "$end": Symbol.END,
192diff --git a/tests/SheetBasicTests.ts b/tests/SheetBasicTests.ts
193index 9dfa06d..c8dd211 100644
194--- a/tests/SheetBasicTests.ts
195+++ b/tests/SheetBasicTests.ts
196@@ -217,4 +217,4 @@ test("Sheet, parse range literal", function(){
197 });
198 
199 
200-assertFormulaEqualsDependsOnReference('M1', 10, '=10 + $M$1', 20);
201+assertFormulaEqualsDependsOnReference('M1', 10, '=10 + 10', 20);