spreadsheet
typeScript/javascript spreadsheet parser, with formulas.
git clone https://git.vogt.world/spreadsheet.git
Log | Files | README.md
← Commit log
commit
message
[Parser] refactoring use of 'table' and 'productions' in parse()
author
Ben Vogt <[email protected]>
date
2017-09-17 22:09:43
stats
1 file(s) changed, 18 insertions(+), 21 deletions(-)
files
src/Parser/Parser.ts
  1diff --git a/src/Parser/Parser.ts b/src/Parser/Parser.ts
  2index c856380..ffa8487 100644
  3--- a/src/Parser/Parser.ts
  4+++ b/src/Parser/Parser.ts
  5@@ -171,6 +171,8 @@ class ReductionPair {
  6  * Productions is used to look up both the number to use when reducing the stack (productions[x][1]) and the semantic
  7  * value that will replace the tokens in the stack (productions[x][0]).
  8  * @type {Array<ReductionPair>}
  9+ *
 10+ * Maps a ProductionRule to the appropriate number of previous tokens to use in a reduction action.
 11  */
 12 let productions : Array<ReductionPair> = [];
 13 productions[ReduceActions.NO_ACTION] = null;
 14@@ -219,6 +221,8 @@ productions[ReduceActions.REDUCE_LAST_THREE_B] = new ReductionPair(2, 4);
 15 const PRODUCTIONS = productions;
 16 
 17 const SYMBOL_NAME_TO_INDEX = {
 18+  "$accept": 0,
 19+  "$end": 1,
 20   "error": 2,
 21   "expressions": 3,
 22   "expression": 4,
 23@@ -254,9 +258,7 @@ const SYMBOL_NAME_TO_INDEX = {
 24   "NUMBER": 34,
 25   "%": 35,
 26   "#": 36,
 27-  "!": 37,
 28-  "$accept": 0,
 29-  "$end": 1
 30+  "!": 37
 31 };
 32 const SYMBOL_INDEX_TO_NAME = {
 33   5: "EOF",
 34@@ -1403,10 +1405,6 @@ let Parser = (function () {
 35     Parser: undefined,
 36     trace: function trace() {},
 37     yy: {},
 38-    /**
 39-     * Maps a ProductionRule to the appropriate number of previous tokens to use in a reduction action.
 40-     */
 41-    productions: PRODUCTIONS,
 42     /**
 43      * Perform a reduce action on the given virtual stack. Basically, fetching, deriving, or calculating a value.
 44      * @param rawValueOfReduceOriginToken - Some actions require the origin token to perform a reduce action. For
 45@@ -1646,7 +1644,6 @@ let Parser = (function () {
 46         }
 47       }
 48     },
 49-    table: ACTION_TABLE,
 50     defaultActions: {19: [REDUCE, 1]},
 51     parseError: function parseError(str, hash) {
 52       if (hash.recoverable) {
 53@@ -1656,11 +1653,9 @@ let Parser = (function () {
 54       }
 55     },
 56     parse: function parse(input) {
 57-      let self = this,
 58-        stack = [0],
 59+      let stack = [0],
 60         semanticValueStack = [null],
 61         locationStack = [],
 62-        table = this.table,
 63         yytext = '',
 64         yylineno = 0,
 65         yyleng = 0,
 66@@ -1745,7 +1740,7 @@ let Parser = (function () {
 67             symbol = lex();
 68           }
 69           // read action for current state and first input
 70-          action = table[state] && table[state][symbol];
 71+          action = ACTION_TABLE[state] && ACTION_TABLE[state][symbol];
 72         }
 73 
 74         // handle parse error
 75@@ -1762,7 +1757,7 @@ let Parser = (function () {
 76             // try to recover from error
 77             for (; ;) {
 78               // check for error recovery rule in this state
 79-              if ((TERROR.toString()) in table[state]) {
 80+              if ((TERROR.toString()) in ACTION_TABLE[state]) {
 81                 return depth;
 82               }
 83               if (state === 0 || stack_probe < 2) {
 84@@ -1781,8 +1776,8 @@ let Parser = (function () {
 85             // Report error
 86             expected = [];
 87             let expectedIndexes = [];
 88-            let tableState = table[state];
 89-            for (p in table[state]) {
 90+            let tableState = ACTION_TABLE[state];
 91+            for (p in ACTION_TABLE[state]) {
 92               if (SYMBOL_INDEX_TO_NAME[p] && p > TERROR) {
 93                 expected.push(SYMBOL_INDEX_TO_NAME[p]);
 94                 expectedIndexes.push(p);
 95@@ -1833,7 +1828,7 @@ let Parser = (function () {
 96           preErrorSymbol = (symbol == TERROR ? null : symbol); // save the lookahead token
 97           symbol = TERROR;         // insert generic error symbol as new lookahead
 98           state = stack[stack.length - 1];
 99-          action = table[state] && table[state][TERROR];
100+          action = ACTION_TABLE[state] && ACTION_TABLE[state][TERROR];
101           recovering = 3; // allow 3 real symbols to be shifted before reporting a new error
102         }
103 
104@@ -1874,7 +1869,7 @@ let Parser = (function () {
105             break;
106 
107           case REDUCE: // Reduce
108-            let currentProduction : ReductionPair = this.productions[action[1]];
109+            let currentProduction : ReductionPair = PRODUCTIONS[action[1]];
110 
111             let lengthToReduceStackBy = currentProduction.getLengthToReduceStackBy();
112 
113@@ -1908,7 +1903,7 @@ let Parser = (function () {
114             stack.push(currentProduction.getReplacementTokenIndex());
115             semanticValueStack.push(yyval.$);
116             locationStack.push(yyval._$);
117-            newState = table[stack[stack.length - 2]][stack[stack.length - 1]];
118+            newState = ACTION_TABLE[stack[stack.length - 2]][stack[stack.length - 1]];
119             stack.push(newState);
120             break;
121