@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.d.ts
ADDED
|
@@ -0,0 +1,740 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Converts the given value to a boolean using common truthiness rules.
|
|
3
|
+
*
|
|
4
|
+
* Numbers are true if non-zero. Strings are true if non-empty.
|
|
5
|
+
* Collections/maps are true if non-empty. Null is false.
|
|
6
|
+
*
|
|
7
|
+
* @param value The value to interpret
|
|
8
|
+
* @returns The boolean interpretation
|
|
9
|
+
*/
|
|
10
|
+
export declare function asBool(value: any): boolean;
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Converts the given value to a double (number).
|
|
14
|
+
*
|
|
15
|
+
* Accepted inputs: number, string parsable as number
|
|
16
|
+
*
|
|
17
|
+
* @param value The value to convert
|
|
18
|
+
* @returns The converted number value
|
|
19
|
+
* @throws Error if the value cannot be converted
|
|
20
|
+
*/
|
|
21
|
+
declare function asDouble(value: any): number;
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Converts the given value to a signed integer (number).
|
|
25
|
+
*
|
|
26
|
+
* Accepted inputs: number, string (parsed as integer), boolean (true=1, false=0)
|
|
27
|
+
*
|
|
28
|
+
* @param value The value to convert
|
|
29
|
+
* @returns The converted integer value
|
|
30
|
+
* @throws Error if the value cannot be converted
|
|
31
|
+
*/
|
|
32
|
+
declare function asInt(value: any): number;
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Converts the given value to its string representation.
|
|
36
|
+
*
|
|
37
|
+
* Returns the literal string "null" for null/undefined values.
|
|
38
|
+
*
|
|
39
|
+
* @param value The value to stringify
|
|
40
|
+
* @returns The string representation
|
|
41
|
+
*/
|
|
42
|
+
declare function asString(value: any): string;
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Converts the given value to an unsigned integer.
|
|
46
|
+
*
|
|
47
|
+
* Negative inputs are not allowed and will result in an exception.
|
|
48
|
+
*
|
|
49
|
+
* @param value The value to convert
|
|
50
|
+
* @returns The non-negative integer value
|
|
51
|
+
* @throws Error if the value is negative or cannot be converted
|
|
52
|
+
*/
|
|
53
|
+
declare function asUInt(value: any): number;
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Binary operation expression (+, -, *, /, etc.).
|
|
57
|
+
*/
|
|
58
|
+
export declare class Binary implements Expression {
|
|
59
|
+
readonly op: BinaryOp;
|
|
60
|
+
readonly left: Expression;
|
|
61
|
+
readonly right: Expression;
|
|
62
|
+
constructor(op: BinaryOp, left: Expression, right: Expression);
|
|
63
|
+
accept<T>(visitor: Visitor<T>): T;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Binary operators in CEL expressions.
|
|
68
|
+
*/
|
|
69
|
+
export declare enum BinaryOp {
|
|
70
|
+
/** Addition (+) or string/list concatenation */
|
|
71
|
+
ADD = "ADD",
|
|
72
|
+
/** Subtraction (-) */
|
|
73
|
+
SUBTRACT = "SUBTRACT",
|
|
74
|
+
/** Multiplication (*) or string/list repetition */
|
|
75
|
+
MULTIPLY = "MULTIPLY",
|
|
76
|
+
/** Division (/) */
|
|
77
|
+
DIVIDE = "DIVIDE",
|
|
78
|
+
/** Modulo (%) */
|
|
79
|
+
MODULO = "MODULO",
|
|
80
|
+
/** Equality (==) */
|
|
81
|
+
EQUAL = "EQUAL",
|
|
82
|
+
/** Inequality (!=) */
|
|
83
|
+
NOT_EQUAL = "NOT_EQUAL",
|
|
84
|
+
/** Less than (<) */
|
|
85
|
+
LESS = "LESS",
|
|
86
|
+
/** Less than or equal (<=) */
|
|
87
|
+
LESS_EQUAL = "LESS_EQUAL",
|
|
88
|
+
/** Greater than (>) */
|
|
89
|
+
GREATER = "GREATER",
|
|
90
|
+
/** Greater than or equal (>=) */
|
|
91
|
+
GREATER_EQUAL = "GREATER_EQUAL",
|
|
92
|
+
/** Membership test (in) */
|
|
93
|
+
IN = "IN",
|
|
94
|
+
/** Logical AND (&&) */
|
|
95
|
+
LOGICAL_AND = "LOGICAL_AND",
|
|
96
|
+
/** Logical OR (||) */
|
|
97
|
+
LOGICAL_OR = "LOGICAL_OR"
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Function or method call expression.
|
|
102
|
+
*/
|
|
103
|
+
export declare class Call implements Expression {
|
|
104
|
+
readonly target: Expression | null;
|
|
105
|
+
readonly functionName: string;
|
|
106
|
+
readonly args: Expression[];
|
|
107
|
+
readonly isMacro: boolean;
|
|
108
|
+
constructor(target: Expression | null, functionName: string, args: Expression[], isMacro?: boolean);
|
|
109
|
+
accept<T>(visitor: Visitor<T>): T;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* The main entry point for evaluating CEL expressions.
|
|
114
|
+
*
|
|
115
|
+
* The CEL class provides methods to compile and evaluate CEL expressions.
|
|
116
|
+
* It supports all standard CEL operators, functions, and macros.
|
|
117
|
+
*
|
|
118
|
+
* @example
|
|
119
|
+
* ```typescript
|
|
120
|
+
* const cel = new CEL();
|
|
121
|
+
* const result = cel.eval('x * 2 + y', { x: 10, y: 5 });
|
|
122
|
+
* console.log(result); // 25
|
|
123
|
+
* ```
|
|
124
|
+
*/
|
|
125
|
+
export declare class CEL {
|
|
126
|
+
private readonly functions;
|
|
127
|
+
/**
|
|
128
|
+
* Creates a new CEL evaluator with the standard function library.
|
|
129
|
+
*/
|
|
130
|
+
constructor();
|
|
131
|
+
/**
|
|
132
|
+
* Creates a new CEL evaluator.
|
|
133
|
+
*
|
|
134
|
+
* @param functions Optional custom function library. If not provided, the standard
|
|
135
|
+
* CEL function library will be used.
|
|
136
|
+
*/
|
|
137
|
+
constructor(functions?: Functions | null);
|
|
138
|
+
/**
|
|
139
|
+
* Compiles a CEL expression using the provided function library.
|
|
140
|
+
*
|
|
141
|
+
* This static convenience method avoids the need to instantiate CEL when
|
|
142
|
+
* callers just need to compile once.
|
|
143
|
+
*
|
|
144
|
+
* @param expression The CEL expression to compile
|
|
145
|
+
* @param functions The function library to use when compiling the program
|
|
146
|
+
* @returns A compiled Program using the provided functions
|
|
147
|
+
* @throws ParseError if the expression is invalid
|
|
148
|
+
*
|
|
149
|
+
* @example
|
|
150
|
+
* ```typescript
|
|
151
|
+
* const program = CEL.compile('x + y', new StandardFunctions());
|
|
152
|
+
* ```
|
|
153
|
+
*/
|
|
154
|
+
static compile(expression: string, functions: Functions): Program;
|
|
155
|
+
/**
|
|
156
|
+
* Evaluates a CEL expression with the given variables using the provided function library.
|
|
157
|
+
*
|
|
158
|
+
* This static convenience method avoids the need to instantiate CEL when callers
|
|
159
|
+
* just need to evaluate an expression once.
|
|
160
|
+
*
|
|
161
|
+
* @param expression The CEL expression to evaluate
|
|
162
|
+
* @param functions The function library to use when evaluating the expression
|
|
163
|
+
* @param variables A map of variable names to their values
|
|
164
|
+
* @returns The result of evaluating the expression
|
|
165
|
+
* @throws ParseError if the expression is invalid
|
|
166
|
+
* @throws EvaluationError if an error occurs during evaluation
|
|
167
|
+
*
|
|
168
|
+
* @example
|
|
169
|
+
* ```typescript
|
|
170
|
+
* const result = CEL.eval('x * 2', new StandardFunctions(), { x: 5 });
|
|
171
|
+
* ```
|
|
172
|
+
*/
|
|
173
|
+
static eval(expression: string, functions: Functions, variables: Record<string, any>): any;
|
|
174
|
+
/**
|
|
175
|
+
* Compiles a CEL expression into a reusable program.
|
|
176
|
+
*
|
|
177
|
+
* This method parses the expression and returns a Program that can be evaluated
|
|
178
|
+
* multiple times with different variables. This is more efficient than calling
|
|
179
|
+
* eval() repeatedly with the same expression.
|
|
180
|
+
*
|
|
181
|
+
* @param expression The CEL expression to compile
|
|
182
|
+
* @returns A compiled program
|
|
183
|
+
* @throws ParseError if the expression is invalid
|
|
184
|
+
*
|
|
185
|
+
* @example
|
|
186
|
+
* ```typescript
|
|
187
|
+
* const program = cel.compile('price * quantity');
|
|
188
|
+
* const result1 = program.evaluate({ price: 10, quantity: 5 });
|
|
189
|
+
* const result2 = program.evaluate({ price: 20, quantity: 3 });
|
|
190
|
+
* ```
|
|
191
|
+
*/
|
|
192
|
+
compile(expression: string): Program;
|
|
193
|
+
/**
|
|
194
|
+
* Evaluates a CEL expression with the given variables.
|
|
195
|
+
*
|
|
196
|
+
* This is a convenience method that compiles and evaluates the expression in one step.
|
|
197
|
+
* For better performance when evaluating the same expression multiple times, use
|
|
198
|
+
* compile() to create a reusable Program.
|
|
199
|
+
*
|
|
200
|
+
* @param expression The CEL expression to evaluate
|
|
201
|
+
* @param variables A map of variable names to their values
|
|
202
|
+
* @returns The result of evaluating the expression
|
|
203
|
+
* @throws ParseError if the expression is invalid
|
|
204
|
+
* @throws EvaluationError if an error occurs during evaluation
|
|
205
|
+
*
|
|
206
|
+
* @example
|
|
207
|
+
* ```typescript
|
|
208
|
+
* const result = cel.eval('user.age >= 18', {
|
|
209
|
+
* user: { name: 'Alice', age: 25 }
|
|
210
|
+
* });
|
|
211
|
+
* ```
|
|
212
|
+
*/
|
|
213
|
+
eval(expression: string, variables?: Record<string, any>): any;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* Compares two values using a common set of rules.
|
|
218
|
+
*
|
|
219
|
+
* Supported comparisons: numbers (by numeric value), strings (lexicographically),
|
|
220
|
+
* booleans, arrays (lexicographically).
|
|
221
|
+
*
|
|
222
|
+
* @param a The first value
|
|
223
|
+
* @param b The second value
|
|
224
|
+
* @returns A negative number, zero, or a positive number as a is less than, equal to, or greater than b
|
|
225
|
+
* @throws Error if the values cannot be compared
|
|
226
|
+
*/
|
|
227
|
+
declare function compare(a: any, b: any): number;
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* Comprehension expression for advanced iteration constructs.
|
|
231
|
+
*/
|
|
232
|
+
export declare class Comprehension implements Expression {
|
|
233
|
+
readonly variable: string;
|
|
234
|
+
readonly range: Expression;
|
|
235
|
+
readonly accumulator: string;
|
|
236
|
+
readonly initializer: Expression;
|
|
237
|
+
readonly condition: Expression;
|
|
238
|
+
readonly step: Expression;
|
|
239
|
+
readonly result: Expression;
|
|
240
|
+
constructor(variable: string, range: Expression, accumulator: string, initializer: Expression, condition: Expression, step: Expression, result: Expression);
|
|
241
|
+
accept<T>(visitor: Visitor<T>): T;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/**
|
|
245
|
+
* Conditional (ternary) expression (condition ? then : otherwise).
|
|
246
|
+
*/
|
|
247
|
+
export declare class Conditional implements Expression {
|
|
248
|
+
readonly condition: Expression;
|
|
249
|
+
readonly thenExpr: Expression;
|
|
250
|
+
readonly otherwiseExpr: Expression;
|
|
251
|
+
constructor(condition: Expression, thenExpr: Expression, otherwiseExpr: Expression);
|
|
252
|
+
accept<T>(visitor: Visitor<T>): T;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
/**
|
|
256
|
+
* Helper for array contains with deep equality.
|
|
257
|
+
*
|
|
258
|
+
* @param array The array to search
|
|
259
|
+
* @param value The value to find
|
|
260
|
+
* @returns true if the array contains the value (using deep equality)
|
|
261
|
+
*/
|
|
262
|
+
declare function containsInArray(array: any[], value: any): boolean;
|
|
263
|
+
|
|
264
|
+
/**
|
|
265
|
+
* Deep equality check for CEL values.
|
|
266
|
+
*
|
|
267
|
+
* @param left First value
|
|
268
|
+
* @param right Second value
|
|
269
|
+
* @returns true if values are deeply equal
|
|
270
|
+
*/
|
|
271
|
+
declare function deepEquals(left: any, right: any): boolean;
|
|
272
|
+
|
|
273
|
+
/**
|
|
274
|
+
* Exception thrown during CEL expression evaluation.
|
|
275
|
+
*/
|
|
276
|
+
export declare class EvaluationError extends Error {
|
|
277
|
+
constructor(message: string);
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
/**
|
|
281
|
+
* Base interface for all CEL expression nodes in the Abstract Syntax Tree.
|
|
282
|
+
* Uses the Visitor pattern to enable different operations on expressions.
|
|
283
|
+
*/
|
|
284
|
+
export declare interface Expression {
|
|
285
|
+
/**
|
|
286
|
+
* Accepts a visitor to perform operations on this expression node.
|
|
287
|
+
*
|
|
288
|
+
* @template T The return type of the visitor's visit operations
|
|
289
|
+
* @param visitor The visitor that will operate on this expression node
|
|
290
|
+
* @returns The result of the visitor's visit operation
|
|
291
|
+
*/
|
|
292
|
+
accept<T>(visitor: Visitor<T>): T;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
/**
|
|
296
|
+
* Field initializer in a struct literal.
|
|
297
|
+
*/
|
|
298
|
+
export declare class FieldInitializer {
|
|
299
|
+
readonly field: string;
|
|
300
|
+
readonly value: Expression;
|
|
301
|
+
constructor(field: string, value: Expression);
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
/**
|
|
305
|
+
* Interface for providing functions to CEL expressions.
|
|
306
|
+
*
|
|
307
|
+
* Implement this interface to provide custom functions that can be called
|
|
308
|
+
* from CEL expressions. The StandardFunctions class provides all standard
|
|
309
|
+
* CEL functions and can be extended for custom functionality.
|
|
310
|
+
*
|
|
311
|
+
* @example
|
|
312
|
+
* ```typescript
|
|
313
|
+
* class MyFunctions extends StandardFunctions {
|
|
314
|
+
* callFunction(name: string, args: any[]): any {
|
|
315
|
+
* if (name === 'customFunc') {
|
|
316
|
+
* return myCustomImplementation(args);
|
|
317
|
+
* }
|
|
318
|
+
* return super.callFunction(name, args);
|
|
319
|
+
* }
|
|
320
|
+
* }
|
|
321
|
+
* ```
|
|
322
|
+
*/
|
|
323
|
+
export declare interface Functions {
|
|
324
|
+
/**
|
|
325
|
+
* Calls a global function by name.
|
|
326
|
+
*
|
|
327
|
+
* @param name The name of the function to call
|
|
328
|
+
* @param args The arguments to pass to the function
|
|
329
|
+
* @returns The result of the function call
|
|
330
|
+
* @throws Error if the function is not found or if the arguments are invalid
|
|
331
|
+
*/
|
|
332
|
+
callFunction(name: string, args: any[]): any;
|
|
333
|
+
/**
|
|
334
|
+
* Calls a method on a target object.
|
|
335
|
+
*
|
|
336
|
+
* @param target The object to call the method on
|
|
337
|
+
* @param method The name of the method to call
|
|
338
|
+
* @param args The arguments to pass to the method
|
|
339
|
+
* @returns The result of the method call
|
|
340
|
+
* @throws Error if the method is not found or if the arguments are invalid
|
|
341
|
+
*/
|
|
342
|
+
callMethod(target: any, method: string, args: any[]): any;
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
/**
|
|
346
|
+
* Checks whether a map contains the given field name.
|
|
347
|
+
*
|
|
348
|
+
* @param target The map-like object to check (must be an object to return true/false)
|
|
349
|
+
* @param field The field/key to look for (must be a string)
|
|
350
|
+
* @returns true if target is a Map/object and contains the given key; false otherwise
|
|
351
|
+
*/
|
|
352
|
+
declare function has(target: any, field: any): boolean;
|
|
353
|
+
|
|
354
|
+
/**
|
|
355
|
+
* Identifier expression (variable reference).
|
|
356
|
+
*/
|
|
357
|
+
export declare class Identifier implements Expression {
|
|
358
|
+
readonly name: string;
|
|
359
|
+
constructor(name: string);
|
|
360
|
+
accept<T>(visitor: Visitor<T>): T;
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
/**
|
|
364
|
+
* Index expression for array/map access (operand[index]).
|
|
365
|
+
*/
|
|
366
|
+
export declare class Index implements Expression {
|
|
367
|
+
readonly operand: Expression;
|
|
368
|
+
readonly index: Expression;
|
|
369
|
+
constructor(operand: Expression, index: Expression);
|
|
370
|
+
accept<T>(visitor: Visitor<T>): T;
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
/**
|
|
374
|
+
* Interpreter for evaluating CEL expressions.
|
|
375
|
+
*
|
|
376
|
+
* Implements the Visitor pattern to traverse and evaluate the AST produced by the parser.
|
|
377
|
+
* Supports all CEL operations including macros, type conversions, and complex expressions.
|
|
378
|
+
*/
|
|
379
|
+
export declare class Interpreter implements Visitor<any> {
|
|
380
|
+
private readonly variables;
|
|
381
|
+
private readonly functions;
|
|
382
|
+
/**
|
|
383
|
+
* Constructs an interpreter with the specified variables and functions.
|
|
384
|
+
*
|
|
385
|
+
* @param variables A map of variable names to their values. If null, an empty object is used.
|
|
386
|
+
* @param functions An instance of Functions to handle function calls. If null, StandardFunctions is used.
|
|
387
|
+
*/
|
|
388
|
+
constructor(variables?: Record<string, any> | null, functions?: Functions | null);
|
|
389
|
+
/**
|
|
390
|
+
* Evaluates a CEL expression and returns its result.
|
|
391
|
+
*
|
|
392
|
+
* @param expr The expression to evaluate
|
|
393
|
+
* @returns The result of the evaluation
|
|
394
|
+
* @throws EvaluationError if evaluation fails
|
|
395
|
+
*/
|
|
396
|
+
evaluate(expr: Expression): any;
|
|
397
|
+
visitLiteral(expr: Literal): any;
|
|
398
|
+
visitIdentifier(expr: Identifier): any;
|
|
399
|
+
visitSelect(expr: Select): any;
|
|
400
|
+
visitCall(expr: Call): any;
|
|
401
|
+
private evaluateMacro;
|
|
402
|
+
visitList(expr: ListExpression): any;
|
|
403
|
+
visitMap(expr: MapExpression): any;
|
|
404
|
+
visitStruct(expr: Struct): any;
|
|
405
|
+
visitComprehension(expr: Comprehension): any;
|
|
406
|
+
visitUnary(expr: Unary): any;
|
|
407
|
+
visitBinary(expr: Binary): any;
|
|
408
|
+
visitConditional(expr: Conditional): any;
|
|
409
|
+
visitIndex(expr: Index): any;
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
/**
|
|
413
|
+
* List literal expression ([1, 2, 3]).
|
|
414
|
+
*/
|
|
415
|
+
export declare class ListExpression implements Expression {
|
|
416
|
+
readonly elements: Expression[];
|
|
417
|
+
constructor(elements: Expression[]);
|
|
418
|
+
accept<T>(visitor: Visitor<T>): T;
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
/**
|
|
422
|
+
* Literal value expression (null, boolean, number, string, bytes).
|
|
423
|
+
*/
|
|
424
|
+
export declare class Literal implements Expression {
|
|
425
|
+
readonly value: any;
|
|
426
|
+
readonly literalType: LiteralType;
|
|
427
|
+
constructor(value: any, literalType: LiteralType);
|
|
428
|
+
accept<T>(visitor: Visitor<T>): T;
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
/**
|
|
432
|
+
* Types of literal values in CEL.
|
|
433
|
+
*/
|
|
434
|
+
export declare enum LiteralType {
|
|
435
|
+
NULL_VALUE = "NULL_VALUE",
|
|
436
|
+
BOOL = "BOOL",
|
|
437
|
+
INT = "INT",
|
|
438
|
+
UINT = "UINT",
|
|
439
|
+
DOUBLE = "DOUBLE",
|
|
440
|
+
STRING = "STRING",
|
|
441
|
+
BYTES = "BYTES"
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
/**
|
|
445
|
+
* Map entry (key-value pair in a map literal).
|
|
446
|
+
*/
|
|
447
|
+
export declare class MapEntry {
|
|
448
|
+
readonly key: Expression;
|
|
449
|
+
readonly value: Expression;
|
|
450
|
+
constructor(key: Expression, value: Expression);
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
/**
|
|
454
|
+
* Map literal expression ({key: value}).
|
|
455
|
+
*/
|
|
456
|
+
export declare class MapExpression implements Expression {
|
|
457
|
+
readonly entries: MapEntry[];
|
|
458
|
+
constructor(entries: MapEntry[]);
|
|
459
|
+
accept<T>(visitor: Visitor<T>): T;
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
/**
|
|
463
|
+
* Tests whether the given regular expression matches any part of the text.
|
|
464
|
+
*
|
|
465
|
+
* Uses RegExp pattern matching with find semantics.
|
|
466
|
+
*
|
|
467
|
+
* @param text The input text
|
|
468
|
+
* @param pattern The regular expression pattern
|
|
469
|
+
* @returns true if the pattern matches anywhere in the text; false otherwise
|
|
470
|
+
* @throws Error if the pattern is invalid
|
|
471
|
+
*/
|
|
472
|
+
declare function matches(text: string, pattern: string): boolean;
|
|
473
|
+
|
|
474
|
+
/**
|
|
475
|
+
* Returns the maximum element from a non-empty array of values.
|
|
476
|
+
*
|
|
477
|
+
* Comparison rules follow compare(). All elements must be mutually comparable.
|
|
478
|
+
*
|
|
479
|
+
* @param values A non-empty array of values
|
|
480
|
+
* @returns The maximum value in the array
|
|
481
|
+
* @throws Error if the array is empty or values are not comparable
|
|
482
|
+
*/
|
|
483
|
+
declare function max(values: any[]): any;
|
|
484
|
+
|
|
485
|
+
/**
|
|
486
|
+
* Returns the minimum element from a non-empty array of values.
|
|
487
|
+
*
|
|
488
|
+
* Comparison rules follow compare(). All elements must be mutually comparable.
|
|
489
|
+
*
|
|
490
|
+
* @param values A non-empty array of values
|
|
491
|
+
* @returns The minimum value in the array
|
|
492
|
+
* @throws Error if the array is empty or values are not comparable
|
|
493
|
+
*/
|
|
494
|
+
declare function min(values: any[]): any;
|
|
495
|
+
|
|
496
|
+
/**
|
|
497
|
+
* ParseError is thrown when the lexer or parser encounters invalid syntax.
|
|
498
|
+
*/
|
|
499
|
+
export declare class ParseError extends Error {
|
|
500
|
+
readonly line: number;
|
|
501
|
+
readonly column: number;
|
|
502
|
+
constructor(message: string, line: number, column: number);
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
/**
|
|
506
|
+
* Recursive descent parser for CEL (Common Expression Language).
|
|
507
|
+
* Parses CEL expressions into an Abstract Syntax Tree (AST).
|
|
508
|
+
*/
|
|
509
|
+
export declare class Parser {
|
|
510
|
+
private readonly lexer;
|
|
511
|
+
private current;
|
|
512
|
+
constructor(input: string);
|
|
513
|
+
/**
|
|
514
|
+
* Parse a CEL expression from the input string.
|
|
515
|
+
* @returns The parsed Expression AST node
|
|
516
|
+
* @throws ParseError if the input contains syntax errors
|
|
517
|
+
*/
|
|
518
|
+
parse(): Expression;
|
|
519
|
+
private parseExpr;
|
|
520
|
+
private parseConditionalOr;
|
|
521
|
+
private parseConditionalAnd;
|
|
522
|
+
private parseRelation;
|
|
523
|
+
private parseAddition;
|
|
524
|
+
private parseMultiplication;
|
|
525
|
+
private parseUnary;
|
|
526
|
+
private parseMember;
|
|
527
|
+
private parsePrimary;
|
|
528
|
+
private parseListLiteral;
|
|
529
|
+
private parseMapOrStructLiteral;
|
|
530
|
+
private parseExprList;
|
|
531
|
+
private parseMapInits;
|
|
532
|
+
private parseMapInit;
|
|
533
|
+
private parseFieldInits;
|
|
534
|
+
private parseFieldInit;
|
|
535
|
+
private parseQualifiedIdent;
|
|
536
|
+
private parseLiteral;
|
|
537
|
+
private parseIntLiteral;
|
|
538
|
+
private parseUintLiteral;
|
|
539
|
+
private parseStringLiteral;
|
|
540
|
+
private parseBytesLiteral;
|
|
541
|
+
private unescapeString;
|
|
542
|
+
private isLiteralToken;
|
|
543
|
+
private isRelationalOp;
|
|
544
|
+
private toBinaryOp;
|
|
545
|
+
private match;
|
|
546
|
+
private expect;
|
|
547
|
+
private expectIdentifier;
|
|
548
|
+
private advance;
|
|
549
|
+
private peekAhead;
|
|
550
|
+
private isQualifiedStructLiteral;
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
/**
|
|
554
|
+
* A compiled CEL program that can be evaluated multiple times.
|
|
555
|
+
*
|
|
556
|
+
* A Program represents a parsed CEL expression that can be efficiently evaluated
|
|
557
|
+
* with different sets of variables. This is more efficient than parsing the
|
|
558
|
+
* expression each time it needs to be evaluated.
|
|
559
|
+
*
|
|
560
|
+
* Programs are created using CEL.compile() and should be reused when the same
|
|
561
|
+
* expression needs to be evaluated multiple times.
|
|
562
|
+
*
|
|
563
|
+
* @example
|
|
564
|
+
* ```typescript
|
|
565
|
+
* const program = cel.compile('price * quantity');
|
|
566
|
+
* const result1 = program.evaluate({ price: 10, quantity: 5 });
|
|
567
|
+
* const result2 = program.evaluate({ price: 20, quantity: 3 });
|
|
568
|
+
* ```
|
|
569
|
+
*/
|
|
570
|
+
export declare class Program {
|
|
571
|
+
private readonly ast;
|
|
572
|
+
private readonly functions;
|
|
573
|
+
/**
|
|
574
|
+
* Creates a new compiled program.
|
|
575
|
+
*
|
|
576
|
+
* This constructor is typically called by CEL.compile() and should not be used directly.
|
|
577
|
+
*
|
|
578
|
+
* @param ast The abstract syntax tree of the compiled expression
|
|
579
|
+
* @param functions The function library to use for evaluation
|
|
580
|
+
*/
|
|
581
|
+
constructor(ast: Expression, functions: Functions);
|
|
582
|
+
/**
|
|
583
|
+
* Evaluates the compiled program with the given variables.
|
|
584
|
+
*
|
|
585
|
+
* @param variables A map of variable names to their values
|
|
586
|
+
* @returns The result of evaluating the expression
|
|
587
|
+
* @throws EvaluationError if an error occurs during evaluation
|
|
588
|
+
*
|
|
589
|
+
* @example
|
|
590
|
+
* ```typescript
|
|
591
|
+
* const result = program.evaluate({
|
|
592
|
+
* x: 10,
|
|
593
|
+
* y: 20,
|
|
594
|
+
* items: [1, 2, 3]
|
|
595
|
+
* });
|
|
596
|
+
* ```
|
|
597
|
+
*/
|
|
598
|
+
evaluate(variables?: Record<string, any>): any;
|
|
599
|
+
}
|
|
600
|
+
|
|
601
|
+
/**
|
|
602
|
+
* Field selection expression (operand.field).
|
|
603
|
+
*/
|
|
604
|
+
export declare class Select implements Expression {
|
|
605
|
+
readonly operand: Expression | null;
|
|
606
|
+
readonly field: string;
|
|
607
|
+
readonly isTest: boolean;
|
|
608
|
+
constructor(operand: Expression | null, field: string, isTest?: boolean);
|
|
609
|
+
accept<T>(visitor: Visitor<T>): T;
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
/**
|
|
613
|
+
* Utility helper methods for libcel.
|
|
614
|
+
* Methods are exported so library users can call them directly.
|
|
615
|
+
*/
|
|
616
|
+
/**
|
|
617
|
+
* Returns the size/length of the given value.
|
|
618
|
+
*
|
|
619
|
+
* Supported types:
|
|
620
|
+
* - String: number of characters
|
|
621
|
+
* - Array: number of elements
|
|
622
|
+
* - Map/Record: number of entries
|
|
623
|
+
* - null: 0
|
|
624
|
+
*
|
|
625
|
+
* @param value The value whose size should be computed; may be null
|
|
626
|
+
* @returns The size for supported types, or 0 for null
|
|
627
|
+
* @throws Error if the value type is unsupported
|
|
628
|
+
*/
|
|
629
|
+
declare function sizeOf(value: any): number;
|
|
630
|
+
|
|
631
|
+
/**
|
|
632
|
+
* Standard CEL function library implementation.
|
|
633
|
+
*
|
|
634
|
+
* Provides all built-in CEL functions including:
|
|
635
|
+
* - Type conversions: int(), double(), string(), bool()
|
|
636
|
+
* - Type checking: type()
|
|
637
|
+
* - Collection operations: size(), has()
|
|
638
|
+
* - String operations: contains(), startsWith(), endsWith(), matches()
|
|
639
|
+
* - Math operations: max(), min()
|
|
640
|
+
*
|
|
641
|
+
* This class can be extended to add custom functions while retaining
|
|
642
|
+
* all standard CEL functionality.
|
|
643
|
+
*/
|
|
644
|
+
export declare class StandardFunctions implements Functions {
|
|
645
|
+
callFunction(name: string, args: any[]): any;
|
|
646
|
+
callMethod(target: any, method: string, args: any[]): any;
|
|
647
|
+
/**
|
|
648
|
+
* Attempts to call a native JavaScript method on the target object.
|
|
649
|
+
*
|
|
650
|
+
* @param target The object to call the method on
|
|
651
|
+
* @param name The method name
|
|
652
|
+
* @param args The arguments
|
|
653
|
+
* @returns The result of the method call
|
|
654
|
+
* @throws Error if the method doesn't exist or call fails
|
|
655
|
+
*/
|
|
656
|
+
private callNativeMethod;
|
|
657
|
+
}
|
|
658
|
+
|
|
659
|
+
/**
|
|
660
|
+
* Struct literal expression (Type{field: value}).
|
|
661
|
+
*/
|
|
662
|
+
export declare class Struct implements Expression {
|
|
663
|
+
readonly typeName: string | null;
|
|
664
|
+
readonly fields: FieldInitializer[];
|
|
665
|
+
constructor(typeName: string | null, fields: FieldInitializer[]);
|
|
666
|
+
accept<T>(visitor: Visitor<T>): T;
|
|
667
|
+
}
|
|
668
|
+
|
|
669
|
+
/**
|
|
670
|
+
* Returns a simple type name for the given value.
|
|
671
|
+
*
|
|
672
|
+
* Possible results: "null", "bool", "int", "double", "string", "list", "map", or "unknown"
|
|
673
|
+
*
|
|
674
|
+
* @param value The value whose type is to be described
|
|
675
|
+
* @returns The simple type name
|
|
676
|
+
*/
|
|
677
|
+
declare function typeOf(value: any): string;
|
|
678
|
+
|
|
679
|
+
/**
|
|
680
|
+
* Unary operation expression (!, -).
|
|
681
|
+
*/
|
|
682
|
+
export declare class Unary implements Expression {
|
|
683
|
+
readonly op: UnaryOp;
|
|
684
|
+
readonly operand: Expression;
|
|
685
|
+
constructor(op: UnaryOp, operand: Expression);
|
|
686
|
+
accept<T>(visitor: Visitor<T>): T;
|
|
687
|
+
}
|
|
688
|
+
|
|
689
|
+
/**
|
|
690
|
+
* Unary operators in CEL expressions.
|
|
691
|
+
*/
|
|
692
|
+
export declare enum UnaryOp {
|
|
693
|
+
/** Logical NOT (!) */
|
|
694
|
+
NOT = "NOT",
|
|
695
|
+
/** Numeric negation (-) */
|
|
696
|
+
NEGATE = "NEGATE"
|
|
697
|
+
}
|
|
698
|
+
|
|
699
|
+
declare namespace Utilities {
|
|
700
|
+
export {
|
|
701
|
+
sizeOf,
|
|
702
|
+
asInt,
|
|
703
|
+
asUInt,
|
|
704
|
+
asDouble,
|
|
705
|
+
asString,
|
|
706
|
+
asBool,
|
|
707
|
+
typeOf,
|
|
708
|
+
has,
|
|
709
|
+
matches,
|
|
710
|
+
max,
|
|
711
|
+
min,
|
|
712
|
+
compare,
|
|
713
|
+
deepEquals,
|
|
714
|
+
containsInArray
|
|
715
|
+
}
|
|
716
|
+
}
|
|
717
|
+
export { Utilities }
|
|
718
|
+
|
|
719
|
+
/**
|
|
720
|
+
* Visitor interface for traversing and operating on CEL expression nodes.
|
|
721
|
+
* Implements the Visitor pattern for AST traversal.
|
|
722
|
+
*
|
|
723
|
+
* @template T The return type of visit operations
|
|
724
|
+
*/
|
|
725
|
+
export declare interface Visitor<T> {
|
|
726
|
+
visitLiteral(expr: Literal): T;
|
|
727
|
+
visitIdentifier(expr: Identifier): T;
|
|
728
|
+
visitSelect(expr: Select): T;
|
|
729
|
+
visitIndex(expr: Index): T;
|
|
730
|
+
visitCall(expr: Call): T;
|
|
731
|
+
visitList(expr: ListExpression): T;
|
|
732
|
+
visitMap(expr: MapExpression): T;
|
|
733
|
+
visitStruct(expr: Struct): T;
|
|
734
|
+
visitComprehension(expr: Comprehension): T;
|
|
735
|
+
visitUnary(expr: Unary): T;
|
|
736
|
+
visitBinary(expr: Binary): T;
|
|
737
|
+
visitConditional(expr: Conditional): T;
|
|
738
|
+
}
|
|
739
|
+
|
|
740
|
+
export { }
|