spreadsheet
typeScript/javascript spreadsheet parser, with formulas.
git clone https://git.vogt.world/spreadsheet.git
Log | Files | README.md
← Commit log
commit
message
[Parser] naming ReduceActions enums
author
Ben Vogt <[email protected]>
date
2017-08-20 16:35:49
stats
1 file(s) changed, 138 insertions(+), 128 deletions(-)
files
src/Parser/Parser.ts
  1diff --git a/src/Parser/Parser.ts b/src/Parser/Parser.ts
  2index c4ed676..0e69afa 100644
  3--- a/src/Parser/Parser.ts
  4+++ b/src/Parser/Parser.ts
  5@@ -60,44 +60,44 @@ const enum ReduceActions {
  6   I2,
  7   I3,
  8   I4,
  9-  I5,
 10-  I6,
 11-  I7,
 12-  I8,
 13-  I9,
 14-  I10,
 15-  I11,
 16-  I12,
 17-  I13,
 18-  I14,
 19-  I15,
 20-  I16,
 21-  I17,
 22-  I18,
 23-  I19,
 24-  I20,
 25-  I21,
 26-  I22,
 27-  I23,
 28-  I24,
 29+  AS_NUMBER,
 30+  AS_STRING,
 31+  AMPERSAND,
 32+  EQUALS,
 33+  PLUS,
 34+  LAST_NUMBER,
 35+  LTE,
 36+  GTE,
 37+  NOT_EQ,
 38+  NOT,
 39+  GT,
 40+  LT,
 41+  MINUS,
 42+  MULTIPLY,
 43+  DIVIDE,
 44+  TO_POWER,
 45+  INVERT_NUM,
 46+  TO_NUMBER_NAN_AS_ZERO,
 47+  CALL_FUNCTION_LAST_BLANK,
 48+  CALL_FUNCTION_LAST_TWO_IN_STACK,
 49   I25,
 50   I26,
 51   I27,
 52-  I28,
 53-  I29,
 54-  I30,
 55-  I31,
 56-  I32,
 57-  I33,
 58-  I34,
 59-  I35,
 60-  I36,
 61-  I37,
 62-  I38,
 63-  I39,
 64-  I40,
 65-  I41,
 66-  I42
 67+  FIXED_CELL_VAL,
 68+  FIX_EDCELL_RANGE_VAL,
 69+  CELL_VALUE,
 70+  CELL_RANGE_VALUE,
 71+  ENSURE_IS_ARRAY,
 72+  ENSURE_YYTEXT_ARRAY,
 73+  REDUCE_INT,
 74+  REDUCE_PERCENT,
 75+  WRAP_CURRENT_INDEX_TOKEN_AS_ARRAY,
 76+  ENSURE_LAST_TWO_IN_ARRAY_AND_PUSH,
 77+  REFLEXIVE_REDUCE,
 78+  REDUCE_FLOAT,
 79+  REUDCE_PREV_AS_PERCENT,
 80+  REDUCE_LAST_THREE_A,
 81+  REDUCE_LAST_THREE_B
 82 }
 83 
 84 
 85@@ -326,7 +326,10 @@ let Parser = (function () {
 86       36: "#",
 87       37: "!"
 88     },
 89-    productions_: [
 90+    /**
 91+     * Maps a ProductionRule to the appropriate number of previous tokens to use in a reduction action.
 92+     */
 93+    productions: [
 94       0,
 95       [3, 2],
 96       [4, 1],
 97@@ -387,92 +390,92 @@ let Parser = (function () {
 98         case ReduceActions.I4:
 99           this.$ = yy.handler.time.call(yy.obj, virtualStack[$0]);
100           break;
101-        case ReduceActions.I5:
102+        case ReduceActions.AS_NUMBER:
103           this.$ = yy.handler.helper.number(virtualStack[$0]);
104           break;
105-        case ReduceActions.I6:
106+        case ReduceActions.AS_STRING:
107           this.$ = yy.handler.helper.string(virtualStack[$0]);
108           break;
109-        case ReduceActions.I7:
110+        case ReduceActions.AMPERSAND:
111           this.$ = yy.handler.helper.specialMatch('&', virtualStack[$0 - 2], virtualStack[$0]);
112           break;
113-        case ReduceActions.I8:
114+        case ReduceActions.EQUALS:
115           this.$ = yy.handler.helper.logicMatch('=', virtualStack[$0 - 2], virtualStack[$0]);
116           break;
117-        case ReduceActions.I9:
118+        case ReduceActions.PLUS:
119           this.$ = yy.handler.helper.mathMatch('+', virtualStack[$0 - 2], virtualStack[$0]);
120           break;
121-        case ReduceActions.I10:
122+        case ReduceActions.LAST_NUMBER:
123           this.$ = yy.handler.helper.number(virtualStack[$0 - 1]);
124           break;
125-        case ReduceActions.I11:
126+        case ReduceActions.LTE:
127           this.$ = yy.handler.helper.logicMatch('<=', virtualStack[$0 - 3], virtualStack[$0]);
128           break;
129-        case ReduceActions.I12:
130+        case ReduceActions.GTE:
131           this.$ = yy.handler.helper.logicMatch('>=', virtualStack[$0 - 3], virtualStack[$0]);
132           break;
133-        case ReduceActions.I13:
134+        case ReduceActions.NOT_EQ:
135           this.$ = yy.handler.helper.logicMatch('<>', virtualStack[$0 - 3], virtualStack[$0]);
136           break;
137-        case ReduceActions.I14:
138+        case ReduceActions.NOT:
139           this.$ = yy.handler.helper.logicMatch('NOT', virtualStack[$0 - 2], virtualStack[$0]);
140           break;
141-        case ReduceActions.I15:
142+        case ReduceActions.GT:
143           this.$ = yy.handler.helper.logicMatch('>', virtualStack[$0 - 2], virtualStack[$0]);
144           break;
145-        case ReduceActions.I16:
146+        case ReduceActions.LT:
147           this.$ = yy.handler.helper.logicMatch('<', virtualStack[$0 - 2], virtualStack[$0]);
148           break;
149-        case ReduceActions.I17:
150+        case ReduceActions.MINUS:
151           this.$ = yy.handler.helper.mathMatch('-', virtualStack[$0 - 2], virtualStack[$0]);
152           break;
153-        case ReduceActions.I18:
154+        case ReduceActions.MULTIPLY:
155           this.$ = yy.handler.helper.mathMatch('*', virtualStack[$0 - 2], virtualStack[$0]);
156           break;
157-        case ReduceActions.I19:
158+        case ReduceActions.DIVIDE:
159           this.$ = yy.handler.helper.mathMatch('/', virtualStack[$0 - 2], virtualStack[$0]);
160           break;
161-        case ReduceActions.I20:
162+        case ReduceActions.TO_POWER:
163           this.$ = yy.handler.helper.mathMatch('^', virtualStack[$0 - 2], virtualStack[$0]);
164           break;
165-        case ReduceActions.I21:
166+        case ReduceActions.INVERT_NUM:
167           this.$ = yy.handler.helper.numberInverted(virtualStack[$0]);
168           if (isNaN(this.$)) {
169             this.$ = 0;
170           }
171           break;
172-        case ReduceActions.I22:
173+        case ReduceActions.TO_NUMBER_NAN_AS_ZERO:
174           this.$ = yy.handler.helper.number(virtualStack[$0]);
175           if (isNaN(this.$)) {
176             this.$ = 0;
177           }
178           break;
179-        case ReduceActions.I23:
180+        case ReduceActions.CALL_FUNCTION_LAST_BLANK:
181           this.$ = yy.handler.helper.callFunction.call(this, virtualStack[$0 - 2], '');
182           break;
183-        case ReduceActions.I24:
184+        case ReduceActions.CALL_FUNCTION_LAST_TWO_IN_STACK:
185           this.$ = yy.handler.helper.callFunction.call(this, virtualStack[$0 - 3], virtualStack[$0 - 1]);
186           break;
187-        case ReduceActions.I28:
188+        case ReduceActions.FIXED_CELL_VAL:
189           this.$ = yy.handler.helper.fixedCellValue.call(yy.obj, virtualStack[$0]);
190           break;
191-        case ReduceActions.I29:
192+        case ReduceActions.FIX_EDCELL_RANGE_VAL:
193           this.$ = yy.handler.helper.fixedCellRangeValue.call(yy.obj, virtualStack[$0 - 2], virtualStack[$0]);
194           break;
195-        case ReduceActions.I30:
196+        case ReduceActions.CELL_VALUE:
197           this.$ = yy.handler.helper.cellValue.call(yy.obj, virtualStack[$0]);
198           break;
199-        case ReduceActions.I31:
200+        case ReduceActions.CELL_RANGE_VALUE:
201           this.$ = yy.handler.helper.cellRangeValue.call(yy.obj, virtualStack[$0 - 2], virtualStack[$0]);
202           break;
203-        case ReduceActions.I32:
204+        case ReduceActions.ENSURE_IS_ARRAY:
205           if (yy.handler.utils.isArray(virtualStack[$0])) {
206             this.$ = virtualStack[$0];
207           } else {
208             this.$ = [virtualStack[$0]];
209           }
210           break;
211-        case ReduceActions.I33:
212+        case ReduceActions.ENSURE_YYTEXT_ARRAY:
213           let result = [],
214             arr = eval("[" + yytext + "]");
215           arr.forEach(function (item) {
216@@ -480,29 +483,36 @@ let Parser = (function () {
217           });
218           this.$ = result;
219           break;
220-        case ReduceActions.I34:
221-        case ReduceActions.I35:
222+        case ReduceActions.REDUCE_INT:
223+        case ReduceActions.REDUCE_PERCENT:
224           virtualStack[$0 - 2].push(virtualStack[$0]);
225           this.$ = virtualStack[$0 - 2];
226           break;
227-        case ReduceActions.I36:
228+        case ReduceActions.WRAP_CURRENT_INDEX_TOKEN_AS_ARRAY:
229           this.$ = [virtualStack[$0]];
230           break;
231-        case ReduceActions.I37:
232+        /**
233+         * As far as I can tell, we don't use this rule, but I'm hesitant to delete it until I understand why it was
234+         * initially written.
235+         */
236+        case ReduceActions.ENSURE_LAST_TWO_IN_ARRAY_AND_PUSH:
237           this.$ = (yy.handler.utils.isArray(virtualStack[$0 - 2]) ? virtualStack[$0 - 2] : [virtualStack[$0 - 2]]);
238           this.$.push(virtualStack[$0]);
239           break;
240-        case ReduceActions.I38:
241+        case ReduceActions.REFLEXIVE_REDUCE:
242           this.$ = virtualStack[$0];
243           break;
244-        case ReduceActions.I39:
245+        case ReduceActions.REDUCE_FLOAT:
246           this.$ = parseFloat(virtualStack[$0 - 2] + '.' + virtualStack[$0]);
247           break;
248-        case ReduceActions.I40:
249+        case ReduceActions.REUDCE_PREV_AS_PERCENT:
250           this.$ = virtualStack[$0 - 1] * 0.01;
251           break;
252-        case ReduceActions.I41:
253-        case ReduceActions.I42:
254+        /**
255+         * I don't understand where these come from as well, but I want to know the intent behind them.
256+         */
257+        case ReduceActions.REDUCE_LAST_THREE_A:
258+        case ReduceActions.REDUCE_LAST_THREE_B:
259           this.$ = virtualStack[$0 - 2] + virtualStack[$0 - 1] + virtualStack[$0];
260           break;
261       }
262@@ -1055,6 +1065,8 @@ let Parser = (function () {
263       extendRules($Vm, [LexActions.REDUCE, 24]),
264       ObjectFromPairs.of([
265         SINGLE_QUOTES_RULE_INDEX, 13,
266+        // As far as I can tell, some of these higher values are lazy ways of indicating an out-of-bounds "production"
267+        // index, forcing a default to len=1 when reducing values.
268         DATE_RULE_INDEX, 74,
269         $_A1_CELL_RULE_INDEX, 3,
270         A1_CELL_RULE_INDEX, $V0,
271@@ -1221,7 +1233,6 @@ let Parser = (function () {
272           action = table[state] && table[state][symbol];
273         }
274 
275-        // console.log("symbol:", symbol, "action:", action);
276         // handle parse error
277         if (typeof action === 'undefined' || !action.length || !action[0]) {
278           let error_rule_depth;
279@@ -1337,7 +1348,7 @@ let Parser = (function () {
280             break;
281 
282           case LexActions.REDUCE: // Reduce
283-            len = this.productions_[action[1]][1];
284+            len = this.productions[action[1]][1];
285 
286             // perform semantic action
287             yyval.$ = semanticValueStack[semanticValueStack.length - len]; // default to $$ = $1
288@@ -1365,7 +1376,7 @@ let Parser = (function () {
289             }
290 
291             // push non-terminal (reduce)
292-            stack.push(this.productions_[action[1]][0]);
293+            stack.push(this.productions[action[1]][0]);
294             semanticValueStack.push(yyval.$);
295             locationStack.push(yyval._$);
296             newState = table[stack[stack.length - 2]][stack[stack.length - 1]];
297@@ -1715,64 +1726,64 @@ let Parser = (function () {
298             // skip whitespace
299             break;
300           case DOUBLE_QUOTES_RULE_INDEX:
301-            return ReduceActions.I10;
302+            return ReduceActions.LAST_NUMBER;
303           case SINGLE_QUOTES_RULE_INDEX:
304-            return ReduceActions.I10;
305+            return ReduceActions.LAST_NUMBER;
306           case FORMULA_NAME_RULE_INDEX:
307-            return ReduceActions.I23;
308+            return ReduceActions.CALL_FUNCTION_LAST_BLANK;
309           case DATE_RULE_INDEX:
310-            return ReduceActions.I7;
311+            return ReduceActions.AMPERSAND;
312           case TIME_RULE_INDEX:
313-            return ReduceActions.I8;
314+            return ReduceActions.EQUALS;
315           case $_A1_CELL_RULE_INDEX:
316             return ReduceActions.I26;
317           case A1_CELL_RULE_INDEX:
318-            return ReduceActions.I28;
319+            return ReduceActions.FIXED_CELL_VAL;
320           case FORMULA_NAME_SIMPLE_RULE_INDEX:
321-            return ReduceActions.I23;
322+            return ReduceActions.CALL_FUNCTION_LAST_BLANK;
323           case VARIABLE_RULE_INDEX:
324-            return ReduceActions.I32;
325+            return ReduceActions.ENSURE_IS_ARRAY;
326           case SIMPLE_VARIABLE_RILE_INDEX:
327-            return ReduceActions.I32;
328+            return ReduceActions.ENSURE_IS_ARRAY;
329           case INTEGER_RULE_INDEX:
330-            return ReduceActions.I34;
331+            return ReduceActions.REDUCE_INT;
332           case OPEN_AND_CLOSE_OF_ARRAY_RULE_INDEX:
333-            return ReduceActions.I29;
334+            return ReduceActions.FIX_EDCELL_RANGE_VAL;
335           case DOLLAR_SIGN_RULE_INDEX:
336             // skip whitespace??
337             break;
338           case AMPERSAND_SIGN_RULE_INDEX:
339-            return ReduceActions.I11;
340+            return ReduceActions.LTE;
341           case SINGLE_WHITESPACE_RULE_INDEX:
342             return ' ';
343           case PERIOD_RULE_INDEX:
344-            return ReduceActions.I33;
345+            return ReduceActions.ENSURE_YYTEXT_ARRAY;
346           case COLON_RULE_INDEX:
347             return ReduceActions.I27;
348           case SEMI_COLON_RULE_INDEX:
349-            return ReduceActions.I30;
350+            return ReduceActions.CELL_VALUE;
351           case COMMA_RULE_INDEX:
352-            return ReduceActions.I31;
353+            return ReduceActions.CELL_RANGE_VALUE;
354           case ASTERISK_RULE_INDEX:
355-            return ReduceActions.I20;
356+            return ReduceActions.TO_POWER;
357           case FORWARD_SLASH_RULE_INDEX:
358-            return ReduceActions.I21;
359+            return ReduceActions.INVERT_NUM;
360           case MINUS_SIGN_RULE_INDEX:
361-            return ReduceActions.I19;
362+            return ReduceActions.DIVIDE;
363           case PLUS_SIGN_RULE_INDEX:
364-            return ReduceActions.I13;
365+            return ReduceActions.NOT_EQ;
366           case CARET_SIGN_RULE_INDEX:
367-            return ReduceActions.I22;
368+            return ReduceActions.TO_NUMBER_NAN_AS_ZERO;
369           case OPEN_PAREN_RULE_INDEX:
370-            return ReduceActions.I14;
371+            return ReduceActions.NOT;
372           case CLOSE_PAREN_RULE_INDEX:
373-            return ReduceActions.I15;
374+            return ReduceActions.GT;
375           case GREATER_THAN_SIGN_RULE_INDEX:
376-            return ReduceActions.I17;
377+            return ReduceActions.MINUS;
378           case LESS_THAN_SIGN_RULE_INDEX:
379-            return ReduceActions.I16;
380+            return ReduceActions.LT;
381           case NOT_RULE_INDEX:
382-            return ReduceActions.I18;
383+            return ReduceActions.MULTIPLY;
384           case OPEN_DOUBLE_QUOTE_INDEX:
385             return '"';
386           case OPEN_SINGLE_QUITE_INDEX:
387@@ -1780,13 +1791,13 @@ let Parser = (function () {
388           case EXCLAMATION_POINT_RULE_INDEX:
389             return "!";
390           case EQUALS_SIGN_RULE_INDEX:
391-            return ReduceActions.I12;
392+            return ReduceActions.GTE;
393           case PERCENT_SIGN_RULE_INDEX:
394-            return ReduceActions.I35;
395+            return ReduceActions.REDUCE_PERCENT;
396           case HASH_SIGN_RULE_INDEX:
397-            return ReduceActions.I36;
398+            return ReduceActions.WRAP_CURRENT_INDEX_TOKEN_AS_ARRAY;
399           case END_OF_STRING_RULE_INDEX:
400-            return ReduceActions.I5;
401+            return ReduceActions.AS_NUMBER;
402         }
403       },
404       rules: RULES,