spreadsheet
typeScript/javascript spreadsheet parser, with formulas.
git clone https://git.vogt.world/spreadsheet.git
Log | Files | README.md
← Commit log
commit
message
[ParseEngine] renaming some things to make more sense, getting closer to having multiplication working
author
Ben Vogt <[email protected]>
date
2017-12-10 01:39:41
stats
3 file(s) changed, 118 insertions(+), 104 deletions(-)
files
src/Parser/ParseEngine.ts
src/Parser/ParserConstants.ts
tests/Parser/ParseEngineTest.ts
  1diff --git a/src/Parser/ParseEngine.ts b/src/Parser/ParseEngine.ts
  2index 8691b8f..635c40b 100644
  3--- a/src/Parser/ParseEngine.ts
  4+++ b/src/Parser/ParseEngine.ts
  5@@ -258,18 +258,20 @@ const enum State {
  6   START = 0,
  7   NUMBER = 1,
  8   VARIABLE = 4,
  9-  MULTIPLICATION = 9,
 10+  ASTERISK = 9,
 11   TERMINATE_NUMBER = 29,
 12-  TERMINATE = 30
 13+  TERMINATE = 30,
 14+  MULTIPLY = 31
 15 }
 16 
 17 let STATE_NAMES = {};
 18 STATE_NAMES[State.START] = "State.START";
 19 STATE_NAMES[State.NUMBER] = "State.NUMBER";
 20 STATE_NAMES[State.VARIABLE] = "State.VARIABLE";
 21-STATE_NAMES[State.MULTIPLICATION] = "State.MULTIPLICATION";
 22+STATE_NAMES[State.ASTERISK] = "State.ASTERISK";
 23 STATE_NAMES[State.TERMINATE_NUMBER] = "State.TERMINATE_NUMBER";
 24 STATE_NAMES[State.TERMINATE] = "State.TERMINATE";
 25+STATE_NAMES[State.MULTIPLY] = "State.MULTIPLY";
 26 
 27 /**
 28  * Productions is used to look up both the number to use when reducing the stack (productions[x][1]) and the semantic
 29@@ -284,7 +286,7 @@ productions[ReduceActions.RETURN_LAST] = new ReductionPair(State.NUMBER, 2);
 30 productions[ReduceActions.AS_NUMBER] = new ReductionPair(State.NUMBER, 1);
 31 productions[ReduceActions.LAST_NUMBER] = new ReductionPair(State.NUMBER, 3);
 32 productions[ReduceActions.MULTIPLY] = new ReductionPair(State.NUMBER, 3);
 33-productions[ReduceActions.REFLEXIVE_REDUCE] = new ReductionPair(State.VARIABLE, 1);
 34+productions[ReduceActions.REFLEXIVE_REDUCE] = new ReductionPair(State.NUMBER, 1);
 35 productions[ReduceActions.RETURN_LAST_AS_NUMBER] = new ReductionPair(State.NUMBER, 2);
 36 const PRODUCTIONS = productions;
 37 
 38@@ -299,18 +301,17 @@ let table = [];
 39 // `=`
 40 table[State.START] = ObjectBuilder
 41   .add(Symbol.NUMBER, [SHIFT, State.NUMBER])
 42-  .add(Symbol.WHITE_SPACE, State.START)
 43-  .add(Symbol.END, State.TERMINATE)
 44+  .add(Symbol.END, [SHIFT, State.TERMINATE_NUMBER])
 45   .build();
 46 table[State.NUMBER] = ObjectBuilder
 47-  .add(Symbol.ASTERISK, [SHIFT, State.MULTIPLICATION])
 48-  .add(Symbol.WHITE_SPACE, State.NUMBER)
 49-  .add(Symbol.END, State.TERMINATE_NUMBER)
 50+  .add(Symbol.ASTERISK, [SHIFT, State.ASTERISK])
 51+  .add(Symbol.END, [SHIFT, State.TERMINATE_NUMBER])
 52   .build();
 53-table[State.MULTIPLICATION] = ObjectBuilder
 54-  .add(Symbol.NUMBER, [REDUCE, ReduceActions.MULTIPLY])
 55-  .add(Symbol.WHITE_SPACE, State.MULTIPLICATION)
 56-  .add(Symbol.END, State.TERMINATE_NUMBER)
 57+table[State.ASTERISK] = ObjectBuilder
 58+  .add(Symbol.NUMBER, [SHIFT, State.MULTIPLY])
 59+  .build();
 60+table[State.MULTIPLY] = ObjectBuilder
 61+  .add(Symbol.END, [REDUCE, ReduceActions.MULTIPLY])
 62   .build();
 63 table[State.TERMINATE] = ObjectBuilder
 64   .add(Symbol.END, [REDUCE, ReduceActions.RETURN_LAST])
 65@@ -493,7 +494,8 @@ let Parser = (function () {
 66           symbol = null;
 67           continue;
 68           // handle parse error
 69-        } else if (typeof action === 'undefined' || !action.length || !action[0]) {
 70+        } else
 71+          if (typeof action === 'undefined' || !action.length || !action[0]) {
 72           let errStr = '';
 73 
 74 
 75@@ -605,6 +607,8 @@ let Parser = (function () {
 76             semanticValueStack.push(yyval.$);
 77             locationStack.push(yyval._$);
 78             newState = ACTION_TABLE[stack[stack.length - 2]][stack[stack.length - 1]];
 79+            console.log('ACTION_TABLE[stack[stack.length - 2]]', ACTION_TABLE[stack[stack.length - 2]]);
 80+            console.log('stack[stack.length - 1]', stack[stack.length - 1]);
 81             stack.push(newState);
 82             break;
 83 
 84@@ -742,7 +746,8 @@ let Parser = (function () {
 85       },
 86 
 87       // test the lexed token: return FALSE when not a match, otherwise return token
 88-      test_match: function (match, indexed_rule) {
 89+      testMatchGetToken: function (match, indexed_rule) {
 90+        console.log('testMatchGetToken', match, indexed_rule);
 91         let token,
 92           lines;
 93 
 94@@ -767,7 +772,20 @@ let Parser = (function () {
 95         this._backtrack = false;
 96         this._input = this._input.slice(match[0].length);
 97         this.matched += match[0];
 98-        token = this.mapActionToActionIndex(indexed_rule);
 99+        switch (indexed_rule) {
100+          case RuleIndex.WHITE_SPACE:
101+            token = undefined;
102+            break;
103+          case RuleIndex.INTEGER:
104+            token = Symbol.NUMBER;
105+            break;
106+          case RuleIndex.ASTERISK:
107+            token = Symbol.ASTERISK;
108+            break;
109+          case RuleIndex.END:
110+            token = Symbol.END;
111+            break;
112+        }
113         if (this.done && this._input) {
114           this.done = false;
115         }
116@@ -803,7 +821,7 @@ let Parser = (function () {
117           }
118         }
119         if (match) {
120-          token = this.test_match(match, rules[index]);
121+          token = this.testMatchGetToken(match, rules[index]);
122           if (token !== false) {
123             return token;
124           }
125@@ -839,17 +857,6 @@ let Parser = (function () {
126           return this.conditions.INITIAL.rules;
127         }
128       },
129-
130-      mapActionToActionIndex: function (ruleIndex) {
131-        switch (ruleIndex) {
132-          case RuleIndex.WHITE_SPACE:
133-            break;
134-          case RuleIndex.INTEGER:
135-            return Symbol.NUMBER;
136-          case RuleIndex.ASTERISK:
137-            return Symbol.ASTERISK;
138-        }
139-      },
140       conditions: {
141         INITIAL: {
142           rules: [
143diff --git a/src/Parser/ParserConstants.ts b/src/Parser/ParserConstants.ts
144index 31bd4b4..0f2d354 100644
145--- a/src/Parser/ParserConstants.ts
146+++ b/src/Parser/ParserConstants.ts
147@@ -347,36 +347,36 @@ table[0] = ObjectBuilder
148   .add(Symbol.EXPRESSIONS, 1)
149   .add(Symbol.EXPRESSION, 2)
150   .add(Symbol.VARIABLE_SEQUENCE, 3)
151-  .add(Symbol.TIME_AMPM, [SHIFT, ReduceActions.TIME_CALL])
152-  .add(Symbol.TIME_24, [SHIFT, ReduceActions.AS_NUMBER])
153+  .add(Symbol.TIME_AMPM, [SHIFT, 4])
154+  .add(Symbol.TIME_24, [SHIFT, 5])
155   .add(Symbol.NUMBER, 6)
156-  .add(Symbol.STRING, [SHIFT, ReduceActions.AMPERSAND])
157-  .add(Symbol.PLUS, [SHIFT, ReduceActions.LAST_NUMBER])
158-  .add(Symbol.LEFT_PAREN, [SHIFT, ReduceActions.EQUALS])
159-  .add(Symbol.MINUS, [SHIFT, ReduceActions.PLUS])
160-  .add(Symbol.FUNCTION, [SHIFT, ReduceActions.LTE])
161+  .add(Symbol.STRING, [SHIFT, 7])
162+  .add(Symbol.PLUS, [SHIFT, 10])
163+  .add(Symbol.LEFT_PAREN, [SHIFT, 8])
164+  .add(Symbol.MINUS, [SHIFT, 9])
165+  .add(Symbol.FUNCTION, [SHIFT, 11])
166   .add(Symbol.CELL, 12)
167-  .add(Symbol.FIXEDCELL, [SHIFT, ReduceActions.LT])
168-  .add(Symbol.CELL_UPPER, [SHIFT, ReduceActions.MINUS])
169-  .add(Symbol.VARIABLE, [SHIFT, ReduceActions.NOT])
170-  .add(Symbol.NUMBER_UPPER, [SHIFT, ReduceActions.GT])
171-  .add(Symbol.POUND, [SHIFT, ReduceActions.MULTIPLY])
172+  .add(Symbol.FIXEDCELL, [SHIFT, 16])
173+  .add(Symbol.CELL_UPPER, [SHIFT, 17])
174+  .add(Symbol.VARIABLE, [SHIFT, 14])
175+  .add(Symbol.NUMBER_UPPER, [SHIFT, 15])
176+  .add(Symbol.POUND, [SHIFT, 18])
177   .build();
178 table[1] = ObjectBuilder
179   .add(Symbol.END, [3])
180   .build();
181 table[2] = ObjectBuilder
182-  .add(Symbol.EOF, [SHIFT, ReduceActions.DIVIDE])
183-  .add(Symbol.AMPERSAND, [SHIFT, ReduceActions.TO_POWER])
184-  .add(Symbol.EQUALS, [SHIFT, ReduceActions.INVERT_NUM])
185-  .add(Symbol.PLUS, [SHIFT, ReduceActions.TO_NUMBER_NAN_AS_ZERO])
186-  .add(Symbol.LESS_THAN, [SHIFT, ReduceActions.CALL_FUNCTION_LAST_BLANK])
187-  .add(Symbol.GREATER_THAN, [SHIFT, ReduceActions.CALL_FUNCTION_LAST_TWO_IN_STACK])
188-  .add(Symbol.NOT, [SHIFT, ReduceActions.I25])
189-  .add(Symbol.MINUS, [SHIFT, ReduceActions.I26])
190-  .add(Symbol.ASTERISK, [SHIFT, ReduceActions.I27])
191-  .add(Symbol.DIVIDE, [SHIFT, ReduceActions.FIXED_CELL_VAL])
192-  .add(Symbol.CARROT, [SHIFT, ReduceActions.FIXED_CELL_RANGE_VAL])
193+  .add(Symbol.EOF, [SHIFT, 19])
194+  .add(Symbol.AMPERSAND, [SHIFT, 20])
195+  .add(Symbol.EQUALS, [SHIFT, 21])
196+  .add(Symbol.PLUS, [SHIFT, 22])
197+  .add(Symbol.LESS_THAN, [SHIFT, 23])
198+  .add(Symbol.GREATER_THAN, [SHIFT, 24])
199+  .add(Symbol.NOT, [SHIFT, 25])
200+  .add(Symbol.MINUS, [SHIFT, 26])
201+  .add(Symbol.ASTERISK, [SHIFT, 27])
202+  .add(Symbol.DIVIDE, [SHIFT, 28])
203+  .add(Symbol.CARROT, [SHIFT, 29])
204   .build();
205 table[3] = ObjectBuilder
206   .add(Symbol.EOF, [REDUCE, ReduceActions.CALL_VARIABLE])
207@@ -481,61 +481,61 @@ table[8] = ObjectBuilder
208   .add(Symbol.ERROR, 13)
209   .add(Symbol.EXPRESSION, 32)
210   .add(Symbol.VARIABLE_SEQUENCE, 3)
211-  .add(Symbol.TIME_AMPM, [SHIFT, ReduceActions.TIME_CALL])
212-  .add(Symbol.TIME_24, [SHIFT, ReduceActions.AS_NUMBER])
213+  .add(Symbol.TIME_AMPM, [SHIFT, 4])
214+  .add(Symbol.TIME_24, [SHIFT, 5])
215   .add(Symbol.NUMBER, 6)
216-  .add(Symbol.STRING, [SHIFT, ReduceActions.AMPERSAND])
217-  .add(Symbol.PLUS, [SHIFT, ReduceActions.LAST_NUMBER])
218-  .add(Symbol.LEFT_PAREN, [SHIFT, ReduceActions.EQUALS])
219-  .add(Symbol.MINUS, [SHIFT, ReduceActions.PLUS])
220-  .add(Symbol.FUNCTION, [SHIFT, ReduceActions.LTE])
221+  .add(Symbol.STRING, [SHIFT, 7])
222+  .add(Symbol.PLUS, [SHIFT, 10])
223+  .add(Symbol.LEFT_PAREN, [SHIFT, 8])
224+  .add(Symbol.MINUS, [SHIFT, 9])
225+  .add(Symbol.FUNCTION, [SHIFT, 11])
226   .add(Symbol.CELL, 12)
227-  .add(Symbol.FIXEDCELL, [SHIFT, ReduceActions.LT])
228-  .add(Symbol.CELL_UPPER, [SHIFT, ReduceActions.MINUS])
229-  .add(Symbol.VARIABLE, [SHIFT, ReduceActions.NOT])
230-  .add(Symbol.NUMBER_UPPER, [SHIFT, ReduceActions.GT])
231-  .add(Symbol.POUND, [SHIFT, ReduceActions.MULTIPLY])
232+  .add(Symbol.FIXEDCELL, [SHIFT, 16])
233+  .add(Symbol.CELL_UPPER, [SHIFT, 17])
234+  .add(Symbol.VARIABLE, [SHIFT, 14])
235+  .add(Symbol.NUMBER_UPPER, [SHIFT, 15])
236+  .add(Symbol.POUND, [SHIFT, 18])
237   .build();
238 table[9] = ObjectBuilder
239   .add(Symbol.ERROR, 13)
240   .add(Symbol.EXPRESSION, 33)
241   .add(Symbol.VARIABLE_SEQUENCE, 3)
242-  .add(Symbol.TIME_AMPM, [SHIFT, ReduceActions.TIME_CALL])
243-  .add(Symbol.TIME_24, [SHIFT, ReduceActions.AS_NUMBER])
244+  .add(Symbol.TIME_AMPM, [SHIFT, 4])
245+  .add(Symbol.TIME_24, [SHIFT, 5])
246   .add(Symbol.NUMBER, 6)
247-  .add(Symbol.STRING, [SHIFT, ReduceActions.AMPERSAND])
248-  .add(Symbol.PLUS, [SHIFT, ReduceActions.LAST_NUMBER])
249-  .add(Symbol.LEFT_PAREN, [SHIFT, ReduceActions.EQUALS])
250-  .add(Symbol.MINUS, [SHIFT, ReduceActions.PLUS])
251-  .add(Symbol.FUNCTION, [SHIFT, ReduceActions.LTE])
252+  .add(Symbol.STRING, [SHIFT, 7])
253+  .add(Symbol.PLUS, [SHIFT, 10])
254+  .add(Symbol.LEFT_PAREN, [SHIFT, 8])
255+  .add(Symbol.MINUS, [SHIFT, 9])
256+  .add(Symbol.FUNCTION, [SHIFT, 11])
257   .add(Symbol.CELL, 12)
258-  .add(Symbol.FIXEDCELL, [SHIFT, ReduceActions.LT])
259-  .add(Symbol.CELL_UPPER, [SHIFT, ReduceActions.MINUS])
260-  .add(Symbol.VARIABLE, [SHIFT, ReduceActions.NOT])
261-  .add(Symbol.NUMBER_UPPER, [SHIFT, ReduceActions.GT])
262-  .add(Symbol.POUND, [SHIFT, ReduceActions.MULTIPLY])
263+  .add(Symbol.FIXEDCELL, [SHIFT, 16])
264+  .add(Symbol.CELL_UPPER, [SHIFT, 17])
265+  .add(Symbol.VARIABLE, [SHIFT, 14])
266+  .add(Symbol.NUMBER_UPPER, [SHIFT, 15])
267+  .add(Symbol.POUND, [SHIFT, 18])
268   .build();
269 table[10] = ObjectBuilder
270   .add(Symbol.ERROR, 13)
271   .add(Symbol.EXPRESSION, 34)
272   .add(Symbol.VARIABLE_SEQUENCE, 3)
273-  .add(Symbol.TIME_AMPM, [SHIFT, ReduceActions.TIME_CALL])
274-  .add(Symbol.TIME_24, [SHIFT, ReduceActions.AS_NUMBER])
275+  .add(Symbol.TIME_AMPM, [SHIFT, 4])
276+  .add(Symbol.TIME_24, [SHIFT, 5])
277   .add(Symbol.NUMBER, 6)
278-  .add(Symbol.STRING, [SHIFT, ReduceActions.AMPERSAND])
279-  .add(Symbol.PLUS, [SHIFT, ReduceActions.LAST_NUMBER])
280-  .add(Symbol.LEFT_PAREN, [SHIFT, ReduceActions.EQUALS])
281-  .add(Symbol.MINUS, [SHIFT, ReduceActions.PLUS])
282-  .add(Symbol.FUNCTION, [SHIFT, ReduceActions.LTE])
283+  .add(Symbol.STRING, [SHIFT, 7])
284+  .add(Symbol.PLUS, [SHIFT, 10])
285+  .add(Symbol.LEFT_PAREN, [SHIFT, 8])
286+  .add(Symbol.MINUS, [SHIFT, 9])
287+  .add(Symbol.FUNCTION, [SHIFT, 11])
288   .add(Symbol.CELL, 12)
289-  .add(Symbol.FIXEDCELL, [SHIFT, ReduceActions.LT])
290-  .add(Symbol.CELL_UPPER, [SHIFT, ReduceActions.MINUS])
291-  .add(Symbol.VARIABLE, [SHIFT, ReduceActions.NOT])
292-  .add(Symbol.NUMBER_UPPER, [SHIFT, ReduceActions.GT])
293-  .add(Symbol.POUND, [SHIFT, ReduceActions.MULTIPLY])
294+  .add(Symbol.FIXEDCELL, [SHIFT, 16])
295+  .add(Symbol.CELL_UPPER, [SHIFT, 17])
296+  .add(Symbol.VARIABLE, [SHIFT, 14])
297+  .add(Symbol.NUMBER_UPPER, [SHIFT, 15])
298+  .add(Symbol.POUND, [SHIFT, 18])
299   .build();
300 table[11] = ObjectBuilder
301-  .add(Symbol.LEFT_PAREN, [SHIFT, ReduceActions.REDUCE_PERCENT])
302+  .add(Symbol.LEFT_PAREN, [SHIFT, 35])
303   .build();
304 table[12] = ObjectBuilder
305   .add(Symbol.EOF, [REDUCE, ReduceActions.I25])
306@@ -644,7 +644,7 @@ table[17] = ObjectBuilder
307   .add(Symbol.COMMA, [REDUCE, ReduceActions.CELL_VALUE])
308   .build();
309 table[18] = ObjectBuilder
310-  .add(Symbol.VARIABLE, [SHIFT, ReduceActions.REDUCE_LAST_THREE_B])
311+  .add(Symbol.VARIABLE, [SHIFT, 42])
312   .add(Symbol.EOF, [REDUCE, ReduceActions.AS_ERROR])
313   .add(Symbol.RIGHT_PAREN, [REDUCE, ReduceActions.AS_ERROR])
314   .add(Symbol.COMMA, [REDUCE, ReduceActions.AS_ERROR])
315@@ -656,19 +656,19 @@ table[20] = ObjectBuilder
316   .add(Symbol.ERROR, 13)
317   .add(Symbol.EXPRESSION, 43)
318   .add(Symbol.VARIABLE_SEQUENCE, 3)
319-  .add(Symbol.TIME_AMPM, [SHIFT, ReduceActions.TIME_CALL])
320-  .add(Symbol.TIME_24, [SHIFT, ReduceActions.AS_NUMBER])
321+  .add(Symbol.TIME_AMPM, [SHIFT, 4])
322+  .add(Symbol.TIME_24, [SHIFT, 5])
323   .add(Symbol.NUMBER, 6)
324-  .add(Symbol.STRING, [SHIFT, ReduceActions.AMPERSAND])
325-  .add(Symbol.PLUS, [SHIFT, ReduceActions.LAST_NUMBER])
326-  .add(Symbol.LEFT_PAREN, [SHIFT, ReduceActions.EQUALS])
327-  .add(Symbol.MINUS, [SHIFT, ReduceActions.PLUS])
328-  .add(Symbol.FUNCTION, [SHIFT, ReduceActions.LTE])
329+  .add(Symbol.STRING, [SHIFT, 7])
330+  .add(Symbol.PLUS, [SHIFT, 10])
331+  .add(Symbol.LEFT_PAREN, [SHIFT, 8])
332+  .add(Symbol.MINUS, [SHIFT, 9])
333+  .add(Symbol.FUNCTION, [SHIFT, 11])
334   .add(Symbol.CELL, 12)
335-  .add(Symbol.FIXEDCELL, [SHIFT, ReduceActions.LT])
336-  .add(Symbol.CELL_UPPER, [SHIFT, ReduceActions.MINUS])
337-  .add(Symbol.VARIABLE, [SHIFT, ReduceActions.NOT])
338-  .add(Symbol.NUMBER_UPPER, [SHIFT, ReduceActions.GT])
339+  .add(Symbol.FIXEDCELL, [SHIFT, 16])
340+  .add(Symbol.CELL_UPPER, [SHIFT, 17])
341+  .add(Symbol.VARIABLE, [SHIFT, 14])
342+  .add(Symbol.NUMBER_UPPER, [SHIFT, 15])
343   .add(Symbol.POUND, [SHIFT, ReduceActions.MULTIPLY])
344   .build();
345 table[21] = ObjectBuilder
346diff --git a/tests/Parser/ParseEngineTest.ts b/tests/Parser/ParseEngineTest.ts
347index efc9170..b1eca81 100644
348--- a/tests/Parser/ParseEngineTest.ts
349+++ b/tests/Parser/ParseEngineTest.ts
350@@ -317,9 +317,10 @@ parser.setObj("A1");
351 
352 test("Declare number", function () {
353   assertEquals(parser.parse('5'), 5);
354-  assertEquals(parser.parse('  5  '), 5);
355 });
356 
357+console.log("\n\n\n\n\n\n\n");
358+
359 test("Number multiplication", function () {
360   assertEquals(parser.parse('5*5'), 25);
361 });
362\ No newline at end of file