@libdbm/libcel-ts 1.0.2-rc.0
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/LICENSE +20 -0
- package/README.md +288 -0
- package/dist/index.cjs +6 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +740 -0
- package/dist/index.mjs +1414 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +59 -0
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,1414 @@
|
|
|
1
|
+
class f extends Error {
|
|
2
|
+
constructor(t, e, i) {
|
|
3
|
+
super(`${t} at ${e}:${i}`), this.line = e, this.column = i, this.name = "ParseError";
|
|
4
|
+
}
|
|
5
|
+
}
|
|
6
|
+
var s = /* @__PURE__ */ ((r) => (r.NULL = "NULL", r.TRUE = "TRUE", r.FALSE = "FALSE", r.INT = "INT", r.UINT = "UINT", r.DOUBLE = "DOUBLE", r.STRING = "STRING", r.BYTES = "BYTES", r.IDENTIFIER = "IDENTIFIER", r.PLUS = "PLUS", r.MINUS = "MINUS", r.STAR = "STAR", r.SLASH = "SLASH", r.PERCENT = "PERCENT", r.EQ = "EQ", r.NE = "NE", r.LT = "LT", r.LE = "LE", r.GT = "GT", r.GE = "GE", r.LOGICAL_AND = "LOGICAL_AND", r.LOGICAL_OR = "LOGICAL_OR", r.BANG = "BANG", r.IN = "IN", r.LPAREN = "LPAREN", r.RPAREN = "RPAREN", r.LBRACKET = "LBRACKET", r.RBRACKET = "RBRACKET", r.LBRACE = "LBRACE", r.RBRACE = "RBRACE", r.DOT = "DOT", r.COMMA = "COMMA", r.COLON = "COLON", r.QUESTION = "QUESTION", r.EOF = "EOF", r))(s || {});
|
|
7
|
+
class h {
|
|
8
|
+
constructor(t, e, i, n) {
|
|
9
|
+
this.type = t, this.value = e, this.line = i, this.column = n;
|
|
10
|
+
}
|
|
11
|
+
toString() {
|
|
12
|
+
return `${this.type}(${this.value})`;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
class F {
|
|
16
|
+
constructor(t) {
|
|
17
|
+
this.input = t, this.position = 0, this.line = 1, this.column = 1, this.lookahead = [];
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Get the next token from the input.
|
|
21
|
+
*/
|
|
22
|
+
next() {
|
|
23
|
+
return this.lookahead.length > 0 ? this.lookahead.shift() : this.token();
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Peek ahead at future tokens without consuming them.
|
|
27
|
+
* @param count Number of tokens to look ahead (1-indexed)
|
|
28
|
+
*/
|
|
29
|
+
peek(t) {
|
|
30
|
+
for (; this.lookahead.length < t; )
|
|
31
|
+
this.lookahead.push(this.token());
|
|
32
|
+
return this.lookahead[t - 1];
|
|
33
|
+
}
|
|
34
|
+
step() {
|
|
35
|
+
if (this.position >= this.input.length) return;
|
|
36
|
+
const t = this.input[this.position];
|
|
37
|
+
this.position++, t === "\r" ? (this.position < this.input.length && this.input[this.position] === `
|
|
38
|
+
` && this.position++, this.line++, this.column = 1) : t === `
|
|
39
|
+
` ? (this.line++, this.column = 1) : this.column++;
|
|
40
|
+
}
|
|
41
|
+
forward(t) {
|
|
42
|
+
for (let e = 0; e < t; e++)
|
|
43
|
+
this.step();
|
|
44
|
+
}
|
|
45
|
+
token() {
|
|
46
|
+
if (this.whitespace(), this.position >= this.input.length)
|
|
47
|
+
return new h("EOF", "", this.line, this.column);
|
|
48
|
+
const t = this.position, e = this.line, i = this.column, n = this.input[this.position];
|
|
49
|
+
switch (n) {
|
|
50
|
+
case "(":
|
|
51
|
+
return this.step(), new h("LPAREN", "(", e, i);
|
|
52
|
+
case ")":
|
|
53
|
+
return this.step(), new h("RPAREN", ")", e, i);
|
|
54
|
+
case "[":
|
|
55
|
+
return this.step(), new h("LBRACKET", "[", e, i);
|
|
56
|
+
case "]":
|
|
57
|
+
return this.step(), new h("RBRACKET", "]", e, i);
|
|
58
|
+
case "{":
|
|
59
|
+
return this.step(), new h("LBRACE", "{", e, i);
|
|
60
|
+
case "}":
|
|
61
|
+
return this.step(), new h("RBRACE", "}", e, i);
|
|
62
|
+
case ",":
|
|
63
|
+
return this.step(), new h("COMMA", ",", e, i);
|
|
64
|
+
case ".":
|
|
65
|
+
return this.step(), new h("DOT", ".", e, i);
|
|
66
|
+
case ":":
|
|
67
|
+
return this.step(), new h("COLON", ":", e, i);
|
|
68
|
+
case "?":
|
|
69
|
+
return this.step(), new h("QUESTION", "?", e, i);
|
|
70
|
+
case "+":
|
|
71
|
+
return this.step(), new h("PLUS", "+", e, i);
|
|
72
|
+
case "*":
|
|
73
|
+
return this.step(), new h("STAR", "*", e, i);
|
|
74
|
+
case "/":
|
|
75
|
+
return this.step(), new h("SLASH", "/", e, i);
|
|
76
|
+
case "%":
|
|
77
|
+
return this.step(), new h("PERCENT", "%", e, i);
|
|
78
|
+
}
|
|
79
|
+
if (n === "&" && this.peekChar() === "&")
|
|
80
|
+
return this.forward(2), new h("LOGICAL_AND", "&&", e, i);
|
|
81
|
+
if (n === "|" && this.peekChar() === "|")
|
|
82
|
+
return this.forward(2), new h("LOGICAL_OR", "||", e, i);
|
|
83
|
+
if (n === "=" && this.peekChar() === "=")
|
|
84
|
+
return this.forward(2), new h("EQ", "==", e, i);
|
|
85
|
+
if (n === "!" && this.peekChar() === "=")
|
|
86
|
+
return this.forward(2), new h("NE", "!=", e, i);
|
|
87
|
+
if (n === "<" && this.peekChar() === "=")
|
|
88
|
+
return this.forward(2), new h("LE", "<=", e, i);
|
|
89
|
+
if (n === ">" && this.peekChar() === "=")
|
|
90
|
+
return this.forward(2), new h("GE", ">=", e, i);
|
|
91
|
+
if (n === "<")
|
|
92
|
+
return this.step(), new h("LT", "<", e, i);
|
|
93
|
+
if (n === ">")
|
|
94
|
+
return this.step(), new h("GT", ">", e, i);
|
|
95
|
+
if (n === "!")
|
|
96
|
+
return this.step(), new h("BANG", "!", e, i);
|
|
97
|
+
if (n === "-")
|
|
98
|
+
return this.step(), new h("MINUS", "-", e, i);
|
|
99
|
+
if (n === '"' || n === "'")
|
|
100
|
+
return this.string(e, i, t);
|
|
101
|
+
if ((n === "r" || n === "R") && this.position + 1 < this.input.length) {
|
|
102
|
+
const o = this.input[this.position + 1];
|
|
103
|
+
if (o === '"' || o === "'")
|
|
104
|
+
return this.string(e, i, t);
|
|
105
|
+
}
|
|
106
|
+
if ((n === "b" || n === "B") && this.position + 1 < this.input.length) {
|
|
107
|
+
const o = this.input[this.position + 1];
|
|
108
|
+
if (o === '"' || o === "'")
|
|
109
|
+
return this.bytes(e, i, t);
|
|
110
|
+
}
|
|
111
|
+
if (this.isDigit(n))
|
|
112
|
+
return this.number(e, i, t);
|
|
113
|
+
if (this.isLetter(n) || n === "_")
|
|
114
|
+
return this.identifier(e, i, t);
|
|
115
|
+
throw new f(`Unexpected character: ${n}`, e, i);
|
|
116
|
+
}
|
|
117
|
+
string(t, e, i) {
|
|
118
|
+
let n = !1;
|
|
119
|
+
if ((this.input[this.position] === "r" || this.input[this.position] === "R") && (n = !0, this.step()), this.position + 2 < this.input.length) {
|
|
120
|
+
const l = this.input.substring(this.position, this.position + 3);
|
|
121
|
+
if (l === '"""' || l === "'''") {
|
|
122
|
+
const u = this.input[this.position];
|
|
123
|
+
for (this.forward(3); this.position + 2 < this.input.length; ) {
|
|
124
|
+
if (this.input[this.position] === u && this.input[this.position + 1] === u && this.input[this.position + 2] === u)
|
|
125
|
+
return this.forward(3), new h("STRING", this.input.substring(i, this.position), t, e);
|
|
126
|
+
this.step();
|
|
127
|
+
}
|
|
128
|
+
throw new f("Unterminated triple-quoted string", t, e);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
const o = this.input[this.position];
|
|
132
|
+
for (this.step(); this.position < this.input.length; ) {
|
|
133
|
+
const l = this.input[this.position];
|
|
134
|
+
if (l === o)
|
|
135
|
+
return this.step(), new h("STRING", this.input.substring(i, this.position), t, e);
|
|
136
|
+
l === "\\" && !n && this.position + 1 < this.input.length ? this.forward(2) : this.step();
|
|
137
|
+
}
|
|
138
|
+
throw new f("Unterminated string", t, e);
|
|
139
|
+
}
|
|
140
|
+
bytes(t, e, i) {
|
|
141
|
+
this.step();
|
|
142
|
+
const n = this.input[this.position];
|
|
143
|
+
for (this.step(); this.position < this.input.length; ) {
|
|
144
|
+
const o = this.input[this.position];
|
|
145
|
+
if (o === n)
|
|
146
|
+
return this.step(), new h("BYTES", this.input.substring(i, this.position), t, e);
|
|
147
|
+
o === "\\" && this.position + 1 < this.input.length ? this.forward(2) : this.step();
|
|
148
|
+
}
|
|
149
|
+
throw new f("Unterminated bytes literal", t, e);
|
|
150
|
+
}
|
|
151
|
+
number(t, e, i) {
|
|
152
|
+
if (this.input[this.position] === "0" && this.position + 1 < this.input.length) {
|
|
153
|
+
const l = this.input[this.position + 1];
|
|
154
|
+
if (l === "x" || l === "X") {
|
|
155
|
+
for (this.forward(2); this.position < this.input.length && this.isHexDigit(this.input[this.position]); )
|
|
156
|
+
this.step();
|
|
157
|
+
if (this.position < this.input.length) {
|
|
158
|
+
const u = this.input[this.position];
|
|
159
|
+
if (u === "u" || u === "U")
|
|
160
|
+
return this.step(), new h("UINT", this.input.substring(i, this.position), t, e);
|
|
161
|
+
}
|
|
162
|
+
return new h("INT", this.input.substring(i, this.position), t, e);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
for (; this.position < this.input.length && this.isDigit(this.input[this.position]); )
|
|
166
|
+
this.step();
|
|
167
|
+
let n = !1;
|
|
168
|
+
if (this.position < this.input.length && this.input[this.position] === ".")
|
|
169
|
+
for (n = !0, this.step(); this.position < this.input.length && this.isDigit(this.input[this.position]); )
|
|
170
|
+
this.step();
|
|
171
|
+
if (this.position < this.input.length) {
|
|
172
|
+
const l = this.input[this.position];
|
|
173
|
+
if (l === "e" || l === "E") {
|
|
174
|
+
if (n = !0, this.step(), this.position < this.input.length) {
|
|
175
|
+
const u = this.input[this.position];
|
|
176
|
+
(u === "+" || u === "-") && this.step();
|
|
177
|
+
}
|
|
178
|
+
for (; this.position < this.input.length && this.isDigit(this.input[this.position]); )
|
|
179
|
+
this.step();
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
if (!n && this.position < this.input.length) {
|
|
183
|
+
const l = this.input[this.position];
|
|
184
|
+
if (l === "u" || l === "U")
|
|
185
|
+
return this.step(), new h("UINT", this.input.substring(i, this.position), t, e);
|
|
186
|
+
}
|
|
187
|
+
const o = n ? "DOUBLE" : "INT";
|
|
188
|
+
return new h(o, this.input.substring(i, this.position), t, e);
|
|
189
|
+
}
|
|
190
|
+
identifier(t, e, i) {
|
|
191
|
+
for (; this.position < this.input.length; ) {
|
|
192
|
+
const l = this.input[this.position];
|
|
193
|
+
if (!this.isLetterOrDigit(l) && l !== "_")
|
|
194
|
+
break;
|
|
195
|
+
this.step();
|
|
196
|
+
}
|
|
197
|
+
const n = this.input.substring(i, this.position), o = this.typeOf(n);
|
|
198
|
+
return new h(o, n, t, e);
|
|
199
|
+
}
|
|
200
|
+
typeOf(t) {
|
|
201
|
+
switch (t) {
|
|
202
|
+
case "null":
|
|
203
|
+
return "NULL";
|
|
204
|
+
case "true":
|
|
205
|
+
return "TRUE";
|
|
206
|
+
case "false":
|
|
207
|
+
return "FALSE";
|
|
208
|
+
case "in":
|
|
209
|
+
return "IN";
|
|
210
|
+
default:
|
|
211
|
+
return "IDENTIFIER";
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
whitespace() {
|
|
215
|
+
for (; this.position < this.input.length; ) {
|
|
216
|
+
const t = this.input[this.position];
|
|
217
|
+
if (t !== " " && t !== " " && t !== `
|
|
218
|
+
` && t !== "\r")
|
|
219
|
+
break;
|
|
220
|
+
this.step();
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
peekChar() {
|
|
224
|
+
const t = this.position + 1;
|
|
225
|
+
return t >= this.input.length ? "\0" : this.input[t];
|
|
226
|
+
}
|
|
227
|
+
isHexDigit(t) {
|
|
228
|
+
return t >= "0" && t <= "9" || t >= "a" && t <= "f" || t >= "A" && t <= "F";
|
|
229
|
+
}
|
|
230
|
+
isDigit(t) {
|
|
231
|
+
return t >= "0" && t <= "9";
|
|
232
|
+
}
|
|
233
|
+
isLetter(t) {
|
|
234
|
+
return t >= "a" && t <= "z" || t >= "A" && t <= "Z";
|
|
235
|
+
}
|
|
236
|
+
isLetterOrDigit(t) {
|
|
237
|
+
return this.isLetter(t) || this.isDigit(t);
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
var c = /* @__PURE__ */ ((r) => (r.ADD = "ADD", r.SUBTRACT = "SUBTRACT", r.MULTIPLY = "MULTIPLY", r.DIVIDE = "DIVIDE", r.MODULO = "MODULO", r.EQUAL = "EQUAL", r.NOT_EQUAL = "NOT_EQUAL", r.LESS = "LESS", r.LESS_EQUAL = "LESS_EQUAL", r.GREATER = "GREATER", r.GREATER_EQUAL = "GREATER_EQUAL", r.IN = "IN", r.LOGICAL_AND = "LOGICAL_AND", r.LOGICAL_OR = "LOGICAL_OR", r))(c || {}), g = /* @__PURE__ */ ((r) => (r.NOT = "NOT", r.NEGATE = "NEGATE", r))(g || {}), w = /* @__PURE__ */ ((r) => (r.NULL_VALUE = "NULL_VALUE", r.BOOL = "BOOL", r.INT = "INT", r.UINT = "UINT", r.DOUBLE = "DOUBLE", r.STRING = "STRING", r.BYTES = "BYTES", r))(w || {});
|
|
241
|
+
class d {
|
|
242
|
+
constructor(t, e) {
|
|
243
|
+
this.value = t, this.literalType = e;
|
|
244
|
+
}
|
|
245
|
+
accept(t) {
|
|
246
|
+
return t.visitLiteral(this);
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
class U {
|
|
250
|
+
constructor(t) {
|
|
251
|
+
this.name = t;
|
|
252
|
+
}
|
|
253
|
+
accept(t) {
|
|
254
|
+
return t.visitIdentifier(this);
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
class N {
|
|
258
|
+
constructor(t, e, i = !1) {
|
|
259
|
+
this.operand = t, this.field = e, this.isTest = i;
|
|
260
|
+
}
|
|
261
|
+
accept(t) {
|
|
262
|
+
return t.visitSelect(this);
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
class Q {
|
|
266
|
+
constructor(t, e) {
|
|
267
|
+
this.operand = t, this.index = e;
|
|
268
|
+
}
|
|
269
|
+
accept(t) {
|
|
270
|
+
return t.visitIndex(this);
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
class y {
|
|
274
|
+
constructor(t, e, i, n = !1) {
|
|
275
|
+
this.target = t, this.functionName = e, this.args = i, this.isMacro = n;
|
|
276
|
+
}
|
|
277
|
+
accept(t) {
|
|
278
|
+
return t.visitCall(this);
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
class q {
|
|
282
|
+
constructor(t) {
|
|
283
|
+
this.elements = t;
|
|
284
|
+
}
|
|
285
|
+
accept(t) {
|
|
286
|
+
return t.visitList(this);
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
class W {
|
|
290
|
+
constructor(t, e) {
|
|
291
|
+
this.key = t, this.value = e;
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
class C {
|
|
295
|
+
constructor(t) {
|
|
296
|
+
this.entries = t;
|
|
297
|
+
}
|
|
298
|
+
accept(t) {
|
|
299
|
+
return t.visitMap(this);
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
class j {
|
|
303
|
+
constructor(t, e) {
|
|
304
|
+
this.field = t, this.value = e;
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
class S {
|
|
308
|
+
constructor(t, e) {
|
|
309
|
+
this.typeName = t, this.fields = e;
|
|
310
|
+
}
|
|
311
|
+
accept(t) {
|
|
312
|
+
return t.visitStruct(this);
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
class X {
|
|
316
|
+
constructor(t, e, i, n, o, l, u) {
|
|
317
|
+
this.variable = t, this.range = e, this.accumulator = i, this.initializer = n, this.condition = o, this.step = l, this.result = u;
|
|
318
|
+
}
|
|
319
|
+
accept(t) {
|
|
320
|
+
return t.visitComprehension(this);
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
class O {
|
|
324
|
+
constructor(t, e) {
|
|
325
|
+
this.op = t, this.operand = e;
|
|
326
|
+
}
|
|
327
|
+
accept(t) {
|
|
328
|
+
return t.visitUnary(this);
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
class L {
|
|
332
|
+
constructor(t, e, i) {
|
|
333
|
+
this.op = t, this.left = e, this.right = i;
|
|
334
|
+
}
|
|
335
|
+
accept(t) {
|
|
336
|
+
return t.visitBinary(this);
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
class K {
|
|
340
|
+
constructor(t, e, i) {
|
|
341
|
+
this.condition = t, this.thenExpr = e, this.otherwiseExpr = i;
|
|
342
|
+
}
|
|
343
|
+
accept(t) {
|
|
344
|
+
return t.visitConditional(this);
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
const z = /* @__PURE__ */ new Set(["map", "filter", "all", "exists", "existsOne"]);
|
|
348
|
+
class Y {
|
|
349
|
+
constructor(t) {
|
|
350
|
+
this.lexer = new F(t), this.current = this.lexer.next();
|
|
351
|
+
}
|
|
352
|
+
/**
|
|
353
|
+
* Parse a CEL expression from the input string.
|
|
354
|
+
* @returns The parsed Expression AST node
|
|
355
|
+
* @throws ParseError if the input contains syntax errors
|
|
356
|
+
*/
|
|
357
|
+
parse() {
|
|
358
|
+
const t = this.parseExpr();
|
|
359
|
+
if (this.current.type !== s.EOF)
|
|
360
|
+
throw new f(
|
|
361
|
+
`Unexpected token after expression: ${this.current.value}`,
|
|
362
|
+
this.current.line,
|
|
363
|
+
this.current.column
|
|
364
|
+
);
|
|
365
|
+
return t;
|
|
366
|
+
}
|
|
367
|
+
// expr = conditionalOr ( '?' conditionalOr ':' expr )?
|
|
368
|
+
parseExpr() {
|
|
369
|
+
const t = this.parseConditionalOr();
|
|
370
|
+
if (this.match(s.QUESTION)) {
|
|
371
|
+
const e = this.parseConditionalOr();
|
|
372
|
+
this.expect(s.COLON);
|
|
373
|
+
const i = this.parseExpr();
|
|
374
|
+
return new K(t, e, i);
|
|
375
|
+
}
|
|
376
|
+
return t;
|
|
377
|
+
}
|
|
378
|
+
// conditionalOr = conditionalAnd ( '||' conditionalAnd )*
|
|
379
|
+
parseConditionalOr() {
|
|
380
|
+
let t = this.parseConditionalAnd();
|
|
381
|
+
for (; this.match(s.LOGICAL_OR); ) {
|
|
382
|
+
const e = this.parseConditionalAnd();
|
|
383
|
+
t = new L(c.LOGICAL_OR, t, e);
|
|
384
|
+
}
|
|
385
|
+
return t;
|
|
386
|
+
}
|
|
387
|
+
// conditionalAnd = relation ( '&&' relation )*
|
|
388
|
+
parseConditionalAnd() {
|
|
389
|
+
let t = this.parseRelation();
|
|
390
|
+
for (; this.match(s.LOGICAL_AND); ) {
|
|
391
|
+
const e = this.parseRelation();
|
|
392
|
+
t = new L(c.LOGICAL_AND, t, e);
|
|
393
|
+
}
|
|
394
|
+
return t;
|
|
395
|
+
}
|
|
396
|
+
// relation = addition ( relop addition )*
|
|
397
|
+
// relop = '<=' | '>=' | '!=' | '==' | '<' | '>' | 'in'
|
|
398
|
+
parseRelation() {
|
|
399
|
+
let t = this.parseAddition();
|
|
400
|
+
for (; this.isRelationalOp(this.current.type); ) {
|
|
401
|
+
const e = this.current.type;
|
|
402
|
+
this.advance();
|
|
403
|
+
const i = this.parseAddition();
|
|
404
|
+
t = new L(this.toBinaryOp(e), t, i);
|
|
405
|
+
}
|
|
406
|
+
return t;
|
|
407
|
+
}
|
|
408
|
+
// addition = multiplication ( ('+' | '-') multiplication )*
|
|
409
|
+
parseAddition() {
|
|
410
|
+
let t = this.parseMultiplication();
|
|
411
|
+
for (; this.current.type === s.PLUS || this.current.type === s.MINUS; ) {
|
|
412
|
+
const e = this.current.type;
|
|
413
|
+
this.advance();
|
|
414
|
+
const i = this.parseMultiplication();
|
|
415
|
+
t = new L(e === s.PLUS ? c.ADD : c.SUBTRACT, t, i);
|
|
416
|
+
}
|
|
417
|
+
return t;
|
|
418
|
+
}
|
|
419
|
+
// multiplication = unary ( ('*' | '/' | '%') unary )*
|
|
420
|
+
parseMultiplication() {
|
|
421
|
+
let t = this.parseUnary();
|
|
422
|
+
for (; this.current.type === s.STAR || this.current.type === s.SLASH || this.current.type === s.PERCENT; ) {
|
|
423
|
+
const e = this.current.type;
|
|
424
|
+
this.advance();
|
|
425
|
+
const i = this.parseUnary(), n = e === s.STAR ? c.MULTIPLY : e === s.SLASH ? c.DIVIDE : c.MODULO;
|
|
426
|
+
t = new L(n, t, i);
|
|
427
|
+
}
|
|
428
|
+
return t;
|
|
429
|
+
}
|
|
430
|
+
// unary = '!'+ member | '-'+ member | member
|
|
431
|
+
parseUnary() {
|
|
432
|
+
return this.current.type === s.BANG ? (this.advance(), new O(g.NOT, this.parseUnary())) : this.current.type === s.MINUS ? (this.advance(), new O(g.NEGATE, this.parseUnary())) : this.parseMember();
|
|
433
|
+
}
|
|
434
|
+
// member = primary ( selector | index | fieldCall )*
|
|
435
|
+
parseMember() {
|
|
436
|
+
let t = this.parsePrimary();
|
|
437
|
+
for (; ; )
|
|
438
|
+
if (this.current.type === s.DOT) {
|
|
439
|
+
this.advance();
|
|
440
|
+
const e = this.expectIdentifier();
|
|
441
|
+
if (this.current.type === s.LPAREN) {
|
|
442
|
+
this.advance();
|
|
443
|
+
const i = this.parseExprList();
|
|
444
|
+
this.expect(s.RPAREN);
|
|
445
|
+
const n = z.has(e);
|
|
446
|
+
t = new y(t, e, i, n);
|
|
447
|
+
} else
|
|
448
|
+
t = new N(t, e);
|
|
449
|
+
} else if (this.current.type === s.LBRACKET) {
|
|
450
|
+
this.advance();
|
|
451
|
+
const e = this.parseExpr();
|
|
452
|
+
this.expect(s.RBRACKET), t = new Q(t, e);
|
|
453
|
+
} else
|
|
454
|
+
break;
|
|
455
|
+
return t;
|
|
456
|
+
}
|
|
457
|
+
// primary = literal | ident callArgs? | listLiteral | mapLiteral | structLiteral | '(' expr ')' | '.' ident callArgs?
|
|
458
|
+
parsePrimary() {
|
|
459
|
+
if (this.isLiteralToken(this.current.type))
|
|
460
|
+
return this.parseLiteral();
|
|
461
|
+
if (this.current.type === s.LBRACKET)
|
|
462
|
+
return this.parseListLiteral();
|
|
463
|
+
if (this.current.type === s.LBRACE)
|
|
464
|
+
return this.parseMapOrStructLiteral(null);
|
|
465
|
+
if (this.current.type === s.LPAREN) {
|
|
466
|
+
this.advance();
|
|
467
|
+
const t = this.parseExpr();
|
|
468
|
+
return this.expect(s.RPAREN), t;
|
|
469
|
+
}
|
|
470
|
+
if (this.current.type === s.DOT) {
|
|
471
|
+
this.advance();
|
|
472
|
+
const t = this.expectIdentifier();
|
|
473
|
+
if (this.current.type === s.LPAREN) {
|
|
474
|
+
this.advance();
|
|
475
|
+
const e = this.parseExprList();
|
|
476
|
+
return this.expect(s.RPAREN), new y(null, t, e);
|
|
477
|
+
}
|
|
478
|
+
return new N(null, t);
|
|
479
|
+
}
|
|
480
|
+
if (this.current.type === s.IDENTIFIER) {
|
|
481
|
+
const t = this.current.value;
|
|
482
|
+
if (this.advance(), this.current.type === s.LPAREN) {
|
|
483
|
+
this.advance();
|
|
484
|
+
const e = this.parseExprList();
|
|
485
|
+
return this.expect(s.RPAREN), new y(null, t, e);
|
|
486
|
+
}
|
|
487
|
+
if (this.current.type === s.DOT && this.isQualifiedStructLiteral()) {
|
|
488
|
+
const e = this.parseQualifiedIdent(t);
|
|
489
|
+
return this.parseMapOrStructLiteral(e);
|
|
490
|
+
}
|
|
491
|
+
return this.current.type === s.LBRACE ? this.parseMapOrStructLiteral(t) : new U(t);
|
|
492
|
+
}
|
|
493
|
+
throw new f(
|
|
494
|
+
`Unexpected token: ${this.current.value}`,
|
|
495
|
+
this.current.line,
|
|
496
|
+
this.current.column
|
|
497
|
+
);
|
|
498
|
+
}
|
|
499
|
+
// listLiteral = '[' exprList? ','? ']'
|
|
500
|
+
parseListLiteral() {
|
|
501
|
+
this.expect(s.LBRACKET);
|
|
502
|
+
const t = [];
|
|
503
|
+
return this.current.type !== s.RBRACKET && (t.push(...this.parseExprList()), this.current.type === s.COMMA && this.advance()), this.expect(s.RBRACKET), new q(t);
|
|
504
|
+
}
|
|
505
|
+
// mapLiteral or structLiteral
|
|
506
|
+
parseMapOrStructLiteral(t) {
|
|
507
|
+
if (this.expect(s.LBRACE), this.current.type === s.RBRACE)
|
|
508
|
+
return this.advance(), t !== null ? new S(t, []) : new C([]);
|
|
509
|
+
if (this.current.type === s.IDENTIFIER && this.peekAhead(1).type === s.COLON || t !== null) {
|
|
510
|
+
const i = this.parseFieldInits();
|
|
511
|
+
return this.current.type === s.COMMA && this.advance(), this.expect(s.RBRACE), new S(t, i);
|
|
512
|
+
} else {
|
|
513
|
+
const i = this.parseMapInits();
|
|
514
|
+
return this.current.type === s.COMMA && this.advance(), this.expect(s.RBRACE), new C(i);
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
// exprList = expr ( ',' expr )*
|
|
518
|
+
parseExprList() {
|
|
519
|
+
const t = [], e = this.current.type;
|
|
520
|
+
if (e === s.RPAREN || e === s.RBRACKET)
|
|
521
|
+
return t;
|
|
522
|
+
for (t.push(this.parseExpr()); this.current.type === s.COMMA && (this.advance(), !(this.current.type === s.RPAREN || this.current.type === s.RBRACKET)); )
|
|
523
|
+
t.push(this.parseExpr());
|
|
524
|
+
return t;
|
|
525
|
+
}
|
|
526
|
+
// mapInits = mapInit ( ',' mapInit )*
|
|
527
|
+
parseMapInits() {
|
|
528
|
+
const t = [];
|
|
529
|
+
for (t.push(this.parseMapInit()); this.current.type === s.COMMA && (this.advance(), this.current.type !== s.RBRACE); )
|
|
530
|
+
t.push(this.parseMapInit());
|
|
531
|
+
return t;
|
|
532
|
+
}
|
|
533
|
+
// mapInit = expr ':' expr
|
|
534
|
+
parseMapInit() {
|
|
535
|
+
const t = this.parseExpr();
|
|
536
|
+
this.expect(s.COLON);
|
|
537
|
+
const e = this.parseExpr();
|
|
538
|
+
return new W(t, e);
|
|
539
|
+
}
|
|
540
|
+
// fieldInits = fieldInit ( ',' fieldInit )*
|
|
541
|
+
parseFieldInits() {
|
|
542
|
+
const t = [];
|
|
543
|
+
for (t.push(this.parseFieldInit()); this.current.type === s.COMMA && (this.advance(), this.current.type !== s.RBRACE); )
|
|
544
|
+
t.push(this.parseFieldInit());
|
|
545
|
+
return t;
|
|
546
|
+
}
|
|
547
|
+
// fieldInit = ident ':' expr
|
|
548
|
+
parseFieldInit() {
|
|
549
|
+
const t = this.expectIdentifier();
|
|
550
|
+
this.expect(s.COLON);
|
|
551
|
+
const e = this.parseExpr();
|
|
552
|
+
return new j(t, e);
|
|
553
|
+
}
|
|
554
|
+
// qualifiedIdent = ident ( '.' ident )*
|
|
555
|
+
parseQualifiedIdent(t) {
|
|
556
|
+
let e = t;
|
|
557
|
+
for (; this.current.type === s.DOT; )
|
|
558
|
+
this.advance(), e += ".", e += this.expectIdentifier();
|
|
559
|
+
return e;
|
|
560
|
+
}
|
|
561
|
+
parseLiteral() {
|
|
562
|
+
const t = this.current;
|
|
563
|
+
switch (this.advance(), t.type) {
|
|
564
|
+
case s.NULL:
|
|
565
|
+
return new d(null, w.NULL_VALUE);
|
|
566
|
+
case s.TRUE:
|
|
567
|
+
return new d(!0, w.BOOL);
|
|
568
|
+
case s.FALSE:
|
|
569
|
+
return new d(!1, w.BOOL);
|
|
570
|
+
case s.INT:
|
|
571
|
+
return new d(this.parseIntLiteral(t.value), w.INT);
|
|
572
|
+
case s.UINT:
|
|
573
|
+
return new d(this.parseUintLiteral(t.value), w.UINT);
|
|
574
|
+
case s.DOUBLE:
|
|
575
|
+
return new d(parseFloat(t.value), w.DOUBLE);
|
|
576
|
+
case s.STRING:
|
|
577
|
+
return new d(this.parseStringLiteral(t.value), w.STRING);
|
|
578
|
+
case s.BYTES:
|
|
579
|
+
return new d(this.parseBytesLiteral(t.value), w.BYTES);
|
|
580
|
+
default:
|
|
581
|
+
throw new f(`Not a literal: ${t.value}`, t.line, t.column);
|
|
582
|
+
}
|
|
583
|
+
}
|
|
584
|
+
parseIntLiteral(t) {
|
|
585
|
+
return t.startsWith("-0x") || t.startsWith("-0X") ? -parseInt(t.substring(3), 16) : t.startsWith("0x") || t.startsWith("0X") ? parseInt(t.substring(2), 16) : parseInt(t, 10);
|
|
586
|
+
}
|
|
587
|
+
parseUintLiteral(t) {
|
|
588
|
+
const e = t.substring(0, t.length - 1);
|
|
589
|
+
return e.startsWith("0x") || e.startsWith("0X") ? parseInt(e.substring(2), 16) : parseInt(e, 10);
|
|
590
|
+
}
|
|
591
|
+
parseStringLiteral(t) {
|
|
592
|
+
const e = t.startsWith("r") || t.startsWith("R");
|
|
593
|
+
let i;
|
|
594
|
+
return e && (t.substring(1).startsWith('"""') || t.substring(1).startsWith("'''")) ? i = t.substring(4, t.length - 3) : t.startsWith('"""') || t.startsWith("'''") ? (i = t.substring(3, t.length - 3), i = this.unescapeString(i)) : e ? i = t.substring(2, t.length - 1) : (i = t.substring(1, t.length - 1), i = this.unescapeString(i)), i;
|
|
595
|
+
}
|
|
596
|
+
parseBytesLiteral(t) {
|
|
597
|
+
const e = t.substring(2, t.length - 1);
|
|
598
|
+
return this.unescapeString(e);
|
|
599
|
+
}
|
|
600
|
+
unescapeString(t) {
|
|
601
|
+
let e = "", i = 0;
|
|
602
|
+
for (; i < t.length; )
|
|
603
|
+
if (t[i] === "\\" && i + 1 < t.length) {
|
|
604
|
+
const n = t[i + 1];
|
|
605
|
+
switch (n) {
|
|
606
|
+
case "\\":
|
|
607
|
+
e += "\\", i += 2;
|
|
608
|
+
break;
|
|
609
|
+
case '"':
|
|
610
|
+
e += '"', i += 2;
|
|
611
|
+
break;
|
|
612
|
+
case "'":
|
|
613
|
+
e += "'", i += 2;
|
|
614
|
+
break;
|
|
615
|
+
case "`":
|
|
616
|
+
e += "`", i += 2;
|
|
617
|
+
break;
|
|
618
|
+
case "?":
|
|
619
|
+
e += "?", i += 2;
|
|
620
|
+
break;
|
|
621
|
+
case "a":
|
|
622
|
+
e += "\x07", i += 2;
|
|
623
|
+
break;
|
|
624
|
+
case "b":
|
|
625
|
+
e += "\b", i += 2;
|
|
626
|
+
break;
|
|
627
|
+
case "f":
|
|
628
|
+
e += "\f", i += 2;
|
|
629
|
+
break;
|
|
630
|
+
case "n":
|
|
631
|
+
e += `
|
|
632
|
+
`, i += 2;
|
|
633
|
+
break;
|
|
634
|
+
case "r":
|
|
635
|
+
e += "\r", i += 2;
|
|
636
|
+
break;
|
|
637
|
+
case "t":
|
|
638
|
+
e += " ", i += 2;
|
|
639
|
+
break;
|
|
640
|
+
case "v":
|
|
641
|
+
e += "\v", i += 2;
|
|
642
|
+
break;
|
|
643
|
+
case "x":
|
|
644
|
+
if (i + 3 < t.length) {
|
|
645
|
+
const o = t.substring(i + 2, i + 4);
|
|
646
|
+
e += String.fromCharCode(parseInt(o, 16)), i += 4;
|
|
647
|
+
} else
|
|
648
|
+
e += t[i], i++;
|
|
649
|
+
break;
|
|
650
|
+
case "u":
|
|
651
|
+
if (i + 5 < t.length) {
|
|
652
|
+
const o = t.substring(i + 2, i + 6);
|
|
653
|
+
e += String.fromCharCode(parseInt(o, 16)), i += 6;
|
|
654
|
+
} else
|
|
655
|
+
e += t[i], i++;
|
|
656
|
+
break;
|
|
657
|
+
case "U":
|
|
658
|
+
if (i + 9 < t.length) {
|
|
659
|
+
const o = t.substring(i + 2, i + 10), l = parseInt(o, 16);
|
|
660
|
+
e += String.fromCodePoint(l), i += 10;
|
|
661
|
+
} else
|
|
662
|
+
e += t[i], i++;
|
|
663
|
+
break;
|
|
664
|
+
default:
|
|
665
|
+
if (i + 3 < t.length && n >= "0" && n <= "3" && t[i + 2] >= "0" && t[i + 2] <= "7" && t[i + 3] >= "0" && t[i + 3] <= "7") {
|
|
666
|
+
const o = t.substring(i + 1, i + 4);
|
|
667
|
+
e += String.fromCharCode(parseInt(o, 8)), i += 4;
|
|
668
|
+
} else
|
|
669
|
+
e += t[i], i++;
|
|
670
|
+
}
|
|
671
|
+
} else
|
|
672
|
+
e += t[i], i++;
|
|
673
|
+
return e;
|
|
674
|
+
}
|
|
675
|
+
isLiteralToken(t) {
|
|
676
|
+
return t === s.NULL || t === s.TRUE || t === s.FALSE || t === s.INT || t === s.UINT || t === s.DOUBLE || t === s.STRING || t === s.BYTES;
|
|
677
|
+
}
|
|
678
|
+
isRelationalOp(t) {
|
|
679
|
+
return t === s.LT || t === s.LE || t === s.GT || t === s.GE || t === s.EQ || t === s.NE || t === s.IN;
|
|
680
|
+
}
|
|
681
|
+
toBinaryOp(t) {
|
|
682
|
+
switch (t) {
|
|
683
|
+
case s.LT:
|
|
684
|
+
return c.LESS;
|
|
685
|
+
case s.LE:
|
|
686
|
+
return c.LESS_EQUAL;
|
|
687
|
+
case s.GT:
|
|
688
|
+
return c.GREATER;
|
|
689
|
+
case s.GE:
|
|
690
|
+
return c.GREATER_EQUAL;
|
|
691
|
+
case s.EQ:
|
|
692
|
+
return c.EQUAL;
|
|
693
|
+
case s.NE:
|
|
694
|
+
return c.NOT_EQUAL;
|
|
695
|
+
case s.IN:
|
|
696
|
+
return c.IN;
|
|
697
|
+
default:
|
|
698
|
+
throw new f(
|
|
699
|
+
`Unknown relational operator: ${t}`,
|
|
700
|
+
this.current.line,
|
|
701
|
+
this.current.column
|
|
702
|
+
);
|
|
703
|
+
}
|
|
704
|
+
}
|
|
705
|
+
match(t) {
|
|
706
|
+
return this.current.type === t ? (this.advance(), !0) : !1;
|
|
707
|
+
}
|
|
708
|
+
expect(t) {
|
|
709
|
+
if (this.current.type !== t)
|
|
710
|
+
throw new f(
|
|
711
|
+
`Expected ${t} but found ${this.current.type}`,
|
|
712
|
+
this.current.line,
|
|
713
|
+
this.current.column
|
|
714
|
+
);
|
|
715
|
+
this.advance();
|
|
716
|
+
}
|
|
717
|
+
expectIdentifier() {
|
|
718
|
+
if (this.current.type !== s.IDENTIFIER)
|
|
719
|
+
throw new f(
|
|
720
|
+
`Expected identifier but found ${this.current.value}`,
|
|
721
|
+
this.current.line,
|
|
722
|
+
this.current.column
|
|
723
|
+
);
|
|
724
|
+
const t = this.current.value;
|
|
725
|
+
return this.advance(), t;
|
|
726
|
+
}
|
|
727
|
+
advance() {
|
|
728
|
+
this.current = this.lexer.next();
|
|
729
|
+
}
|
|
730
|
+
peekAhead(t) {
|
|
731
|
+
return this.lexer.peek(t);
|
|
732
|
+
}
|
|
733
|
+
// Check if we have a pattern like: . ident ( . ident )* {
|
|
734
|
+
isQualifiedStructLiteral() {
|
|
735
|
+
let t = 1, e = this.peekAhead(t);
|
|
736
|
+
if (e.type !== s.IDENTIFIER)
|
|
737
|
+
return !1;
|
|
738
|
+
for (t++, e = this.peekAhead(t); e.type === s.DOT; ) {
|
|
739
|
+
if (t++, e = this.peekAhead(t), e.type !== s.IDENTIFIER)
|
|
740
|
+
return !1;
|
|
741
|
+
t++, e = this.peekAhead(t);
|
|
742
|
+
}
|
|
743
|
+
return e.type === s.LBRACE;
|
|
744
|
+
}
|
|
745
|
+
}
|
|
746
|
+
class a extends Error {
|
|
747
|
+
constructor(t) {
|
|
748
|
+
super(t), this.name = "EvaluationError";
|
|
749
|
+
}
|
|
750
|
+
}
|
|
751
|
+
function I(r) {
|
|
752
|
+
if (r == null)
|
|
753
|
+
return 0;
|
|
754
|
+
if (typeof r == "string" || Array.isArray(r))
|
|
755
|
+
return r.length;
|
|
756
|
+
if (typeof r == "object" && r !== null)
|
|
757
|
+
return Object.keys(r).length;
|
|
758
|
+
throw new Error(`size() not supported for type: ${typeof r}`);
|
|
759
|
+
}
|
|
760
|
+
function b(r) {
|
|
761
|
+
if (typeof r == "number")
|
|
762
|
+
return Math.trunc(r);
|
|
763
|
+
if (typeof r == "string") {
|
|
764
|
+
const t = parseInt(r, 10);
|
|
765
|
+
if (isNaN(t))
|
|
766
|
+
throw new Error(`Cannot convert to int: ${r}`);
|
|
767
|
+
return t;
|
|
768
|
+
}
|
|
769
|
+
if (typeof r == "boolean")
|
|
770
|
+
return r ? 1 : 0;
|
|
771
|
+
throw new Error(`Cannot convert to int: ${r}`);
|
|
772
|
+
}
|
|
773
|
+
function v(r) {
|
|
774
|
+
const t = b(r);
|
|
775
|
+
if (t < 0)
|
|
776
|
+
throw new Error(`Cannot convert negative value to uint: ${r}`);
|
|
777
|
+
return t;
|
|
778
|
+
}
|
|
779
|
+
function M(r) {
|
|
780
|
+
if (typeof r == "number")
|
|
781
|
+
return r;
|
|
782
|
+
if (typeof r == "string") {
|
|
783
|
+
const t = parseFloat(r);
|
|
784
|
+
if (isNaN(t))
|
|
785
|
+
throw new Error(`Cannot convert to double: ${r}`);
|
|
786
|
+
return t;
|
|
787
|
+
}
|
|
788
|
+
throw new Error(`Cannot convert to double: ${r}`);
|
|
789
|
+
}
|
|
790
|
+
function D(r) {
|
|
791
|
+
return r == null ? "null" : String(r);
|
|
792
|
+
}
|
|
793
|
+
function T(r) {
|
|
794
|
+
return typeof r == "boolean" ? r : typeof r == "number" ? r !== 0 : typeof r == "string" || Array.isArray(r) ? r.length > 0 : typeof r == "object" && r !== null ? Object.keys(r).length > 0 : r != null;
|
|
795
|
+
}
|
|
796
|
+
function x(r) {
|
|
797
|
+
return r == null ? "null" : typeof r == "boolean" ? "bool" : typeof r == "number" ? Number.isInteger(r) ? "int" : "double" : typeof r == "string" ? "string" : Array.isArray(r) ? "list" : typeof r == "object" ? "map" : "unknown";
|
|
798
|
+
}
|
|
799
|
+
function B(r, t) {
|
|
800
|
+
return typeof r == "object" && r !== null && typeof t == "string" ? t in r : !1;
|
|
801
|
+
}
|
|
802
|
+
function G(r, t) {
|
|
803
|
+
try {
|
|
804
|
+
return new RegExp(t).test(r);
|
|
805
|
+
} catch {
|
|
806
|
+
throw new Error(`Invalid regex pattern: ${t}`);
|
|
807
|
+
}
|
|
808
|
+
}
|
|
809
|
+
function $(r) {
|
|
810
|
+
if (r.length === 0)
|
|
811
|
+
throw new Error("max() requires at least one argument");
|
|
812
|
+
let t = r[0];
|
|
813
|
+
for (let e = 1; e < r.length; e++)
|
|
814
|
+
E(r[e], t) > 0 && (t = r[e]);
|
|
815
|
+
return t;
|
|
816
|
+
}
|
|
817
|
+
function P(r) {
|
|
818
|
+
if (r.length === 0)
|
|
819
|
+
throw new Error("min() requires at least one argument");
|
|
820
|
+
let t = r[0];
|
|
821
|
+
for (let e = 1; e < r.length; e++)
|
|
822
|
+
E(r[e], t) < 0 && (t = r[e]);
|
|
823
|
+
return t;
|
|
824
|
+
}
|
|
825
|
+
function E(r, t) {
|
|
826
|
+
if (r === null && t === null)
|
|
827
|
+
return 0;
|
|
828
|
+
if (r === null)
|
|
829
|
+
return -1;
|
|
830
|
+
if (t === null)
|
|
831
|
+
return 1;
|
|
832
|
+
if (typeof r == "number" && typeof t == "number")
|
|
833
|
+
return r - t;
|
|
834
|
+
if (typeof r == "string" && typeof t == "string")
|
|
835
|
+
return r.localeCompare(t);
|
|
836
|
+
if (typeof r == "boolean" && typeof t == "boolean")
|
|
837
|
+
return r === t ? 0 : r ? 1 : -1;
|
|
838
|
+
if (Array.isArray(r) && Array.isArray(t)) {
|
|
839
|
+
const e = Math.min(r.length, t.length);
|
|
840
|
+
for (let i = 0; i < e; i++) {
|
|
841
|
+
const n = E(r[i], t[i]);
|
|
842
|
+
if (n !== 0)
|
|
843
|
+
return n;
|
|
844
|
+
}
|
|
845
|
+
return r.length - t.length;
|
|
846
|
+
}
|
|
847
|
+
throw new Error(`Cannot compare types: ${typeof r} and ${typeof t}`);
|
|
848
|
+
}
|
|
849
|
+
function A(r, t) {
|
|
850
|
+
if (r == null || t === null || t === void 0)
|
|
851
|
+
return r === t;
|
|
852
|
+
if (Array.isArray(r) && Array.isArray(t)) {
|
|
853
|
+
if (r.length !== t.length)
|
|
854
|
+
return !1;
|
|
855
|
+
for (let e = 0; e < r.length; e++)
|
|
856
|
+
if (!A(r[e], t[e]))
|
|
857
|
+
return !1;
|
|
858
|
+
return !0;
|
|
859
|
+
}
|
|
860
|
+
if (typeof r == "object" && typeof t == "object" && !Array.isArray(r) && !Array.isArray(t)) {
|
|
861
|
+
const e = Object.keys(r), i = Object.keys(t);
|
|
862
|
+
if (e.length !== i.length)
|
|
863
|
+
return !1;
|
|
864
|
+
for (const n of e)
|
|
865
|
+
if (!(n in t) || !A(r[n], t[n]))
|
|
866
|
+
return !1;
|
|
867
|
+
return !0;
|
|
868
|
+
}
|
|
869
|
+
return r === t;
|
|
870
|
+
}
|
|
871
|
+
function _(r, t) {
|
|
872
|
+
for (const e of r)
|
|
873
|
+
if (A(e, t))
|
|
874
|
+
return !0;
|
|
875
|
+
return !1;
|
|
876
|
+
}
|
|
877
|
+
const Z = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
|
|
878
|
+
__proto__: null,
|
|
879
|
+
asBool: T,
|
|
880
|
+
asDouble: M,
|
|
881
|
+
asInt: b,
|
|
882
|
+
asString: D,
|
|
883
|
+
asUInt: v,
|
|
884
|
+
compare: E,
|
|
885
|
+
containsInArray: _,
|
|
886
|
+
deepEquals: A,
|
|
887
|
+
has: B,
|
|
888
|
+
matches: G,
|
|
889
|
+
max: $,
|
|
890
|
+
min: P,
|
|
891
|
+
sizeOf: I,
|
|
892
|
+
typeOf: x
|
|
893
|
+
}, Symbol.toStringTag, { value: "Module" }));
|
|
894
|
+
class k {
|
|
895
|
+
callFunction(t, e) {
|
|
896
|
+
switch (t) {
|
|
897
|
+
case "size":
|
|
898
|
+
return I(e[0]);
|
|
899
|
+
case "int":
|
|
900
|
+
return b(e[0]);
|
|
901
|
+
case "uint":
|
|
902
|
+
return v(e[0]);
|
|
903
|
+
case "double":
|
|
904
|
+
return M(e[0]);
|
|
905
|
+
case "string":
|
|
906
|
+
return D(e[0]);
|
|
907
|
+
case "bool":
|
|
908
|
+
return T(e[0]);
|
|
909
|
+
case "type":
|
|
910
|
+
return x(e[0]);
|
|
911
|
+
case "has":
|
|
912
|
+
if (e.length !== 2)
|
|
913
|
+
throw new Error("has() requires 2 arguments");
|
|
914
|
+
return B(e[0], e[1]);
|
|
915
|
+
case "matches":
|
|
916
|
+
if (e.length !== 2)
|
|
917
|
+
throw new Error("matches() requires 2 arguments");
|
|
918
|
+
return G(e[0], e[1]);
|
|
919
|
+
case "max":
|
|
920
|
+
return $(e);
|
|
921
|
+
case "min":
|
|
922
|
+
return P(e);
|
|
923
|
+
default:
|
|
924
|
+
throw new Error(`Unknown function: ${t}`);
|
|
925
|
+
}
|
|
926
|
+
}
|
|
927
|
+
callMethod(t, e, i) {
|
|
928
|
+
if (t == null)
|
|
929
|
+
throw new Error("Cannot call method on null");
|
|
930
|
+
switch (e) {
|
|
931
|
+
case "contains":
|
|
932
|
+
if (typeof t == "string" && i.length === 1 && typeof i[0] == "string")
|
|
933
|
+
return t.includes(i[0]);
|
|
934
|
+
if (Array.isArray(t) && i.length === 1)
|
|
935
|
+
return t.includes(i[0]);
|
|
936
|
+
throw new Error("Invalid arguments for contains()");
|
|
937
|
+
case "startsWith":
|
|
938
|
+
if (typeof t == "string" && i.length === 1 && typeof i[0] == "string")
|
|
939
|
+
return t.startsWith(i[0]);
|
|
940
|
+
throw new Error("startsWith() requires string target and argument");
|
|
941
|
+
case "endsWith":
|
|
942
|
+
if (typeof t == "string" && i.length === 1 && typeof i[0] == "string")
|
|
943
|
+
return t.endsWith(i[0]);
|
|
944
|
+
throw new Error("endsWith() requires string target and argument");
|
|
945
|
+
case "toLowerCase":
|
|
946
|
+
if (typeof t == "string" && i.length === 0)
|
|
947
|
+
return t.toLowerCase();
|
|
948
|
+
throw new Error("toLowerCase() requires string target");
|
|
949
|
+
case "toUpperCase":
|
|
950
|
+
if (typeof t == "string" && i.length === 0)
|
|
951
|
+
return t.toUpperCase();
|
|
952
|
+
throw new Error("toUpperCase() requires string target");
|
|
953
|
+
case "trim":
|
|
954
|
+
if (typeof t == "string" && i.length === 0)
|
|
955
|
+
return t.trim();
|
|
956
|
+
throw new Error("trim() requires string target");
|
|
957
|
+
case "replace":
|
|
958
|
+
if (typeof t == "string" && i.length === 2 && typeof i[0] == "string" && typeof i[1] == "string")
|
|
959
|
+
return t.replaceAll(i[0], i[1]);
|
|
960
|
+
throw new Error("replace() requires string target and 2 string arguments");
|
|
961
|
+
case "split":
|
|
962
|
+
if (typeof t == "string" && i.length === 1 && typeof i[0] == "string")
|
|
963
|
+
return t.split(i[0]);
|
|
964
|
+
throw new Error("split() requires string target and separator");
|
|
965
|
+
case "size":
|
|
966
|
+
return I(t);
|
|
967
|
+
// Macro functions are handled in the interpreter with special logic
|
|
968
|
+
case "map":
|
|
969
|
+
case "filter":
|
|
970
|
+
case "all":
|
|
971
|
+
case "exists":
|
|
972
|
+
case "existsOne":
|
|
973
|
+
throw new a(
|
|
974
|
+
`Macro function ${e} was not properly handled by the interpreter`
|
|
975
|
+
);
|
|
976
|
+
default:
|
|
977
|
+
return this.callNativeMethod(t, e, i);
|
|
978
|
+
}
|
|
979
|
+
}
|
|
980
|
+
/**
|
|
981
|
+
* Attempts to call a native JavaScript method on the target object.
|
|
982
|
+
*
|
|
983
|
+
* @param target The object to call the method on
|
|
984
|
+
* @param name The method name
|
|
985
|
+
* @param args The arguments
|
|
986
|
+
* @returns The result of the method call
|
|
987
|
+
* @throws Error if the method doesn't exist or call fails
|
|
988
|
+
*/
|
|
989
|
+
callNativeMethod(t, e, i) {
|
|
990
|
+
if (typeof t[e] == "function")
|
|
991
|
+
try {
|
|
992
|
+
return t[e](...i);
|
|
993
|
+
} catch (n) {
|
|
994
|
+
throw new a(
|
|
995
|
+
`Invocation of method '${e}' failed: ${n instanceof Error ? n.message : String(n)}`
|
|
996
|
+
);
|
|
997
|
+
}
|
|
998
|
+
throw new Error(
|
|
999
|
+
`No such method '${e}' on type ${typeof t} with ${i.length} argument(s)`
|
|
1000
|
+
);
|
|
1001
|
+
}
|
|
1002
|
+
}
|
|
1003
|
+
class H {
|
|
1004
|
+
/**
|
|
1005
|
+
* Constructs an interpreter with the specified variables and functions.
|
|
1006
|
+
*
|
|
1007
|
+
* @param variables A map of variable names to their values. If null, an empty object is used.
|
|
1008
|
+
* @param functions An instance of Functions to handle function calls. If null, StandardFunctions is used.
|
|
1009
|
+
*/
|
|
1010
|
+
constructor(t, e) {
|
|
1011
|
+
this.variables = t ?? {}, this.functions = e ?? new k();
|
|
1012
|
+
}
|
|
1013
|
+
/**
|
|
1014
|
+
* Evaluates a CEL expression and returns its result.
|
|
1015
|
+
*
|
|
1016
|
+
* @param expr The expression to evaluate
|
|
1017
|
+
* @returns The result of the evaluation
|
|
1018
|
+
* @throws EvaluationError if evaluation fails
|
|
1019
|
+
*/
|
|
1020
|
+
evaluate(t) {
|
|
1021
|
+
return t.accept(this);
|
|
1022
|
+
}
|
|
1023
|
+
visitLiteral(t) {
|
|
1024
|
+
return t.value;
|
|
1025
|
+
}
|
|
1026
|
+
visitIdentifier(t) {
|
|
1027
|
+
if (!(t.name in this.variables))
|
|
1028
|
+
throw new a(`Undefined variable: ${t.name}`);
|
|
1029
|
+
return this.variables[t.name];
|
|
1030
|
+
}
|
|
1031
|
+
visitSelect(t) {
|
|
1032
|
+
const e = t.operand !== null ? this.evaluate(t.operand) : this.variables;
|
|
1033
|
+
if (e == null) {
|
|
1034
|
+
if (t.isTest)
|
|
1035
|
+
return !1;
|
|
1036
|
+
throw new a(`Cannot select field ${t.field} from null`);
|
|
1037
|
+
}
|
|
1038
|
+
if (typeof e == "object") {
|
|
1039
|
+
if (t.isTest)
|
|
1040
|
+
return t.field in e;
|
|
1041
|
+
if (!(t.field in e))
|
|
1042
|
+
throw new a(`Field ${t.field} not found`);
|
|
1043
|
+
return e[t.field];
|
|
1044
|
+
}
|
|
1045
|
+
throw new a("Cannot select field from non-object type");
|
|
1046
|
+
}
|
|
1047
|
+
visitCall(t) {
|
|
1048
|
+
if (t.isMacro && t.target !== null) {
|
|
1049
|
+
const i = this.evaluate(t.target);
|
|
1050
|
+
if (t.args.length === 0)
|
|
1051
|
+
throw new a(`Macro ${t.functionName} requires arguments`);
|
|
1052
|
+
const n = t.args[0];
|
|
1053
|
+
if (!(n instanceof U))
|
|
1054
|
+
throw new a(
|
|
1055
|
+
`First argument to macro ${t.functionName} must be a variable name`
|
|
1056
|
+
);
|
|
1057
|
+
const o = n.name;
|
|
1058
|
+
if (t.args.length < 2)
|
|
1059
|
+
throw new a(`Macro ${t.functionName} requires an expression argument`);
|
|
1060
|
+
const l = t.args[1];
|
|
1061
|
+
return this.evaluateMacro(i, t.functionName, o, l);
|
|
1062
|
+
}
|
|
1063
|
+
const e = [];
|
|
1064
|
+
for (const i of t.args)
|
|
1065
|
+
e.push(this.evaluate(i));
|
|
1066
|
+
if (t.target !== null) {
|
|
1067
|
+
const i = this.evaluate(t.target);
|
|
1068
|
+
return this.functions.callMethod(i, t.functionName, e);
|
|
1069
|
+
} else
|
|
1070
|
+
return this.functions.callFunction(t.functionName, e);
|
|
1071
|
+
}
|
|
1072
|
+
evaluateMacro(t, e, i, n) {
|
|
1073
|
+
if (!Array.isArray(t))
|
|
1074
|
+
throw new a(`Macro ${e} requires a list target`);
|
|
1075
|
+
const o = this.variables[i], l = i in this.variables;
|
|
1076
|
+
try {
|
|
1077
|
+
switch (e) {
|
|
1078
|
+
case "map": {
|
|
1079
|
+
const u = [];
|
|
1080
|
+
for (const p of t)
|
|
1081
|
+
this.variables[i] = p, u.push(this.evaluate(n));
|
|
1082
|
+
return u;
|
|
1083
|
+
}
|
|
1084
|
+
case "filter": {
|
|
1085
|
+
const u = [];
|
|
1086
|
+
for (const p of t)
|
|
1087
|
+
this.variables[i] = p, this.evaluate(n) === !0 && u.push(p);
|
|
1088
|
+
return u;
|
|
1089
|
+
}
|
|
1090
|
+
case "all": {
|
|
1091
|
+
for (const u of t)
|
|
1092
|
+
if (this.variables[i] = u, this.evaluate(n) !== !0)
|
|
1093
|
+
return !1;
|
|
1094
|
+
return !0;
|
|
1095
|
+
}
|
|
1096
|
+
case "exists": {
|
|
1097
|
+
for (const u of t)
|
|
1098
|
+
if (this.variables[i] = u, this.evaluate(n) === !0)
|
|
1099
|
+
return !0;
|
|
1100
|
+
return !1;
|
|
1101
|
+
}
|
|
1102
|
+
case "existsOne": {
|
|
1103
|
+
let u = 0;
|
|
1104
|
+
for (const p of t)
|
|
1105
|
+
if (this.variables[i] = p, this.evaluate(n) === !0 && (u++, u > 1))
|
|
1106
|
+
return !1;
|
|
1107
|
+
return u === 1;
|
|
1108
|
+
}
|
|
1109
|
+
default:
|
|
1110
|
+
throw new a(`Unknown macro function: ${e}`);
|
|
1111
|
+
}
|
|
1112
|
+
} finally {
|
|
1113
|
+
l ? this.variables[i] = o : delete this.variables[i];
|
|
1114
|
+
}
|
|
1115
|
+
}
|
|
1116
|
+
visitList(t) {
|
|
1117
|
+
const e = [];
|
|
1118
|
+
for (const i of t.elements)
|
|
1119
|
+
e.push(this.evaluate(i));
|
|
1120
|
+
return e;
|
|
1121
|
+
}
|
|
1122
|
+
visitMap(t) {
|
|
1123
|
+
const e = {};
|
|
1124
|
+
for (const i of t.entries) {
|
|
1125
|
+
const n = this.evaluate(i.key), o = this.evaluate(i.value);
|
|
1126
|
+
e[n] = o;
|
|
1127
|
+
}
|
|
1128
|
+
return e;
|
|
1129
|
+
}
|
|
1130
|
+
visitStruct(t) {
|
|
1131
|
+
const e = {};
|
|
1132
|
+
for (const i of t.fields)
|
|
1133
|
+
e[i.field] = this.evaluate(i.value);
|
|
1134
|
+
return e;
|
|
1135
|
+
}
|
|
1136
|
+
visitComprehension(t) {
|
|
1137
|
+
const e = this.evaluate(t.range);
|
|
1138
|
+
if (!Array.isArray(e))
|
|
1139
|
+
throw new a("Comprehension range must be a list");
|
|
1140
|
+
const i = this.variables[t.variable], n = this.variables[t.accumulator], o = t.variable in this.variables, l = t.accumulator in this.variables;
|
|
1141
|
+
try {
|
|
1142
|
+
let u = this.evaluate(t.initializer);
|
|
1143
|
+
this.variables[t.accumulator] = u;
|
|
1144
|
+
for (const p of e)
|
|
1145
|
+
this.variables[t.variable] = p, this.evaluate(t.condition) === !0 && (u = this.evaluate(t.step), this.variables[t.accumulator] = u);
|
|
1146
|
+
return this.evaluate(t.result);
|
|
1147
|
+
} finally {
|
|
1148
|
+
o ? this.variables[t.variable] = i : delete this.variables[t.variable], l ? this.variables[t.accumulator] = n : delete this.variables[t.accumulator];
|
|
1149
|
+
}
|
|
1150
|
+
}
|
|
1151
|
+
visitUnary(t) {
|
|
1152
|
+
const e = this.evaluate(t.operand);
|
|
1153
|
+
switch (t.op) {
|
|
1154
|
+
case g.NOT:
|
|
1155
|
+
if (typeof e != "boolean")
|
|
1156
|
+
throw new a("NOT operator requires boolean operand");
|
|
1157
|
+
return !e;
|
|
1158
|
+
case g.NEGATE:
|
|
1159
|
+
if (typeof e != "number")
|
|
1160
|
+
throw new a("Negation requires numeric operand");
|
|
1161
|
+
return -e;
|
|
1162
|
+
default:
|
|
1163
|
+
throw new a(`Unknown unary operator: ${t.op}`);
|
|
1164
|
+
}
|
|
1165
|
+
}
|
|
1166
|
+
visitBinary(t) {
|
|
1167
|
+
if (t.op === c.LOGICAL_AND)
|
|
1168
|
+
return this.evaluate(t.left) !== !0 ? !1 : this.evaluate(t.right) === !0;
|
|
1169
|
+
if (t.op === c.LOGICAL_OR)
|
|
1170
|
+
return this.evaluate(t.left) === !0 ? !0 : this.evaluate(t.right) === !0;
|
|
1171
|
+
const e = this.evaluate(t.left), i = this.evaluate(t.right);
|
|
1172
|
+
switch (t.op) {
|
|
1173
|
+
case c.ADD:
|
|
1174
|
+
if (typeof e == "string" || typeof i == "string")
|
|
1175
|
+
return String(e) + String(i);
|
|
1176
|
+
if (Array.isArray(e) && Array.isArray(i))
|
|
1177
|
+
return [...e, ...i];
|
|
1178
|
+
if (typeof e == "number" && typeof i == "number")
|
|
1179
|
+
return e + i;
|
|
1180
|
+
throw new a("Invalid operands for addition");
|
|
1181
|
+
case c.SUBTRACT:
|
|
1182
|
+
if (typeof e == "number" && typeof i == "number")
|
|
1183
|
+
return e - i;
|
|
1184
|
+
throw new a("Subtraction requires numeric operands");
|
|
1185
|
+
case c.MULTIPLY:
|
|
1186
|
+
if (typeof e == "number" && typeof i == "number")
|
|
1187
|
+
return e * i;
|
|
1188
|
+
if (typeof e == "string" && typeof i == "number")
|
|
1189
|
+
return e.repeat(i);
|
|
1190
|
+
if (Array.isArray(e) && typeof i == "number") {
|
|
1191
|
+
const n = [];
|
|
1192
|
+
for (let o = 0; o < i; o++)
|
|
1193
|
+
n.push(...e);
|
|
1194
|
+
return n;
|
|
1195
|
+
}
|
|
1196
|
+
throw new a("Invalid operands for multiplication");
|
|
1197
|
+
case c.DIVIDE:
|
|
1198
|
+
if (typeof e == "number" && typeof i == "number") {
|
|
1199
|
+
if (i === 0)
|
|
1200
|
+
throw new a("Division by zero");
|
|
1201
|
+
return e / i;
|
|
1202
|
+
}
|
|
1203
|
+
throw new a("Division requires numeric operands");
|
|
1204
|
+
case c.MODULO:
|
|
1205
|
+
if (typeof e == "number" && typeof i == "number") {
|
|
1206
|
+
if (i === 0)
|
|
1207
|
+
throw new a("Modulo by zero");
|
|
1208
|
+
return e % i;
|
|
1209
|
+
}
|
|
1210
|
+
throw new a("Modulo requires integer operands");
|
|
1211
|
+
case c.EQUAL:
|
|
1212
|
+
return A(e, i);
|
|
1213
|
+
case c.NOT_EQUAL:
|
|
1214
|
+
return !A(e, i);
|
|
1215
|
+
case c.LESS:
|
|
1216
|
+
return E(e, i) < 0;
|
|
1217
|
+
case c.LESS_EQUAL:
|
|
1218
|
+
return E(e, i) <= 0;
|
|
1219
|
+
case c.GREATER:
|
|
1220
|
+
return E(e, i) > 0;
|
|
1221
|
+
case c.GREATER_EQUAL:
|
|
1222
|
+
return E(e, i) >= 0;
|
|
1223
|
+
case c.IN:
|
|
1224
|
+
if (Array.isArray(i))
|
|
1225
|
+
return _(i, e);
|
|
1226
|
+
if (typeof i == "object" && i !== null)
|
|
1227
|
+
return e in i;
|
|
1228
|
+
if (typeof i == "string" && typeof e == "string")
|
|
1229
|
+
return i.includes(e);
|
|
1230
|
+
throw new a("IN operator requires list, map, or string on right side");
|
|
1231
|
+
default:
|
|
1232
|
+
throw new a(`Unknown binary operator: ${t.op}`);
|
|
1233
|
+
}
|
|
1234
|
+
}
|
|
1235
|
+
visitConditional(t) {
|
|
1236
|
+
return this.evaluate(t.condition) === !0 ? this.evaluate(t.thenExpr) : this.evaluate(t.otherwiseExpr);
|
|
1237
|
+
}
|
|
1238
|
+
visitIndex(t) {
|
|
1239
|
+
const e = this.evaluate(t.operand), i = this.evaluate(t.index);
|
|
1240
|
+
if (e == null)
|
|
1241
|
+
throw new a("Cannot index null value");
|
|
1242
|
+
if (Array.isArray(e)) {
|
|
1243
|
+
if (typeof i != "number")
|
|
1244
|
+
throw new a("List index must be an integer");
|
|
1245
|
+
const n = Math.trunc(i);
|
|
1246
|
+
if (n < 0 || n >= e.length)
|
|
1247
|
+
throw new a(`List index out of bounds: ${n}`);
|
|
1248
|
+
return e[n];
|
|
1249
|
+
} else if (typeof e == "object") {
|
|
1250
|
+
if (!(i in e))
|
|
1251
|
+
throw new a(`Map key not found: ${i}`);
|
|
1252
|
+
return e[i];
|
|
1253
|
+
} else if (typeof e == "string") {
|
|
1254
|
+
if (typeof i != "number")
|
|
1255
|
+
throw new a("String index must be an integer");
|
|
1256
|
+
const n = Math.trunc(i);
|
|
1257
|
+
if (n < 0 || n >= e.length)
|
|
1258
|
+
throw new a(`String index out of bounds: ${n}`);
|
|
1259
|
+
return e[n];
|
|
1260
|
+
}
|
|
1261
|
+
throw new a(`Cannot index type: ${typeof e}`);
|
|
1262
|
+
}
|
|
1263
|
+
}
|
|
1264
|
+
class V {
|
|
1265
|
+
/**
|
|
1266
|
+
* Creates a new compiled program.
|
|
1267
|
+
*
|
|
1268
|
+
* This constructor is typically called by CEL.compile() and should not be used directly.
|
|
1269
|
+
*
|
|
1270
|
+
* @param ast The abstract syntax tree of the compiled expression
|
|
1271
|
+
* @param functions The function library to use for evaluation
|
|
1272
|
+
*/
|
|
1273
|
+
constructor(t, e) {
|
|
1274
|
+
this.ast = t, this.functions = e;
|
|
1275
|
+
}
|
|
1276
|
+
/**
|
|
1277
|
+
* Evaluates the compiled program with the given variables.
|
|
1278
|
+
*
|
|
1279
|
+
* @param variables A map of variable names to their values
|
|
1280
|
+
* @returns The result of evaluating the expression
|
|
1281
|
+
* @throws EvaluationError if an error occurs during evaluation
|
|
1282
|
+
*
|
|
1283
|
+
* @example
|
|
1284
|
+
* ```typescript
|
|
1285
|
+
* const result = program.evaluate({
|
|
1286
|
+
* x: 10,
|
|
1287
|
+
* y: 20,
|
|
1288
|
+
* items: [1, 2, 3]
|
|
1289
|
+
* });
|
|
1290
|
+
* ```
|
|
1291
|
+
*/
|
|
1292
|
+
evaluate(t = {}) {
|
|
1293
|
+
return new H({ ...t }, this.functions).evaluate(this.ast);
|
|
1294
|
+
}
|
|
1295
|
+
}
|
|
1296
|
+
class R {
|
|
1297
|
+
constructor(t) {
|
|
1298
|
+
this.functions = t ?? new k();
|
|
1299
|
+
}
|
|
1300
|
+
/**
|
|
1301
|
+
* Compiles a CEL expression using the provided function library.
|
|
1302
|
+
*
|
|
1303
|
+
* This static convenience method avoids the need to instantiate CEL when
|
|
1304
|
+
* callers just need to compile once.
|
|
1305
|
+
*
|
|
1306
|
+
* @param expression The CEL expression to compile
|
|
1307
|
+
* @param functions The function library to use when compiling the program
|
|
1308
|
+
* @returns A compiled Program using the provided functions
|
|
1309
|
+
* @throws ParseError if the expression is invalid
|
|
1310
|
+
*
|
|
1311
|
+
* @example
|
|
1312
|
+
* ```typescript
|
|
1313
|
+
* const program = CEL.compile('x + y', new StandardFunctions());
|
|
1314
|
+
* ```
|
|
1315
|
+
*/
|
|
1316
|
+
static compile(t, e) {
|
|
1317
|
+
const i = new Y(t);
|
|
1318
|
+
return new V(i.parse(), e);
|
|
1319
|
+
}
|
|
1320
|
+
/**
|
|
1321
|
+
* Evaluates a CEL expression with the given variables using the provided function library.
|
|
1322
|
+
*
|
|
1323
|
+
* This static convenience method avoids the need to instantiate CEL when callers
|
|
1324
|
+
* just need to evaluate an expression once.
|
|
1325
|
+
*
|
|
1326
|
+
* @param expression The CEL expression to evaluate
|
|
1327
|
+
* @param functions The function library to use when evaluating the expression
|
|
1328
|
+
* @param variables A map of variable names to their values
|
|
1329
|
+
* @returns The result of evaluating the expression
|
|
1330
|
+
* @throws ParseError if the expression is invalid
|
|
1331
|
+
* @throws EvaluationError if an error occurs during evaluation
|
|
1332
|
+
*
|
|
1333
|
+
* @example
|
|
1334
|
+
* ```typescript
|
|
1335
|
+
* const result = CEL.eval('x * 2', new StandardFunctions(), { x: 5 });
|
|
1336
|
+
* ```
|
|
1337
|
+
*/
|
|
1338
|
+
static eval(t, e, i) {
|
|
1339
|
+
return this.compile(t, e).evaluate(i);
|
|
1340
|
+
}
|
|
1341
|
+
/**
|
|
1342
|
+
* Compiles a CEL expression into a reusable program.
|
|
1343
|
+
*
|
|
1344
|
+
* This method parses the expression and returns a Program that can be evaluated
|
|
1345
|
+
* multiple times with different variables. This is more efficient than calling
|
|
1346
|
+
* eval() repeatedly with the same expression.
|
|
1347
|
+
*
|
|
1348
|
+
* @param expression The CEL expression to compile
|
|
1349
|
+
* @returns A compiled program
|
|
1350
|
+
* @throws ParseError if the expression is invalid
|
|
1351
|
+
*
|
|
1352
|
+
* @example
|
|
1353
|
+
* ```typescript
|
|
1354
|
+
* const program = cel.compile('price * quantity');
|
|
1355
|
+
* const result1 = program.evaluate({ price: 10, quantity: 5 });
|
|
1356
|
+
* const result2 = program.evaluate({ price: 20, quantity: 3 });
|
|
1357
|
+
* ```
|
|
1358
|
+
*/
|
|
1359
|
+
compile(t) {
|
|
1360
|
+
return R.compile(t, this.functions);
|
|
1361
|
+
}
|
|
1362
|
+
/**
|
|
1363
|
+
* Evaluates a CEL expression with the given variables.
|
|
1364
|
+
*
|
|
1365
|
+
* This is a convenience method that compiles and evaluates the expression in one step.
|
|
1366
|
+
* For better performance when evaluating the same expression multiple times, use
|
|
1367
|
+
* compile() to create a reusable Program.
|
|
1368
|
+
*
|
|
1369
|
+
* @param expression The CEL expression to evaluate
|
|
1370
|
+
* @param variables A map of variable names to their values
|
|
1371
|
+
* @returns The result of evaluating the expression
|
|
1372
|
+
* @throws ParseError if the expression is invalid
|
|
1373
|
+
* @throws EvaluationError if an error occurs during evaluation
|
|
1374
|
+
*
|
|
1375
|
+
* @example
|
|
1376
|
+
* ```typescript
|
|
1377
|
+
* const result = cel.eval('user.age >= 18', {
|
|
1378
|
+
* user: { name: 'Alice', age: 25 }
|
|
1379
|
+
* });
|
|
1380
|
+
* ```
|
|
1381
|
+
*/
|
|
1382
|
+
eval(t, e = {}) {
|
|
1383
|
+
return R.eval(t, this.functions, e);
|
|
1384
|
+
}
|
|
1385
|
+
}
|
|
1386
|
+
export {
|
|
1387
|
+
L as Binary,
|
|
1388
|
+
c as BinaryOp,
|
|
1389
|
+
R as CEL,
|
|
1390
|
+
y as Call,
|
|
1391
|
+
X as Comprehension,
|
|
1392
|
+
K as Conditional,
|
|
1393
|
+
a as EvaluationError,
|
|
1394
|
+
j as FieldInitializer,
|
|
1395
|
+
U as Identifier,
|
|
1396
|
+
Q as Index,
|
|
1397
|
+
H as Interpreter,
|
|
1398
|
+
q as ListExpression,
|
|
1399
|
+
d as Literal,
|
|
1400
|
+
w as LiteralType,
|
|
1401
|
+
W as MapEntry,
|
|
1402
|
+
C as MapExpression,
|
|
1403
|
+
f as ParseError,
|
|
1404
|
+
Y as Parser,
|
|
1405
|
+
V as Program,
|
|
1406
|
+
N as Select,
|
|
1407
|
+
k as StandardFunctions,
|
|
1408
|
+
S as Struct,
|
|
1409
|
+
O as Unary,
|
|
1410
|
+
g as UnaryOp,
|
|
1411
|
+
Z as Utilities,
|
|
1412
|
+
T as asBool
|
|
1413
|
+
};
|
|
1414
|
+
//# sourceMappingURL=index.mjs.map
|