spreadsheet
typeScript/javascript spreadsheet parser, with formulas.
git clone https://git.vogt.world/spreadsheet.git
Log | Files | README.md
← Commit log
commit
message
[Parser] continuing to name production indicies, documenting performAction function
author
Ben Vogt <[email protected]>
date
2017-08-20 17:52:38
stats
1 file(s) changed, 91 insertions(+), 103 deletions(-)
files
src/Parser/Parser.ts
  1diff --git a/src/Parser/Parser.ts b/src/Parser/Parser.ts
  2index 1be51c9..b8d2b89 100644
  3--- a/src/Parser/Parser.ts
  4+++ b/src/Parser/Parser.ts
  5@@ -54,13 +54,12 @@ const enum LexActions {
  6   ACCEPT
  7 }
  8 
  9-
 10 const enum ReduceActions {
 11   NO_ACTION = 0,
 12   RETURN_LAST,
 13   CALL_VARIABLE,
 14-  I3,
 15-  I4,
 16+  TIME_CALL_TRUE,
 17+  TIME_CALL,
 18   AS_NUMBER,
 19   AS_STRING,
 20   AMPERSAND,
 21@@ -105,8 +104,8 @@ let productions = [];
 22 productions[ReduceActions.NO_ACTION] = 0;
 23 productions[ReduceActions.RETURN_LAST] = [3, 2];
 24 productions[ReduceActions.CALL_VARIABLE] = [4, 1];
 25-productions[ReduceActions.I3] = [4, 1];
 26-productions[ReduceActions.I4] = [4, 1];
 27+productions[ReduceActions.TIME_CALL_TRUE] = [4, 1];
 28+productions[ReduceActions.TIME_CALL] = [4, 1];
 29 productions[ReduceActions.AS_NUMBER] = [4, 1];
 30 productions[ReduceActions.AS_STRING] = [4, 1];
 31 productions[ReduceActions.AMPERSAND] = [4, 3];
 32@@ -378,118 +377,118 @@ let Parser = (function () {
 33     productions: PRODUCTIONS,
 34     /**
 35      * Perform a reduce action on the given virtual stack. Basically, fetching, deriving, or calculating a value.
 36-     * @param yytext ???
 37-     * @param yyleng ???
 38-     * @param yylineno ???
 39-     * @param yy ???
 40-     * @param reduceActionToPerform - the ReduceAction to perform with the current virtual stack
 41+     * @param rawValueOfReduceOriginToken - Some actions require the origin token to perform a reduce action. For
 42+     * example, when reducing the cell reference A1 to it's actual value this value would be "A1".
 43+     * @param sharedStateYY - the shared state that has all helpers, and current working object.
 44+     * @param reduceActionToPerform - the ReduceAction to perform with the current virtual stack. Since this function
 45+     * is only called in one place, this should always be action[1] in that context.
 46      * @param virtualStack - Array of values to use in action.
 47      * @returns {number|boolean|string}
 48      */
 49-    performAction: function (yytext, yyleng, yylineno, yy, reduceActionToPerform /* action[1] */, virtualStack : Array<any>) {
 50-      // For context, this function is only called with `call` or `apply`, so `this` is `yyval`.
 51+    performAction: function (rawValueOfReduceOriginToken, sharedStateYY, reduceActionToPerform, virtualStack : Array<any>) {
 52+      // For context, this function is only called with `apply`, so `this` is `yyval`.
 53 
 54-      let $0 = virtualStack.length - 1;
 55+      const vsl = virtualStack.length - 1;
 56       switch (reduceActionToPerform) {
 57         case ReduceActions.RETURN_LAST:
 58-          return virtualStack[$0 - 1];
 59+          return virtualStack[vsl - 1];
 60         case ReduceActions.CALL_VARIABLE:
 61-          this.$ = yy.handler.helper.callVariable.call(this, virtualStack[$0]);
 62+          this.$ = sharedStateYY.handler.helper.callVariable.call(this, virtualStack[vsl]);
 63           break;
 64-        case ReduceActions.I3:
 65-          this.$ = yy.handler.time.call(yy.obj, virtualStack[$0], true);
 66+        case ReduceActions.TIME_CALL_TRUE:
 67+          this.$ = sharedStateYY.handler.time.call(sharedStateYY.obj, virtualStack[vsl], true);
 68           break;
 69-        case ReduceActions.I4:
 70-          this.$ = yy.handler.time.call(yy.obj, virtualStack[$0]);
 71+        case ReduceActions.TIME_CALL:
 72+          this.$ = sharedStateYY.handler.time.call(sharedStateYY.obj, virtualStack[vsl]);
 73           break;
 74         case ReduceActions.AS_NUMBER:
 75-          this.$ = yy.handler.helper.number(virtualStack[$0]);
 76+          this.$ = sharedStateYY.handler.helper.number(virtualStack[vsl]);
 77           break;
 78         case ReduceActions.AS_STRING:
 79-          this.$ = yy.handler.helper.string(virtualStack[$0]);
 80+          this.$ = sharedStateYY.handler.helper.string(virtualStack[vsl]);
 81           break;
 82         case ReduceActions.AMPERSAND:
 83-          this.$ = yy.handler.helper.specialMatch('&', virtualStack[$0 - 2], virtualStack[$0]);
 84+          this.$ = sharedStateYY.handler.helper.specialMatch('&', virtualStack[vsl - 2], virtualStack[vsl]);
 85           break;
 86         case ReduceActions.EQUALS:
 87-          this.$ = yy.handler.helper.logicMatch('=', virtualStack[$0 - 2], virtualStack[$0]);
 88+          this.$ = sharedStateYY.handler.helper.logicMatch('=', virtualStack[vsl - 2], virtualStack[vsl]);
 89           break;
 90         case ReduceActions.PLUS:
 91-          this.$ = yy.handler.helper.mathMatch('+', virtualStack[$0 - 2], virtualStack[$0]);
 92+          this.$ = sharedStateYY.handler.helper.mathMatch('+', virtualStack[vsl - 2], virtualStack[vsl]);
 93           break;
 94         case ReduceActions.LAST_NUMBER:
 95-          this.$ = yy.handler.helper.number(virtualStack[$0 - 1]);
 96+          this.$ = sharedStateYY.handler.helper.number(virtualStack[vsl - 1]);
 97           break;
 98         case ReduceActions.LTE:
 99-          this.$ = yy.handler.helper.logicMatch('<=', virtualStack[$0 - 3], virtualStack[$0]);
100+          this.$ = sharedStateYY.handler.helper.logicMatch('<=', virtualStack[vsl - 3], virtualStack[vsl]);
101           break;
102         case ReduceActions.GTE:
103-          this.$ = yy.handler.helper.logicMatch('>=', virtualStack[$0 - 3], virtualStack[$0]);
104+          this.$ = sharedStateYY.handler.helper.logicMatch('>=', virtualStack[vsl - 3], virtualStack[vsl]);
105           break;
106         case ReduceActions.NOT_EQ:
107-          this.$ = yy.handler.helper.logicMatch('<>', virtualStack[$0 - 3], virtualStack[$0]);
108+          this.$ = sharedStateYY.handler.helper.logicMatch('<>', virtualStack[vsl - 3], virtualStack[vsl]);
109           break;
110         case ReduceActions.NOT:
111-          this.$ = yy.handler.helper.logicMatch('NOT', virtualStack[$0 - 2], virtualStack[$0]);
112+          this.$ = sharedStateYY.handler.helper.logicMatch('NOT', virtualStack[vsl - 2], virtualStack[vsl]);
113           break;
114         case ReduceActions.GT:
115-          this.$ = yy.handler.helper.logicMatch('>', virtualStack[$0 - 2], virtualStack[$0]);
116+          this.$ = sharedStateYY.handler.helper.logicMatch('>', virtualStack[vsl - 2], virtualStack[vsl]);
117           break;
118         case ReduceActions.LT:
119-          this.$ = yy.handler.helper.logicMatch('<', virtualStack[$0 - 2], virtualStack[$0]);
120+          this.$ = sharedStateYY.handler.helper.logicMatch('<', virtualStack[vsl - 2], virtualStack[vsl]);
121           break;
122         case ReduceActions.MINUS:
123-          this.$ = yy.handler.helper.mathMatch('-', virtualStack[$0 - 2], virtualStack[$0]);
124+          this.$ = sharedStateYY.handler.helper.mathMatch('-', virtualStack[vsl - 2], virtualStack[vsl]);
125           break;
126         case ReduceActions.MULTIPLY:
127-          this.$ = yy.handler.helper.mathMatch('*', virtualStack[$0 - 2], virtualStack[$0]);
128+          this.$ = sharedStateYY.handler.helper.mathMatch('*', virtualStack[vsl - 2], virtualStack[vsl]);
129           break;
130         case ReduceActions.DIVIDE:
131-          this.$ = yy.handler.helper.mathMatch('/', virtualStack[$0 - 2], virtualStack[$0]);
132+          this.$ = sharedStateYY.handler.helper.mathMatch('/', virtualStack[vsl - 2], virtualStack[vsl]);
133           break;
134         case ReduceActions.TO_POWER:
135-          this.$ = yy.handler.helper.mathMatch('^', virtualStack[$0 - 2], virtualStack[$0]);
136+          this.$ = sharedStateYY.handler.helper.mathMatch('^', virtualStack[vsl - 2], virtualStack[vsl]);
137           break;
138         case ReduceActions.INVERT_NUM:
139-          this.$ = yy.handler.helper.numberInverted(virtualStack[$0]);
140+          this.$ = sharedStateYY.handler.helper.numberInverted(virtualStack[vsl]);
141           if (isNaN(this.$)) {
142             this.$ = 0;
143           }
144           break;
145         case ReduceActions.TO_NUMBER_NAN_AS_ZERO:
146-          this.$ = yy.handler.helper.number(virtualStack[$0]);
147+          this.$ = sharedStateYY.handler.helper.number(virtualStack[vsl]);
148           if (isNaN(this.$)) {
149             this.$ = 0;
150           }
151           break;
152         case ReduceActions.CALL_FUNCTION_LAST_BLANK:
153-          this.$ = yy.handler.helper.callFunction.call(this, virtualStack[$0 - 2], '');
154+          this.$ = sharedStateYY.handler.helper.callFunction.call(this, virtualStack[vsl - 2], '');
155           break;
156         case ReduceActions.CALL_FUNCTION_LAST_TWO_IN_STACK:
157-          this.$ = yy.handler.helper.callFunction.call(this, virtualStack[$0 - 3], virtualStack[$0 - 1]);
158+          this.$ = sharedStateYY.handler.helper.callFunction.call(this, virtualStack[vsl - 3], virtualStack[vsl - 1]);
159           break;
160         case ReduceActions.FIXED_CELL_VAL:
161-          this.$ = yy.handler.helper.fixedCellValue.call(yy.obj, virtualStack[$0]);
162+          this.$ = sharedStateYY.handler.helper.fixedCellValue.call(sharedStateYY.obj, virtualStack[vsl]);
163           break;
164         case ReduceActions.FIXED_CELL_RANGE_VAL:
165-          this.$ = yy.handler.helper.fixedCellRangeValue.call(yy.obj, virtualStack[$0 - 2], virtualStack[$0]);
166+          this.$ = sharedStateYY.handler.helper.fixedCellRangeValue.call(sharedStateYY.obj, virtualStack[vsl - 2], virtualStack[vsl]);
167           break;
168         case ReduceActions.CELL_VALUE:
169-          this.$ = yy.handler.helper.cellValue.call(yy.obj, virtualStack[$0]);
170+          this.$ = sharedStateYY.handler.helper.cellValue.call(sharedStateYY.obj, virtualStack[vsl]);
171           break;
172         case ReduceActions.CELL_RANGE_VALUE:
173-          this.$ = yy.handler.helper.cellRangeValue.call(yy.obj, virtualStack[$0 - 2], virtualStack[$0]);
174+          this.$ = sharedStateYY.handler.helper.cellRangeValue.call(sharedStateYY.obj, virtualStack[vsl - 2], virtualStack[vsl]);
175           break;
176         case ReduceActions.ENSURE_IS_ARRAY:
177-          if (yy.handler.utils.isArray(virtualStack[$0])) {
178-            this.$ = virtualStack[$0];
179+          if (sharedStateYY.handler.utils.isArray(virtualStack[vsl])) {
180+            this.$ = virtualStack[vsl];
181           } else {
182-            this.$ = [virtualStack[$0]];
183+            this.$ = [virtualStack[vsl]];
184           }
185           break;
186         case ReduceActions.ENSURE_YYTEXT_ARRAY:
187           let result = [],
188-            arr = eval("[" + yytext + "]");
189+            arr = eval("[" + rawValueOfReduceOriginToken + "]");
190           arr.forEach(function (item) {
191             result.push(item);
192           });
193@@ -497,58 +496,40 @@ let Parser = (function () {
194           break;
195         case ReduceActions.REDUCE_INT:
196         case ReduceActions.REDUCE_PERCENT:
197-          virtualStack[$0 - 2].push(virtualStack[$0]);
198-          this.$ = virtualStack[$0 - 2];
199+          virtualStack[vsl - 2].push(virtualStack[vsl]);
200+          this.$ = virtualStack[vsl - 2];
201           break;
202         case ReduceActions.WRAP_CURRENT_INDEX_TOKEN_AS_ARRAY:
203-          this.$ = [virtualStack[$0]];
204+          this.$ = [virtualStack[vsl]];
205           break;
206         /**
207          * 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
208          * initially written.
209          */
210         case ReduceActions.ENSURE_LAST_TWO_IN_ARRAY_AND_PUSH:
211-          this.$ = (yy.handler.utils.isArray(virtualStack[$0 - 2]) ? virtualStack[$0 - 2] : [virtualStack[$0 - 2]]);
212-          this.$.push(virtualStack[$0]);
213+          this.$ = (sharedStateYY.handler.utils.isArray(virtualStack[vsl - 2]) ? virtualStack[vsl - 2] : [virtualStack[vsl - 2]]);
214+          this.$.push(virtualStack[vsl]);
215           break;
216         case ReduceActions.REFLEXIVE_REDUCE:
217-          this.$ = virtualStack[$0];
218+          this.$ = virtualStack[vsl];
219           break;
220         case ReduceActions.REDUCE_FLOAT:
221-          this.$ = parseFloat(virtualStack[$0 - 2] + '.' + virtualStack[$0]);
222+          this.$ = parseFloat(virtualStack[vsl - 2] + '.' + virtualStack[vsl]);
223           break;
224         case ReduceActions.REDUCE_PREV_AS_PERCENT:
225-          this.$ = virtualStack[$0 - 1] * 0.01;
226+          this.$ = virtualStack[vsl - 1] * 0.01;
227           break;
228         /**
229          * I don't understand where these come from as well, but I want to know the intent behind them.
230          */
231         case ReduceActions.REDUCE_LAST_THREE_A:
232         case ReduceActions.REDUCE_LAST_THREE_B:
233-          this.$ = virtualStack[$0 - 2] + virtualStack[$0 - 1] + virtualStack[$0];
234+          this.$ = virtualStack[vsl - 2] + virtualStack[vsl - 1] + virtualStack[vsl];
235           break;
236       }
237     },
238     /**
239-     * The `table` is an array of objects that map {@link RULES} to LexActions and tokens. Eg:
240-     *  { '2': 13,            SINGLE_QUOTES_RULE:
241-          '3': 1,
242-          '4': 2,
243-          '6': 3,
244-          '7': [ 1, 4 ],
245-          '8': [ 1, 5 ],
246-          '9': 6,
247-          '10': [ 1, 7 ],
248-          '13': [ 1, 10 ],
249-          '14': [ 1, 8 ],
250-          '19': [ 1, 9 ],
251-          '23': [ 1, 11 ],
252-          '25': 12,
253-          '26': [ 1, 16 ],
254-          '28': [ 1, 17 ],
255-          '32': [ 1, 14 ],
256-          '34': [ 1, 15 ],
257-          '36': [ 1, 18 ] }
258+     * The `table` is an array of objects that map {@link RULES} to LexActions and tokens.
259      */
260     table: [
261       ObjectFromPairs.of([
262@@ -1045,7 +1026,9 @@ let Parser = (function () {
263         FORWARD_SLASH_RULE_INDEX, $Vk,
264         MINUS_SIGN_RULE_INDEX, $Vl
265       ])),
266-      extendRules($Vt, [LexActions.REDUCE, 33]), ObjectFromPairs.of([37, [LexActions.SHIFT, 73]]), // index 37?
267+      extendRules($Vt, [LexActions.REDUCE, 33]), ObjectFromPairs.of([
268+        37, [LexActions.SHIFT, 73] // index 37?
269+      ]),
270       extendRules($Vp, [LexActions.REDUCE, 39]),
271       extendRules($Vm, [LexActions.REDUCE, 29]),
272       extendRules($Vm, [LexActions.REDUCE, 31]),
273@@ -1059,12 +1042,12 @@ let Parser = (function () {
274         MINUS_SIGN_RULE_INDEX, $Vl
275       ])),
276       extendRules($Vr, [LexActions.REDUCE, 13], ObjectFromPairs.of([
277-        11, $Vc,
278-        13, $Ve,
279-        19, $Vi,
280-        20, $Vj,
281-        21, $Vk,
282-        22, $Vl
283+        INTEGER_RULE_INDEX, $Vc,
284+        DOLLAR_SIGN_RULE_INDEX, $Ve,
285+        COMMA_RULE_INDEX, $Vi,
286+        ASTERISK_RULE_INDEX, $Vj,
287+        FORWARD_SLASH_RULE_INDEX, $Vk,
288+        MINUS_SIGN_RULE_INDEX, $Vl
289       ])),
290       extendRules($Vr, [LexActions.REDUCE, 12], ObjectFromPairs.of([
291         INTEGER_RULE_INDEX, $Vc,
292@@ -1374,7 +1357,7 @@ let Parser = (function () {
293             if (ranges) {
294               yyval._$.range = [locationStack[locationStack.length - (len || 1)].range[0], locationStack[locationStack.length - 1].range[1]];
295             }
296-            result = this.performAction.apply(yyval, [yytext, yyleng, yylineno, sharedState.yy, action[1], semanticValueStack, locationStack].concat(args));
297+            result = this.performAction.apply(yyval, [yytext, sharedState.yy, action[1], semanticValueStack].concat(args));
298 
299             if (typeof result !== 'undefined') {
300               return result;