cel-proto-parser 0.2.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.
@@ -0,0 +1,242 @@
1
+ "use strict";
2
+ /**
3
+ * CEL AST Converter
4
+ *
5
+ * Converts AST from @marcbachmann/cel-js format to CEL proto format
6
+ * used by our deparser.
7
+ */
8
+ Object.defineProperty(exports, "__esModule", { value: true });
9
+ exports.convertToProtoExpr = convertToProtoExpr;
10
+ /**
11
+ * Convert @marcbachmann/cel-js AST to CEL proto Expr format
12
+ */
13
+ function convertToProtoExpr(node, idCounter = { value: 1 }) {
14
+ const id = BigInt(idCounter.value++);
15
+ switch (node.op) {
16
+ case 'value':
17
+ return {
18
+ id,
19
+ constExpr: convertValue(node.args)
20
+ };
21
+ case 'id':
22
+ return {
23
+ id,
24
+ identExpr: { name: node.args }
25
+ };
26
+ case '.': {
27
+ const args = node.args;
28
+ return {
29
+ id,
30
+ selectExpr: {
31
+ operand: convertToProtoExpr(args[0], idCounter),
32
+ field: args[1]
33
+ }
34
+ };
35
+ }
36
+ case 'call': {
37
+ const args = node.args;
38
+ const funcName = args[0];
39
+ const funcArgs = args[1];
40
+ // Special handling for has() macro
41
+ if (funcName === 'has' && funcArgs.length === 1) {
42
+ const selectNode = funcArgs[0];
43
+ if (selectNode.op === '.') {
44
+ const selectArgs = selectNode.args;
45
+ return {
46
+ id,
47
+ selectExpr: {
48
+ operand: convertToProtoExpr(selectArgs[0], idCounter),
49
+ field: selectArgs[1],
50
+ testOnly: true
51
+ }
52
+ };
53
+ }
54
+ }
55
+ return {
56
+ id,
57
+ callExpr: {
58
+ function: funcName,
59
+ args: funcArgs.map((a) => convertToProtoExpr(a, idCounter))
60
+ }
61
+ };
62
+ }
63
+ case 'rcall': {
64
+ // Receiver call: target.method(args)
65
+ const args = node.args;
66
+ const funcName = args[0];
67
+ const target = args[1];
68
+ const funcArgs = args[2];
69
+ return {
70
+ id,
71
+ callExpr: {
72
+ target: convertToProtoExpr(target, idCounter),
73
+ function: funcName,
74
+ args: funcArgs.map((a) => convertToProtoExpr(a, idCounter))
75
+ }
76
+ };
77
+ }
78
+ case 'list': {
79
+ const elements = node.args;
80
+ return {
81
+ id,
82
+ listExpr: {
83
+ elements: elements.map((e) => convertToProtoExpr(e, idCounter))
84
+ }
85
+ };
86
+ }
87
+ case 'map': {
88
+ const entries = node.args;
89
+ return {
90
+ id,
91
+ structExpr: {
92
+ entries: entries.map(([key, value]) => ({
93
+ id: BigInt(idCounter.value++),
94
+ mapKey: convertToProtoExpr(key, idCounter),
95
+ value: convertToProtoExpr(value, idCounter)
96
+ }))
97
+ }
98
+ };
99
+ }
100
+ case 'struct': {
101
+ const args = node.args;
102
+ const messageName = args[0];
103
+ const fields = args[1];
104
+ return {
105
+ id,
106
+ structExpr: {
107
+ messageName,
108
+ entries: fields.map(([fieldKey, value]) => ({
109
+ id: BigInt(idCounter.value++),
110
+ fieldKey,
111
+ value: convertToProtoExpr(value, idCounter)
112
+ }))
113
+ }
114
+ };
115
+ }
116
+ case '?:': {
117
+ // Ternary conditional
118
+ const args = node.args;
119
+ return {
120
+ id,
121
+ callExpr: {
122
+ function: '_?_:_',
123
+ args: args.map((a) => convertToProtoExpr(a, idCounter))
124
+ }
125
+ };
126
+ }
127
+ case 'index':
128
+ case '[]': {
129
+ // Index access: obj[key]
130
+ const args = node.args;
131
+ return {
132
+ id,
133
+ callExpr: {
134
+ function: '_[_]',
135
+ args: args.map((a) => convertToProtoExpr(a, idCounter))
136
+ }
137
+ };
138
+ }
139
+ // Binary operators
140
+ case '+':
141
+ case '-':
142
+ case '*':
143
+ case '/':
144
+ case '%':
145
+ case '==':
146
+ case '!=':
147
+ case '<':
148
+ case '<=':
149
+ case '>':
150
+ case '>=':
151
+ case '&&':
152
+ case '||':
153
+ case 'in': {
154
+ const args = node.args;
155
+ const funcName = getBinaryOperatorName(node.op);
156
+ return {
157
+ id,
158
+ callExpr: {
159
+ function: funcName,
160
+ args: args.map((a) => convertToProtoExpr(a, idCounter))
161
+ }
162
+ };
163
+ }
164
+ // Unary operators
165
+ case '!':
166
+ case '!_':
167
+ case 'neg':
168
+ case '-_': {
169
+ // Args can be either a single node or an array with one element
170
+ const argNode = Array.isArray(node.args)
171
+ ? node.args[0]
172
+ : node.args;
173
+ const funcName = node.op === '!' || node.op === '!_' ? '!_' : '-_';
174
+ return {
175
+ id,
176
+ callExpr: {
177
+ function: funcName,
178
+ args: [convertToProtoExpr(argNode, idCounter)]
179
+ }
180
+ };
181
+ }
182
+ default:
183
+ console.warn(`Unknown operator: ${node.op}`);
184
+ return { id };
185
+ }
186
+ }
187
+ /**
188
+ * Convert a value from @marcbachmann/cel-js to CEL proto Constant
189
+ */
190
+ function convertValue(value) {
191
+ if (value === null) {
192
+ return { nullValue: null };
193
+ }
194
+ if (typeof value === 'boolean') {
195
+ return { boolValue: value };
196
+ }
197
+ if (typeof value === 'bigint') {
198
+ return { int64Value: value };
199
+ }
200
+ if (typeof value === 'number') {
201
+ if (Number.isInteger(value)) {
202
+ return { int64Value: BigInt(value) };
203
+ }
204
+ return { doubleValue: value };
205
+ }
206
+ if (typeof value === 'string') {
207
+ return { stringValue: value };
208
+ }
209
+ if (value instanceof Uint8Array) {
210
+ return { bytesValue: value };
211
+ }
212
+ // Handle unsigned integers (represented as objects in some cases)
213
+ if (typeof value === 'object' && value !== null) {
214
+ const obj = value;
215
+ if ('uint' in obj) {
216
+ return { uint64Value: obj.uint };
217
+ }
218
+ }
219
+ return { stringValue: String(value) };
220
+ }
221
+ /**
222
+ * Get the CEL proto function name for a binary operator
223
+ */
224
+ function getBinaryOperatorName(op) {
225
+ const mapping = {
226
+ '+': '_+_',
227
+ '-': '_-_',
228
+ '*': '_*_',
229
+ '/': '_/_',
230
+ '%': '_%%_',
231
+ '==': '_==_',
232
+ '!=': '_!=_',
233
+ '<': '_<_',
234
+ '<=': '_<=_',
235
+ '>': '_>_',
236
+ '>=': '_>=_',
237
+ '&&': '_&&_',
238
+ '||': '_||_',
239
+ 'in': '_in_'
240
+ };
241
+ return mapping[op] || op;
242
+ }
@@ -0,0 +1,95 @@
1
+ /**
2
+ * CEL Deparser - Converts CEL AST back to CEL expression strings
3
+ *
4
+ * This module provides functionality to convert a CEL Abstract Syntax Tree (AST)
5
+ * back into a valid CEL expression string. It handles all CEL constructs including:
6
+ * - Literals (int, uint, double, string, bytes, bool, null)
7
+ * - Identifiers and field selection
8
+ * - Function calls and operators
9
+ * - Lists and maps
10
+ * - Comprehensions (macros like all, exists, filter, map)
11
+ * - Ternary conditionals
12
+ */
13
+ export declare const PRECEDENCE: {
14
+ readonly CONDITIONAL: 1;
15
+ readonly OR: 2;
16
+ readonly AND: 3;
17
+ readonly RELATION: 4;
18
+ readonly ADDITION: 5;
19
+ readonly MULTIPLICATION: 6;
20
+ readonly UNARY: 7;
21
+ readonly MEMBER: 8;
22
+ readonly PRIMARY: 9;
23
+ };
24
+ /**
25
+ * CEL AST Types (simplified for deparser use)
26
+ */
27
+ export interface Expr {
28
+ id?: bigint | number;
29
+ constExpr?: Constant;
30
+ identExpr?: Ident;
31
+ selectExpr?: Select;
32
+ callExpr?: Call;
33
+ listExpr?: CreateList;
34
+ structExpr?: CreateStruct;
35
+ comprehensionExpr?: Comprehension;
36
+ }
37
+ export interface Constant {
38
+ nullValue?: unknown;
39
+ boolValue?: boolean;
40
+ int64Value?: bigint | number;
41
+ uint64Value?: bigint | number;
42
+ doubleValue?: number;
43
+ stringValue?: string;
44
+ bytesValue?: Uint8Array | string;
45
+ }
46
+ export interface Ident {
47
+ name?: string;
48
+ }
49
+ export interface Select {
50
+ operand?: Expr;
51
+ field?: string;
52
+ testOnly?: boolean;
53
+ }
54
+ export interface Call {
55
+ target?: Expr;
56
+ function?: string;
57
+ args?: Expr[];
58
+ }
59
+ export interface CreateList {
60
+ elements?: Expr[];
61
+ optionalIndices?: number[];
62
+ }
63
+ export interface CreateStruct {
64
+ messageName?: string;
65
+ entries?: Entry[];
66
+ }
67
+ export interface Entry {
68
+ id?: bigint | number;
69
+ fieldKey?: string;
70
+ mapKey?: Expr;
71
+ value?: Expr;
72
+ optionalEntry?: boolean;
73
+ }
74
+ export interface Comprehension {
75
+ iterVar?: string;
76
+ iterVar2?: string;
77
+ iterRange?: Expr;
78
+ accuVar?: string;
79
+ accuInit?: Expr;
80
+ loopCondition?: Expr;
81
+ loopStep?: Expr;
82
+ result?: Expr;
83
+ }
84
+ export interface DeparserOptions {
85
+ /** Whether to add spaces around operators */
86
+ spaces?: boolean;
87
+ }
88
+ /**
89
+ * Deparse a CEL expression AST to a string
90
+ */
91
+ export declare function deparse(expr: Expr, options?: DeparserOptions): string;
92
+ /**
93
+ * Generate TypeScript code for the deparser module
94
+ */
95
+ export declare function generateDeparserCode(): string;