spreadsheet
typeScript/javascript spreadsheet parser, with formulas.
git clone https://git.vogt.world/spreadsheet.git
Log | Files | README.md
← Commit log
commit
message
[Parser/Rules] moving rules out of parser for readability
author
Ben Vogt <[email protected]>
date
2017-08-13 22:27:39
stats
3 file(s) changed, 169 insertions(+), 40 deletions(-)
files
src/Parser.ts
src/Parser/Parser.ts
src/Parser/Rules.ts
src/Sheet.ts
  1diff --git a/src/Parser.ts b/src/Parser/Parser.ts
  2similarity index 96%
  3rename from src/Parser.ts
  4rename to src/Parser/Parser.ts
  5index f8ca569..36dd299 100644
  6--- a/src/Parser.ts
  7+++ b/src/Parser/Parser.ts
  8@@ -1,3 +1,8 @@
  9+import {
 10+  RULES
 11+} from "./Rules";
 12+
 13+
 14 /* parser generated by jison 0.4.15 */
 15 /*
 16  Returns a Parser object of the following structure:
 17@@ -1498,46 +1503,7 @@ var Parser = (function () {
 18             return 5;
 19         }
 20       },
 21-      // NOTE: Alterations made in some regular-expressions to allow for formulas containing dot-notation. Eg: F.INV
 22-      rules: [/^(?:\s+)/, // rule 0
 23-        /^(?:"(\\["]|[^"])*")/, // rule 1
 24-        /^(?:'(\\[']|[^'])*')/, // rule 2
 25-        // Changed from /^(?:[A-Za-z]{1,}[A-Za-z_0-9]+(?=[(]))/
 26-        /^(?:[A-Za-z.]{1,}[A-Za-z_0-9]+(?=[(]))/, // rule 3
 27-        /^(?:([0]?[1-9]|1[0-2])[:][0-5][0-9]([:][0-5][0-9])?[ ]?(AM|am|aM|Am|PM|pm|pM|Pm))/, // rule 4
 28-        /^(?:([0]?[0-9]|1[0-9]|2[0-3])[:][0-5][0-9]([:][0-5][0-9])?)/, // rule 5
 29-        /^(?:\$[A-Za-z]+\$[0-9]+)/, // rule 6
 30-        /^(?:[A-Za-z]+[0-9]+)/, // rule 7
 31-        // Changed from /^(?:[A-Za-z.]+(?=[(]))/
 32-        /^(?:[A-Za-z.]+(?=[(]))/, // rule 8
 33-        /^(?:[A-Za-z]{1,}[A-Za-z_0-9]+)/, // rule 9
 34-        /^(?:[A-Za-z_]+)/, // rule 10
 35-        /^(?:[0-9]+)/, // rule 11
 36-        /^(?:\[(.*)?\])/, // rule 12
 37-        /^(?:\$)/, // rule 13
 38-        /^(?:&)/, // rule 14
 39-        /^(?: )/, // rule 15
 40-        /^(?:[.])/, // rule 16
 41-        /^(?::)/, // rule 17
 42-        /^(?:;)/, // rule 18
 43-        /^(?:,)/, // rule 19
 44-        /^(?:\*)/, // rule 20
 45-        /^(?:\/)/, // rule 21
 46-        /^(?:-)/, // rule 22
 47-        /^(?:\+)/, // rule 23
 48-        /^(?:\^)/, // rule 24
 49-        /^(?:\()/, // rule 25
 50-        /^(?:\))/, // rule 26
 51-        /^(?:>)/, // rule 27
 52-        /^(?:<)/, // rule 28
 53-        /^(?:NOT\b)/, // rule 29
 54-        /^(?:")/, // rule 30
 55-        /^(?:')/, // rule 31
 56-        /^(?:!)/, // rule 32
 57-        /^(?:=)/, // rule 33
 58-        /^(?:%)/, // rule 34
 59-        /^(?:[#])/, // rule 35
 60-        /^(?:$)/], // rule 36
 61+      rules: RULES,
 62       conditions: {
 63         "INITIAL": {
 64           "rules": [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37],
 65diff --git a/src/Parser/Rules.ts b/src/Parser/Rules.ts
 66new file mode 100644
 67index 0000000..d1dba0e
 68--- /dev/null
 69+++ b/src/Parser/Rules.ts
 70@@ -0,0 +1,163 @@
 71+// Rules represent the Regular Expressions that will be used in sequence to match a given input to the Parser.
 72+const WHITE_SPACE_RULE = /^(?:\s+)/; // rule 0
 73+const DOUBLE_QUOTES_RULE = /^(?:"(\\["]|[^"])*")/; // rule 1
 74+const SINGLE_QUOTES_RULE = /^(?:'(\\[']|[^'])*')/; // rule 2
 75+const FORMULA_NAME_RULE = /^(?:[A-Za-z.]{1,}[A-Za-z_0-9]+(?=[(]))/; // Changed from /^(?:[A-Za-z]{1,}[A-Za-z_0-9]+(?=[(]))/
 76+const DATE_RULE= /^(?:([0]?[1-9]|1[0-2])[:][0-5][0-9]([:][0-5][0-9])?[ ]?(AM|am|aM|Am|PM|pm|pM|Pm))/; // rule 4
 77+const TIME_RULE = /^(?:([0]?[0-9]|1[0-9]|2[0-3])[:][0-5][0-9]([:][0-5][0-9])?)/; // rule 5
 78+const $_A1_CELL_RULE = /^(?:\$[A-Za-z]+\$[0-9]+)/; // rule 6
 79+const A1_CELL_RULE = /^(?:[A-Za-z]+[0-9]+)/; // rules 7
 80+const FORMULA_NAME_SIMPLE_RULE = /^(?:[A-Za-z.]+(?=[(]))/; // rule 8
 81+const VARIABLE_RULE = /^(?:[A-Za-z]{1,}[A-Za-z_0-9]+)/; // rule 9
 82+const SIMPLE_VARIABLE_RILE = /^(?:[A-Za-z_]+)/; //rule 10
 83+const INTEGER_RULE = /^(?:[0-9]+)/; //rule 11
 84+const OPEN_AND_CLOSE_OF_ARRAY_RULE = /^(?:\[(.*)?\])/; // rule 12
 85+const DOLLAR_SIGN_RULE = /^(?:\$)/; // rule 13
 86+const AMPERSAND_SIGN_RULE = /^(?:&)/; //rule 14
 87+const SINGLE_WHITESPACE_RULE = /^(?: )/; // rule 15
 88+const PERIOD_RULE = /^(?:[.])/; // rule 16
 89+const COLON_RULE = /^(?::)/; //rule 17
 90+const SEMI_COLON_RULE = /^(?:;)/; // rule 18
 91+const COMMA_RULE = /^(?:,)/; // rule 19
 92+const ASTERISK_RULE = /^(?:\*)/; //rule 20
 93+const FORWARD_SLASH_RULE = /^(?:\/)/; // rule 21
 94+const MINUS_SIGN_RULE = /^(?:-)/; // rule 22
 95+const PLUS_SIGN_RULE = /^(?:\+)/; // rule 23
 96+const CARET_SIGN_RULE = /^(?:\^)/; //rule 24
 97+const OPEN_PAREN_RULE = /^(?:\()/; // rule 25
 98+const CLOSE_PAREN_RULE = /^(?:\))/; // rule 26
 99+const GREATER_THAN_SIGN_RULE = /^(?:>)/; // rule 27
100+const LESS_THAN_SIGN_RULE = /^(?:<)/; // rule 28
101+const NOT_RULE = /^(?:NOT\b)/; // rule 29
102+const OPEN_DOUBLE_QUOTE = /^(?:")/; // rule 30
103+const OPEN_SINGLE_QUITE = /^(?:')/; // rule 31
104+const EXCLAMATION_POINT_RULE = /^(?:!)/; // rule 32
105+const EQUALS_SIGN_RULE = /^(?:=)/; // rule 33
106+const PERCENT_SIGN_RULE = /^(?:%)/; // rule 34
107+const HASH_SIGN_RULE = /^(?:[#])/; // rule 35
108+const END_OF_STRING_RULE = /^(?:$)/; // rule 36
109+
110+
111+// Sequential rules to use when parsing a given input.
112+const RULES = [
113+  WHITE_SPACE_RULE,
114+  DOUBLE_QUOTES_RULE,
115+  SINGLE_QUOTES_RULE,
116+  FORMULA_NAME_RULE,
117+  DATE_RULE,
118+  TIME_RULE,
119+  $_A1_CELL_RULE,
120+  A1_CELL_RULE,
121+  FORMULA_NAME_SIMPLE_RULE,
122+  VARIABLE_RULE,
123+  SIMPLE_VARIABLE_RILE,
124+  INTEGER_RULE,
125+  OPEN_AND_CLOSE_OF_ARRAY_RULE,
126+  DOLLAR_SIGN_RULE,
127+  AMPERSAND_SIGN_RULE,
128+  SINGLE_WHITESPACE_RULE,
129+  PERIOD_RULE,
130+  COLON_RULE,
131+  SEMI_COLON_RULE,
132+  COMMA_RULE,
133+  ASTERISK_RULE,
134+  FORWARD_SLASH_RULE,
135+  MINUS_SIGN_RULE,
136+  PLUS_SIGN_RULE,
137+  CARET_SIGN_RULE,
138+  OPEN_PAREN_RULE,
139+  CLOSE_PAREN_RULE,
140+  GREATER_THAN_SIGN_RULE,
141+  LESS_THAN_SIGN_RULE,
142+  NOT_RULE,
143+  OPEN_DOUBLE_QUOTE,
144+  OPEN_SINGLE_QUITE,
145+  EXCLAMATION_POINT_RULE,
146+  EQUALS_SIGN_RULE,
147+  PERCENT_SIGN_RULE,
148+  HASH_SIGN_RULE,
149+  END_OF_STRING_RULE
150+];
151+
152+// While it is unlikely that an index for a given rule will change, initializing them with `indexOf` will allow me to
153+// change their ordering without having to find and replace indexes manually.
154+const WHITE_SPACE_RULE_INDEX = RULES.indexOf(WHITE_SPACE_RULE);
155+const DOUBLE_QUOTES_RULE_INDEX = RULES.indexOf(DOUBLE_QUOTES_RULE);
156+const SINGLE_QUOTES_RULE_INDEX = RULES.indexOf(SINGLE_QUOTES_RULE);
157+const FORMULA_NAME_RULE_INDEX = RULES.indexOf(FORMULA_NAME_RULE);
158+const DATE_RULE_INDEX = RULES.indexOf(DATE_RULE);
159+const TIME_RULE_INDEX = RULES.indexOf(TIME_RULE);
160+const $_A1_CELL_RULE_INDEX = RULES.indexOf($_A1_CELL_RULE);
161+const A1_CELL_RULE_INDEX = RULES.indexOf(A1_CELL_RULE);
162+const FORMULA_NAME_SIMPLE_RULE_INDEX = RULES.indexOf(FORMULA_NAME_SIMPLE_RULE);
163+const VARIABLE_RULE_INDEX = RULES.indexOf(VARIABLE_RULE);
164+const SIMPLE_VARIABLE_RILE_INDEX = RULES.indexOf(SIMPLE_VARIABLE_RILE);
165+const INTEGER_RULE_INDEX = RULES.indexOf(INTEGER_RULE);
166+const OPEN_AND_CLOSE_OF_ARRAY_RULE_INDEX = RULES.indexOf(OPEN_AND_CLOSE_OF_ARRAY_RULE);
167+const DOLLAR_SIGN_RULE_INDEX = RULES.indexOf(DOLLAR_SIGN_RULE);
168+const AMPERSAND_SIGN_RULE_INDEX = RULES.indexOf(AMPERSAND_SIGN_RULE);
169+const SINGLE_WHITESPACE_RULE_INDEX = RULES.indexOf(SINGLE_WHITESPACE_RULE);
170+const PERIOD_RULE_INDEX = RULES.indexOf(PERIOD_RULE);
171+const COLON_RULE_INDEX = RULES.indexOf(COLON_RULE);
172+const SEMI_COLON_RULE_INDEX = RULES.indexOf(SEMI_COLON_RULE);
173+const COMMA_RULE_INDEX = RULES.indexOf(COMMA_RULE);
174+const ASTERISK_RULE_INDEX = RULES.indexOf(ASTERISK_RULE);
175+const FORWARD_SLASH_RULE_INDEX = RULES.indexOf(FORWARD_SLASH_RULE);
176+const MINUS_SIGN_RULE_INDEX = RULES.indexOf(MINUS_SIGN_RULE);
177+const PLUS_SIGN_RULE_INDEX = RULES.indexOf(PLUS_SIGN_RULE);
178+const CARET_SIGN_RULE_INDEX = RULES.indexOf(CARET_SIGN_RULE);
179+const OPEN_PAREN_RULE_INDEX = RULES.indexOf(OPEN_PAREN_RULE);
180+const CLOSE_PAREN_RULE_INDEX = RULES.indexOf(CLOSE_PAREN_RULE);
181+const GREATER_THAN_SIGN_RULE_INDEX = RULES.indexOf(GREATER_THAN_SIGN_RULE);
182+const LESS_THAN_SIGN_RULE_INDEX = RULES.indexOf(LESS_THAN_SIGN_RULE);
183+const NOT_RULE_INDEX = RULES.indexOf(NOT_RULE);
184+const OPEN_DOUBLE_QUOTE_INDEX = RULES.indexOf(OPEN_DOUBLE_QUOTE);
185+const OPEN_SINGLE_QUITE_INDEX = RULES.indexOf(OPEN_SINGLE_QUITE);
186+const EXCLAMATION_POINT_RULE_INDEX = RULES.indexOf(EXCLAMATION_POINT_RULE);
187+const EQUALS_SIGN_RULE_INDEX = RULES.indexOf(EQUALS_SIGN_RULE);
188+const PERCENT_SIGN_RULE_INDEX = RULES.indexOf(PERCENT_SIGN_RULE);
189+const HASH_SIGN_RULE_INDEX = RULES.indexOf(HASH_SIGN_RULE);
190+const END_OF_STRING_RULE_INDEX = RULES.indexOf(END_OF_STRING_RULE);
191+
192+
193+export {
194+  RULES,
195+
196+  WHITE_SPACE_RULE_INDEX,
197+  DOUBLE_QUOTES_RULE_INDEX,
198+  SINGLE_QUOTES_RULE_INDEX,
199+  FORMULA_NAME_RULE_INDEX,
200+  DATE_RULE_INDEX,
201+  TIME_RULE_INDEX,
202+  $_A1_CELL_RULE_INDEX,
203+  A1_CELL_RULE_INDEX,
204+  FORMULA_NAME_SIMPLE_RULE_INDEX,
205+  VARIABLE_RULE_INDEX,
206+  SIMPLE_VARIABLE_RILE_INDEX,
207+  INTEGER_RULE_INDEX,
208+  OPEN_AND_CLOSE_OF_ARRAY_RULE_INDEX,
209+  DOLLAR_SIGN_RULE_INDEX,
210+  AMPERSAND_SIGN_RULE_INDEX,
211+  SINGLE_WHITESPACE_RULE_INDEX,
212+  PERIOD_RULE_INDEX,
213+  COLON_RULE_INDEX,
214+  SEMI_COLON_RULE_INDEX,
215+  COMMA_RULE_INDEX,
216+  ASTERISK_RULE_INDEX,
217+  FORWARD_SLASH_RULE_INDEX,
218+  MINUS_SIGN_RULE_INDEX,
219+  PLUS_SIGN_RULE_INDEX,
220+  CARET_SIGN_RULE_INDEX,
221+  OPEN_PAREN_RULE_INDEX,
222+  CLOSE_PAREN_RULE_INDEX,
223+  GREATER_THAN_SIGN_RULE_INDEX,
224+  LESS_THAN_SIGN_RULE_INDEX,
225+  NOT_RULE_INDEX,
226+  OPEN_DOUBLE_QUOTE_INDEX,
227+  OPEN_SINGLE_QUITE_INDEX,
228+  EXCLAMATION_POINT_RULE_INDEX,
229+  EQUALS_SIGN_RULE_INDEX,
230+  PERCENT_SIGN_RULE_INDEX,
231+  HASH_SIGN_RULE_INDEX,
232+  END_OF_STRING_RULE_INDEX
233+}
234\ No newline at end of file
235diff --git a/src/Sheet.ts b/src/Sheet.ts
236index bd1204a..b0dd3bd 100644
237--- a/src/Sheet.ts
238+++ b/src/Sheet.ts
239@@ -1,6 +1,6 @@
240 import {
241   Parser
242-} from "./Parser";
243+} from "./Parser/Parser";
244 import {
245   Cell
246 } from "./Cell";