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.
- package/LICENSE +21 -0
- package/README.md +136 -0
- package/ast/enums.d.ts +14 -0
- package/ast/enums.js +59 -0
- package/ast/index.d.ts +2 -0
- package/ast/index.js +18 -0
- package/ast/types.d.ts +40 -0
- package/ast/types.js +158 -0
- package/converter/index.d.ts +22 -0
- package/converter/index.js +242 -0
- package/deparser/index.d.ts +95 -0
- package/deparser/index.js +504 -0
- package/esm/ast/enums.js +22 -0
- package/esm/ast/index.js +2 -0
- package/esm/ast/types.js +117 -0
- package/esm/converter/index.js +239 -0
- package/esm/deparser/index.js +499 -0
- package/esm/index.js +6 -0
- package/esm/options/defaults.js +68 -0
- package/esm/options/index.js +2 -0
- package/esm/options/types.js +4 -0
- package/esm/parser.js +211 -0
- package/esm/utils/index.js +79 -0
- package/index.d.ts +6 -0
- package/index.js +28 -0
- package/options/defaults.d.ts +2 -0
- package/options/defaults.js +71 -0
- package/options/index.d.ts +2 -0
- package/options/index.js +18 -0
- package/options/types.d.ts +84 -0
- package/options/types.js +5 -0
- package/package.json +55 -0
- package/parser.d.ts +68 -0
- package/parser.js +251 -0
- package/utils/index.d.ts +46 -0
- package/utils/index.js +127 -0
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* CEL AST Converter
|
|
3
|
+
*
|
|
4
|
+
* Converts AST from @marcbachmann/cel-js format to CEL proto format
|
|
5
|
+
* used by our deparser.
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* Convert @marcbachmann/cel-js AST to CEL proto Expr format
|
|
9
|
+
*/
|
|
10
|
+
export function convertToProtoExpr(node, idCounter = { value: 1 }) {
|
|
11
|
+
const id = BigInt(idCounter.value++);
|
|
12
|
+
switch (node.op) {
|
|
13
|
+
case 'value':
|
|
14
|
+
return {
|
|
15
|
+
id,
|
|
16
|
+
constExpr: convertValue(node.args)
|
|
17
|
+
};
|
|
18
|
+
case 'id':
|
|
19
|
+
return {
|
|
20
|
+
id,
|
|
21
|
+
identExpr: { name: node.args }
|
|
22
|
+
};
|
|
23
|
+
case '.': {
|
|
24
|
+
const args = node.args;
|
|
25
|
+
return {
|
|
26
|
+
id,
|
|
27
|
+
selectExpr: {
|
|
28
|
+
operand: convertToProtoExpr(args[0], idCounter),
|
|
29
|
+
field: args[1]
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
case 'call': {
|
|
34
|
+
const args = node.args;
|
|
35
|
+
const funcName = args[0];
|
|
36
|
+
const funcArgs = args[1];
|
|
37
|
+
// Special handling for has() macro
|
|
38
|
+
if (funcName === 'has' && funcArgs.length === 1) {
|
|
39
|
+
const selectNode = funcArgs[0];
|
|
40
|
+
if (selectNode.op === '.') {
|
|
41
|
+
const selectArgs = selectNode.args;
|
|
42
|
+
return {
|
|
43
|
+
id,
|
|
44
|
+
selectExpr: {
|
|
45
|
+
operand: convertToProtoExpr(selectArgs[0], idCounter),
|
|
46
|
+
field: selectArgs[1],
|
|
47
|
+
testOnly: true
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
return {
|
|
53
|
+
id,
|
|
54
|
+
callExpr: {
|
|
55
|
+
function: funcName,
|
|
56
|
+
args: funcArgs.map((a) => convertToProtoExpr(a, idCounter))
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
case 'rcall': {
|
|
61
|
+
// Receiver call: target.method(args)
|
|
62
|
+
const args = node.args;
|
|
63
|
+
const funcName = args[0];
|
|
64
|
+
const target = args[1];
|
|
65
|
+
const funcArgs = args[2];
|
|
66
|
+
return {
|
|
67
|
+
id,
|
|
68
|
+
callExpr: {
|
|
69
|
+
target: convertToProtoExpr(target, idCounter),
|
|
70
|
+
function: funcName,
|
|
71
|
+
args: funcArgs.map((a) => convertToProtoExpr(a, idCounter))
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
case 'list': {
|
|
76
|
+
const elements = node.args;
|
|
77
|
+
return {
|
|
78
|
+
id,
|
|
79
|
+
listExpr: {
|
|
80
|
+
elements: elements.map((e) => convertToProtoExpr(e, idCounter))
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
case 'map': {
|
|
85
|
+
const entries = node.args;
|
|
86
|
+
return {
|
|
87
|
+
id,
|
|
88
|
+
structExpr: {
|
|
89
|
+
entries: entries.map(([key, value]) => ({
|
|
90
|
+
id: BigInt(idCounter.value++),
|
|
91
|
+
mapKey: convertToProtoExpr(key, idCounter),
|
|
92
|
+
value: convertToProtoExpr(value, idCounter)
|
|
93
|
+
}))
|
|
94
|
+
}
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
case 'struct': {
|
|
98
|
+
const args = node.args;
|
|
99
|
+
const messageName = args[0];
|
|
100
|
+
const fields = args[1];
|
|
101
|
+
return {
|
|
102
|
+
id,
|
|
103
|
+
structExpr: {
|
|
104
|
+
messageName,
|
|
105
|
+
entries: fields.map(([fieldKey, value]) => ({
|
|
106
|
+
id: BigInt(idCounter.value++),
|
|
107
|
+
fieldKey,
|
|
108
|
+
value: convertToProtoExpr(value, idCounter)
|
|
109
|
+
}))
|
|
110
|
+
}
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
case '?:': {
|
|
114
|
+
// Ternary conditional
|
|
115
|
+
const args = node.args;
|
|
116
|
+
return {
|
|
117
|
+
id,
|
|
118
|
+
callExpr: {
|
|
119
|
+
function: '_?_:_',
|
|
120
|
+
args: args.map((a) => convertToProtoExpr(a, idCounter))
|
|
121
|
+
}
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
case 'index':
|
|
125
|
+
case '[]': {
|
|
126
|
+
// Index access: obj[key]
|
|
127
|
+
const args = node.args;
|
|
128
|
+
return {
|
|
129
|
+
id,
|
|
130
|
+
callExpr: {
|
|
131
|
+
function: '_[_]',
|
|
132
|
+
args: args.map((a) => convertToProtoExpr(a, idCounter))
|
|
133
|
+
}
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
// Binary operators
|
|
137
|
+
case '+':
|
|
138
|
+
case '-':
|
|
139
|
+
case '*':
|
|
140
|
+
case '/':
|
|
141
|
+
case '%':
|
|
142
|
+
case '==':
|
|
143
|
+
case '!=':
|
|
144
|
+
case '<':
|
|
145
|
+
case '<=':
|
|
146
|
+
case '>':
|
|
147
|
+
case '>=':
|
|
148
|
+
case '&&':
|
|
149
|
+
case '||':
|
|
150
|
+
case 'in': {
|
|
151
|
+
const args = node.args;
|
|
152
|
+
const funcName = getBinaryOperatorName(node.op);
|
|
153
|
+
return {
|
|
154
|
+
id,
|
|
155
|
+
callExpr: {
|
|
156
|
+
function: funcName,
|
|
157
|
+
args: args.map((a) => convertToProtoExpr(a, idCounter))
|
|
158
|
+
}
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
// Unary operators
|
|
162
|
+
case '!':
|
|
163
|
+
case '!_':
|
|
164
|
+
case 'neg':
|
|
165
|
+
case '-_': {
|
|
166
|
+
// Args can be either a single node or an array with one element
|
|
167
|
+
const argNode = Array.isArray(node.args)
|
|
168
|
+
? node.args[0]
|
|
169
|
+
: node.args;
|
|
170
|
+
const funcName = node.op === '!' || node.op === '!_' ? '!_' : '-_';
|
|
171
|
+
return {
|
|
172
|
+
id,
|
|
173
|
+
callExpr: {
|
|
174
|
+
function: funcName,
|
|
175
|
+
args: [convertToProtoExpr(argNode, idCounter)]
|
|
176
|
+
}
|
|
177
|
+
};
|
|
178
|
+
}
|
|
179
|
+
default:
|
|
180
|
+
console.warn(`Unknown operator: ${node.op}`);
|
|
181
|
+
return { id };
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* Convert a value from @marcbachmann/cel-js to CEL proto Constant
|
|
186
|
+
*/
|
|
187
|
+
function convertValue(value) {
|
|
188
|
+
if (value === null) {
|
|
189
|
+
return { nullValue: null };
|
|
190
|
+
}
|
|
191
|
+
if (typeof value === 'boolean') {
|
|
192
|
+
return { boolValue: value };
|
|
193
|
+
}
|
|
194
|
+
if (typeof value === 'bigint') {
|
|
195
|
+
return { int64Value: value };
|
|
196
|
+
}
|
|
197
|
+
if (typeof value === 'number') {
|
|
198
|
+
if (Number.isInteger(value)) {
|
|
199
|
+
return { int64Value: BigInt(value) };
|
|
200
|
+
}
|
|
201
|
+
return { doubleValue: value };
|
|
202
|
+
}
|
|
203
|
+
if (typeof value === 'string') {
|
|
204
|
+
return { stringValue: value };
|
|
205
|
+
}
|
|
206
|
+
if (value instanceof Uint8Array) {
|
|
207
|
+
return { bytesValue: value };
|
|
208
|
+
}
|
|
209
|
+
// Handle unsigned integers (represented as objects in some cases)
|
|
210
|
+
if (typeof value === 'object' && value !== null) {
|
|
211
|
+
const obj = value;
|
|
212
|
+
if ('uint' in obj) {
|
|
213
|
+
return { uint64Value: obj.uint };
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
return { stringValue: String(value) };
|
|
217
|
+
}
|
|
218
|
+
/**
|
|
219
|
+
* Get the CEL proto function name for a binary operator
|
|
220
|
+
*/
|
|
221
|
+
function getBinaryOperatorName(op) {
|
|
222
|
+
const mapping = {
|
|
223
|
+
'+': '_+_',
|
|
224
|
+
'-': '_-_',
|
|
225
|
+
'*': '_*_',
|
|
226
|
+
'/': '_/_',
|
|
227
|
+
'%': '_%%_',
|
|
228
|
+
'==': '_==_',
|
|
229
|
+
'!=': '_!=_',
|
|
230
|
+
'<': '_<_',
|
|
231
|
+
'<=': '_<=_',
|
|
232
|
+
'>': '_>_',
|
|
233
|
+
'>=': '_>=_',
|
|
234
|
+
'&&': '_&&_',
|
|
235
|
+
'||': '_||_',
|
|
236
|
+
'in': '_in_'
|
|
237
|
+
};
|
|
238
|
+
return mapping[op] || op;
|
|
239
|
+
}
|