Skip to content

Commit a268c09

Browse files
committed
feat(cel-proto-parser): add CEL proto parser and deparser package
- Add new cel-proto-parser package for working with CEL ASTs - Implement CelProtoParser class to parse CEL proto files and generate TypeScript types - Implement deparser to convert CEL AST back to expression strings - Support all CEL expression types: constants, identifiers, field access, function calls, operators, ternary, lists, maps, message construction, comprehensions - Add AST helper generation for constructing CEL AST nodes - Include comprehensive test suite with 34 passing tests - Include CEL syntax.proto fixture from official cel-spec
1 parent 882d5e9 commit a268c09

19 files changed

Lines changed: 2581 additions & 0 deletions

File tree

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
# cel-proto-parser
2+
3+
CEL (Common Expression Language) proto parser and deparser for TypeScript.
4+
5+
## Overview
6+
7+
This package provides tools for working with CEL (Common Expression Language) Abstract Syntax Trees (ASTs):
8+
9+
1. **Proto Parser**: Parse CEL protobuf definitions to generate TypeScript types
10+
2. **Deparser**: Convert CEL AST back to CEL expression strings
11+
3. **AST Helpers**: Factory functions for constructing CEL AST nodes
12+
13+
## Installation
14+
15+
```bash
16+
npm install cel-proto-parser
17+
```
18+
19+
## Usage
20+
21+
### Deparser
22+
23+
Convert CEL AST to expression strings:
24+
25+
```typescript
26+
import { deparse, Expr } from 'cel-proto-parser';
27+
28+
// Simple expression: x > 5
29+
const expr: Expr = {
30+
callExpr: {
31+
function: '_>_',
32+
args: [
33+
{ identExpr: { name: 'x' } },
34+
{ constExpr: { int64Value: 5 } }
35+
]
36+
}
37+
};
38+
39+
console.log(deparse(expr)); // Output: "x > 5"
40+
```
41+
42+
### Proto Parser
43+
44+
Generate TypeScript types from CEL proto files:
45+
46+
```typescript
47+
import { CelProtoParser } from 'cel-proto-parser';
48+
49+
const parser = new CelProtoParser('path/to/syntax.proto', {
50+
outDir: './generated',
51+
types: { enabled: true },
52+
enums: { enabled: true },
53+
utils: { astHelpers: { enabled: true } },
54+
deparser: { enabled: true }
55+
});
56+
57+
parser.write();
58+
```
59+
60+
## CEL Expression Types
61+
62+
The deparser supports all CEL expression types:
63+
64+
- **Constants**: `null`, `true`, `false`, integers, floats, strings, bytes
65+
- **Identifiers**: `request`, `user`, etc.
66+
- **Field Access**: `request.auth.claims`
67+
- **Function Calls**: `size(list)`, `str.startsWith("prefix")`
68+
- **Operators**: `+`, `-`, `*`, `/`, `%`, `==`, `!=`, `<`, `<=`, `>`, `>=`, `&&`, `||`, `!`, `-` (unary)
69+
- **Ternary**: `condition ? trueExpr : falseExpr`
70+
- **Index Access**: `list[0]`, `map["key"]`
71+
- **Lists**: `[1, 2, 3]`
72+
- **Maps**: `{"key": value}`
73+
- **Message Construction**: `MyMessage{field: value}`
74+
- **Macros**: `has()`, `all()`, `exists()`, etc.
75+
76+
## API Reference
77+
78+
### `deparse(expr: Expr, options?: DeparserOptions): string`
79+
80+
Converts a CEL AST expression to a string.
81+
82+
Options:
83+
- `spaces`: Whether to add spaces around operators (default: `true`)
84+
85+
### `CelProtoParser`
86+
87+
Parses CEL proto files and generates TypeScript code.
88+
89+
Constructor options:
90+
- `outDir`: Output directory for generated files
91+
- `types.enabled`: Generate TypeScript interfaces
92+
- `enums.enabled`: Generate TypeScript enums
93+
- `utils.astHelpers.enabled`: Generate AST helper functions
94+
- `deparser.enabled`: Generate deparser module
95+
96+
## License
97+
98+
MIT

0 commit comments

Comments
 (0)