forked from msironi/expr-eval
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathparser.ts
More file actions
396 lines (375 loc) · 10.6 KB
/
Copy pathparser.ts
File metadata and controls
396 lines (375 loc) · 10.6 KB
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
// cSpell:words TEOF fndef
import { TEOF } from './token.js';
import { TokenStream } from './token-stream.js';
import { ParserState } from './parser-state.js';
import { Expression } from '../core/expression.js';
import type { Value, VariableResolveResult, Values } from '../types/values.js';
import type { Instruction } from './instruction.js';
import type { OperatorFunction } from '../types/parser.js';
import { atan2, condition, fac, filter, fold, gamma, hypot, indexOf, join, map, max, min, random, roundTo, sum, json, stringLength, isEmpty, stringContains, startsWith, endsWith, searchCount, trim, toUpper, toLower, toTitle, split, repeat, reverse, left, right, replace, replaceFirst, naturalSort, toNumber, toBoolean, padLeft, padRight, padBoth, slice, urlEncode, base64Encode, base64Decode, coalesceString, merge, keys, values, flatten } from '../functions/index.js';
import {
add,
sub,
mul,
div,
mod,
pow,
concat,
equal,
notEqual,
greaterThan,
lessThan,
greaterThanEqual,
lessThanEqual,
setVar,
arrayIndexOrProperty,
andOperator,
orOperator,
inOperator,
notInOperator,
coalesce,
asOperator
} from '../operators/binary/index.js';
import {
pos,
abs,
acos,
sinh,
tanh,
asin,
asinh,
acosh,
atan,
atanh,
cbrt,
ceil,
cos,
cosh,
exp,
floor,
log,
log10,
neg,
not,
round,
sin,
sqrt,
tan,
trunc,
length,
sign,
expm1,
log1p,
log2
} from '../operators/unary/index.js';
/**
* Parser options configuration
*/
interface ParserOptions {
allowMemberAccess?: boolean;
operators?: Record<string, boolean>;
}
/**
* Variable resolver function type for custom variable resolution
*/
type VariableResolver = (token: string) => VariableResolveResult;
export class Parser {
public options: ParserOptions;
public keywords: string[];
public unaryOps: Record<string, OperatorFunction>;
public binaryOps: Record<string, OperatorFunction>;
public ternaryOps: Record<string, OperatorFunction>;
public functions: Record<string, OperatorFunction>;
public numericConstants: Record<string, Value>;
public buildInLiterals: Record<string, Value>;
public resolve: VariableResolver;
/**
* Creates a new Parser instance with the specified options.
*
* @param options - Configuration options for the parser
* @example
* ```typescript
* const parser = new Parser({
* allowMemberAccess: false,
* operators: { add: true, multiply: true }
* });
* ```
*/
constructor(options?: ParserOptions) {
this.options = options || { operators: { conversion: false } };
this.keywords = [
'case',
'when',
'then',
'else',
'end'
] as const;
this.unaryOps = {
'-': neg,
'+': pos,
'!': fac,
abs: abs,
acos: acos,
acosh: acosh,
asin: asin,
asinh: asinh,
atan: atan,
atanh: atanh,
// 11
cbrt: cbrt,
ceil: ceil,
cos: cos,
cosh: cosh,
exp: exp,
expm1: expm1,
floor: floor,
length: length,
lg: log10,
ln: log,
// 21
log: log,
log1p: log1p,
log2: log2,
log10: log10,
not: not,
round: round,
sign: sign,
sin: sin,
sinh: sinh,
sqrt: sqrt,
// 31
tan: tan,
tanh: tanh,
trunc: trunc
};
this.binaryOps = {
'+': add,
'-': sub,
'*': mul,
'/': div,
'%': mod,
'^': pow,
'|': concat,
'==': equal,
'!=': notEqual,
'>': greaterThan,
// 11
'<': lessThan,
'>=': greaterThanEqual,
'<=': lessThanEqual,
'=': setVar,
'[': arrayIndexOrProperty,
and: andOperator,
'&&': andOperator,
in: inOperator,
'not in': notInOperator,
or: orOperator,
'||': orOperator,
'??': coalesce,
'as': asOperator
};
this.ternaryOps = {
'?': condition
};
this.functions = {
atan2: atan2,
fac: fac,
filter: filter,
fold: fold,
gamma: gamma,
hypot: hypot,
indexOf: indexOf,
if: condition,
join: join,
map: map,
max: max,
min: min,
pow: pow,
json: json,
random: random,
roundTo: roundTo,
sum: sum,
// String manipulation functions
length: stringLength,
isEmpty: isEmpty,
contains: stringContains,
startsWith: startsWith,
endsWith: endsWith,
searchCount: searchCount,
trim: trim,
toUpper: toUpper,
toLower: toLower,
toTitle: toTitle,
split: split,
repeat: repeat,
reverse: reverse,
left: left,
right: right,
replace: replace,
replaceFirst: replaceFirst,
naturalSort: naturalSort,
toNumber: toNumber,
toBoolean: toBoolean,
padLeft: padLeft,
padRight: padRight,
padBoth: padBoth,
slice: slice,
urlEncode: urlEncode,
base64Encode: base64Encode,
base64Decode: base64Decode,
coalesce: coalesceString,
// Object manipulation functions
merge: merge,
keys: keys,
values: values,
flatten: flatten
};
this.numericConstants = {
E: Math.E,
PI: Math.PI
};
this.buildInLiterals = {
true: true,
false: false,
null: null
};
// A callback that evaluate will call if it doesn't recognize a variable. The default
// implementation returns undefined to indicate that it won't resolve the variable. This
// gives the code using the Parser a chance to resolve unrecognized variables to add support for
// things like $myVar, $$myVar, %myVar%, etc. For example when an expression is evaluated a variables
// object could be passed in and $myVar could resolve to a property of that object.
// The return value can be any of:
// - { alias: "xxx" } the token is an alias for xxx, i.e. use xxx as the token.
// - { value: <something> } use <something> as the value for the variable
// - any other value is treated as the value to use for the token.
this.resolve = (): VariableResolveResult => undefined;
}
/**
* Parses a mathematical expression into an Expression object.
*
* @param expr - The mathematical expression string to parse
* @returns An Expression object that can be evaluated
* @throws {ParseError} When the expression contains syntax errors
* @example
* ```typescript
* const parser = new Parser();
* const expression = parser.parse('2 + 3 * x');
* const result = expression.evaluate({ x: 4 }); // Returns 14
* ```
*/
parse(expr: string): Expression {
const instr: Instruction[] = [];
const parserState = new ParserState(
this,
new TokenStream(this, expr),
{ allowMemberAccess: this.options.allowMemberAccess }
);
parserState.parseExpression(instr);
parserState.expect(TEOF, 'EOF');
return new Expression(instr, this);
}
/**
* Parses and immediately evaluates a mathematical expression.
* This is a convenience method equivalent to `parser.parse(expr).evaluate(variables)`.
*
* @param expr - The mathematical expression string to evaluate
* @param variables - Optional object containing variable values
* @returns The result of evaluating the expression
* @throws {ParseError} When the expression contains syntax errors
* @throws {VariableError} When the expression references undefined variables
* @throws {EvaluationError} When runtime evaluation fails
* @example
* ```typescript
* const parser = new Parser();
* const result = parser.evaluate('2 + 3 * x', { x: 4 }); // Returns 14
* ```
*/
evaluate(expr: string, variables?: Values): Value | Promise<Value> {
return this.parse(expr).evaluate(variables);
}
private static readonly optionNameMap: Record<string, string> = {
'+': 'add',
'-': 'subtract',
'*': 'multiply',
'/': 'divide',
'%': 'remainder',
'^': 'power',
'!': 'factorial',
'<': 'comparison',
'>': 'comparison',
'<=': 'comparison',
'>=': 'comparison',
'==': 'comparison',
'!=': 'comparison',
'|': 'concatenate',
'and': 'logical',
'or': 'logical',
'not': 'logical',
'&&': 'logical',
'||': 'logical',
'?': 'conditional',
':': 'conditional',
'=': 'assignment',
'[': 'array',
'()=': 'fndef',
'=>': 'fndef',
'??': 'coalesce',
'as': 'conversion'
} as const;
private static getOptionName(op: string): string {
return Parser.optionNameMap.hasOwnProperty(op) ? Parser.optionNameMap[op] : op;
}
/**
* Checks if a specific operator is enabled in this parser's configuration.
*
* @param op - The operator to check
* @returns True if the operator is enabled, false otherwise
* @example
* ```typescript
* const parser = new Parser({ operators: { add: false } });
* console.log(parser.isOperatorEnabled('+')); // false
* console.log(parser.isOperatorEnabled('*')); // true (default enabled)
* ```
*/
isOperatorEnabled(op: string): boolean {
const optionName = Parser.getOptionName(op);
const operators = this.options.operators || {};
return !(optionName in operators) || !!operators[optionName];
}
// Static methods for the shared parser instance
private static sharedParser = new Parser();
/**
* Parses a mathematical expression using the default shared parser instance.
* This is a static convenience method.
*
* @param expr - The mathematical expression string to parse
* @returns An Expression object that can be evaluated
* @throws {ParseError} When the expression contains syntax errors
* @example
* ```typescript
* const expression = Parser.parse('2 + 3 * x');
* const result = expression.evaluate({ x: 4 }); // Returns 14
* ```
*/
static parse(expr: string): Expression {
return Parser.sharedParser.parse(expr);
}
/**
* Parses and immediately evaluates a mathematical expression using the default shared parser instance.
* This is a static convenience method equivalent to `Parser.parse(expr).evaluate(variables)`.
*
* @param expr - The mathematical expression string to evaluate
* @param variables - Optional object containing variable values
* @returns The result of evaluating the expression
* @throws {ParseError} When the expression contains syntax errors
* @throws {VariableError} When the expression references undefined variables
* @throws {EvaluationError} When runtime evaluation fails
* @example
* ```typescript
* const result = Parser.evaluate('2 + 3 * x', { x: 4 }); // Returns 14
* ```
*/
static evaluate(expr: string, variables?: Values): Value | Promise<Value> {
return Parser.sharedParser.parse(expr).evaluate(variables);
}
}