@daz4126/helium 0.30.0 → 1.0.0-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.claude/settings.local.json +17 -0
- package/CLAUDE.md +137 -0
- package/README.md +46 -18
- package/examples/index-lite.html +387 -0
- package/examples/index.html +486 -0
- package/examples/password-utils.js +80 -0
- package/examples/sse-demo.html +125 -0
- package/examples/sse-server.js +138 -0
- package/examples/sse-simple.html +49 -0
- package/examples/test-password.html +251 -0
- package/helium-csp.js +240 -0
- package/helium-csp.test.js +776 -0
- package/helium-lite.js +366 -0
- package/helium-lite.test.js +120 -0
- package/helium.js +580 -337
- package/helium.test.js +562 -212
- package/jexpr.js +666 -0
- package/jexpr.test.js +551 -0
- package/package.json +11 -2
- package/test-csp.html +50 -0
package/jexpr.js
ADDED
|
@@ -0,0 +1,666 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* jexpr - A simple expression parser and evaluator
|
|
3
|
+
* Vendored and adapted for Hydrogen
|
|
4
|
+
*
|
|
5
|
+
* @license BSD-3-Clause
|
|
6
|
+
* Copyright 2013, the Dart project authors. All rights reserved.
|
|
7
|
+
* Redistribution and use in source and binary forms, with or without
|
|
8
|
+
* modification, are permitted provided that the following conditions are met:
|
|
9
|
+
*
|
|
10
|
+
* * Redistributions of source code must retain the above copyright notice,
|
|
11
|
+
* this list of conditions and the following disclaimer.
|
|
12
|
+
* * Redistributions in binary form must reproduce the above copyright notice,
|
|
13
|
+
* this list of conditions and the following disclaimer in the documentation
|
|
14
|
+
* and/or other materials provided with the distribution.
|
|
15
|
+
* * Neither the name of Google Inc. nor the names of its contributors may be
|
|
16
|
+
* used to endorse or promote products derived from this software without
|
|
17
|
+
* specific prior written permission.
|
|
18
|
+
*
|
|
19
|
+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
20
|
+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
21
|
+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
22
|
+
* ARE DISCLAIMED.
|
|
23
|
+
*
|
|
24
|
+
* Based on https://github.com/nicehub/nicr-jexpr by Justin Fagnani
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
// ============================================================================
|
|
28
|
+
// Constants
|
|
29
|
+
// ============================================================================
|
|
30
|
+
|
|
31
|
+
const UNARY_OPERATORS = ['+', '-', '!'];
|
|
32
|
+
const BINARY_OPERATORS = [
|
|
33
|
+
'=', '+', '-', '*', '/', '%',
|
|
34
|
+
'==', '!=', '===', '!==',
|
|
35
|
+
'>', '<', '>=', '<=',
|
|
36
|
+
'||', '&&', '??',
|
|
37
|
+
];
|
|
38
|
+
|
|
39
|
+
const PRECEDENCE = {
|
|
40
|
+
'!': 0, ':': 0, ',': 0, ')': 0, ']': 0, '}': 0,
|
|
41
|
+
'?': 2, '??': 3, '||': 4, '&&': 5,
|
|
42
|
+
'!=': 9, '==': 9, '!==': 9, '===': 9,
|
|
43
|
+
'>=': 10, '>': 10, '<=': 10, '<': 10,
|
|
44
|
+
'+': 11, '-': 11,
|
|
45
|
+
'%': 12, '/': 12, '*': 12,
|
|
46
|
+
'(': 13, '[': 13, '.': 13, '{': 13,
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
const POSTFIX_PRECEDENCE = 13;
|
|
50
|
+
|
|
51
|
+
// ============================================================================
|
|
52
|
+
// Tokenizer
|
|
53
|
+
// ============================================================================
|
|
54
|
+
|
|
55
|
+
const Kind = {
|
|
56
|
+
STRING: 1,
|
|
57
|
+
IDENTIFIER: 2,
|
|
58
|
+
DOT: 3,
|
|
59
|
+
COMMA: 4,
|
|
60
|
+
COLON: 5,
|
|
61
|
+
INTEGER: 6,
|
|
62
|
+
DECIMAL: 7,
|
|
63
|
+
OPERATOR: 8,
|
|
64
|
+
GROUPER: 9,
|
|
65
|
+
KEYWORD: 10,
|
|
66
|
+
ARROW: 11,
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
const token = (kind, value, precedence = 0) => ({ kind, value, precedence });
|
|
70
|
+
|
|
71
|
+
const _isWhitespace = (ch) => ch === 9 || ch === 10 || ch === 13 || ch === 32;
|
|
72
|
+
const _isIdentOrKeywordStart = (ch) => ch === 95 || ch === 36 || ((ch &= ~32), 65 <= ch && ch <= 90);
|
|
73
|
+
const _isIdentifier = (ch) => _isIdentOrKeywordStart(ch) || _isNumber(ch);
|
|
74
|
+
const _isQuote = (ch) => ch === 34 || ch === 39;
|
|
75
|
+
const _isNumber = (ch) => 48 <= ch && ch <= 57;
|
|
76
|
+
const _isOperator = (ch) => ch === 43 || ch === 45 || ch === 42 || ch === 47 || ch === 33 ||
|
|
77
|
+
ch === 38 || ch === 37 || ch === 60 || ch === 61 || ch === 62 ||
|
|
78
|
+
ch === 63 || ch === 94 || ch === 124;
|
|
79
|
+
const _isGrouper = (ch) => ch === 40 || ch === 41 || ch === 91 || ch === 93 || ch === 123 || ch === 125;
|
|
80
|
+
|
|
81
|
+
const _escapeString = (str) => str.replace(/\\(.)/g, (_, g) => {
|
|
82
|
+
switch (g) {
|
|
83
|
+
case 'n': return '\n';
|
|
84
|
+
case 'r': return '\r';
|
|
85
|
+
case 't': return '\t';
|
|
86
|
+
case 'b': return '\b';
|
|
87
|
+
case 'f': return '\f';
|
|
88
|
+
default: return g;
|
|
89
|
+
}
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
class Tokenizer {
|
|
93
|
+
constructor(input) {
|
|
94
|
+
this._input = input;
|
|
95
|
+
this._index = -1;
|
|
96
|
+
this._tokenStart = 0;
|
|
97
|
+
this._advance();
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
nextToken() {
|
|
101
|
+
while (_isWhitespace(this._next)) this._advance(true);
|
|
102
|
+
if (_isQuote(this._next)) return this._tokenizeString();
|
|
103
|
+
if (_isIdentOrKeywordStart(this._next)) return this._tokenizeIdentOrKeyword();
|
|
104
|
+
if (_isNumber(this._next)) return this._tokenizeNumber();
|
|
105
|
+
if (this._next === 46) return this._tokenizeDot();
|
|
106
|
+
if (this._next === 44) return this._tokenizeComma();
|
|
107
|
+
if (this._next === 58) return this._tokenizeColon();
|
|
108
|
+
if (_isOperator(this._next)) return this._tokenizeOperator();
|
|
109
|
+
if (_isGrouper(this._next)) return this._tokenizeGrouper();
|
|
110
|
+
this._advance();
|
|
111
|
+
if (this._next !== undefined) throw new Error(`Expected end of input, got ${this._next}`);
|
|
112
|
+
return undefined;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
_advance(resetTokenStart) {
|
|
116
|
+
this._index++;
|
|
117
|
+
if (this._index < this._input.length) {
|
|
118
|
+
this._next = this._input.charCodeAt(this._index);
|
|
119
|
+
if (resetTokenStart) this._tokenStart = this._index;
|
|
120
|
+
} else {
|
|
121
|
+
this._next = undefined;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
_getValue(lookahead = 0) {
|
|
126
|
+
const v = this._input.substring(this._tokenStart, this._index + lookahead);
|
|
127
|
+
if (lookahead === 0) this._tokenStart = this._index;
|
|
128
|
+
return v;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
_tokenizeString() {
|
|
132
|
+
const quoteChar = this._next;
|
|
133
|
+
this._advance(true);
|
|
134
|
+
while (this._next !== quoteChar) {
|
|
135
|
+
if (this._next === undefined) throw new Error('unterminated string');
|
|
136
|
+
if (this._next === 92) {
|
|
137
|
+
this._advance();
|
|
138
|
+
if (this._next === undefined) throw new Error('unterminated string');
|
|
139
|
+
}
|
|
140
|
+
this._advance();
|
|
141
|
+
}
|
|
142
|
+
const t = token(Kind.STRING, _escapeString(this._getValue()));
|
|
143
|
+
this._advance();
|
|
144
|
+
return t;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
_tokenizeIdentOrKeyword() {
|
|
148
|
+
do { this._advance(); } while (_isIdentifier(this._next));
|
|
149
|
+
const value = this._getValue();
|
|
150
|
+
return token(value === 'this' ? Kind.KEYWORD : Kind.IDENTIFIER, value);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
_tokenizeNumber() {
|
|
154
|
+
do { this._advance(); } while (_isNumber(this._next));
|
|
155
|
+
if (this._next === 46) return this._tokenizeDot();
|
|
156
|
+
return token(Kind.INTEGER, this._getValue());
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
_tokenizeDot() {
|
|
160
|
+
this._advance();
|
|
161
|
+
if (_isNumber(this._next)) return this._tokenizeFraction();
|
|
162
|
+
this._tokenStart = this._index;
|
|
163
|
+
return token(Kind.DOT, '.', POSTFIX_PRECEDENCE);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
_tokenizeComma() {
|
|
167
|
+
this._advance(true);
|
|
168
|
+
return token(Kind.COMMA, ',');
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
_tokenizeColon() {
|
|
172
|
+
this._advance(true);
|
|
173
|
+
return token(Kind.COLON, ':');
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
_tokenizeFraction() {
|
|
177
|
+
do { this._advance(); } while (_isNumber(this._next));
|
|
178
|
+
return token(Kind.DECIMAL, this._getValue());
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
_tokenizeOperator() {
|
|
182
|
+
this._advance();
|
|
183
|
+
let op = this._getValue(2);
|
|
184
|
+
if (['===', '!=='].includes(op)) {
|
|
185
|
+
this._advance();
|
|
186
|
+
this._advance();
|
|
187
|
+
} else {
|
|
188
|
+
op = this._getValue(1);
|
|
189
|
+
if (op === '=>') {
|
|
190
|
+
this._advance();
|
|
191
|
+
return token(Kind.ARROW, op);
|
|
192
|
+
}
|
|
193
|
+
if (['==', '!=', '<=', '>=', '||', '&&', '??'].includes(op)) {
|
|
194
|
+
this._advance();
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
op = this._getValue();
|
|
198
|
+
return token(Kind.OPERATOR, op, PRECEDENCE[op]);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
_tokenizeGrouper() {
|
|
202
|
+
const value = String.fromCharCode(this._next);
|
|
203
|
+
const t = token(Kind.GROUPER, value, PRECEDENCE[value]);
|
|
204
|
+
this._advance(true);
|
|
205
|
+
return t;
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
// ============================================================================
|
|
210
|
+
// Parser
|
|
211
|
+
// ============================================================================
|
|
212
|
+
|
|
213
|
+
const parse = (expr, astFactory) => new Parser(expr, astFactory).parse();
|
|
214
|
+
|
|
215
|
+
class Parser {
|
|
216
|
+
constructor(input, astFactory) {
|
|
217
|
+
this._tokenizer = new Tokenizer(input);
|
|
218
|
+
this._ast = astFactory;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
parse() {
|
|
222
|
+
this._advance();
|
|
223
|
+
return this._parseExpression();
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
_advance(kind, value) {
|
|
227
|
+
if (!this._matches(kind, value)) {
|
|
228
|
+
throw new Error(`Expected kind ${kind} (${value}), was ${this._token?.kind} (${this._token?.value})`);
|
|
229
|
+
}
|
|
230
|
+
const t = this._tokenizer.nextToken();
|
|
231
|
+
this._token = t;
|
|
232
|
+
this._kind = t?.kind;
|
|
233
|
+
this._value = t?.value;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
_matches(kind, value) {
|
|
237
|
+
return !((kind && this._kind !== kind) || (value && this._value !== value));
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
_parseExpression() {
|
|
241
|
+
if (!this._token) return this._ast.empty();
|
|
242
|
+
const expr = this._parseUnary();
|
|
243
|
+
return expr === undefined ? undefined : this._parsePrecedence(expr, 0);
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
_parsePrecedence(left, precedence) {
|
|
247
|
+
if (left === undefined) throw new Error('Expected left to be defined.');
|
|
248
|
+
while (this._token) {
|
|
249
|
+
if (this._matches(Kind.GROUPER, '(')) {
|
|
250
|
+
left = this._ast.invoke(left, undefined, this._parseArguments());
|
|
251
|
+
} else if (this._matches(Kind.GROUPER, '[')) {
|
|
252
|
+
left = this._ast.index(left, this._parseIndex());
|
|
253
|
+
} else if (this._matches(Kind.DOT)) {
|
|
254
|
+
this._advance();
|
|
255
|
+
left = this._makeInvokeOrGetter(left, this._parseUnary());
|
|
256
|
+
} else if (this._matches(Kind.KEYWORD)) {
|
|
257
|
+
break;
|
|
258
|
+
} else if (this._matches(Kind.OPERATOR) && this._token.precedence >= precedence) {
|
|
259
|
+
left = this._value === '?' ? this._parseTernary(left) : this._parseBinary(left, this._token);
|
|
260
|
+
} else {
|
|
261
|
+
break;
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
return left;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
_makeInvokeOrGetter(left, right) {
|
|
268
|
+
if (right === undefined) throw new Error('expected identifier');
|
|
269
|
+
if (right.type === 'ID') return this._ast.getter(left, right.value);
|
|
270
|
+
if (right.type === 'Invoke' && right.receiver.type === 'ID') {
|
|
271
|
+
return this._ast.invoke(left, right.receiver.value, right.arguments);
|
|
272
|
+
}
|
|
273
|
+
throw new Error(`expected identifier: ${right}`);
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
_parseBinary(left, op) {
|
|
277
|
+
if (BINARY_OPERATORS.indexOf(op.value) === -1) throw new Error(`unknown operator: ${op.value}`);
|
|
278
|
+
this._advance();
|
|
279
|
+
let right = this._parseUnary();
|
|
280
|
+
while ((this._kind === Kind.OPERATOR || this._kind === Kind.DOT || this._kind === Kind.GROUPER) &&
|
|
281
|
+
this._token.precedence > op.precedence) {
|
|
282
|
+
right = this._parsePrecedence(right, this._token.precedence);
|
|
283
|
+
}
|
|
284
|
+
return this._ast.binary(left, op.value, right);
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
_parseUnary() {
|
|
288
|
+
if (this._matches(Kind.OPERATOR)) {
|
|
289
|
+
const value = this._value;
|
|
290
|
+
this._advance();
|
|
291
|
+
if (value === '+' || value === '-') {
|
|
292
|
+
if (this._matches(Kind.INTEGER)) return this._parseInteger(value);
|
|
293
|
+
if (this._matches(Kind.DECIMAL)) return this._parseDecimal(value);
|
|
294
|
+
}
|
|
295
|
+
if (UNARY_OPERATORS.indexOf(value) === -1) throw new Error(`unexpected token: ${value}`);
|
|
296
|
+
return this._ast.unary(value, this._parsePrecedence(this._parsePrimary(), POSTFIX_PRECEDENCE));
|
|
297
|
+
}
|
|
298
|
+
return this._parsePrimary();
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
_parseTernary(condition) {
|
|
302
|
+
this._advance(Kind.OPERATOR, '?');
|
|
303
|
+
const trueExpr = this._parseExpression();
|
|
304
|
+
this._advance(Kind.COLON);
|
|
305
|
+
const falseExpr = this._parseExpression();
|
|
306
|
+
return this._ast.ternary(condition, trueExpr, falseExpr);
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
_parsePrimary() {
|
|
310
|
+
switch (this._kind) {
|
|
311
|
+
case Kind.KEYWORD:
|
|
312
|
+
if (this._value === 'this') {
|
|
313
|
+
this._advance();
|
|
314
|
+
return this._ast.id('this');
|
|
315
|
+
}
|
|
316
|
+
throw new Error(`unexpected keyword: ${this._value}`);
|
|
317
|
+
case Kind.IDENTIFIER:
|
|
318
|
+
return this._parseInvokeOrIdentifier();
|
|
319
|
+
case Kind.STRING:
|
|
320
|
+
return this._parseString();
|
|
321
|
+
case Kind.INTEGER:
|
|
322
|
+
return this._parseInteger();
|
|
323
|
+
case Kind.DECIMAL:
|
|
324
|
+
return this._parseDecimal();
|
|
325
|
+
case Kind.GROUPER:
|
|
326
|
+
if (this._value === '(') return this._parseParenOrFunction();
|
|
327
|
+
if (this._value === '{') return this._parseMap();
|
|
328
|
+
if (this._value === '[') return this._parseList();
|
|
329
|
+
return undefined;
|
|
330
|
+
case Kind.COLON:
|
|
331
|
+
throw new Error('unexpected token ":"');
|
|
332
|
+
default:
|
|
333
|
+
return undefined;
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
_parseList() {
|
|
338
|
+
const items = [];
|
|
339
|
+
do {
|
|
340
|
+
this._advance();
|
|
341
|
+
if (this._matches(Kind.GROUPER, ']')) break;
|
|
342
|
+
items.push(this._parseExpression());
|
|
343
|
+
} while (this._matches(Kind.COMMA));
|
|
344
|
+
this._advance(Kind.GROUPER, ']');
|
|
345
|
+
return this._ast.list(items);
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
_parseMap() {
|
|
349
|
+
const entries = {};
|
|
350
|
+
do {
|
|
351
|
+
this._advance();
|
|
352
|
+
if (this._matches(Kind.GROUPER, '}')) break;
|
|
353
|
+
const key = this._value;
|
|
354
|
+
if (this._matches(Kind.STRING) || this._matches(Kind.IDENTIFIER)) this._advance();
|
|
355
|
+
this._advance(Kind.COLON);
|
|
356
|
+
entries[key] = this._parseExpression();
|
|
357
|
+
} while (this._matches(Kind.COMMA));
|
|
358
|
+
this._advance(Kind.GROUPER, '}');
|
|
359
|
+
return this._ast.map(entries);
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
_parseInvokeOrIdentifier() {
|
|
363
|
+
const value = this._value;
|
|
364
|
+
if (value === 'true') { this._advance(); return this._ast.literal(true); }
|
|
365
|
+
if (value === 'false') { this._advance(); return this._ast.literal(false); }
|
|
366
|
+
if (value === 'null') { this._advance(); return this._ast.literal(null); }
|
|
367
|
+
if (value === 'undefined') { this._advance(); return this._ast.literal(undefined); }
|
|
368
|
+
const identifier = this._parseIdentifier();
|
|
369
|
+
// Support single-param arrow function without parens: data => ...
|
|
370
|
+
if (this._matches(Kind.ARROW)) {
|
|
371
|
+
this._advance();
|
|
372
|
+
const body = this._parseExpression();
|
|
373
|
+
return this._ast.arrowFunction([identifier.value], body);
|
|
374
|
+
}
|
|
375
|
+
const args = this._parseArguments();
|
|
376
|
+
return args ? this._ast.invoke(identifier, undefined, args) : identifier;
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
_parseIdentifier() {
|
|
380
|
+
if (!this._matches(Kind.IDENTIFIER)) throw new Error(`expected identifier: ${this._value}`);
|
|
381
|
+
const value = this._value;
|
|
382
|
+
this._advance();
|
|
383
|
+
return this._ast.id(value);
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
_parseArguments() {
|
|
387
|
+
if (!this._matches(Kind.GROUPER, '(')) return undefined;
|
|
388
|
+
const args = [];
|
|
389
|
+
do {
|
|
390
|
+
this._advance();
|
|
391
|
+
if (this._matches(Kind.GROUPER, ')')) break;
|
|
392
|
+
args.push(this._parseExpression());
|
|
393
|
+
} while (this._matches(Kind.COMMA));
|
|
394
|
+
this._advance(Kind.GROUPER, ')');
|
|
395
|
+
return args;
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
_parseIndex() {
|
|
399
|
+
this._advance();
|
|
400
|
+
const expr = this._parseExpression();
|
|
401
|
+
this._advance(Kind.GROUPER, ']');
|
|
402
|
+
return expr;
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
_parseParenOrFunction() {
|
|
406
|
+
const expressions = this._parseArguments();
|
|
407
|
+
if (this._matches(Kind.ARROW)) {
|
|
408
|
+
this._advance();
|
|
409
|
+
const body = this._parseExpression();
|
|
410
|
+
const params = expressions?.map((e) => e.value) ?? [];
|
|
411
|
+
return this._ast.arrowFunction(params, body);
|
|
412
|
+
}
|
|
413
|
+
return this._ast.paren(expressions[0]);
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
_parseString() {
|
|
417
|
+
const value = this._ast.literal(this._value);
|
|
418
|
+
this._advance();
|
|
419
|
+
return value;
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
_parseInteger(prefix = '') {
|
|
423
|
+
const value = this._ast.literal(parseInt(`${prefix}${this._value}`, 10));
|
|
424
|
+
this._advance();
|
|
425
|
+
return value;
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
_parseDecimal(prefix = '') {
|
|
429
|
+
const value = this._ast.literal(parseFloat(`${prefix}${this._value}`));
|
|
430
|
+
this._advance();
|
|
431
|
+
return value;
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
// ============================================================================
|
|
436
|
+
// Evaluator (AST Factory)
|
|
437
|
+
// ============================================================================
|
|
438
|
+
|
|
439
|
+
const _BINARY_OPS = {
|
|
440
|
+
'+': (a, b) => a + b,
|
|
441
|
+
'-': (a, b) => a - b,
|
|
442
|
+
'*': (a, b) => a * b,
|
|
443
|
+
'/': (a, b) => a / b,
|
|
444
|
+
'%': (a, b) => a % b,
|
|
445
|
+
'==': (a, b) => a == b,
|
|
446
|
+
'!=': (a, b) => a != b,
|
|
447
|
+
'===': (a, b) => a === b,
|
|
448
|
+
'!==': (a, b) => a !== b,
|
|
449
|
+
'>': (a, b) => a > b,
|
|
450
|
+
'>=': (a, b) => a >= b,
|
|
451
|
+
'<': (a, b) => a < b,
|
|
452
|
+
'<=': (a, b) => a <= b,
|
|
453
|
+
'||': (a, b) => a || b,
|
|
454
|
+
'&&': (a, b) => a && b,
|
|
455
|
+
'??': (a, b) => a ?? b,
|
|
456
|
+
};
|
|
457
|
+
|
|
458
|
+
const _UNARY_OPS = {
|
|
459
|
+
'+': (a) => a,
|
|
460
|
+
'-': (a) => -a,
|
|
461
|
+
'!': (a) => !a,
|
|
462
|
+
};
|
|
463
|
+
|
|
464
|
+
class EvalAstFactory {
|
|
465
|
+
empty() {
|
|
466
|
+
return {
|
|
467
|
+
evaluate: (scope) => scope,
|
|
468
|
+
getIds: (idents) => idents,
|
|
469
|
+
};
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
literal(v) {
|
|
473
|
+
return {
|
|
474
|
+
value: v,
|
|
475
|
+
evaluate() { return this.value; },
|
|
476
|
+
getIds: (idents) => idents,
|
|
477
|
+
};
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
id(v) {
|
|
481
|
+
return {
|
|
482
|
+
type: 'ID',
|
|
483
|
+
value: v,
|
|
484
|
+
evaluate(scope) {
|
|
485
|
+
if (this.value === 'this') return scope;
|
|
486
|
+
return scope?.[this.value];
|
|
487
|
+
},
|
|
488
|
+
getIds(idents) {
|
|
489
|
+
idents.push(this.value);
|
|
490
|
+
return idents;
|
|
491
|
+
},
|
|
492
|
+
};
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
unary(op, expr) {
|
|
496
|
+
const f = _UNARY_OPS[op];
|
|
497
|
+
return {
|
|
498
|
+
child: expr,
|
|
499
|
+
evaluate(scope) { return f(this.child.evaluate(scope)); },
|
|
500
|
+
getIds(idents) { return this.child.getIds(idents); },
|
|
501
|
+
};
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
binary(l, op, r) {
|
|
505
|
+
const f = _BINARY_OPS[op];
|
|
506
|
+
return {
|
|
507
|
+
operator: op,
|
|
508
|
+
left: l,
|
|
509
|
+
right: r,
|
|
510
|
+
evaluate(scope) {
|
|
511
|
+
if (this.operator === '=') {
|
|
512
|
+
if (this.left.type !== 'ID' && this.left.type !== 'Getter' && this.left.type !== 'Index') {
|
|
513
|
+
throw new Error(`Invalid assignment target: ${this.left}`);
|
|
514
|
+
}
|
|
515
|
+
const value = this.right.evaluate(scope);
|
|
516
|
+
let receiver, property;
|
|
517
|
+
if (this.left.type === 'Getter') {
|
|
518
|
+
receiver = this.left.receiver.evaluate(scope);
|
|
519
|
+
property = this.left.name;
|
|
520
|
+
} else if (this.left.type === 'Index') {
|
|
521
|
+
receiver = this.left.receiver.evaluate(scope);
|
|
522
|
+
property = this.left.argument.evaluate(scope);
|
|
523
|
+
} else {
|
|
524
|
+
receiver = scope;
|
|
525
|
+
property = this.left.value;
|
|
526
|
+
}
|
|
527
|
+
return receiver === undefined ? undefined : (receiver[property] = value);
|
|
528
|
+
}
|
|
529
|
+
return f(this.left.evaluate(scope), this.right.evaluate(scope));
|
|
530
|
+
},
|
|
531
|
+
getIds(idents) {
|
|
532
|
+
this.left.getIds(idents);
|
|
533
|
+
this.right.getIds(idents);
|
|
534
|
+
return idents;
|
|
535
|
+
},
|
|
536
|
+
};
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
getter(g, n) {
|
|
540
|
+
return {
|
|
541
|
+
type: 'Getter',
|
|
542
|
+
receiver: g,
|
|
543
|
+
name: n,
|
|
544
|
+
evaluate(scope) { return this.receiver.evaluate(scope)?.[this.name]; },
|
|
545
|
+
getIds(idents) { return this.receiver.getIds(idents); },
|
|
546
|
+
};
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
invoke(receiver, method, args) {
|
|
550
|
+
return {
|
|
551
|
+
type: 'Invoke',
|
|
552
|
+
receiver,
|
|
553
|
+
method,
|
|
554
|
+
arguments: args,
|
|
555
|
+
evaluate(scope) {
|
|
556
|
+
const recv = this.receiver.evaluate(scope);
|
|
557
|
+
const _this = this.method ? recv : scope?.['this'] ?? scope;
|
|
558
|
+
const f = this.method ? recv?.[this.method] : recv;
|
|
559
|
+
const argValues = (this.arguments ?? []).map((a) => a?.evaluate(scope));
|
|
560
|
+
return f?.apply?.(_this, argValues);
|
|
561
|
+
},
|
|
562
|
+
getIds(idents) {
|
|
563
|
+
this.receiver.getIds(idents);
|
|
564
|
+
this.arguments?.forEach((a) => a?.getIds(idents));
|
|
565
|
+
return idents;
|
|
566
|
+
},
|
|
567
|
+
};
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
paren(e) { return e; }
|
|
571
|
+
|
|
572
|
+
index(e, a) {
|
|
573
|
+
return {
|
|
574
|
+
type: 'Index',
|
|
575
|
+
receiver: e,
|
|
576
|
+
argument: a,
|
|
577
|
+
evaluate(scope) { return this.receiver.evaluate(scope)?.[this.argument.evaluate(scope)]; },
|
|
578
|
+
getIds(idents) { return this.receiver.getIds(idents); },
|
|
579
|
+
};
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
ternary(c, t, f) {
|
|
583
|
+
return {
|
|
584
|
+
condition: c,
|
|
585
|
+
trueExpr: t,
|
|
586
|
+
falseExpr: f,
|
|
587
|
+
evaluate(scope) {
|
|
588
|
+
return this.condition.evaluate(scope) ? this.trueExpr.evaluate(scope) : this.falseExpr.evaluate(scope);
|
|
589
|
+
},
|
|
590
|
+
getIds(idents) {
|
|
591
|
+
this.condition.getIds(idents);
|
|
592
|
+
this.trueExpr.getIds(idents);
|
|
593
|
+
this.falseExpr.getIds(idents);
|
|
594
|
+
return idents;
|
|
595
|
+
},
|
|
596
|
+
};
|
|
597
|
+
}
|
|
598
|
+
|
|
599
|
+
map(entries) {
|
|
600
|
+
return {
|
|
601
|
+
entries,
|
|
602
|
+
evaluate(scope) {
|
|
603
|
+
const map = {};
|
|
604
|
+
if (this.entries) {
|
|
605
|
+
for (const key in this.entries) {
|
|
606
|
+
if (this.entries[key]) map[key] = this.entries[key].evaluate(scope);
|
|
607
|
+
}
|
|
608
|
+
}
|
|
609
|
+
return map;
|
|
610
|
+
},
|
|
611
|
+
getIds(idents) {
|
|
612
|
+
if (this.entries) {
|
|
613
|
+
for (const key in this.entries) {
|
|
614
|
+
if (this.entries[key]) this.entries[key].getIds(idents);
|
|
615
|
+
}
|
|
616
|
+
}
|
|
617
|
+
return idents;
|
|
618
|
+
},
|
|
619
|
+
};
|
|
620
|
+
}
|
|
621
|
+
|
|
622
|
+
list(l) {
|
|
623
|
+
return {
|
|
624
|
+
items: l,
|
|
625
|
+
evaluate(scope) { return this.items?.map((a) => a?.evaluate(scope)); },
|
|
626
|
+
getIds(idents) {
|
|
627
|
+
this.items?.forEach((i) => i?.getIds(idents));
|
|
628
|
+
return idents;
|
|
629
|
+
},
|
|
630
|
+
};
|
|
631
|
+
}
|
|
632
|
+
|
|
633
|
+
arrowFunction(params, body) {
|
|
634
|
+
return {
|
|
635
|
+
params,
|
|
636
|
+
body,
|
|
637
|
+
evaluate(scope) {
|
|
638
|
+
const params = this.params;
|
|
639
|
+
const body = this.body;
|
|
640
|
+
return function (...args) {
|
|
641
|
+
const paramsObj = Object.fromEntries(params.map((p, i) => [p, args[i]]));
|
|
642
|
+
const newScope = new Proxy(scope ?? {}, {
|
|
643
|
+
set(target, prop, value) {
|
|
644
|
+
if (Object.hasOwn(paramsObj, prop)) paramsObj[prop] = value;
|
|
645
|
+
return (target[prop] = value);
|
|
646
|
+
},
|
|
647
|
+
get(target, prop) {
|
|
648
|
+
if (Object.hasOwn(paramsObj, prop)) return paramsObj[prop];
|
|
649
|
+
return target[prop];
|
|
650
|
+
},
|
|
651
|
+
});
|
|
652
|
+
return body.evaluate(newScope);
|
|
653
|
+
};
|
|
654
|
+
},
|
|
655
|
+
getIds(idents) {
|
|
656
|
+
return this.body.getIds(idents).filter((id) => !this.params.includes(id));
|
|
657
|
+
},
|
|
658
|
+
};
|
|
659
|
+
}
|
|
660
|
+
}
|
|
661
|
+
|
|
662
|
+
// ============================================================================
|
|
663
|
+
// Exports
|
|
664
|
+
// ============================================================================
|
|
665
|
+
|
|
666
|
+
export { parse, EvalAstFactory };
|