bonescript-compiler 0.5.3 → 0.5.5
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 -21
- package/dist/algorithm_catalog.js +166 -166
- package/dist/cli.d.ts +1 -2
- package/dist/cli.js +617 -75
- package/dist/cli.js.map +1 -1
- package/dist/emit_capability.d.ts +0 -13
- package/dist/emit_capability.js +134 -296
- package/dist/emit_capability.js.map +1 -1
- package/dist/emit_composition.js +3 -37
- package/dist/emit_composition.js.map +1 -1
- package/dist/emit_deploy.js +167 -165
- package/dist/emit_deploy.js.map +1 -1
- package/dist/emit_events.d.ts +0 -1
- package/dist/emit_events.js +275 -325
- package/dist/emit_events.js.map +1 -1
- package/dist/emit_extras.js +5 -3
- package/dist/emit_extras.js.map +1 -1
- package/dist/emit_full.js +112 -272
- package/dist/emit_full.js.map +1 -1
- package/dist/emit_maintenance.js +249 -249
- package/dist/emit_nakama.d.ts +23 -0
- package/dist/emit_nakama.js +510 -0
- package/dist/emit_nakama.js.map +1 -0
- package/dist/emit_runtime.d.ts +11 -17
- package/dist/emit_runtime.js +688 -29
- package/dist/emit_runtime.js.map +1 -1
- package/dist/emit_sourcemap.js +66 -66
- package/dist/emit_tests.js +12 -47
- package/dist/emit_tests.js.map +1 -1
- package/dist/emit_websocket.js +3 -0
- package/dist/emit_websocket.js.map +1 -1
- package/dist/emitter.js +49 -94
- package/dist/emitter.js.map +1 -1
- package/dist/extension_manager.d.ts +2 -2
- package/dist/extension_manager.js +20 -9
- package/dist/extension_manager.js.map +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.js +3 -1
- package/dist/index.js.map +1 -1
- package/dist/ir.d.ts +0 -4
- package/dist/lowering.d.ts +14 -5
- package/dist/lowering.js +417 -66
- package/dist/lowering.js.map +1 -1
- package/dist/module_loader.d.ts +2 -2
- package/dist/module_loader.js +23 -20
- package/dist/module_loader.js.map +1 -1
- package/dist/optimizer.js +3 -6
- package/dist/optimizer.js.map +1 -1
- package/dist/scaffold.d.ts +2 -2
- package/dist/scaffold.js +319 -315
- package/dist/scaffold.js.map +1 -1
- package/dist/solver.js +1 -1
- package/dist/solver.js.map +1 -1
- package/dist/source_map.js.map +1 -0
- package/dist/test.js.map +1 -0
- package/dist/test_typechecker.d.ts +5 -0
- package/dist/test_typechecker.js +126 -0
- package/dist/test_typechecker.js.map +1 -0
- package/dist/typechecker.d.ts +0 -7
- package/dist/typechecker.js +16 -103
- package/dist/typechecker.js.map +1 -1
- package/dist/verifier.d.ts +1 -5
- package/dist/verifier.js +38 -142
- package/dist/verifier.js.map +1 -1
- package/package.json +53 -62
- package/src/algorithm_catalog.ts +345 -345
- package/src/ast.d.ts +244 -0
- package/src/ast.ts +334 -334
- package/src/cli.ts +704 -98
- package/src/emit_batch.ts +140 -140
- package/src/emit_capability.ts +436 -613
- package/src/emit_composition.ts +196 -229
- package/src/emit_deploy.ts +190 -187
- package/src/emit_events.ts +307 -362
- package/src/emit_extras.ts +240 -237
- package/src/emit_full.ts +309 -472
- package/src/emit_maintenance.ts +459 -459
- package/src/emit_nakama.ts +576 -0
- package/src/emit_runtime.ts +730 -17
- package/src/emit_sourcemap.ts +140 -140
- package/src/emit_tests.ts +205 -243
- package/src/emit_websocket.ts +229 -226
- package/src/emitter.ts +578 -626
- package/src/extension_manager.ts +187 -177
- package/src/formatter.ts +297 -297
- package/src/index.ts +90 -88
- package/src/ir.ts +215 -216
- package/src/lexer.d.ts +195 -0
- package/src/lexer.ts +630 -630
- package/src/lowering.ts +556 -168
- package/src/module_loader.ts +114 -112
- package/src/optimizer.ts +196 -199
- package/src/parse_decls.d.ts +13 -0
- package/src/parse_decls.ts +409 -409
- package/src/parse_decls2.d.ts +13 -0
- package/src/parse_decls2.ts +244 -244
- package/src/parse_expr.d.ts +7 -0
- package/src/parse_expr.ts +197 -197
- package/src/parse_types.d.ts +6 -0
- package/src/parse_types.ts +54 -54
- package/src/parser.d.ts +10 -0
- package/src/parser.ts +1 -1
- package/src/parser_base.d.ts +19 -0
- package/src/parser_base.ts +57 -57
- package/src/parser_recovery.ts +153 -153
- package/src/scaffold.ts +375 -371
- package/src/solver.ts +330 -330
- package/src/typechecker.d.ts +52 -0
- package/src/typechecker.ts +591 -700
- package/src/types.d.ts +38 -0
- package/src/types.ts +122 -122
- package/src/verifier.ts +49 -154
- package/README.md +0 -382
- package/dist/commands/check.d.ts +0 -5
- package/dist/commands/check.js +0 -34
- package/dist/commands/check.js.map +0 -1
- package/dist/commands/compile.d.ts +0 -5
- package/dist/commands/compile.js +0 -215
- package/dist/commands/compile.js.map +0 -1
- package/dist/commands/debug.d.ts +0 -5
- package/dist/commands/debug.js +0 -59
- package/dist/commands/debug.js.map +0 -1
- package/dist/commands/diff.d.ts +0 -5
- package/dist/commands/diff.js +0 -123
- package/dist/commands/diff.js.map +0 -1
- package/dist/commands/fmt.d.ts +0 -5
- package/dist/commands/fmt.js +0 -49
- package/dist/commands/fmt.js.map +0 -1
- package/dist/commands/init.d.ts +0 -5
- package/dist/commands/init.js +0 -96
- package/dist/commands/init.js.map +0 -1
- package/dist/commands/ir.d.ts +0 -5
- package/dist/commands/ir.js +0 -27
- package/dist/commands/ir.js.map +0 -1
- package/dist/commands/lex.d.ts +0 -5
- package/dist/commands/lex.js +0 -21
- package/dist/commands/lex.js.map +0 -1
- package/dist/commands/parse.d.ts +0 -5
- package/dist/commands/parse.js +0 -30
- package/dist/commands/parse.js.map +0 -1
- package/dist/commands/test.d.ts +0 -5
- package/dist/commands/test.js +0 -61
- package/dist/commands/test.js.map +0 -1
- package/dist/commands/verify_determinism.d.ts +0 -5
- package/dist/commands/verify_determinism.js +0 -64
- package/dist/commands/verify_determinism.js.map +0 -1
- package/dist/commands/watch.d.ts +0 -5
- package/dist/commands/watch.js +0 -50
- package/dist/commands/watch.js.map +0 -1
- package/dist/emit_auth.d.ts +0 -18
- package/dist/emit_auth.js +0 -507
- package/dist/emit_auth.js.map +0 -1
- package/dist/emit_database.d.ts +0 -7
- package/dist/emit_database.js +0 -72
- package/dist/emit_database.js.map +0 -1
- package/dist/emit_index.d.ts +0 -6
- package/dist/emit_index.js +0 -202
- package/dist/emit_index.js.map +0 -1
- package/dist/emit_models.d.ts +0 -12
- package/dist/emit_models.js +0 -171
- package/dist/emit_models.js.map +0 -1
- package/dist/emit_openapi.d.ts +0 -9
- package/dist/emit_openapi.js +0 -306
- package/dist/emit_openapi.js.map +0 -1
- package/dist/emit_package.d.ts +0 -7
- package/dist/emit_package.js +0 -68
- package/dist/emit_package.js.map +0 -1
- package/dist/emit_router.d.ts +0 -12
- package/dist/emit_router.js +0 -389
- package/dist/emit_router.js.map +0 -1
- package/dist/lowering_channels.d.ts +0 -11
- package/dist/lowering_channels.js +0 -103
- package/dist/lowering_channels.js.map +0 -1
- package/dist/lowering_entities.d.ts +0 -11
- package/dist/lowering_entities.js +0 -232
- package/dist/lowering_entities.js.map +0 -1
- package/dist/lowering_helpers.d.ts +0 -13
- package/dist/lowering_helpers.js +0 -76
- package/dist/lowering_helpers.js.map +0 -1
- package/src/commands/check.ts +0 -33
- package/src/commands/compile.ts +0 -191
- package/src/commands/debug.ts +0 -33
- package/src/commands/diff.ts +0 -105
- package/src/commands/fmt.ts +0 -22
- package/src/commands/init.ts +0 -72
- package/src/commands/ir.ts +0 -23
- package/src/commands/lex.ts +0 -17
- package/src/commands/parse.ts +0 -24
- package/src/commands/test.ts +0 -36
- package/src/commands/verify_determinism.ts +0 -66
- package/src/commands/watch.ts +0 -25
- package/src/emit_auth.ts +0 -513
- package/src/emit_database.ts +0 -72
- package/src/emit_index.ts +0 -210
- package/src/emit_models.ts +0 -176
- package/src/emit_openapi.ts +0 -315
- package/src/emit_package.ts +0 -66
- package/src/emit_router.ts +0 -408
- package/src/lowering_channels.ts +0 -108
- package/src/lowering_entities.ts +0 -258
- package/src/lowering_helpers.ts +0 -75
package/src/parse_decls.ts
CHANGED
|
@@ -1,409 +1,409 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* BoneScript Declaration Parsers
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
import { TokenKind } from "./lexer";
|
|
6
|
-
import { TokenStream, ParseError } from "./parser_base";
|
|
7
|
-
import * as AST from "./ast";
|
|
8
|
-
import { parseExpr, parseExprList } from "./parse_expr";
|
|
9
|
-
import { parseTypeExpr } from "./parse_types";
|
|
10
|
-
|
|
11
|
-
// ─── Shared Utilities ────────────────────────────────────────────────────────
|
|
12
|
-
|
|
13
|
-
/** Accept an identifier OR a keyword token as a name (for contexts where keywords are valid names) */
|
|
14
|
-
function parseIdentOrKeyword(s: TokenStream): string {
|
|
15
|
-
const tok = s.peek();
|
|
16
|
-
if (tok.kind === TokenKind.Identifier) return s.advance().value;
|
|
17
|
-
// Allow any keyword to be used as a name in parameter/field position
|
|
18
|
-
if (tok.value && /^[a-zA-Z_][a-zA-Z0-9_]*$/.test(tok.value)) return s.advance().value;
|
|
19
|
-
throw new ParseError(`Expected identifier, got ${tok.kind}`, tok.loc);
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
export function parseFieldList(s: TokenStream): AST.FieldNode[] {
|
|
23
|
-
const fields: AST.FieldNode[] = [];
|
|
24
|
-
if (s.check(TokenKind.RBracket) || s.check(TokenKind.RBrace)) return fields;
|
|
25
|
-
do { fields.push(parseField(s)); } while (s.match(TokenKind.Comma));
|
|
26
|
-
return fields;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
export function parseField(s: TokenStream): AST.FieldNode {
|
|
30
|
-
const loc = s.peek().loc;
|
|
31
|
-
const name = parseIdentOrKeyword(s);
|
|
32
|
-
s.expect(TokenKind.Colon, "field type");
|
|
33
|
-
const type = parseTypeExpr(s);
|
|
34
|
-
let defaultValue: AST.ExprNode | null = null;
|
|
35
|
-
if (s.match(TokenKind.Equals)) { defaultValue = parseExpr(s); }
|
|
36
|
-
return { kind: "Field", loc, name, type, defaultValue };
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
export function parseIdentList(s: TokenStream): string[] {
|
|
40
|
-
const ids: string[] = [];
|
|
41
|
-
ids.push(s.expect(TokenKind.Identifier, "identifier").value);
|
|
42
|
-
while (s.match(TokenKind.Comma)) {
|
|
43
|
-
ids.push(s.expect(TokenKind.Identifier, "identifier").value);
|
|
44
|
-
}
|
|
45
|
-
return ids;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
export function parseDuration(s: TokenStream): string {
|
|
49
|
-
const tok = s.peek();
|
|
50
|
-
if (tok.kind === TokenKind.IntLiteral || tok.kind === TokenKind.Identifier) {
|
|
51
|
-
return s.advance().value;
|
|
52
|
-
}
|
|
53
|
-
throw new ParseError(`Expected duration, got ${tok.kind}`, tok.loc);
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
function parseFieldRef(s: TokenStream): AST.FieldRefNode {
|
|
57
|
-
const loc = s.peek().loc;
|
|
58
|
-
const path: string[] = [];
|
|
59
|
-
// Accept keywords as field names too
|
|
60
|
-
path.push(parseIdentOrKeyword(s));
|
|
61
|
-
while (s.match(TokenKind.Dot)) {
|
|
62
|
-
const next = s.peek();
|
|
63
|
-
if (next.kind === TokenKind.Identifier || next.kind === TokenKind.KwUnique ||
|
|
64
|
-
(/^[a-zA-Z_][a-zA-Z0-9_]*$/.test(next.value) && next.kind !== TokenKind.EOF)) {
|
|
65
|
-
path.push(s.advance().value);
|
|
66
|
-
} else { break; }
|
|
67
|
-
}
|
|
68
|
-
return { kind: "FieldRef", loc, path };
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
// ─── Entity ──────────────────────────────────────────────────────────────────
|
|
72
|
-
|
|
73
|
-
export function parseEntityDecl(s: TokenStream): AST.EntityDeclNode {
|
|
74
|
-
const loc = s.peek().loc;
|
|
75
|
-
s.expect(TokenKind.KwEntity, "entity");
|
|
76
|
-
const name = s.expect(TokenKind.Identifier, "entity name").value;
|
|
77
|
-
s.expect(TokenKind.LBrace, "entity body");
|
|
78
|
-
|
|
79
|
-
const node: AST.EntityDeclNode = {
|
|
80
|
-
kind: "EntityDecl", loc, name,
|
|
81
|
-
owns: [], constraints: [], states: null, auth: null,
|
|
82
|
-
relations: [], indexes: [], derived: [],
|
|
83
|
-
};
|
|
84
|
-
|
|
85
|
-
while (!s.check(TokenKind.RBrace) && !s.check(TokenKind.EOF)) {
|
|
86
|
-
const tok = s.peek();
|
|
87
|
-
switch (tok.kind) {
|
|
88
|
-
case TokenKind.KwOwns:
|
|
89
|
-
s.advance(); s.expect(TokenKind.Colon, "owns");
|
|
90
|
-
s.expect(TokenKind.LBracket, "owns fields");
|
|
91
|
-
node.owns = parseFieldList(s);
|
|
92
|
-
s.expect(TokenKind.RBracket, "owns close");
|
|
93
|
-
break;
|
|
94
|
-
case TokenKind.KwConstraints:
|
|
95
|
-
s.advance(); s.expect(TokenKind.Colon, "constraints");
|
|
96
|
-
s.expect(TokenKind.LBracket, "constraints list");
|
|
97
|
-
node.constraints = parseExprList(s);
|
|
98
|
-
s.expect(TokenKind.RBracket, "constraints close");
|
|
99
|
-
break;
|
|
100
|
-
case TokenKind.KwStates:
|
|
101
|
-
s.advance(); s.expect(TokenKind.Colon, "states");
|
|
102
|
-
node.states = parseStateGraph(s);
|
|
103
|
-
break;
|
|
104
|
-
case TokenKind.KwAuth:
|
|
105
|
-
s.advance(); s.expect(TokenKind.Colon, "auth");
|
|
106
|
-
node.auth = parseAuthMethod(s);
|
|
107
|
-
break;
|
|
108
|
-
case TokenKind.KwIndex:
|
|
109
|
-
s.advance(); s.expect(TokenKind.Colon, "index");
|
|
110
|
-
s.expect(TokenKind.LBracket, "index list");
|
|
111
|
-
node.indexes.push(parseIdentList(s));
|
|
112
|
-
s.expect(TokenKind.RBracket, "index close");
|
|
113
|
-
break;
|
|
114
|
-
case TokenKind.KwRelation:
|
|
115
|
-
node.relations.push(parseRelation(s));
|
|
116
|
-
break;
|
|
117
|
-
case TokenKind.KwDerived:
|
|
118
|
-
node.derived.push(parseDerived(s));
|
|
119
|
-
break;
|
|
120
|
-
default:
|
|
121
|
-
throw new ParseError(`Unexpected in entity: ${tok.kind}`, tok.loc);
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
s.expect(TokenKind.RBrace, "entity close");
|
|
125
|
-
return node;
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
function parseAuthMethod(s: TokenStream): string {
|
|
129
|
-
const auths = [TokenKind.KwJwt, TokenKind.KwOauth2, TokenKind.KwApikey, TokenKind.KwSession, TokenKind.KwNone];
|
|
130
|
-
if (!auths.includes(s.peek().kind)) {
|
|
131
|
-
throw new ParseError(`Expected auth method, got ${s.peek().kind}`, s.peek().loc);
|
|
132
|
-
}
|
|
133
|
-
return s.advance().value;
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
function parseStateGraph(s: TokenStream): AST.StateGraphNode {
|
|
137
|
-
const loc = s.peek().loc;
|
|
138
|
-
const nodes: AST.StateNodeEntry[] = [];
|
|
139
|
-
let current: AST.StateNodeEntry = { name: s.expect(TokenKind.Identifier, "state").value, guard: null, transitions: [], branches: [] };
|
|
140
|
-
nodes.push(current);
|
|
141
|
-
|
|
142
|
-
while (s.check(TokenKind.Arrow) || s.check(TokenKind.Pipe)) {
|
|
143
|
-
const isArrow = s.check(TokenKind.Arrow);
|
|
144
|
-
s.advance();
|
|
145
|
-
const next: AST.StateNodeEntry = { name: s.expect(TokenKind.Identifier, "state").value, guard: null, transitions: [], branches: [] };
|
|
146
|
-
if (isArrow) { current.transitions.push(next.name); } else { current.branches.push(next.name); }
|
|
147
|
-
nodes.push(next);
|
|
148
|
-
if (isArrow) current = next;
|
|
149
|
-
}
|
|
150
|
-
return { kind: "StateGraph", loc, nodes };
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
function parseRelation(s: TokenStream): AST.RelationNode {
|
|
154
|
-
const loc = s.peek().loc;
|
|
155
|
-
s.expect(TokenKind.KwRelation, "relation");
|
|
156
|
-
const name = s.expect(TokenKind.Identifier, "relation name").value;
|
|
157
|
-
s.expect(TokenKind.Colon, "relation type");
|
|
158
|
-
const types = [TokenKind.KwHasOne, TokenKind.KwHasMany, TokenKind.KwBelongsTo, TokenKind.KwManyToMany];
|
|
159
|
-
if (!types.includes(s.peek().kind)) throw new ParseError(`Expected relation type`, s.peek().loc);
|
|
160
|
-
const relationType = s.advance().value as AST.RelationNode["relationType"];
|
|
161
|
-
const target = s.expect(TokenKind.Identifier, "relation target").value;
|
|
162
|
-
let cardinality: AST.RelationNode["cardinality"] = null;
|
|
163
|
-
if (s.match(TokenKind.LBracket)) {
|
|
164
|
-
const min = parseInt(s.expect(TokenKind.IntLiteral, "min").value, 10);
|
|
165
|
-
s.expect(TokenKind.DotDot, "..");
|
|
166
|
-
const max: number | "*" = s.peek().kind === TokenKind.Star ? (s.advance(), "*") : parseInt(s.expect(TokenKind.IntLiteral, "max").value, 10);
|
|
167
|
-
s.expect(TokenKind.RBracket, "]");
|
|
168
|
-
cardinality = { min, max };
|
|
169
|
-
}
|
|
170
|
-
return { kind: "Relation", loc, name, relationType, target, cardinality };
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
function parseDerived(s: TokenStream): AST.DerivedFieldNode {
|
|
174
|
-
const loc = s.peek().loc;
|
|
175
|
-
s.expect(TokenKind.KwDerived, "derived");
|
|
176
|
-
const name = s.expect(TokenKind.Identifier, "name").value;
|
|
177
|
-
s.expect(TokenKind.Colon, ":");
|
|
178
|
-
const expr = parseExpr(s);
|
|
179
|
-
return { kind: "DerivedField", loc, name, expr };
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
// ─── Capability ──────────────────────────────────────────────────────────────
|
|
183
|
-
|
|
184
|
-
export function parseCapabilityDecl(s: TokenStream): AST.CapabilityDeclNode {
|
|
185
|
-
const loc = s.peek().loc;
|
|
186
|
-
s.expect(TokenKind.KwCapability, "capability");
|
|
187
|
-
const name = s.expect(TokenKind.Identifier, "name").value;
|
|
188
|
-
s.expect(TokenKind.LParen, "(");
|
|
189
|
-
const params: AST.ParamNode[] = [];
|
|
190
|
-
if (!s.check(TokenKind.RParen)) {
|
|
191
|
-
do {
|
|
192
|
-
const ploc = s.peek().loc;
|
|
193
|
-
// Allow keywords as param names (e.g., "from", "to")
|
|
194
|
-
const pname = parseIdentOrKeyword(s);
|
|
195
|
-
s.expect(TokenKind.Colon, ":");
|
|
196
|
-
const ptype = parseTypeExpr(s);
|
|
197
|
-
params.push({ kind: "Param", loc: ploc, name: pname, type: ptype });
|
|
198
|
-
} while (s.match(TokenKind.Comma));
|
|
199
|
-
}
|
|
200
|
-
s.expect(TokenKind.RParen, ")");
|
|
201
|
-
s.expect(TokenKind.LBrace, "{");
|
|
202
|
-
|
|
203
|
-
const node: AST.CapabilityDeclNode = {
|
|
204
|
-
kind: "CapabilityDecl", loc, name, params,
|
|
205
|
-
requires: [], effects: [], emits: [],
|
|
206
|
-
sync: null, timeout: null, retry: null, idempotent: null,
|
|
207
|
-
pipeline: null, algorithm: null, returns: null,
|
|
208
|
-
};
|
|
209
|
-
|
|
210
|
-
while (!s.check(TokenKind.RBrace) && !s.check(TokenKind.EOF)) {
|
|
211
|
-
const tok = s.peek();
|
|
212
|
-
switch (tok.kind) {
|
|
213
|
-
case TokenKind.KwRequires:
|
|
214
|
-
s.advance(); s.expect(TokenKind.Colon, ":");
|
|
215
|
-
s.expect(TokenKind.LBracket, "[");
|
|
216
|
-
node.requires = parseExprList(s);
|
|
217
|
-
s.expect(TokenKind.RBracket, "]");
|
|
218
|
-
break;
|
|
219
|
-
case TokenKind.KwEffects:
|
|
220
|
-
s.advance(); s.expect(TokenKind.Colon, ":");
|
|
221
|
-
s.expect(TokenKind.LBracket, "[");
|
|
222
|
-
node.effects = parseEffectList(s);
|
|
223
|
-
s.expect(TokenKind.RBracket, "]");
|
|
224
|
-
break;
|
|
225
|
-
case TokenKind.KwEmits:
|
|
226
|
-
s.advance(); s.expect(TokenKind.Colon, ":");
|
|
227
|
-
node.emits = parseEmitList(s);
|
|
228
|
-
break;
|
|
229
|
-
case TokenKind.KwSync:
|
|
230
|
-
s.advance(); s.expect(TokenKind.Colon, ":");
|
|
231
|
-
node.sync = s.advance().value;
|
|
232
|
-
break;
|
|
233
|
-
case TokenKind.KwTimeout:
|
|
234
|
-
s.advance(); s.expect(TokenKind.Colon, ":");
|
|
235
|
-
node.timeout = parseDuration(s);
|
|
236
|
-
break;
|
|
237
|
-
case TokenKind.KwIdempotent:
|
|
238
|
-
s.advance(); s.expect(TokenKind.Colon, ":");
|
|
239
|
-
node.idempotent = s.advance().kind === TokenKind.KwTrue;
|
|
240
|
-
break;
|
|
241
|
-
case TokenKind.KwRetry:
|
|
242
|
-
s.advance(); s.expect(TokenKind.Colon, ":");
|
|
243
|
-
node.retry = parseRetryPolicy(s);
|
|
244
|
-
break;
|
|
245
|
-
case TokenKind.KwReturns:
|
|
246
|
-
s.advance(); s.expect(TokenKind.Colon, ":");
|
|
247
|
-
node.returns = parseTypeExpr(s);
|
|
248
|
-
break;
|
|
249
|
-
case TokenKind.KwPipeline:
|
|
250
|
-
node.pipeline = parsePipeline(s);
|
|
251
|
-
break;
|
|
252
|
-
case TokenKind.KwParallel:
|
|
253
|
-
node.pipeline = parsePipeline(s, true);
|
|
254
|
-
break;
|
|
255
|
-
case TokenKind.KwAlgorithm:
|
|
256
|
-
node.algorithm = parseAlgorithm(s);
|
|
257
|
-
break;
|
|
258
|
-
default:
|
|
259
|
-
throw new ParseError(`Unexpected in capability: ${tok.kind}`, tok.loc);
|
|
260
|
-
}
|
|
261
|
-
}
|
|
262
|
-
s.expect(TokenKind.RBrace, "}");
|
|
263
|
-
return node;
|
|
264
|
-
}
|
|
265
|
-
|
|
266
|
-
function parseEffectList(s: TokenStream): AST.EffectNode[] {
|
|
267
|
-
const effects: AST.EffectNode[] = [];
|
|
268
|
-
if (s.check(TokenKind.RBracket)) return effects;
|
|
269
|
-
do {
|
|
270
|
-
const loc = s.peek().loc;
|
|
271
|
-
const target = parseFieldRef(s);
|
|
272
|
-
const opTok = s.peek();
|
|
273
|
-
let op: "=" | "+=" | "-=";
|
|
274
|
-
if (opTok.kind === TokenKind.Equals) { op = "="; s.advance(); }
|
|
275
|
-
else if (opTok.kind === TokenKind.PlusEq) { op = "+="; s.advance(); }
|
|
276
|
-
else if (opTok.kind === TokenKind.MinusEq) { op = "-="; s.advance(); }
|
|
277
|
-
else throw new ParseError(`Expected =, +=, -= in effect`, opTok.loc);
|
|
278
|
-
const value = parseExpr(s);
|
|
279
|
-
effects.push({ kind: "Effect", loc, target, op, value });
|
|
280
|
-
} while (s.match(TokenKind.Comma));
|
|
281
|
-
return effects;
|
|
282
|
-
}
|
|
283
|
-
|
|
284
|
-
function parseEmitList(s: TokenStream): AST.EmitNode[] {
|
|
285
|
-
const emits: AST.EmitNode[] = [];
|
|
286
|
-
do {
|
|
287
|
-
const loc = s.peek().loc;
|
|
288
|
-
const eventName = s.expect(TokenKind.Identifier, "event name").value;
|
|
289
|
-
const args: AST.ExprNode[] = [];
|
|
290
|
-
if (s.match(TokenKind.LParen)) {
|
|
291
|
-
if (!s.check(TokenKind.RParen)) {
|
|
292
|
-
do { args.push(parseExpr(s)); } while (s.match(TokenKind.Comma));
|
|
293
|
-
}
|
|
294
|
-
s.expect(TokenKind.RParen, ")");
|
|
295
|
-
}
|
|
296
|
-
emits.push({ kind: "Emit", loc, eventName, args });
|
|
297
|
-
} while (s.match(TokenKind.Comma));
|
|
298
|
-
return emits;
|
|
299
|
-
}
|
|
300
|
-
|
|
301
|
-
function parseRetryPolicy(s: TokenStream): AST.RetryPolicyNode {
|
|
302
|
-
const loc = s.peek().loc;
|
|
303
|
-
s.expect(TokenKind.LBrace, "{");
|
|
304
|
-
const node: AST.RetryPolicyNode = { kind: "RetryPolicy", loc, maxAttempts: null, backoff: null, interval: null };
|
|
305
|
-
while (!s.check(TokenKind.RBrace)) {
|
|
306
|
-
const tok = s.peek();
|
|
307
|
-
if (tok.kind === TokenKind.KwMaxAttempts) { s.advance(); s.expect(TokenKind.Colon, ":"); node.maxAttempts = parseInt(s.expect(TokenKind.IntLiteral, "n").value, 10); }
|
|
308
|
-
else if (tok.kind === TokenKind.KwBackoff) { s.advance(); s.expect(TokenKind.Colon, ":"); node.backoff = s.advance().value; }
|
|
309
|
-
else if (tok.kind === TokenKind.KwInterval) { s.advance(); s.expect(TokenKind.Colon, ":"); node.interval = parseDuration(s); }
|
|
310
|
-
else throw new ParseError(`Unexpected in retry: ${tok.kind}`, tok.loc);
|
|
311
|
-
if (!s.match(TokenKind.Comma)) break;
|
|
312
|
-
}
|
|
313
|
-
s.expect(TokenKind.RBrace, "}");
|
|
314
|
-
return node;
|
|
315
|
-
}
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
// ─── Pipeline (Leap 1) ───────────────────────────────────────────────────────
|
|
319
|
-
|
|
320
|
-
export function parsePipeline(s: TokenStream, parallel: boolean = false): AST.PipelineNode {
|
|
321
|
-
const loc = s.peek().loc;
|
|
322
|
-
s.advance(); // consume pipeline or parallel
|
|
323
|
-
s.expect(TokenKind.Colon, ":");
|
|
324
|
-
s.expect(TokenKind.LBrace, "{");
|
|
325
|
-
|
|
326
|
-
const steps: AST.PipelineStepNode[] = [];
|
|
327
|
-
let onError: AST.PipelineErrorHandler | null = null;
|
|
328
|
-
|
|
329
|
-
while (!s.check(TokenKind.RBrace) && !s.check(TokenKind.EOF)) {
|
|
330
|
-
if (s.check(TokenKind.KwOnError)) {
|
|
331
|
-
onError = parsePipelineErrorHandler(s);
|
|
332
|
-
continue;
|
|
333
|
-
}
|
|
334
|
-
steps.push(parsePipelineStep(s));
|
|
335
|
-
}
|
|
336
|
-
|
|
337
|
-
s.expect(TokenKind.RBrace, "}");
|
|
338
|
-
return { kind: "Pipeline", loc, steps, parallel, onError };
|
|
339
|
-
}
|
|
340
|
-
|
|
341
|
-
function parsePipelineStep(s: TokenStream): AST.PipelineStepNode {
|
|
342
|
-
const loc = s.peek().loc;
|
|
343
|
-
const name = s.expect(TokenKind.Identifier, "step name").value;
|
|
344
|
-
s.expect(TokenKind.LParen, "(");
|
|
345
|
-
const args: AST.ExprNode[] = [];
|
|
346
|
-
if (!s.check(TokenKind.RParen)) {
|
|
347
|
-
do { args.push(parseExpr(s)); } while (s.match(TokenKind.Comma));
|
|
348
|
-
}
|
|
349
|
-
s.expect(TokenKind.RParen, ")");
|
|
350
|
-
|
|
351
|
-
let bindAs: string | null = null;
|
|
352
|
-
// Optional `as <name>` binding for capturing output
|
|
353
|
-
if (s.peek().kind === TokenKind.Identifier && s.peek().value === "as") {
|
|
354
|
-
s.advance();
|
|
355
|
-
bindAs = s.expect(TokenKind.Identifier, "binding name").value;
|
|
356
|
-
}
|
|
357
|
-
|
|
358
|
-
const call: AST.CallExprNode = { kind: "CallExpr", loc, name, args };
|
|
359
|
-
return { kind: "PipelineStep", loc, call, bindAs };
|
|
360
|
-
}
|
|
361
|
-
|
|
362
|
-
function parsePipelineErrorHandler(s: TokenStream): AST.PipelineErrorHandler {
|
|
363
|
-
s.expect(TokenKind.KwOnError, "on_error");
|
|
364
|
-
s.expect(TokenKind.Colon, ":");
|
|
365
|
-
const tok = s.peek();
|
|
366
|
-
const action = s.advance().value as AST.PipelineErrorHandler["action"];
|
|
367
|
-
|
|
368
|
-
let call: AST.CallExprNode | null = null;
|
|
369
|
-
// Optional call expression for compensate / retry
|
|
370
|
-
if (s.check(TokenKind.LParen) || (s.peek().kind === TokenKind.Identifier && s.peek(1).kind === TokenKind.LParen)) {
|
|
371
|
-
const callLoc = s.peek().loc;
|
|
372
|
-
if (s.peek().kind === TokenKind.Identifier) {
|
|
373
|
-
const name = s.advance().value;
|
|
374
|
-
s.expect(TokenKind.LParen, "(");
|
|
375
|
-
const args: AST.ExprNode[] = [];
|
|
376
|
-
if (!s.check(TokenKind.RParen)) {
|
|
377
|
-
do { args.push(parseExpr(s)); } while (s.match(TokenKind.Comma));
|
|
378
|
-
}
|
|
379
|
-
s.expect(TokenKind.RParen, ")");
|
|
380
|
-
call = { kind: "CallExpr", loc: callLoc, name, args };
|
|
381
|
-
}
|
|
382
|
-
}
|
|
383
|
-
|
|
384
|
-
return { kind: "PipelineErrorHandler", action, call };
|
|
385
|
-
}
|
|
386
|
-
|
|
387
|
-
// ─── Algorithm (Leap 2) ──────────────────────────────────────────────────────
|
|
388
|
-
|
|
389
|
-
export function parseAlgorithm(s: TokenStream): AST.AlgorithmNode {
|
|
390
|
-
const loc = s.peek().loc;
|
|
391
|
-
s.expect(TokenKind.KwAlgorithm, "algorithm");
|
|
392
|
-
s.expect(TokenKind.Colon, ":");
|
|
393
|
-
const name = s.expect(TokenKind.Identifier, "algorithm name").value;
|
|
394
|
-
const using: AST.AlgorithmBinding[] = [];
|
|
395
|
-
|
|
396
|
-
if (s.match(TokenKind.KwUsing)) {
|
|
397
|
-
s.expect(TokenKind.LBrace, "{");
|
|
398
|
-
while (!s.check(TokenKind.RBrace) && !s.check(TokenKind.EOF)) {
|
|
399
|
-
const param = s.expect(TokenKind.Identifier, "param name").value;
|
|
400
|
-
s.expect(TokenKind.Colon, ":");
|
|
401
|
-
const value = parseExpr(s);
|
|
402
|
-
using.push({ param, value });
|
|
403
|
-
if (!s.match(TokenKind.Comma)) break;
|
|
404
|
-
}
|
|
405
|
-
s.expect(TokenKind.RBrace, "}");
|
|
406
|
-
}
|
|
407
|
-
|
|
408
|
-
return { kind: "Algorithm", loc, name, using };
|
|
409
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* BoneScript Declaration Parsers
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { TokenKind } from "./lexer";
|
|
6
|
+
import { TokenStream, ParseError } from "./parser_base";
|
|
7
|
+
import * as AST from "./ast";
|
|
8
|
+
import { parseExpr, parseExprList } from "./parse_expr";
|
|
9
|
+
import { parseTypeExpr } from "./parse_types";
|
|
10
|
+
|
|
11
|
+
// ─── Shared Utilities ────────────────────────────────────────────────────────
|
|
12
|
+
|
|
13
|
+
/** Accept an identifier OR a keyword token as a name (for contexts where keywords are valid names) */
|
|
14
|
+
function parseIdentOrKeyword(s: TokenStream): string {
|
|
15
|
+
const tok = s.peek();
|
|
16
|
+
if (tok.kind === TokenKind.Identifier) return s.advance().value;
|
|
17
|
+
// Allow any keyword to be used as a name in parameter/field position
|
|
18
|
+
if (tok.value && /^[a-zA-Z_][a-zA-Z0-9_]*$/.test(tok.value)) return s.advance().value;
|
|
19
|
+
throw new ParseError(`Expected identifier, got ${tok.kind}`, tok.loc);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function parseFieldList(s: TokenStream): AST.FieldNode[] {
|
|
23
|
+
const fields: AST.FieldNode[] = [];
|
|
24
|
+
if (s.check(TokenKind.RBracket) || s.check(TokenKind.RBrace)) return fields;
|
|
25
|
+
do { fields.push(parseField(s)); } while (s.match(TokenKind.Comma));
|
|
26
|
+
return fields;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function parseField(s: TokenStream): AST.FieldNode {
|
|
30
|
+
const loc = s.peek().loc;
|
|
31
|
+
const name = parseIdentOrKeyword(s);
|
|
32
|
+
s.expect(TokenKind.Colon, "field type");
|
|
33
|
+
const type = parseTypeExpr(s);
|
|
34
|
+
let defaultValue: AST.ExprNode | null = null;
|
|
35
|
+
if (s.match(TokenKind.Equals)) { defaultValue = parseExpr(s); }
|
|
36
|
+
return { kind: "Field", loc, name, type, defaultValue };
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function parseIdentList(s: TokenStream): string[] {
|
|
40
|
+
const ids: string[] = [];
|
|
41
|
+
ids.push(s.expect(TokenKind.Identifier, "identifier").value);
|
|
42
|
+
while (s.match(TokenKind.Comma)) {
|
|
43
|
+
ids.push(s.expect(TokenKind.Identifier, "identifier").value);
|
|
44
|
+
}
|
|
45
|
+
return ids;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function parseDuration(s: TokenStream): string {
|
|
49
|
+
const tok = s.peek();
|
|
50
|
+
if (tok.kind === TokenKind.IntLiteral || tok.kind === TokenKind.Identifier) {
|
|
51
|
+
return s.advance().value;
|
|
52
|
+
}
|
|
53
|
+
throw new ParseError(`Expected duration, got ${tok.kind}`, tok.loc);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function parseFieldRef(s: TokenStream): AST.FieldRefNode {
|
|
57
|
+
const loc = s.peek().loc;
|
|
58
|
+
const path: string[] = [];
|
|
59
|
+
// Accept keywords as field names too
|
|
60
|
+
path.push(parseIdentOrKeyword(s));
|
|
61
|
+
while (s.match(TokenKind.Dot)) {
|
|
62
|
+
const next = s.peek();
|
|
63
|
+
if (next.kind === TokenKind.Identifier || next.kind === TokenKind.KwUnique ||
|
|
64
|
+
(/^[a-zA-Z_][a-zA-Z0-9_]*$/.test(next.value) && next.kind !== TokenKind.EOF)) {
|
|
65
|
+
path.push(s.advance().value);
|
|
66
|
+
} else { break; }
|
|
67
|
+
}
|
|
68
|
+
return { kind: "FieldRef", loc, path };
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// ─── Entity ──────────────────────────────────────────────────────────────────
|
|
72
|
+
|
|
73
|
+
export function parseEntityDecl(s: TokenStream): AST.EntityDeclNode {
|
|
74
|
+
const loc = s.peek().loc;
|
|
75
|
+
s.expect(TokenKind.KwEntity, "entity");
|
|
76
|
+
const name = s.expect(TokenKind.Identifier, "entity name").value;
|
|
77
|
+
s.expect(TokenKind.LBrace, "entity body");
|
|
78
|
+
|
|
79
|
+
const node: AST.EntityDeclNode = {
|
|
80
|
+
kind: "EntityDecl", loc, name,
|
|
81
|
+
owns: [], constraints: [], states: null, auth: null,
|
|
82
|
+
relations: [], indexes: [], derived: [],
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
while (!s.check(TokenKind.RBrace) && !s.check(TokenKind.EOF)) {
|
|
86
|
+
const tok = s.peek();
|
|
87
|
+
switch (tok.kind) {
|
|
88
|
+
case TokenKind.KwOwns:
|
|
89
|
+
s.advance(); s.expect(TokenKind.Colon, "owns");
|
|
90
|
+
s.expect(TokenKind.LBracket, "owns fields");
|
|
91
|
+
node.owns = parseFieldList(s);
|
|
92
|
+
s.expect(TokenKind.RBracket, "owns close");
|
|
93
|
+
break;
|
|
94
|
+
case TokenKind.KwConstraints:
|
|
95
|
+
s.advance(); s.expect(TokenKind.Colon, "constraints");
|
|
96
|
+
s.expect(TokenKind.LBracket, "constraints list");
|
|
97
|
+
node.constraints = parseExprList(s);
|
|
98
|
+
s.expect(TokenKind.RBracket, "constraints close");
|
|
99
|
+
break;
|
|
100
|
+
case TokenKind.KwStates:
|
|
101
|
+
s.advance(); s.expect(TokenKind.Colon, "states");
|
|
102
|
+
node.states = parseStateGraph(s);
|
|
103
|
+
break;
|
|
104
|
+
case TokenKind.KwAuth:
|
|
105
|
+
s.advance(); s.expect(TokenKind.Colon, "auth");
|
|
106
|
+
node.auth = parseAuthMethod(s);
|
|
107
|
+
break;
|
|
108
|
+
case TokenKind.KwIndex:
|
|
109
|
+
s.advance(); s.expect(TokenKind.Colon, "index");
|
|
110
|
+
s.expect(TokenKind.LBracket, "index list");
|
|
111
|
+
node.indexes.push(parseIdentList(s));
|
|
112
|
+
s.expect(TokenKind.RBracket, "index close");
|
|
113
|
+
break;
|
|
114
|
+
case TokenKind.KwRelation:
|
|
115
|
+
node.relations.push(parseRelation(s));
|
|
116
|
+
break;
|
|
117
|
+
case TokenKind.KwDerived:
|
|
118
|
+
node.derived.push(parseDerived(s));
|
|
119
|
+
break;
|
|
120
|
+
default:
|
|
121
|
+
throw new ParseError(`Unexpected in entity: ${tok.kind}`, tok.loc);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
s.expect(TokenKind.RBrace, "entity close");
|
|
125
|
+
return node;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
function parseAuthMethod(s: TokenStream): string {
|
|
129
|
+
const auths = [TokenKind.KwJwt, TokenKind.KwOauth2, TokenKind.KwApikey, TokenKind.KwSession, TokenKind.KwNone];
|
|
130
|
+
if (!auths.includes(s.peek().kind)) {
|
|
131
|
+
throw new ParseError(`Expected auth method, got ${s.peek().kind}`, s.peek().loc);
|
|
132
|
+
}
|
|
133
|
+
return s.advance().value;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
function parseStateGraph(s: TokenStream): AST.StateGraphNode {
|
|
137
|
+
const loc = s.peek().loc;
|
|
138
|
+
const nodes: AST.StateNodeEntry[] = [];
|
|
139
|
+
let current: AST.StateNodeEntry = { name: s.expect(TokenKind.Identifier, "state").value, guard: null, transitions: [], branches: [] };
|
|
140
|
+
nodes.push(current);
|
|
141
|
+
|
|
142
|
+
while (s.check(TokenKind.Arrow) || s.check(TokenKind.Pipe)) {
|
|
143
|
+
const isArrow = s.check(TokenKind.Arrow);
|
|
144
|
+
s.advance();
|
|
145
|
+
const next: AST.StateNodeEntry = { name: s.expect(TokenKind.Identifier, "state").value, guard: null, transitions: [], branches: [] };
|
|
146
|
+
if (isArrow) { current.transitions.push(next.name); } else { current.branches.push(next.name); }
|
|
147
|
+
nodes.push(next);
|
|
148
|
+
if (isArrow) current = next;
|
|
149
|
+
}
|
|
150
|
+
return { kind: "StateGraph", loc, nodes };
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
function parseRelation(s: TokenStream): AST.RelationNode {
|
|
154
|
+
const loc = s.peek().loc;
|
|
155
|
+
s.expect(TokenKind.KwRelation, "relation");
|
|
156
|
+
const name = s.expect(TokenKind.Identifier, "relation name").value;
|
|
157
|
+
s.expect(TokenKind.Colon, "relation type");
|
|
158
|
+
const types = [TokenKind.KwHasOne, TokenKind.KwHasMany, TokenKind.KwBelongsTo, TokenKind.KwManyToMany];
|
|
159
|
+
if (!types.includes(s.peek().kind)) throw new ParseError(`Expected relation type`, s.peek().loc);
|
|
160
|
+
const relationType = s.advance().value as AST.RelationNode["relationType"];
|
|
161
|
+
const target = s.expect(TokenKind.Identifier, "relation target").value;
|
|
162
|
+
let cardinality: AST.RelationNode["cardinality"] = null;
|
|
163
|
+
if (s.match(TokenKind.LBracket)) {
|
|
164
|
+
const min = parseInt(s.expect(TokenKind.IntLiteral, "min").value, 10);
|
|
165
|
+
s.expect(TokenKind.DotDot, "..");
|
|
166
|
+
const max: number | "*" = s.peek().kind === TokenKind.Star ? (s.advance(), "*") : parseInt(s.expect(TokenKind.IntLiteral, "max").value, 10);
|
|
167
|
+
s.expect(TokenKind.RBracket, "]");
|
|
168
|
+
cardinality = { min, max };
|
|
169
|
+
}
|
|
170
|
+
return { kind: "Relation", loc, name, relationType, target, cardinality };
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
function parseDerived(s: TokenStream): AST.DerivedFieldNode {
|
|
174
|
+
const loc = s.peek().loc;
|
|
175
|
+
s.expect(TokenKind.KwDerived, "derived");
|
|
176
|
+
const name = s.expect(TokenKind.Identifier, "name").value;
|
|
177
|
+
s.expect(TokenKind.Colon, ":");
|
|
178
|
+
const expr = parseExpr(s);
|
|
179
|
+
return { kind: "DerivedField", loc, name, expr };
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
// ─── Capability ──────────────────────────────────────────────────────────────
|
|
183
|
+
|
|
184
|
+
export function parseCapabilityDecl(s: TokenStream): AST.CapabilityDeclNode {
|
|
185
|
+
const loc = s.peek().loc;
|
|
186
|
+
s.expect(TokenKind.KwCapability, "capability");
|
|
187
|
+
const name = s.expect(TokenKind.Identifier, "name").value;
|
|
188
|
+
s.expect(TokenKind.LParen, "(");
|
|
189
|
+
const params: AST.ParamNode[] = [];
|
|
190
|
+
if (!s.check(TokenKind.RParen)) {
|
|
191
|
+
do {
|
|
192
|
+
const ploc = s.peek().loc;
|
|
193
|
+
// Allow keywords as param names (e.g., "from", "to")
|
|
194
|
+
const pname = parseIdentOrKeyword(s);
|
|
195
|
+
s.expect(TokenKind.Colon, ":");
|
|
196
|
+
const ptype = parseTypeExpr(s);
|
|
197
|
+
params.push({ kind: "Param", loc: ploc, name: pname, type: ptype });
|
|
198
|
+
} while (s.match(TokenKind.Comma));
|
|
199
|
+
}
|
|
200
|
+
s.expect(TokenKind.RParen, ")");
|
|
201
|
+
s.expect(TokenKind.LBrace, "{");
|
|
202
|
+
|
|
203
|
+
const node: AST.CapabilityDeclNode = {
|
|
204
|
+
kind: "CapabilityDecl", loc, name, params,
|
|
205
|
+
requires: [], effects: [], emits: [],
|
|
206
|
+
sync: null, timeout: null, retry: null, idempotent: null,
|
|
207
|
+
pipeline: null, algorithm: null, returns: null,
|
|
208
|
+
};
|
|
209
|
+
|
|
210
|
+
while (!s.check(TokenKind.RBrace) && !s.check(TokenKind.EOF)) {
|
|
211
|
+
const tok = s.peek();
|
|
212
|
+
switch (tok.kind) {
|
|
213
|
+
case TokenKind.KwRequires:
|
|
214
|
+
s.advance(); s.expect(TokenKind.Colon, ":");
|
|
215
|
+
s.expect(TokenKind.LBracket, "[");
|
|
216
|
+
node.requires = parseExprList(s);
|
|
217
|
+
s.expect(TokenKind.RBracket, "]");
|
|
218
|
+
break;
|
|
219
|
+
case TokenKind.KwEffects:
|
|
220
|
+
s.advance(); s.expect(TokenKind.Colon, ":");
|
|
221
|
+
s.expect(TokenKind.LBracket, "[");
|
|
222
|
+
node.effects = parseEffectList(s);
|
|
223
|
+
s.expect(TokenKind.RBracket, "]");
|
|
224
|
+
break;
|
|
225
|
+
case TokenKind.KwEmits:
|
|
226
|
+
s.advance(); s.expect(TokenKind.Colon, ":");
|
|
227
|
+
node.emits = parseEmitList(s);
|
|
228
|
+
break;
|
|
229
|
+
case TokenKind.KwSync:
|
|
230
|
+
s.advance(); s.expect(TokenKind.Colon, ":");
|
|
231
|
+
node.sync = s.advance().value;
|
|
232
|
+
break;
|
|
233
|
+
case TokenKind.KwTimeout:
|
|
234
|
+
s.advance(); s.expect(TokenKind.Colon, ":");
|
|
235
|
+
node.timeout = parseDuration(s);
|
|
236
|
+
break;
|
|
237
|
+
case TokenKind.KwIdempotent:
|
|
238
|
+
s.advance(); s.expect(TokenKind.Colon, ":");
|
|
239
|
+
node.idempotent = s.advance().kind === TokenKind.KwTrue;
|
|
240
|
+
break;
|
|
241
|
+
case TokenKind.KwRetry:
|
|
242
|
+
s.advance(); s.expect(TokenKind.Colon, ":");
|
|
243
|
+
node.retry = parseRetryPolicy(s);
|
|
244
|
+
break;
|
|
245
|
+
case TokenKind.KwReturns:
|
|
246
|
+
s.advance(); s.expect(TokenKind.Colon, ":");
|
|
247
|
+
node.returns = parseTypeExpr(s);
|
|
248
|
+
break;
|
|
249
|
+
case TokenKind.KwPipeline:
|
|
250
|
+
node.pipeline = parsePipeline(s);
|
|
251
|
+
break;
|
|
252
|
+
case TokenKind.KwParallel:
|
|
253
|
+
node.pipeline = parsePipeline(s, true);
|
|
254
|
+
break;
|
|
255
|
+
case TokenKind.KwAlgorithm:
|
|
256
|
+
node.algorithm = parseAlgorithm(s);
|
|
257
|
+
break;
|
|
258
|
+
default:
|
|
259
|
+
throw new ParseError(`Unexpected in capability: ${tok.kind}`, tok.loc);
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
s.expect(TokenKind.RBrace, "}");
|
|
263
|
+
return node;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
function parseEffectList(s: TokenStream): AST.EffectNode[] {
|
|
267
|
+
const effects: AST.EffectNode[] = [];
|
|
268
|
+
if (s.check(TokenKind.RBracket)) return effects;
|
|
269
|
+
do {
|
|
270
|
+
const loc = s.peek().loc;
|
|
271
|
+
const target = parseFieldRef(s);
|
|
272
|
+
const opTok = s.peek();
|
|
273
|
+
let op: "=" | "+=" | "-=";
|
|
274
|
+
if (opTok.kind === TokenKind.Equals) { op = "="; s.advance(); }
|
|
275
|
+
else if (opTok.kind === TokenKind.PlusEq) { op = "+="; s.advance(); }
|
|
276
|
+
else if (opTok.kind === TokenKind.MinusEq) { op = "-="; s.advance(); }
|
|
277
|
+
else throw new ParseError(`Expected =, +=, -= in effect`, opTok.loc);
|
|
278
|
+
const value = parseExpr(s);
|
|
279
|
+
effects.push({ kind: "Effect", loc, target, op, value });
|
|
280
|
+
} while (s.match(TokenKind.Comma));
|
|
281
|
+
return effects;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
function parseEmitList(s: TokenStream): AST.EmitNode[] {
|
|
285
|
+
const emits: AST.EmitNode[] = [];
|
|
286
|
+
do {
|
|
287
|
+
const loc = s.peek().loc;
|
|
288
|
+
const eventName = s.expect(TokenKind.Identifier, "event name").value;
|
|
289
|
+
const args: AST.ExprNode[] = [];
|
|
290
|
+
if (s.match(TokenKind.LParen)) {
|
|
291
|
+
if (!s.check(TokenKind.RParen)) {
|
|
292
|
+
do { args.push(parseExpr(s)); } while (s.match(TokenKind.Comma));
|
|
293
|
+
}
|
|
294
|
+
s.expect(TokenKind.RParen, ")");
|
|
295
|
+
}
|
|
296
|
+
emits.push({ kind: "Emit", loc, eventName, args });
|
|
297
|
+
} while (s.match(TokenKind.Comma));
|
|
298
|
+
return emits;
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
function parseRetryPolicy(s: TokenStream): AST.RetryPolicyNode {
|
|
302
|
+
const loc = s.peek().loc;
|
|
303
|
+
s.expect(TokenKind.LBrace, "{");
|
|
304
|
+
const node: AST.RetryPolicyNode = { kind: "RetryPolicy", loc, maxAttempts: null, backoff: null, interval: null };
|
|
305
|
+
while (!s.check(TokenKind.RBrace)) {
|
|
306
|
+
const tok = s.peek();
|
|
307
|
+
if (tok.kind === TokenKind.KwMaxAttempts) { s.advance(); s.expect(TokenKind.Colon, ":"); node.maxAttempts = parseInt(s.expect(TokenKind.IntLiteral, "n").value, 10); }
|
|
308
|
+
else if (tok.kind === TokenKind.KwBackoff) { s.advance(); s.expect(TokenKind.Colon, ":"); node.backoff = s.advance().value; }
|
|
309
|
+
else if (tok.kind === TokenKind.KwInterval) { s.advance(); s.expect(TokenKind.Colon, ":"); node.interval = parseDuration(s); }
|
|
310
|
+
else throw new ParseError(`Unexpected in retry: ${tok.kind}`, tok.loc);
|
|
311
|
+
if (!s.match(TokenKind.Comma)) break;
|
|
312
|
+
}
|
|
313
|
+
s.expect(TokenKind.RBrace, "}");
|
|
314
|
+
return node;
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
|
|
318
|
+
// ─── Pipeline (Leap 1) ───────────────────────────────────────────────────────
|
|
319
|
+
|
|
320
|
+
export function parsePipeline(s: TokenStream, parallel: boolean = false): AST.PipelineNode {
|
|
321
|
+
const loc = s.peek().loc;
|
|
322
|
+
s.advance(); // consume pipeline or parallel
|
|
323
|
+
s.expect(TokenKind.Colon, ":");
|
|
324
|
+
s.expect(TokenKind.LBrace, "{");
|
|
325
|
+
|
|
326
|
+
const steps: AST.PipelineStepNode[] = [];
|
|
327
|
+
let onError: AST.PipelineErrorHandler | null = null;
|
|
328
|
+
|
|
329
|
+
while (!s.check(TokenKind.RBrace) && !s.check(TokenKind.EOF)) {
|
|
330
|
+
if (s.check(TokenKind.KwOnError)) {
|
|
331
|
+
onError = parsePipelineErrorHandler(s);
|
|
332
|
+
continue;
|
|
333
|
+
}
|
|
334
|
+
steps.push(parsePipelineStep(s));
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
s.expect(TokenKind.RBrace, "}");
|
|
338
|
+
return { kind: "Pipeline", loc, steps, parallel, onError };
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
function parsePipelineStep(s: TokenStream): AST.PipelineStepNode {
|
|
342
|
+
const loc = s.peek().loc;
|
|
343
|
+
const name = s.expect(TokenKind.Identifier, "step name").value;
|
|
344
|
+
s.expect(TokenKind.LParen, "(");
|
|
345
|
+
const args: AST.ExprNode[] = [];
|
|
346
|
+
if (!s.check(TokenKind.RParen)) {
|
|
347
|
+
do { args.push(parseExpr(s)); } while (s.match(TokenKind.Comma));
|
|
348
|
+
}
|
|
349
|
+
s.expect(TokenKind.RParen, ")");
|
|
350
|
+
|
|
351
|
+
let bindAs: string | null = null;
|
|
352
|
+
// Optional `as <name>` binding for capturing output
|
|
353
|
+
if (s.peek().kind === TokenKind.Identifier && s.peek().value === "as") {
|
|
354
|
+
s.advance();
|
|
355
|
+
bindAs = s.expect(TokenKind.Identifier, "binding name").value;
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
const call: AST.CallExprNode = { kind: "CallExpr", loc, name, args };
|
|
359
|
+
return { kind: "PipelineStep", loc, call, bindAs };
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
function parsePipelineErrorHandler(s: TokenStream): AST.PipelineErrorHandler {
|
|
363
|
+
s.expect(TokenKind.KwOnError, "on_error");
|
|
364
|
+
s.expect(TokenKind.Colon, ":");
|
|
365
|
+
const tok = s.peek();
|
|
366
|
+
const action = s.advance().value as AST.PipelineErrorHandler["action"];
|
|
367
|
+
|
|
368
|
+
let call: AST.CallExprNode | null = null;
|
|
369
|
+
// Optional call expression for compensate / retry
|
|
370
|
+
if (s.check(TokenKind.LParen) || (s.peek().kind === TokenKind.Identifier && s.peek(1).kind === TokenKind.LParen)) {
|
|
371
|
+
const callLoc = s.peek().loc;
|
|
372
|
+
if (s.peek().kind === TokenKind.Identifier) {
|
|
373
|
+
const name = s.advance().value;
|
|
374
|
+
s.expect(TokenKind.LParen, "(");
|
|
375
|
+
const args: AST.ExprNode[] = [];
|
|
376
|
+
if (!s.check(TokenKind.RParen)) {
|
|
377
|
+
do { args.push(parseExpr(s)); } while (s.match(TokenKind.Comma));
|
|
378
|
+
}
|
|
379
|
+
s.expect(TokenKind.RParen, ")");
|
|
380
|
+
call = { kind: "CallExpr", loc: callLoc, name, args };
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
return { kind: "PipelineErrorHandler", action, call };
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
// ─── Algorithm (Leap 2) ──────────────────────────────────────────────────────
|
|
388
|
+
|
|
389
|
+
export function parseAlgorithm(s: TokenStream): AST.AlgorithmNode {
|
|
390
|
+
const loc = s.peek().loc;
|
|
391
|
+
s.expect(TokenKind.KwAlgorithm, "algorithm");
|
|
392
|
+
s.expect(TokenKind.Colon, ":");
|
|
393
|
+
const name = s.expect(TokenKind.Identifier, "algorithm name").value;
|
|
394
|
+
const using: AST.AlgorithmBinding[] = [];
|
|
395
|
+
|
|
396
|
+
if (s.match(TokenKind.KwUsing)) {
|
|
397
|
+
s.expect(TokenKind.LBrace, "{");
|
|
398
|
+
while (!s.check(TokenKind.RBrace) && !s.check(TokenKind.EOF)) {
|
|
399
|
+
const param = s.expect(TokenKind.Identifier, "param name").value;
|
|
400
|
+
s.expect(TokenKind.Colon, ":");
|
|
401
|
+
const value = parseExpr(s);
|
|
402
|
+
using.push({ param, value });
|
|
403
|
+
if (!s.match(TokenKind.Comma)) break;
|
|
404
|
+
}
|
|
405
|
+
s.expect(TokenKind.RBrace, "}");
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
return { kind: "Algorithm", loc, name, using };
|
|
409
|
+
}
|