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,499 @@
|
|
|
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
|
+
// CEL operator precedence (higher number = higher precedence)
|
|
14
|
+
export const PRECEDENCE = {
|
|
15
|
+
CONDITIONAL: 1, // ?:
|
|
16
|
+
OR: 2, // ||
|
|
17
|
+
AND: 3, // &&
|
|
18
|
+
RELATION: 4, // == != < <= > >= in
|
|
19
|
+
ADDITION: 5, // + -
|
|
20
|
+
MULTIPLICATION: 6, // * / %
|
|
21
|
+
UNARY: 7, // ! -
|
|
22
|
+
MEMBER: 8, // . [] ()
|
|
23
|
+
PRIMARY: 9 // literals, identifiers, parentheses
|
|
24
|
+
};
|
|
25
|
+
// Binary operators and their precedence
|
|
26
|
+
const BINARY_OPERATORS = {
|
|
27
|
+
'_||_': { symbol: '||', precedence: PRECEDENCE.OR },
|
|
28
|
+
'_&&_': { symbol: '&&', precedence: PRECEDENCE.AND },
|
|
29
|
+
'_==_': { symbol: '==', precedence: PRECEDENCE.RELATION },
|
|
30
|
+
'_!=_': { symbol: '!=', precedence: PRECEDENCE.RELATION },
|
|
31
|
+
'_<_': { symbol: '<', precedence: PRECEDENCE.RELATION },
|
|
32
|
+
'_<=_': { symbol: '<=', precedence: PRECEDENCE.RELATION },
|
|
33
|
+
'_>_': { symbol: '>', precedence: PRECEDENCE.RELATION },
|
|
34
|
+
'_>=_': { symbol: '>=', precedence: PRECEDENCE.RELATION },
|
|
35
|
+
'@in': { symbol: 'in', precedence: PRECEDENCE.RELATION },
|
|
36
|
+
'_in_': { symbol: 'in', precedence: PRECEDENCE.RELATION },
|
|
37
|
+
'_+_': { symbol: '+', precedence: PRECEDENCE.ADDITION },
|
|
38
|
+
'_-_': { symbol: '-', precedence: PRECEDENCE.ADDITION },
|
|
39
|
+
'_*_': { symbol: '*', precedence: PRECEDENCE.MULTIPLICATION },
|
|
40
|
+
'_/_': { symbol: '/', precedence: PRECEDENCE.MULTIPLICATION },
|
|
41
|
+
'_%%_': { symbol: '%', precedence: PRECEDENCE.MULTIPLICATION }
|
|
42
|
+
};
|
|
43
|
+
// Unary operators
|
|
44
|
+
const UNARY_OPERATORS = {
|
|
45
|
+
'!_': '!',
|
|
46
|
+
'-_': '-'
|
|
47
|
+
};
|
|
48
|
+
/**
|
|
49
|
+
* Escape a string for CEL string literal
|
|
50
|
+
*/
|
|
51
|
+
function escapeString(str) {
|
|
52
|
+
return str
|
|
53
|
+
.replace(/\\/g, '\\\\')
|
|
54
|
+
.replace(/"/g, '\\"')
|
|
55
|
+
.replace(/\n/g, '\\n')
|
|
56
|
+
.replace(/\r/g, '\\r')
|
|
57
|
+
.replace(/\t/g, '\\t');
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Escape bytes for CEL bytes literal
|
|
61
|
+
*/
|
|
62
|
+
function escapeBytes(bytes) {
|
|
63
|
+
if (typeof bytes === 'string') {
|
|
64
|
+
// Assume it's already a hex string or similar
|
|
65
|
+
return bytes;
|
|
66
|
+
}
|
|
67
|
+
return Array.from(bytes)
|
|
68
|
+
.map((b) => {
|
|
69
|
+
if (b >= 32 && b < 127 && b !== 34 && b !== 92) {
|
|
70
|
+
return String.fromCharCode(b);
|
|
71
|
+
}
|
|
72
|
+
return '\\x' + b.toString(16).padStart(2, '0');
|
|
73
|
+
})
|
|
74
|
+
.join('');
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Get the precedence of an expression
|
|
78
|
+
*/
|
|
79
|
+
function getExprPrecedence(expr) {
|
|
80
|
+
if (expr.constExpr)
|
|
81
|
+
return PRECEDENCE.PRIMARY;
|
|
82
|
+
if (expr.identExpr)
|
|
83
|
+
return PRECEDENCE.PRIMARY;
|
|
84
|
+
if (expr.listExpr)
|
|
85
|
+
return PRECEDENCE.PRIMARY;
|
|
86
|
+
if (expr.structExpr)
|
|
87
|
+
return PRECEDENCE.PRIMARY;
|
|
88
|
+
if (expr.selectExpr)
|
|
89
|
+
return PRECEDENCE.MEMBER;
|
|
90
|
+
if (expr.callExpr) {
|
|
91
|
+
const fn = expr.callExpr.function || '';
|
|
92
|
+
if (UNARY_OPERATORS[fn])
|
|
93
|
+
return PRECEDENCE.UNARY;
|
|
94
|
+
if (BINARY_OPERATORS[fn])
|
|
95
|
+
return BINARY_OPERATORS[fn].precedence;
|
|
96
|
+
if (fn === '_?_:_')
|
|
97
|
+
return PRECEDENCE.CONDITIONAL;
|
|
98
|
+
if (fn === '_[_]')
|
|
99
|
+
return PRECEDENCE.MEMBER;
|
|
100
|
+
return PRECEDENCE.MEMBER; // function call
|
|
101
|
+
}
|
|
102
|
+
if (expr.comprehensionExpr)
|
|
103
|
+
return PRECEDENCE.PRIMARY;
|
|
104
|
+
return PRECEDENCE.PRIMARY;
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Deparse a CEL expression AST to a string
|
|
108
|
+
*/
|
|
109
|
+
export function deparse(expr, options = {}) {
|
|
110
|
+
const spaces = options.spaces ?? true;
|
|
111
|
+
const sp = spaces ? ' ' : '';
|
|
112
|
+
function deparseExpr(e, parentPrecedence = 0) {
|
|
113
|
+
// Constant expression
|
|
114
|
+
if (e.constExpr) {
|
|
115
|
+
return deparseConstant(e.constExpr);
|
|
116
|
+
}
|
|
117
|
+
// Identifier expression
|
|
118
|
+
if (e.identExpr) {
|
|
119
|
+
return e.identExpr.name || '';
|
|
120
|
+
}
|
|
121
|
+
// Select expression (field access)
|
|
122
|
+
if (e.selectExpr) {
|
|
123
|
+
const operand = e.selectExpr.operand
|
|
124
|
+
? deparseExpr(e.selectExpr.operand, PRECEDENCE.MEMBER)
|
|
125
|
+
: '';
|
|
126
|
+
const field = e.selectExpr.field || '';
|
|
127
|
+
if (e.selectExpr.testOnly) {
|
|
128
|
+
// This is the result of has() macro
|
|
129
|
+
return `has(${operand}.${field})`;
|
|
130
|
+
}
|
|
131
|
+
return `${operand}.${field}`;
|
|
132
|
+
}
|
|
133
|
+
// Call expression (function calls and operators)
|
|
134
|
+
if (e.callExpr) {
|
|
135
|
+
return deparseCall(e.callExpr, parentPrecedence);
|
|
136
|
+
}
|
|
137
|
+
// List expression
|
|
138
|
+
if (e.listExpr) {
|
|
139
|
+
const elements = e.listExpr.elements || [];
|
|
140
|
+
const optionalIndices = new Set(e.listExpr.optionalIndices || []);
|
|
141
|
+
const items = elements.map((el, i) => {
|
|
142
|
+
const prefix = optionalIndices.has(i) ? '?' : '';
|
|
143
|
+
return prefix + deparseExpr(el);
|
|
144
|
+
});
|
|
145
|
+
return `[${items.join(', ')}]`;
|
|
146
|
+
}
|
|
147
|
+
// Struct expression (map or message)
|
|
148
|
+
if (e.structExpr) {
|
|
149
|
+
return deparseStruct(e.structExpr);
|
|
150
|
+
}
|
|
151
|
+
// Comprehension expression (macros)
|
|
152
|
+
if (e.comprehensionExpr) {
|
|
153
|
+
return deparseComprehension(e.comprehensionExpr);
|
|
154
|
+
}
|
|
155
|
+
return '';
|
|
156
|
+
}
|
|
157
|
+
function deparseConstant(c) {
|
|
158
|
+
if (c.nullValue !== undefined) {
|
|
159
|
+
return 'null';
|
|
160
|
+
}
|
|
161
|
+
if (c.boolValue !== undefined) {
|
|
162
|
+
return c.boolValue ? 'true' : 'false';
|
|
163
|
+
}
|
|
164
|
+
if (c.int64Value !== undefined) {
|
|
165
|
+
return String(c.int64Value);
|
|
166
|
+
}
|
|
167
|
+
if (c.uint64Value !== undefined) {
|
|
168
|
+
return String(c.uint64Value) + 'u';
|
|
169
|
+
}
|
|
170
|
+
if (c.doubleValue !== undefined) {
|
|
171
|
+
const d = c.doubleValue;
|
|
172
|
+
// Ensure it looks like a float
|
|
173
|
+
if (Number.isInteger(d)) {
|
|
174
|
+
return d.toFixed(1);
|
|
175
|
+
}
|
|
176
|
+
return String(d);
|
|
177
|
+
}
|
|
178
|
+
if (c.stringValue !== undefined) {
|
|
179
|
+
return `"${escapeString(c.stringValue)}"`;
|
|
180
|
+
}
|
|
181
|
+
if (c.bytesValue !== undefined) {
|
|
182
|
+
return `b"${escapeBytes(c.bytesValue)}"`;
|
|
183
|
+
}
|
|
184
|
+
return '';
|
|
185
|
+
}
|
|
186
|
+
function deparseCall(call, parentPrecedence) {
|
|
187
|
+
const fn = call.function || '';
|
|
188
|
+
const args = call.args || [];
|
|
189
|
+
const target = call.target;
|
|
190
|
+
// Ternary conditional
|
|
191
|
+
if (fn === '_?_:_' && args.length === 3) {
|
|
192
|
+
const condition = deparseExpr(args[0], PRECEDENCE.CONDITIONAL);
|
|
193
|
+
const trueExpr = deparseExpr(args[1], PRECEDENCE.CONDITIONAL);
|
|
194
|
+
const falseExpr = deparseExpr(args[2], PRECEDENCE.CONDITIONAL);
|
|
195
|
+
const result = `${condition}${sp}?${sp}${trueExpr}${sp}:${sp}${falseExpr}`;
|
|
196
|
+
return parentPrecedence > PRECEDENCE.CONDITIONAL
|
|
197
|
+
? `(${result})`
|
|
198
|
+
: result;
|
|
199
|
+
}
|
|
200
|
+
// Index operator
|
|
201
|
+
if (fn === '_[_]' && args.length === 2) {
|
|
202
|
+
const obj = deparseExpr(args[0], PRECEDENCE.MEMBER);
|
|
203
|
+
const index = deparseExpr(args[1]);
|
|
204
|
+
return `${obj}[${index}]`;
|
|
205
|
+
}
|
|
206
|
+
// Unary operators
|
|
207
|
+
if (UNARY_OPERATORS[fn] && args.length === 1) {
|
|
208
|
+
const op = UNARY_OPERATORS[fn];
|
|
209
|
+
const operand = deparseExpr(args[0], PRECEDENCE.UNARY);
|
|
210
|
+
return `${op}${operand}`;
|
|
211
|
+
}
|
|
212
|
+
// Binary operators
|
|
213
|
+
if (BINARY_OPERATORS[fn] && args.length === 2) {
|
|
214
|
+
const { symbol, precedence } = BINARY_OPERATORS[fn];
|
|
215
|
+
const left = deparseExpr(args[0], precedence);
|
|
216
|
+
const right = deparseExpr(args[1], precedence + 1); // Right associative needs higher precedence
|
|
217
|
+
const result = `${left}${sp}${symbol}${sp}${right}`;
|
|
218
|
+
return parentPrecedence > precedence ? `(${result})` : result;
|
|
219
|
+
}
|
|
220
|
+
// Method call (target.function(args))
|
|
221
|
+
if (target) {
|
|
222
|
+
const targetStr = deparseExpr(target, PRECEDENCE.MEMBER);
|
|
223
|
+
const argsStr = args.map((a) => deparseExpr(a)).join(', ');
|
|
224
|
+
return `${targetStr}.${fn}(${argsStr})`;
|
|
225
|
+
}
|
|
226
|
+
// Regular function call
|
|
227
|
+
const argsStr = args.map((a) => deparseExpr(a)).join(', ');
|
|
228
|
+
return `${fn}(${argsStr})`;
|
|
229
|
+
}
|
|
230
|
+
function deparseStruct(struct) {
|
|
231
|
+
const messageName = struct.messageName || '';
|
|
232
|
+
const entries = struct.entries || [];
|
|
233
|
+
const items = entries.map((entry) => {
|
|
234
|
+
const prefix = entry.optionalEntry ? '?' : '';
|
|
235
|
+
if (entry.fieldKey) {
|
|
236
|
+
// Message field
|
|
237
|
+
const value = entry.value ? deparseExpr(entry.value) : '';
|
|
238
|
+
return `${prefix}${entry.fieldKey}: ${value}`;
|
|
239
|
+
}
|
|
240
|
+
else if (entry.mapKey) {
|
|
241
|
+
// Map entry
|
|
242
|
+
const key = deparseExpr(entry.mapKey);
|
|
243
|
+
const value = entry.value ? deparseExpr(entry.value) : '';
|
|
244
|
+
return `${prefix}${key}: ${value}`;
|
|
245
|
+
}
|
|
246
|
+
return '';
|
|
247
|
+
});
|
|
248
|
+
if (messageName) {
|
|
249
|
+
// Message construction
|
|
250
|
+
return `${messageName}{${items.join(', ')}}`;
|
|
251
|
+
}
|
|
252
|
+
else {
|
|
253
|
+
// Map literal
|
|
254
|
+
return `{${items.join(', ')}}`;
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
function deparseComprehension(comp) {
|
|
258
|
+
// Comprehensions are typically the result of macro expansion
|
|
259
|
+
// We try to reconstruct the original macro call
|
|
260
|
+
const iterVar = comp.iterVar || '';
|
|
261
|
+
const iterRange = comp.iterRange ? deparseExpr(comp.iterRange) : '';
|
|
262
|
+
const loopCondition = comp.loopCondition
|
|
263
|
+
? deparseExpr(comp.loopCondition)
|
|
264
|
+
: '';
|
|
265
|
+
const loopStep = comp.loopStep ? deparseExpr(comp.loopStep) : '';
|
|
266
|
+
const result = comp.result ? deparseExpr(comp.result) : '';
|
|
267
|
+
const accuVar = comp.accuVar || '';
|
|
268
|
+
const accuInit = comp.accuInit ? deparseExpr(comp.accuInit) : '';
|
|
269
|
+
// Try to detect common macros based on structure
|
|
270
|
+
// exists: __result__ == true, accu starts false, step is || condition
|
|
271
|
+
if (accuVar === '__result__' && accuInit === 'false') {
|
|
272
|
+
// This looks like exists()
|
|
273
|
+
return `${iterRange}.exists(${iterVar}, ${loopStep.replace('__result__ || ', '')})`;
|
|
274
|
+
}
|
|
275
|
+
// all: __result__ == true, accu starts true, step is && condition
|
|
276
|
+
if (accuVar === '__result__' && accuInit === 'true') {
|
|
277
|
+
// This looks like all()
|
|
278
|
+
return `${iterRange}.all(${iterVar}, ${loopStep.replace('__result__ && ', '')})`;
|
|
279
|
+
}
|
|
280
|
+
// For other comprehensions, output a comment indicating it's a comprehension
|
|
281
|
+
// This is a fallback - ideally we'd reconstruct the original macro
|
|
282
|
+
return `/* comprehension: ${iterVar} in ${iterRange} */`;
|
|
283
|
+
}
|
|
284
|
+
return deparseExpr(expr);
|
|
285
|
+
}
|
|
286
|
+
/**
|
|
287
|
+
* Generate TypeScript code for the deparser module
|
|
288
|
+
*/
|
|
289
|
+
export function generateDeparserCode() {
|
|
290
|
+
// Return the deparser code as a string that can be written to a file
|
|
291
|
+
return `/**
|
|
292
|
+
* CEL Deparser - Converts CEL AST back to CEL expression strings
|
|
293
|
+
*
|
|
294
|
+
* This file was automatically generated by cel-proto-parser.
|
|
295
|
+
* DO NOT MODIFY IT BY HAND.
|
|
296
|
+
*/
|
|
297
|
+
|
|
298
|
+
import type { Expr, Constant, Call, CreateStruct, Comprehension } from './types';
|
|
299
|
+
|
|
300
|
+
// CEL operator precedence (higher number = higher precedence)
|
|
301
|
+
export const PRECEDENCE = {
|
|
302
|
+
CONDITIONAL: 1, // ?:
|
|
303
|
+
OR: 2, // ||
|
|
304
|
+
AND: 3, // &&
|
|
305
|
+
RELATION: 4, // == != < <= > >= in
|
|
306
|
+
ADDITION: 5, // + -
|
|
307
|
+
MULTIPLICATION: 6, // * / %
|
|
308
|
+
UNARY: 7, // ! -
|
|
309
|
+
MEMBER: 8, // . [] ()
|
|
310
|
+
PRIMARY: 9 // literals, identifiers, parentheses
|
|
311
|
+
} as const;
|
|
312
|
+
|
|
313
|
+
// Binary operators and their precedence
|
|
314
|
+
const BINARY_OPERATORS: Record<string, { symbol: string; precedence: number }> = {
|
|
315
|
+
'_||_': { symbol: '||', precedence: PRECEDENCE.OR },
|
|
316
|
+
'_&&_': { symbol: '&&', precedence: PRECEDENCE.AND },
|
|
317
|
+
'_==_': { symbol: '==', precedence: PRECEDENCE.RELATION },
|
|
318
|
+
'_!=_': { symbol: '!=', precedence: PRECEDENCE.RELATION },
|
|
319
|
+
'_<_': { symbol: '<', precedence: PRECEDENCE.RELATION },
|
|
320
|
+
'_<=_': { symbol: '<=', precedence: PRECEDENCE.RELATION },
|
|
321
|
+
'_>_': { symbol: '>', precedence: PRECEDENCE.RELATION },
|
|
322
|
+
'_>=_': { symbol: '>=', precedence: PRECEDENCE.RELATION },
|
|
323
|
+
'@in': { symbol: 'in', precedence: PRECEDENCE.RELATION },
|
|
324
|
+
'_in_': { symbol: 'in', precedence: PRECEDENCE.RELATION },
|
|
325
|
+
'_+_': { symbol: '+', precedence: PRECEDENCE.ADDITION },
|
|
326
|
+
'_-_': { symbol: '-', precedence: PRECEDENCE.ADDITION },
|
|
327
|
+
'_*_': { symbol: '*', precedence: PRECEDENCE.MULTIPLICATION },
|
|
328
|
+
'_/_': { symbol: '/', precedence: PRECEDENCE.MULTIPLICATION },
|
|
329
|
+
'_%%_': { symbol: '%', precedence: PRECEDENCE.MULTIPLICATION }
|
|
330
|
+
};
|
|
331
|
+
|
|
332
|
+
// Unary operators
|
|
333
|
+
const UNARY_OPERATORS: Record<string, string> = {
|
|
334
|
+
'!_': '!',
|
|
335
|
+
'-_': '-'
|
|
336
|
+
};
|
|
337
|
+
|
|
338
|
+
export interface DeparserOptions {
|
|
339
|
+
/** Whether to add spaces around operators */
|
|
340
|
+
spaces?: boolean;
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
/**
|
|
344
|
+
* Escape a string for CEL string literal
|
|
345
|
+
*/
|
|
346
|
+
function escapeString(str: string): string {
|
|
347
|
+
return str
|
|
348
|
+
.replace(/\\\\/g, '\\\\\\\\')
|
|
349
|
+
.replace(/"/g, '\\\\"')
|
|
350
|
+
.replace(/\\n/g, '\\\\n')
|
|
351
|
+
.replace(/\\r/g, '\\\\r')
|
|
352
|
+
.replace(/\\t/g, '\\\\t');
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
/**
|
|
356
|
+
* Escape bytes for CEL bytes literal
|
|
357
|
+
*/
|
|
358
|
+
function escapeBytes(bytes: Uint8Array | string): string {
|
|
359
|
+
if (typeof bytes === 'string') {
|
|
360
|
+
return bytes;
|
|
361
|
+
}
|
|
362
|
+
return Array.from(bytes)
|
|
363
|
+
.map((b) => {
|
|
364
|
+
if (b >= 32 && b < 127 && b !== 34 && b !== 92) {
|
|
365
|
+
return String.fromCharCode(b);
|
|
366
|
+
}
|
|
367
|
+
return '\\\\x' + b.toString(16).padStart(2, '0');
|
|
368
|
+
})
|
|
369
|
+
.join('');
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
/**
|
|
373
|
+
* Deparse a CEL expression AST to a string
|
|
374
|
+
*/
|
|
375
|
+
export function deparse(expr: Expr, options: DeparserOptions = {}): string {
|
|
376
|
+
const spaces = options.spaces ?? true;
|
|
377
|
+
const sp = spaces ? ' ' : '';
|
|
378
|
+
|
|
379
|
+
function deparseExpr(e: Expr, parentPrecedence: number = 0): string {
|
|
380
|
+
if (e.constExpr) return deparseConstant(e.constExpr);
|
|
381
|
+
if (e.identExpr) return e.identExpr.name || '';
|
|
382
|
+
if (e.selectExpr) {
|
|
383
|
+
const operand = e.selectExpr.operand ? deparseExpr(e.selectExpr.operand, PRECEDENCE.MEMBER) : '';
|
|
384
|
+
const field = e.selectExpr.field || '';
|
|
385
|
+
if (e.selectExpr.testOnly) return \`has(\${operand}.\${field})\`;
|
|
386
|
+
return \`\${operand}.\${field}\`;
|
|
387
|
+
}
|
|
388
|
+
if (e.callExpr) return deparseCall(e.callExpr, parentPrecedence);
|
|
389
|
+
if (e.listExpr) {
|
|
390
|
+
const elements = e.listExpr.elements || [];
|
|
391
|
+
const optionalIndices = new Set(e.listExpr.optionalIndices || []);
|
|
392
|
+
const items = elements.map((el, i) => {
|
|
393
|
+
const prefix = optionalIndices.has(i) ? '?' : '';
|
|
394
|
+
return prefix + deparseExpr(el);
|
|
395
|
+
});
|
|
396
|
+
return \`[\${items.join(', ')}]\`;
|
|
397
|
+
}
|
|
398
|
+
if (e.structExpr) return deparseStruct(e.structExpr);
|
|
399
|
+
if (e.comprehensionExpr) return deparseComprehension(e.comprehensionExpr);
|
|
400
|
+
return '';
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
function deparseConstant(c: Constant): string {
|
|
404
|
+
if (c.nullValue !== undefined) return 'null';
|
|
405
|
+
if (c.boolValue !== undefined) return c.boolValue ? 'true' : 'false';
|
|
406
|
+
if (c.int64Value !== undefined) return String(c.int64Value);
|
|
407
|
+
if (c.uint64Value !== undefined) return String(c.uint64Value) + 'u';
|
|
408
|
+
if (c.doubleValue !== undefined) {
|
|
409
|
+
const d = c.doubleValue;
|
|
410
|
+
if (Number.isInteger(d)) return d.toFixed(1);
|
|
411
|
+
return String(d);
|
|
412
|
+
}
|
|
413
|
+
if (c.stringValue !== undefined) return \`"\${escapeString(c.stringValue)}"\`;
|
|
414
|
+
if (c.bytesValue !== undefined) return \`b"\${escapeBytes(c.bytesValue)}"\`;
|
|
415
|
+
return '';
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
function deparseCall(call: Call, parentPrecedence: number): string {
|
|
419
|
+
const fn = call.function || '';
|
|
420
|
+
const args = call.args || [];
|
|
421
|
+
const target = call.target;
|
|
422
|
+
|
|
423
|
+
if (fn === '_?_:_' && args.length === 3) {
|
|
424
|
+
const condition = deparseExpr(args[0], PRECEDENCE.CONDITIONAL);
|
|
425
|
+
const trueExpr = deparseExpr(args[1], PRECEDENCE.CONDITIONAL);
|
|
426
|
+
const falseExpr = deparseExpr(args[2], PRECEDENCE.CONDITIONAL);
|
|
427
|
+
const result = \`\${condition}\${sp}?\${sp}\${trueExpr}\${sp}:\${sp}\${falseExpr}\`;
|
|
428
|
+
return parentPrecedence > PRECEDENCE.CONDITIONAL ? \`(\${result})\` : result;
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
if (fn === '_[_]' && args.length === 2) {
|
|
432
|
+
const obj = deparseExpr(args[0], PRECEDENCE.MEMBER);
|
|
433
|
+
const index = deparseExpr(args[1]);
|
|
434
|
+
return \`\${obj}[\${index}]\`;
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
if (UNARY_OPERATORS[fn] && args.length === 1) {
|
|
438
|
+
const op = UNARY_OPERATORS[fn];
|
|
439
|
+
const operand = deparseExpr(args[0], PRECEDENCE.UNARY);
|
|
440
|
+
return \`\${op}\${operand}\`;
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
if (BINARY_OPERATORS[fn] && args.length === 2) {
|
|
444
|
+
const { symbol, precedence } = BINARY_OPERATORS[fn];
|
|
445
|
+
const left = deparseExpr(args[0], precedence);
|
|
446
|
+
const right = deparseExpr(args[1], precedence + 1);
|
|
447
|
+
const result = \`\${left}\${sp}\${symbol}\${sp}\${right}\`;
|
|
448
|
+
return parentPrecedence > precedence ? \`(\${result})\` : result;
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
if (target) {
|
|
452
|
+
const targetStr = deparseExpr(target, PRECEDENCE.MEMBER);
|
|
453
|
+
const argsStr = args.map((a) => deparseExpr(a)).join(', ');
|
|
454
|
+
return \`\${targetStr}.\${fn}(\${argsStr})\`;
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
const argsStr = args.map((a) => deparseExpr(a)).join(', ');
|
|
458
|
+
return \`\${fn}(\${argsStr})\`;
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
function deparseStruct(struct: CreateStruct): string {
|
|
462
|
+
const messageName = struct.messageName || '';
|
|
463
|
+
const entries = struct.entries || [];
|
|
464
|
+
const items = entries.map((entry) => {
|
|
465
|
+
const prefix = entry.optionalEntry ? '?' : '';
|
|
466
|
+
if (entry.fieldKey) {
|
|
467
|
+
const value = entry.value ? deparseExpr(entry.value) : '';
|
|
468
|
+
return \`\${prefix}\${entry.fieldKey}: \${value}\`;
|
|
469
|
+
} else if (entry.mapKey) {
|
|
470
|
+
const key = deparseExpr(entry.mapKey);
|
|
471
|
+
const value = entry.value ? deparseExpr(entry.value) : '';
|
|
472
|
+
return \`\${prefix}\${key}: \${value}\`;
|
|
473
|
+
}
|
|
474
|
+
return '';
|
|
475
|
+
});
|
|
476
|
+
if (messageName) return \`\${messageName}{\${items.join(', ')}}\`;
|
|
477
|
+
return \`{\${items.join(', ')}}\`;
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
function deparseComprehension(comp: Comprehension): string {
|
|
481
|
+
const iterVar = comp.iterVar || '';
|
|
482
|
+
const iterRange = comp.iterRange ? deparseExpr(comp.iterRange) : '';
|
|
483
|
+
const accuVar = comp.accuVar || '';
|
|
484
|
+
const accuInit = comp.accuInit ? deparseExpr(comp.accuInit) : '';
|
|
485
|
+
const loopStep = comp.loopStep ? deparseExpr(comp.loopStep) : '';
|
|
486
|
+
|
|
487
|
+
if (accuVar === '__result__' && accuInit === 'false') {
|
|
488
|
+
return \`\${iterRange}.exists(\${iterVar}, \${loopStep.replace('__result__ || ', '')})\`;
|
|
489
|
+
}
|
|
490
|
+
if (accuVar === '__result__' && accuInit === 'true') {
|
|
491
|
+
return \`\${iterRange}.all(\${iterVar}, \${loopStep.replace('__result__ && ', '')})\`;
|
|
492
|
+
}
|
|
493
|
+
return \`/* comprehension: \${iterVar} in \${iterRange} */\`;
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
return deparseExpr(expr);
|
|
497
|
+
}
|
|
498
|
+
`;
|
|
499
|
+
}
|
package/esm/index.js
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
const defaultOptions = {
|
|
2
|
+
outDir: process.cwd() + '/out',
|
|
3
|
+
exclude: [],
|
|
4
|
+
parser: {
|
|
5
|
+
keepCase: false
|
|
6
|
+
},
|
|
7
|
+
types: {
|
|
8
|
+
enabled: true,
|
|
9
|
+
filename: 'types.ts',
|
|
10
|
+
optionalFields: true,
|
|
11
|
+
enumsSource: './enums'
|
|
12
|
+
},
|
|
13
|
+
enums: {
|
|
14
|
+
enabled: true,
|
|
15
|
+
filename: 'enums.ts',
|
|
16
|
+
enumsAsTypeUnion: true
|
|
17
|
+
},
|
|
18
|
+
utils: {
|
|
19
|
+
astHelpers: {
|
|
20
|
+
enabled: true,
|
|
21
|
+
filename: 'asts.ts',
|
|
22
|
+
typesSource: './types'
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
deparser: {
|
|
26
|
+
enabled: true,
|
|
27
|
+
filename: 'deparser.ts',
|
|
28
|
+
typesSource: './types'
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
export function getOptionsWithDefaults(options) {
|
|
32
|
+
if (!options) {
|
|
33
|
+
return { ...defaultOptions };
|
|
34
|
+
}
|
|
35
|
+
return {
|
|
36
|
+
outDir: options.outDir ?? defaultOptions.outDir,
|
|
37
|
+
exclude: options.exclude ?? defaultOptions.exclude,
|
|
38
|
+
parser: {
|
|
39
|
+
keepCase: options.parser?.keepCase ?? defaultOptions.parser.keepCase
|
|
40
|
+
},
|
|
41
|
+
types: {
|
|
42
|
+
enabled: options.types?.enabled ?? defaultOptions.types.enabled,
|
|
43
|
+
filename: options.types?.filename ?? defaultOptions.types.filename,
|
|
44
|
+
optionalFields: options.types?.optionalFields ?? defaultOptions.types.optionalFields,
|
|
45
|
+
enumsSource: options.types?.enumsSource ?? defaultOptions.types.enumsSource
|
|
46
|
+
},
|
|
47
|
+
enums: {
|
|
48
|
+
enabled: options.enums?.enabled ?? defaultOptions.enums.enabled,
|
|
49
|
+
filename: options.enums?.filename ?? defaultOptions.enums.filename,
|
|
50
|
+
enumsAsTypeUnion: options.enums?.enumsAsTypeUnion ?? defaultOptions.enums.enumsAsTypeUnion
|
|
51
|
+
},
|
|
52
|
+
utils: {
|
|
53
|
+
astHelpers: {
|
|
54
|
+
enabled: options.utils?.astHelpers?.enabled ??
|
|
55
|
+
defaultOptions.utils.astHelpers.enabled,
|
|
56
|
+
filename: options.utils?.astHelpers?.filename ??
|
|
57
|
+
defaultOptions.utils.astHelpers.filename,
|
|
58
|
+
typesSource: options.utils?.astHelpers?.typesSource ??
|
|
59
|
+
defaultOptions.utils.astHelpers.typesSource
|
|
60
|
+
}
|
|
61
|
+
},
|
|
62
|
+
deparser: {
|
|
63
|
+
enabled: options.deparser?.enabled ?? defaultOptions.deparser.enabled,
|
|
64
|
+
filename: options.deparser?.filename ?? defaultOptions.deparser.filename,
|
|
65
|
+
typesSource: options.deparser?.typesSource ?? defaultOptions.deparser.typesSource
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
}
|