-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformula.js
284 lines (247 loc) · 7.52 KB
/
formula.js
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
import { createToken, tokenMatcher, Lexer, CstParser } from "chevrotain";
import _ from "lodash";
// using the NA pattern marks this Token class as 'irrelevant' for the Lexer.
// AdditionOperator defines a Tokens hierarchy but only the leafs in this hierarchy define
// actual Tokens that can appear in the text
const AdditionOperator = createToken({
name: "AdditionOperator",
pattern: Lexer.NA,
});
const Plus = createToken({
name: "Plus",
pattern: /\+/,
categories: AdditionOperator,
});
const Minus = createToken({
name: "Minus",
pattern: /-/,
categories: AdditionOperator,
});
const MultiplicationOperator = createToken({
name: "MultiplicationOperator",
pattern: Lexer.NA,
});
const Multi = createToken({
name: "Multi",
pattern: /\*/,
categories: MultiplicationOperator,
});
const Div = createToken({
name: "Div",
pattern: /\//,
categories: MultiplicationOperator,
});
const LParen = createToken({ name: "LParen", pattern: /\(/ });
const RParen = createToken({ name: "RParen", pattern: /\)/ });
const NumberLiteral = createToken({
name: "NumberLiteral",
pattern: /(0|[1-9]\d*)(\.\d+)?/,
});
const ReferenceLiteral = createToken({
name: "ReferenceLiteral",
pattern: /\w+/,
});
const FormulaLiteral = createToken({
name: "FormulaLiteral",
pattern: Lexer.NA,
});
const Sum = createToken({
name: "Sum",
pattern: /SUM/,
categories: FormulaLiteral,
});
const Avg = createToken({
name: "Avg",
pattern: /AVG/,
categories: FormulaLiteral,
});
const Max = createToken({
name: "Max",
pattern: /MAX/,
categories: FormulaLiteral,
});
const Min = createToken({
name: "Min",
pattern: /MIN/,
categories: FormulaLiteral,
});
// marking WhiteSpace as 'SKIPPED' makes the lexer skip it.
const WhiteSpace = createToken({
name: "WhiteSpace",
pattern: /\s+/,
group: Lexer.SKIPPED,
});
const allTokens = [
WhiteSpace, // whitespace is normally very common so it should be placed first to speed up the lexer's performance
Plus,
Minus,
Multi,
Div,
LParen,
RParen,
NumberLiteral,
AdditionOperator,
MultiplicationOperator,
Sum,
Avg,
Max,
Min,
FormulaLiteral,
ReferenceLiteral,
];
const FormulaLexer = new Lexer(allTokens);
class FormulaParser extends CstParser{
constructor() {
super(allTokens);
const $ = this;
$.RULE("expression", () => {
$.OR([
{ ALT: () => $.SUBRULE($.uminusExpression) },
{ ALT: () => $.SUBRULE($.additionExpression) },
]);
});
$.RULE("uminusExpression", () => {
$.CONSUME(Minus);
$.SUBRULE($.expression);
});
// lowest precedence thus it is first in the rule chain
// The precedence of binary expressions is determined by how far down the Parse Tree
// The binary expression appears.
$.RULE("additionExpression", () => {
$.SUBRULE($.multiplicationExpression, { LABEL: "lhs" });
$.MANY(() => {
// consuming 'AdditionOperator' will consume either Plus or Minus as they are subclasses of AdditionOperator
$.CONSUME(AdditionOperator);
// the index "2" in SUBRULE2 is needed to identify the unique position in the grammar during runtime
$.SUBRULE2($.multiplicationExpression, { LABEL: "rhs" });
});
});
$.RULE("multiplicationExpression", () => {
$.SUBRULE($.atomicExpression, { LABEL: "lhs" });
$.MANY(() => {
$.CONSUME(MultiplicationOperator);
// the index "2" in SUBRULE2 is needed to identify the unique position in the grammar during runtime
$.SUBRULE2($.atomicExpression, { LABEL: "rhs" });
});
});
$.RULE("formulaExpression", () => {
$.CONSUME(FormulaLiteral, {LABEL: "formula"});
$.CONSUME(LParen);
$.CONSUME(ReferenceLiteral, {LABEL: "reference"});
$.CONSUME(RParen);
})
$.RULE("atomicExpression", () =>
$.OR([
// parenthesisExpression has the highest precedence and thus it appears
// in the "lowest" leaf in the expression ParseTree.
{ ALT: () => $.SUBRULE($.parenthesisExpression) },
{ ALT: () => $.CONSUME(NumberLiteral) },
{ ALT: () => $.SUBRULE($.formulaExpression) },
])
);
$.RULE("parenthesisExpression", () => {
$.CONSUME(LParen);
$.SUBRULE($.expression);
$.CONSUME(RParen);
});
this.performSelfAnalysis();
}
}
// wrapping it all together
// reuse the same parser instance.
const parser = new FormulaParser([]);
const defaultRuleName = 'expression';
// ----------------- Interpreter -----------------
const BaseCstVisitor = parser.getBaseCstVisitorConstructor();
class FormulaInterpreter extends BaseCstVisitor {
constructor(data) {
super();
this.data = data;
// This helper will detect any missing or redundant methods on this visitor
this.validateVisitor();
}
expression(ctx) {
if (ctx.additionExpression){
return this.visit(ctx.additionExpression);
} else { // uminus
return this.visit(ctx.uminusExpression);
}
}
uminusExpression(ctx) {
return -1 * this.visit(ctx.expression);
}
additionExpression(ctx) {
let result = this.visit(ctx.lhs);
// "rhs" key may be undefined as the grammar defines it as optional (MANY === zero or more).
if (ctx.rhs) {
ctx.rhs.forEach((rhsOperand, idx) => {
// there will be one operator for each rhs operand
let rhsValue = this.visit(rhsOperand);
let operator = ctx.AdditionOperator[idx];
if (tokenMatcher(operator, Plus)) {
result += rhsValue;
} else {
// Minus
result -= rhsValue;
}
});
}
return result;
}
multiplicationExpression(ctx) {
let result = this.visit(ctx.lhs);
// "rhs" key may be undefined as the grammar defines it as optional (MANY === zero or more).
if (ctx.rhs) {
ctx.rhs.forEach((rhsOperand, idx) => {
// there will be one operator for each rhs operand
let rhsValue = this.visit(rhsOperand);
let operator = ctx.MultiplicationOperator[idx];
if (tokenMatcher(operator, Multi)) {
result *= rhsValue;
} else {
// Division
result /= rhsValue;
}
});
}
return result;
}
atomicExpression(ctx) {
if (ctx.parenthesisExpression) {
// passing an array to "this.visit" is equivalent
// to passing the array's first element
return this.visit(ctx.parenthesisExpression);
} else if (ctx.NumberLiteral) {
// If a key exists on the ctx, at least one element is guaranteed
return parseFloat(ctx.NumberLiteral[0].image);
} else if (ctx.formulaExpression) {
return this.visit(ctx.formulaExpression);
}
}
parenthesisExpression(ctx) {
// The ctx will also contain the parenthesis tokens, but we don't care about those
// in the context of calculating the result.
return this.visit(ctx.expression);
}
formulaExpression(ctx) {
let formula = ctx.formula[0];
let reference = ctx.reference[0].image;
if (!this.data.hasOwnProperty(reference)) {
throw new Error(`Unknown reference: ${reference}`);
}
let values = this.data[reference];
if (values.length === 0) {
throw new Error(`Empty reference: ${reference}`);
}
if (tokenMatcher(formula, Sum)) {
return _.sum(values);
} else if (tokenMatcher(formula, Avg)) {
return _.sum(values) / values.length;
} else if (tokenMatcher(formula, Max)) {
return _.max(values);
} else if (tokenMatcher(formula, Min)) {
return _.min(values);
}
}
}
export {FormulaLexer, FormulaParser, FormulaInterpreter, parser, defaultRuleName}