spreadsheet
typeScript/javascript spreadsheet parser, with formulas.
git clone https://git.vogt.world/spreadsheet.git
Log | Files | README.md
← Commit log
commit
message
[Parser,ParserConstants] refactoring constant names, rule-to-symbol mappings
author
Ben Vogt <[email protected]>
date
2017-12-10 04:03:39
stats
2 file(s) changed, 163 insertions(+), 117 deletions(-)
files
src/Parser/Parser.ts
src/Parser/ParserConstants.ts
  1diff --git a/src/Parser/Parser.ts b/src/Parser/Parser.ts
  2index c4d84ad..14b1b51 100644
  3--- a/src/Parser/Parser.ts
  4+++ b/src/Parser/Parser.ts
  5@@ -15,7 +15,7 @@ import {
  6   SHIFT,
  7   SYMBOL_INDEX_TO_NAME,
  8   SYMBOL_NAME_TO_INDEX,
  9-  PRODUCTIONS
 10+  PRODUCTIONS, RuleIndex, Symbol
 11 } from "./ParserConstants"
 12 import {isUndefined} from "../Utilities/MoreUtils";
 13 
 14@@ -689,7 +689,7 @@ let Parser = (function () {
 15       },
 16 
 17       // test the lexed token: return FALSE when not a match, otherwise return token
 18-      test_match: function (match, indexed_rule) {
 19+      testMatch: function (match, indexed_rule) {
 20         let token,
 21           lines,
 22           backup;
 23@@ -744,7 +744,7 @@ let Parser = (function () {
 24         this._backtrack = false;
 25         this._input = this._input.slice(match[0].length);
 26         this.matched += match[0];
 27-        token = this.mapActionToActionIndex(indexed_rule);
 28+        token = this.mapRuleIndexToSymbolEnumeration(indexed_rule);
 29         if (this.done && this._input) {
 30           this.done = false;
 31         }
 32@@ -784,7 +784,7 @@ let Parser = (function () {
 33             match = tempMatch;
 34             index = i;
 35             if (this.options.backtrack_lexer) {
 36-              token = this.test_match(tempMatch, rules[i]);
 37+              token = this.testMatch(tempMatch, rules[i]);
 38               if (token !== false) {
 39                 return token;
 40               } else if (this._backtrack) {
 41@@ -801,7 +801,7 @@ let Parser = (function () {
 42           }
 43         }
 44         if (match) {
 45-          token = this.test_match(match, rules[index]);
 46+          token = this.testMatch(match, rules[index]);
 47           if (token !== false) {
 48             return token;
 49           }
 50@@ -844,84 +844,84 @@ let Parser = (function () {
 51         // flex?
 52       },
 53 
 54-      mapActionToActionIndex: function (ruleIndex) {
 55+      mapRuleIndexToSymbolEnumeration: function (ruleIndex) {
 56         switch (ruleIndex) {
 57-          case 0:
 58+          case RuleIndex.WHITE_SPACE:
 59             // skip whitespace
 60             break;
 61-          case 1:
 62-            return ReduceActions.LAST_NUMBER;
 63-          case 2:
 64-            return ReduceActions.LAST_NUMBER;
 65-          case 3:
 66-            return ReduceActions.CALL_FUNCTION_LAST_BLANK;
 67-          case 4:
 68-            return ReduceActions.AMPERSAND;
 69-          case 5:
 70-            return ReduceActions.EQUALS;
 71-          case 6:
 72-            return ReduceActions.I26;
 73-          case 7:
 74-            return ReduceActions.FIXED_CELL_VAL;
 75-          case 8:
 76-            return ReduceActions.CALL_FUNCTION_LAST_BLANK;
 77-          case 9:
 78-            return ReduceActions.ENSURE_IS_ARRAY;
 79-          case 10:
 80-            return ReduceActions.ENSURE_IS_ARRAY;
 81-          case 11:
 82-            return ReduceActions.REDUCE_INT;
 83-          case 12:
 84-            return ReduceActions.FIXED_CELL_RANGE_VAL;
 85-          case 13:
 86+          case RuleIndex.DOUBLE_QUOTES:
 87+            return Symbol.STRING;
 88+          case RuleIndex.SINGLE_QUOTES:
 89+            return Symbol.STRING;
 90+          case RuleIndex.FORMULA_NAME:
 91+            return Symbol.FUNCTION;
 92+          case RuleIndex.DATE:
 93+            return Symbol.TIME_AMPM;
 94+          case RuleIndex.TIME:
 95+            return Symbol.TIME_24;
 96+          case RuleIndex.$_A1_CELL:
 97+            return Symbol.FIXEDCELL;
 98+          case RuleIndex.A1_CELL:
 99+            return Symbol.CELL_UPPER;
100+          case RuleIndex.FORMULA_NAME_SIMPLE:
101+            return Symbol.FUNCTION;
102+          case RuleIndex.VARIABLE:
103+            return Symbol.VARIABLE;
104+          case RuleIndex.SIMPLE_VARIABLE:
105+            return Symbol.VARIABLE;
106+          case RuleIndex.INTEGER:
107+            return Symbol.NUMBER_UPPER;
108+          case RuleIndex.OPEN_AND_CLOSE_OF_ARRAY:
109+            return Symbol.ARRAY;
110+          case RuleIndex.DOLLAR_SIGN:
111             // skip whitespace??
112             break;
113-          case 14:
114-            return ReduceActions.LTE;
115-          case 15:
116+          case RuleIndex.AMPERSAND_SIGN:
117+            return Symbol.AMPERSAND;
118+          case RuleIndex.SINGLE_WHITESPACE:
119             return ' ';
120-          case 16:
121-            return ReduceActions.ENSURE_YYTEXT_ARRAY;
122-          case 17:
123-            return ReduceActions.I27;
124-          case 18:
125-            return ReduceActions.CELL_VALUE;
126-          case 19:
127-            return ReduceActions.CELL_RANGE_VALUE;
128-          case 20:
129-            return ReduceActions.TO_POWER;
130-          case 21:
131-            return ReduceActions.INVERT_NUM;
132-          case 22:
133-            return ReduceActions.DIVIDE;
134-          case 23:
135-            return ReduceActions.NOT_EQ;
136-          case 24:
137-            return ReduceActions.TO_NUMBER_NAN_AS_ZERO;
138-          case 25:
139-            return ReduceActions.NOT;
140-          case 26:
141-            return ReduceActions.GT;
142-          case 27:
143-            return ReduceActions.MINUS;
144-          case 28:
145-            return ReduceActions.LT;
146-          case 29:
147-            return ReduceActions.MULTIPLY;
148-          case 30:
149+          case RuleIndex.PERIOD:
150+            return Symbol.DECIMAL;
151+          case RuleIndex.COLON:
152+            return Symbol.COLON;
153+          case RuleIndex.SEMI_COLON:
154+            return Symbol.SEMI_COLON;
155+          case RuleIndex.COMMA:
156+            return Symbol.COMMA;
157+          case RuleIndex.ASTERISK:
158+            return Symbol.ASTERISK;
159+          case RuleIndex.FORWARD_SLASH:
160+            return Symbol.DIVIDE;
161+          case RuleIndex.MINUS_SIGN:
162+            return Symbol.MINUS;
163+          case RuleIndex.PLUS_SIGN:
164+            return Symbol.PLUS;
165+          case RuleIndex.CARET_SIGN:
166+            return Symbol.CARROT;
167+          case RuleIndex.OPEN_PAREN:
168+            return Symbol.LEFT_PAREN;
169+          case RuleIndex.CLOSE_PAREN:
170+            return Symbol.RIGHT_PAREN;
171+          case RuleIndex.GREATER_THAN_SIGN:
172+            return Symbol.GREATER_THAN;
173+          case RuleIndex.LESS_THAN_SIGN:
174+            return Symbol.LESS_THAN;
175+          case RuleIndex.NOT:
176+            return Symbol.NOT;
177+          case RuleIndex.OPEN_DOUBLE_QUOTE:
178             return '"';
179-          case 31:
180+          case RuleIndex.OPEN_SINGLE_QUITE:
181             return "'";
182-          case 32:
183+          case RuleIndex.EXCLAMATION_POINT_RULE:
184             return "!";
185-          case 33:
186-            return ReduceActions.GTE;
187-          case 34:
188-            return ReduceActions.REDUCE_PERCENT;
189-          case 35:
190-            return ReduceActions.WRAP_CURRENT_INDEX_TOKEN_AS_ARRAY;
191-          case 36:
192-            return ReduceActions.AS_NUMBER;
193+          case RuleIndex.EQUALS_SIGN:
194+            return Symbol.EQUALS;
195+          case RuleIndex.PERCENT_SIGN:
196+            return Symbol.PERCENT;
197+          case RuleIndex.POUND_SIGN:
198+            return Symbol.POUND;
199+          case RuleIndex.END_OF_STRING:
200+            return Symbol.EOF;
201         }
202       },
203       conditions: {
204diff --git a/src/Parser/ParserConstants.ts b/src/Parser/ParserConstants.ts
205index f049bf5..a30ed50 100644
206--- a/src/Parser/ParserConstants.ts
207+++ b/src/Parser/ParserConstants.ts
208@@ -38,51 +38,93 @@ const OPEN_SINGLE_QUITE = /^(?:')/; // rule 31
209 const EXCLAMATION_POINT_RULE = /^(?:!)/; // rule 32
210 const EQUALS_SIGN_RULE = /^(?:=)/; // rule 33
211 const PERCENT_SIGN_RULE = /^(?:%)/; // rule 34
212-// TODO: POUND_SIGN_RULE Modified from /^(?:[#])/, which matches pound sign exclusively. Now specific to errors.
213+// TODO: POUND_SIGN Modified from /^(?:[#])/, which matches pound sign exclusively. Now specific to errors.
214 // TODO: Should be renamed.
215 const POUND_SIGN_RULE = /^(?:#N\/A|#NUM\!|#NULL\!|#DIV\/0\!|#VALUE\!|#REF\!|#ERROR)/; // rule 35
216 const END_OF_STRING_RULE = /^(?:$)/; // rule 36
217 
218+
219+const enum RuleIndex {
220+  WHITE_SPACE = 0,
221+  DOUBLE_QUOTES = 1,
222+  SINGLE_QUOTES = 2,
223+  FORMULA_NAME = 3,
224+  DATE = 4,
225+  TIME = 5,
226+  $_A1_CELL = 6,
227+  A1_CELL = 7,
228+  FORMULA_NAME_SIMPLE = 8,
229+  VARIABLE = 9,
230+  SIMPLE_VARIABLE = 10,
231+  INTEGER = 11,
232+  OPEN_AND_CLOSE_OF_ARRAY = 12,
233+  DOLLAR_SIGN = 13,
234+  AMPERSAND_SIGN = 14,
235+  SINGLE_WHITESPACE = 15,
236+  PERIOD = 16,
237+  COLON = 17,
238+  SEMI_COLON = 18,
239+  COMMA = 19,
240+  ASTERISK = 20,
241+  FORWARD_SLASH = 21,
242+  MINUS_SIGN = 22,
243+  PLUS_SIGN = 23,
244+  CARET_SIGN = 24,
245+  OPEN_PAREN = 25,
246+  CLOSE_PAREN = 26,
247+  GREATER_THAN_SIGN = 27,
248+  LESS_THAN_SIGN = 28,
249+  NOT = 29,
250+  OPEN_DOUBLE_QUOTE = 30,
251+  OPEN_SINGLE_QUITE = 31,
252+  EXCLAMATION_POINT_RULE = 32,
253+  EQUALS_SIGN = 33,
254+  PERCENT_SIGN = 34,
255+  POUND_SIGN = 35,
256+  END_OF_STRING = 36
257+};
258+
259+
260 // Sequential rules to use when parsing a given input.
261-const RULES = [
262-  WHITE_SPACE_RULE,
263-  DOUBLE_QUOTES_RULE,
264-  SINGLE_QUOTES_RULE,
265-  FORMULA_NAME_RULE,
266-  DATE_RULE,
267-  TIME_RULE,
268-  $_A1_CELL_RULE,
269-  A1_CELL_RULE,
270-  FORMULA_NAME_SIMPLE_RULE,
271-  VARIABLE_RULE,
272-  SIMPLE_VARIABLE_RILE,
273-  INTEGER_RULE,
274-  OPEN_AND_CLOSE_OF_ARRAY_RULE,
275-  DOLLAR_SIGN_RULE,
276-  AMPERSAND_SIGN_RULE,
277-  SINGLE_WHITESPACE_RULE,
278-  PERIOD_RULE,
279-  COLON_RULE,
280-  SEMI_COLON_RULE,
281-  COMMA_RULE,
282-  ASTERISK_RULE,
283-  FORWARD_SLASH_RULE,
284-  MINUS_SIGN_RULE,
285-  PLUS_SIGN_RULE,
286-  CARET_SIGN_RULE,
287-  OPEN_PAREN_RULE,
288-  CLOSE_PAREN_RULE,
289-  GREATER_THAN_SIGN_RULE,
290-  LESS_THAN_SIGN_RULE,
291-  NOT_RULE,
292-  OPEN_DOUBLE_QUOTE,
293-  OPEN_SINGLE_QUITE,
294-  EXCLAMATION_POINT_RULE,
295-  EQUALS_SIGN_RULE,
296-  PERCENT_SIGN_RULE,
297-  POUND_SIGN_RULE,
298-  END_OF_STRING_RULE
299-];
300+let RULES = [];
301+RULES[RuleIndex.WHITE_SPACE] = WHITE_SPACE_RULE;
302+RULES[RuleIndex.DOUBLE_QUOTES] = DOUBLE_QUOTES_RULE;
303+RULES[RuleIndex.SINGLE_QUOTES] = SINGLE_QUOTES_RULE;
304+RULES[RuleIndex.FORMULA_NAME] = FORMULA_NAME_RULE;
305+RULES[RuleIndex.DATE] = DATE_RULE;
306+RULES[RuleIndex.TIME] = TIME_RULE;
307+RULES[RuleIndex.$_A1_CELL] = $_A1_CELL_RULE;
308+RULES[RuleIndex.A1_CELL] = A1_CELL_RULE;
309+RULES[RuleIndex.FORMULA_NAME_SIMPLE] = FORMULA_NAME_SIMPLE_RULE;
310+RULES[RuleIndex.VARIABLE] = VARIABLE_RULE;
311+RULES[RuleIndex.SIMPLE_VARIABLE] = SIMPLE_VARIABLE_RILE;
312+RULES[RuleIndex.INTEGER] = INTEGER_RULE;
313+RULES[RuleIndex.OPEN_AND_CLOSE_OF_ARRAY] = OPEN_AND_CLOSE_OF_ARRAY_RULE;
314+RULES[RuleIndex.DOLLAR_SIGN] = DOLLAR_SIGN_RULE;
315+RULES[RuleIndex.AMPERSAND_SIGN] = AMPERSAND_SIGN_RULE;
316+RULES[RuleIndex.SINGLE_WHITESPACE] = SINGLE_WHITESPACE_RULE;
317+RULES[RuleIndex.PERIOD] = PERIOD_RULE;
318+RULES[RuleIndex.COLON] = COLON_RULE;
319+RULES[RuleIndex.SEMI_COLON] = SEMI_COLON_RULE;
320+RULES[RuleIndex.COMMA] = COMMA_RULE;
321+RULES[RuleIndex.ASTERISK] = ASTERISK_RULE;
322+RULES[RuleIndex.FORWARD_SLASH] = FORWARD_SLASH_RULE;
323+RULES[RuleIndex.MINUS_SIGN] = MINUS_SIGN_RULE;
324+RULES[RuleIndex.PLUS_SIGN] = PLUS_SIGN_RULE;
325+RULES[RuleIndex.CARET_SIGN] = CARET_SIGN_RULE;
326+RULES[RuleIndex.OPEN_PAREN] = OPEN_PAREN_RULE;
327+RULES[RuleIndex.CLOSE_PAREN] = CLOSE_PAREN_RULE;
328+RULES[RuleIndex.GREATER_THAN_SIGN] = GREATER_THAN_SIGN_RULE;
329+RULES[RuleIndex.LESS_THAN_SIGN] = LESS_THAN_SIGN_RULE;
330+RULES[RuleIndex.NOT] = NOT_RULE;
331+RULES[RuleIndex.OPEN_DOUBLE_QUOTE] = OPEN_DOUBLE_QUOTE;
332+RULES[RuleIndex.OPEN_SINGLE_QUITE] = OPEN_SINGLE_QUITE;
333+RULES[RuleIndex.EXCLAMATION_POINT_RULE] = EXCLAMATION_POINT_RULE;
334+RULES[RuleIndex.EQUALS_SIGN] = EQUALS_SIGN_RULE;
335+RULES[RuleIndex.PERCENT_SIGN] = PERCENT_SIGN_RULE;
336+RULES[RuleIndex.POUND_SIGN] = POUND_SIGN_RULE;
337+RULES[RuleIndex.END_OF_STRING] = END_OF_STRING_RULE;
338+
339 
340 /**
341  * Actions to take when processing tokens one by one. We're always either taking the next token, reducing our current
342@@ -1492,5 +1534,6 @@ export {
343   SHIFT,
344   SYMBOL_INDEX_TO_NAME,
345   SYMBOL_NAME_TO_INDEX,
346-  PRODUCTIONS
347+  PRODUCTIONS,
348+  RuleIndex
349 }
350\ No newline at end of file