commit
message
[ParseEngine] starting work on just getting a simple syntax working
author
Ben Vogt <[email protected]>
date
2017-12-09 17:47:41
stats
1 file(s) changed,
52 insertions(+),
153 deletions(-)
files
src/Parser/ParseEngine.ts
1diff --git a/src/Parser/ParseEngine.ts b/src/Parser/ParseEngine.ts
2index 6d435b5..3fae5e4 100644
3--- a/src/Parser/ParseEngine.ts
4+++ b/src/Parser/ParseEngine.ts
5@@ -195,13 +195,15 @@ const enum ReduceActions {
6 AS_ERROR = 19,
7 TO_NUMBER_NAN_AS_ZERO = 20,
8 CALL_FUNCTION_LAST_BLANK = 21,
9- FIXED_CELL_VAL = 22,
10- FIXED_CELL_RANGE_VAL = 23,
11- CELL_VALUE = 24,
12- CELL_RANGE_VALUE = 25,
13- PERCENT = 26,
14- START_ARRAY = 27,
15- INVERT_NUMBER = 28
16+ CALL_FUNCTION = 22,
17+ FIXED_CELL_VAL = 23,
18+ FIXED_CELL_RANGE_VAL = 24,
19+ CELL_VALUE = 25,
20+ CELL_RANGE_VALUE = 26,
21+ PERCENT = 27,
22+ START_ARRAY = 28,
23+ INVERT_NUMBER = 29,
24+ EXPRESSION = 30
25 };
26
27 /**
28@@ -234,9 +236,6 @@ class ReductionPair {
29
30 const enum Tree {
31 START = 0,
32- NUMBER = 1,
33- STRING = 2,
34- BOOLEAN = 3,
35 VARIABLE = 4,
36 ERROR = 5,
37 FORMULA = 6,
38@@ -247,7 +246,6 @@ const enum Tree {
39 CARROT = 11,
40 AMPERSAND = 12,
41 PERCENT = 13,
42- PERIOD = 14,
43 LESS_THAN = 15,
44 GREATER_THAN = 16,
45 EQUALS = 17,
46@@ -258,10 +256,11 @@ const enum Tree {
47 FIXED_CELL_REF = 22,
48 CELL_RANGE_REF = 23,
49 FIXED_CELL_RANGE_REF = 24,
50- OPEN_ARRAY = 25,
51- CLOSE_ARRAY = 26,
52+ // OPEN_ARRAY = 25,
53+ // CLOSE_ARRAY = 26,
54 INVERT_NEXT = 27,
55- TERMINATE = 28
56+ EXPRESSION = 28,
57+ TERMINATE = 29
58 }
59
60 /**
61@@ -274,29 +273,29 @@ const enum Tree {
62 let productions : Array<ReductionPair> = [];
63 productions[ReduceActions.NO_ACTION] = null;
64 productions[ReduceActions.RETURN_LAST] = new ReductionPair(3, 2);
65-productions[ReduceActions.CALL_VARIABLE] = new ReductionPair(Tree.NUMBER, 1);
66-productions[ReduceActions.AS_NUMBER] = new ReductionPair(Tree.NUMBER, 1);
67-productions[ReduceActions.INVERT_NUMBER] = new ReductionPair(Tree.NUMBER, 1);
68-productions[ReduceActions.AS_STRING] = new ReductionPair(Tree.STRING, 1);
69+productions[ReduceActions.CALL_VARIABLE] = new ReductionPair(Tree.VARIABLE, 1);
70+productions[ReduceActions.AS_NUMBER] = new ReductionPair(Tree.VARIABLE, 1);
71+productions[ReduceActions.INVERT_NUMBER] = new ReductionPair(Tree.VARIABLE, 1);
72+productions[ReduceActions.AS_STRING] = new ReductionPair(Tree.VARIABLE, 1);
73 productions[ReduceActions.AMPERSAND] = new ReductionPair(Tree.AMPERSAND, 3);
74 productions[ReduceActions.EQUALS] = new ReductionPair(Tree.EQUALS, 3);
75 productions[ReduceActions.PLUS] = new ReductionPair(Tree.PLUS, 3);
76-productions[ReduceActions.LAST_NUMBER] = new ReductionPair(Tree.NUMBER, 3);
77-productions[ReduceActions.LTE] = new ReductionPair(Tree.BOOLEAN, 4);
78-productions[ReduceActions.GTE] = new ReductionPair(Tree.BOOLEAN, 4);
79-productions[ReduceActions.NOT_EQ] = new ReductionPair(Tree.BOOLEAN, 4);
80-productions[ReduceActions.GT] = new ReductionPair(Tree.BOOLEAN, 3);
81-productions[ReduceActions.LT] = new ReductionPair(Tree.BOOLEAN, 3);
82-productions[ReduceActions.MINUS] = new ReductionPair(Tree.NUMBER, 3);
83-productions[ReduceActions.MULTIPLY] = new ReductionPair(Tree.NUMBER, 3);
84-productions[ReduceActions.DIVIDE] = new ReductionPair(Tree.NUMBER, 3);
85-productions[ReduceActions.TO_POWER] = new ReductionPair(Tree.NUMBER, 3);
86-productions[ReduceActions.TO_NUMBER_NAN_AS_ZERO] = new ReductionPair(Tree.NUMBER, 2);
87+productions[ReduceActions.LAST_NUMBER] = new ReductionPair(Tree.VARIABLE, 3);
88+productions[ReduceActions.LTE] = new ReductionPair(Tree.VARIABLE, 4);
89+productions[ReduceActions.GTE] = new ReductionPair(Tree.VARIABLE, 4);
90+productions[ReduceActions.NOT_EQ] = new ReductionPair(Tree.VARIABLE, 4);
91+productions[ReduceActions.GT] = new ReductionPair(Tree.VARIABLE, 3);
92+productions[ReduceActions.LT] = new ReductionPair(Tree.VARIABLE, 3);
93+productions[ReduceActions.MINUS] = new ReductionPair(Tree.VARIABLE, 3);
94+productions[ReduceActions.MULTIPLY] = new ReductionPair(Tree.VARIABLE, 3);
95+productions[ReduceActions.DIVIDE] = new ReductionPair(Tree.VARIABLE, 3);
96+productions[ReduceActions.TO_POWER] = new ReductionPair(Tree.VARIABLE, 3);
97+productions[ReduceActions.TO_NUMBER_NAN_AS_ZERO] = new ReductionPair(Tree.VARIABLE, 2);
98 productions[ReduceActions.FIXED_CELL_VAL] = new ReductionPair(Tree.VARIABLE, 1);
99 productions[ReduceActions.FIXED_CELL_RANGE_VAL] = new ReductionPair(Tree.VARIABLE, 3);
100 productions[ReduceActions.CELL_VALUE] = new ReductionPair(Tree.VARIABLE, 1);
101 productions[ReduceActions.CELL_RANGE_VALUE] = new ReductionPair(Tree.VARIABLE, 3);
102-productions[ReduceActions.PERCENT] = new ReductionPair(Tree.NUMBER, 3);
103+productions[ReduceActions.PERCENT] = new ReductionPair(Tree.VARIABLE, 3);
104 productions[ReduceActions.AS_ERROR] = new ReductionPair(Tree.ERROR, 1);
105 const PRODUCTIONS = productions;
106
107@@ -308,72 +307,35 @@ const PRODUCTIONS = productions;
108 */
109 let table = [];
110 // All functions in the spreadsheet start with a 0-token.
111+// `=`
112 table[Tree.START] = ObjectBuilder
113- .add(Symbol.NUMBER, Tree.NUMBER)
114- .add(Symbol.STRING, Tree.STRING)
115- .add(Symbol.BOOLEAN, Tree.BOOLEAN)
116- .add(Symbol.FORMULA, Tree.FORMULA)
117+ .add(Symbol.VARIABLE, Tree.VARIABLE)
118 .add(Symbol.CELL_REF, [REDUCE, ReduceActions.CELL_VALUE])
119 .add(Symbol.FIXED_CELL_REF, [REDUCE, ReduceActions.FIXED_CELL_VAL])
120 .add(Symbol.OPEN_ARRAY, null) // Start array, push until done? Come back to this one.
121 .add(Symbol.OPEN_PAREN, null)
122 .add(Symbol.PLUS, [SHIFT, ReduceActions.AS_NUMBER]) // If we're starting out, the operator is just the regular number.
123 .add(Symbol.MINUS, [SHIFT, ReduceActions.INVERT_NUMBER]) // If we're starting out, the operator is inverting the next number
124- // .add(Symbol.PERIOD, null) // removing for now, maybe we can capture this with numbers, idk.
125 .add(Symbol.WHITE_SPACE, Tree.START) // loop back.
126 .add(Symbol.END, Tree.TERMINATE)
127 .build();
128-table[Tree.NUMBER] = ObjectBuilder
129+table[Tree.VARIABLE] = ObjectBuilder
130 .add(Symbol.PLUS, [SHIFT, ReduceActions.PLUS])
131 .add(Symbol.MINUS, [SHIFT, ReduceActions.MINUS])
132 .add(Symbol.ASTERISK, [SHIFT, ReduceActions.AS_NUMBER]) // maybe
133- .add(Symbol.DIVIDE, [SHIFT, ReduceActions.DIVIDE]) // At this poing in processing we have "X /" but we need the second variable
134+ .add(Symbol.DIVIDE, [SHIFT, ReduceActions.DIVIDE]) // At this point in processing we have "X /" but we need the second variable
135 .add(Symbol.CARROT, [SHIFT, ReduceActions.TO_POWER])
136 .add(Symbol.PERCENT, [REDUCE, ReduceActions.PERCENT])
137 .add(Symbol.AMPERSAND, [SHIFT, ReduceActions.AMPERSAND])
138- .add(Symbol.GREATER_THAN, [SHIFT, ReduceActions.GT])
139- .add(Symbol.LESS_THAN, null)
140- .add(Symbol.EQUALS, null)
141- .add(Symbol.COMMA, null)
142- .add(Symbol.CLOSE_PAREN, null)
143- .add(Symbol.CLOSE_ARRAY, null)
144- .add(Symbol.WHITE_SPACE, null)
145- .add(Symbol.END, null)
146- .build();
147-table[Tree.STRING] = ObjectBuilder
148- .add(Symbol.PLUS, null)
149- .add(Symbol.MINUS, null)
150- .add(Symbol.ASTERISK, null)
151- .add(Symbol.DIVIDE, null)
152- .add(Symbol.CARROT, null)
153- .add(Symbol.PERCENT, null)
154- .add(Symbol.AMPERSAND, null)
155- .add(Symbol.GREATER_THAN, null)
156- .add(Symbol.LESS_THAN, null)
157- .add(Symbol.EQUALS, null)
158- .add(Symbol.COMMA, null)
159- .add(Symbol.CLOSE_PAREN, null)
160- .add(Symbol.CLOSE_ARRAY, null)
161- .add(Symbol.WHITE_SPACE, null)
162- .add(Symbol.END, null)
163- .build();
164-table[Tree.BOOLEAN] = ObjectBuilder
165- .add(Symbol.PLUS, null)
166- .add(Symbol.MINUS, null)
167- .add(Symbol.ASTERISK, null)
168- .add(Symbol.DIVIDE, null)
169- .add(Symbol.CARROT, null)
170- .add(Symbol.PERCENT, null)
171- .add(Symbol.AMPERSAND, null)
172- .add(Symbol.GREATER_THAN, null)
173- .add(Symbol.LESS_THAN, null)
174- .add(Symbol.EQUALS, null)
175- .add(Symbol.COMMA, null)
176- .add(Symbol.CLOSE_PAREN, null)
177- .add(Symbol.CLOSE_ARRAY, null)
178- .add(Symbol.WHITE_SPACE, null)
179- .add(Symbol.END, null)
180+ .add(Symbol.GREATER_THAN, Tree.GREATER_THAN) // X>__
181+ .add(Symbol.LESS_THAN, Tree.LESS_THAN) // X>__
182+ .add(Symbol.EQUALS, Tree.EQUALS) // X=__
183+ .add(Symbol.COMMA, Tree.COMMA)
184+ .add(Symbol.CLOSE_PAREN, Tree.CLOSE_PAREN)
185+ .add(Symbol.WHITE_SPACE, Tree.VARIABLE)
186+ .add(Symbol.END, Tree.TERMINATE)
187 .build();
188+// TODO: While almost anything can follow an error, we should really just throw the error, right?
189 table[Tree.ERROR] = ObjectBuilder
190 .add(Symbol.PLUS, null)
191 .add(Symbol.MINUS, null)
192@@ -389,17 +351,14 @@ table[Tree.ERROR] = ObjectBuilder
193 .add(Symbol.END, null)
194 .build();
195 table[Tree.FORMULA] = ObjectBuilder
196- .add(Symbol.NUMBER, null)
197- .add(Symbol.STRING, null)
198- .add(Symbol.BOOLEAN, null)
199- .add(Symbol.FORMULA, null)
200- .add(Symbol.CELL_REF, null)
201- .add(Symbol.FIXED_CELL_REF, null)
202- .add(Symbol.PERIOD, null) // TODO: Do we really need period? Why can't we capture this as a number w/ the rule-regex?
203+ .add(Symbol.VARIABLE, Tree.VARIABLE)
204+ .add(Symbol.FORMULA, Tree.FORMULA)
205+ .add(Symbol.CELL_REF, [REDUCE, ReduceActions.CELL_VALUE])
206+ .add(Symbol.FIXED_CELL_REF, [REDUCE, ReduceActions.FIXED_CELL_VAL])
207 .add(Symbol.ERROR, null)
208- .add(Symbol.POUND, null)
209 .add(Symbol.PLUS, null)
210 .add(Symbol.MINUS, null)
211+ .add(Symbol.CLOSE_PAREN, [REDUCE, ReduceActions.CALL_FUNCTION])
212 .add(Symbol.OPEN_PAREN, null)
213 .add(Symbol.OPEN_ARRAY, null)
214 .add(Symbol.WHITE_SPACE, null)
215@@ -507,25 +466,9 @@ table[Tree.PERCENT] = ObjectBuilder
216 .add(Symbol.OPEN_ARRAY, null)
217 .add(Symbol.WHITE_SPACE, null)
218 .add(Symbol.END, null)
219- .build();
220-table[Tree.PERIOD] = ObjectBuilder
221- .add(Symbol.PLUS, null)
222- .add(Symbol.MINUS, null)
223- .add(Symbol.ASTERISK, null)
224- .add(Symbol.DIVIDE, null)
225- .add(Symbol.CARROT, null)
226- .add(Symbol.PERCENT, null)
227- .add(Symbol.AMPERSAND, null)
228- .add(Symbol.GREATER_THAN, null)
229- .add(Symbol.LESS_THAN, null)
230- .add(Symbol.EQUALS, null)
231- .add(Symbol.WHITE_SPACE, null)
232- .add(Symbol.END, null)
233- .build();
234+ .build();;
235 table[Tree.LESS_THAN] = ObjectBuilder
236- .add(Symbol.NUMBER, Tree.NUMBER)
237- .add(Symbol.STRING, null)
238- .add(Symbol.BOOLEAN, null)
239+ .add(Symbol.VARIABLE, null)
240 .add(Symbol.FORMULA, null)
241 .add(Symbol.CELL_REF, null)
242 .add(Symbol.FIXED_CELL_REF, null)
243@@ -539,9 +482,7 @@ table[Tree.LESS_THAN] = ObjectBuilder
244 .add(Symbol.END, null)
245 .build();
246 table[Tree.GREATER_THAN] = ObjectBuilder
247- .add(Symbol.NUMBER, Tree.NUMBER)
248- .add(Symbol.STRING, null)
249- .add(Symbol.BOOLEAN, null)
250+ .add(Symbol.VARIABLE, Tree.VARIABLE)
251 .add(Symbol.FORMULA, null)
252 .add(Symbol.CELL_REF, null)
253 .add(Symbol.FIXED_CELL_REF, null)
254@@ -555,9 +496,7 @@ table[Tree.GREATER_THAN] = ObjectBuilder
255 .add(Symbol.END, null)
256 .build();;
257 table[Tree.EQUALS] = ObjectBuilder
258- .add(Symbol.NUMBER, Tree.NUMBER)
259- .add(Symbol.STRING, null)
260- .add(Symbol.BOOLEAN, null)
261+ .add(Symbol.VARIABLE, Tree.VARIABLE)
262 .add(Symbol.FORMULA, null)
263 .add(Symbol.CELL_REF, null)
264 .add(Symbol.FIXED_CELL_REF, null)
265@@ -571,9 +510,7 @@ table[Tree.EQUALS] = ObjectBuilder
266 .add(Symbol.END, null)
267 .build();;
268 table[Tree.COMMA] = ObjectBuilder
269- .add(Symbol.NUMBER, Tree.NUMBER)
270- .add(Symbol.STRING, null)
271- .add(Symbol.BOOLEAN, null)
272+ .add(Symbol.VARIABLE, Tree.VARIABLE)
273 .add(Symbol.FORMULA, null)
274 .add(Symbol.CELL_REF, null)
275 .add(Symbol.FIXED_CELL_REF, null)
276@@ -587,9 +524,7 @@ table[Tree.COMMA] = ObjectBuilder
277 .add(Symbol.END, null)
278 .build();
279 table[Tree.OPEN_PAREN] = ObjectBuilder
280- .add(Symbol.NUMBER, Tree.NUMBER)
281- .add(Symbol.STRING, null)
282- .add(Symbol.BOOLEAN, null)
283+ .add(Symbol.VARIABLE, Tree.VARIABLE)
284 .add(Symbol.FORMULA, null)
285 .add(Symbol.CELL_REF, null)
286 .add(Symbol.FIXED_CELL_REF, null)
287@@ -681,35 +616,7 @@ table[Tree.FIXED_CELL_RANGE_REF] = ObjectBuilder
288 .add(Symbol.WHITE_SPACE, null)
289 .add(Symbol.END, null)
290 .build();
291-table[Tree.OPEN_ARRAY] = ObjectBuilder
292- .add(Symbol.NUMBER, Tree.NUMBER)
293- .add(Symbol.STRING, null)
294- .add(Symbol.BOOLEAN, null)
295- .add(Symbol.FORMULA, null)
296- .add(Symbol.CELL_REF, null)
297- .add(Symbol.FIXED_CELL_REF, null)
298- .add(Symbol.ERROR, null)
299- .add(Symbol.PLUS, null)
300- .add(Symbol.MINUS, null)
301- .add(Symbol.PERIOD, null)
302- .add(Symbol.OPEN_PAREN, null)
303- .add(Symbol.OPEN_ARRAY, null)
304- .add(Symbol.CLOSE_ARRAY, null)
305- .add(Symbol.WHITE_SPACE, null)
306- .build();
307-table[Tree.CLOSE_ARRAY] = ObjectBuilder
308- .add(Symbol.PLUS, null)
309- .add(Symbol.MINUS, null)
310- .add(Symbol.ASTERISK, null)
311- .add(Symbol.DIVIDE, null)
312- .add(Symbol.PERCENT, null)
313- .add(Symbol.CARROT, null)
314- .add(Symbol.COMMA, null)
315- .add(Symbol.CLOSE_PAREN, null)
316- .add(Symbol.CLOSE_ARRAY, null)
317- .add(Symbol.WHITE_SPACE, null)
318- .add(Symbol.END, null)
319- .build();
320+table[Tree.EXPRESSION] = null;
321 table[Tree.INVERT_NEXT] = null;
322
323