bonescript-compiler 0.2.1 → 0.3.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.
Files changed (146) hide show
  1. package/LICENSE +21 -21
  2. package/dist/algorithm_catalog.js +166 -166
  3. package/dist/cli.d.ts +2 -1
  4. package/dist/cli.js +75 -543
  5. package/dist/cli.js.map +1 -1
  6. package/dist/commands/check.d.ts +5 -0
  7. package/dist/commands/check.js +34 -0
  8. package/dist/commands/check.js.map +1 -0
  9. package/dist/commands/compile.d.ts +5 -0
  10. package/dist/commands/compile.js +183 -0
  11. package/dist/commands/compile.js.map +1 -0
  12. package/dist/commands/debug.d.ts +5 -0
  13. package/dist/commands/debug.js +59 -0
  14. package/dist/commands/debug.js.map +1 -0
  15. package/dist/commands/diff.d.ts +5 -0
  16. package/dist/commands/diff.js +125 -0
  17. package/dist/commands/diff.js.map +1 -0
  18. package/dist/commands/fmt.d.ts +5 -0
  19. package/dist/commands/fmt.js +49 -0
  20. package/dist/commands/fmt.js.map +1 -0
  21. package/dist/commands/init.d.ts +5 -0
  22. package/dist/commands/init.js +69 -0
  23. package/dist/commands/init.js.map +1 -0
  24. package/dist/commands/ir.d.ts +5 -0
  25. package/dist/commands/ir.js +27 -0
  26. package/dist/commands/ir.js.map +1 -0
  27. package/dist/commands/lex.d.ts +5 -0
  28. package/dist/commands/lex.js +21 -0
  29. package/dist/commands/lex.js.map +1 -0
  30. package/dist/commands/parse.d.ts +5 -0
  31. package/dist/commands/parse.js +30 -0
  32. package/dist/commands/parse.js.map +1 -0
  33. package/dist/commands/test.d.ts +5 -0
  34. package/dist/commands/test.js +61 -0
  35. package/dist/commands/test.js.map +1 -0
  36. package/dist/commands/verify_determinism.d.ts +5 -0
  37. package/dist/commands/verify_determinism.js +64 -0
  38. package/dist/commands/verify_determinism.js.map +1 -0
  39. package/dist/commands/watch.d.ts +5 -0
  40. package/dist/commands/watch.js +50 -0
  41. package/dist/commands/watch.js.map +1 -0
  42. package/dist/emit_auth.d.ts +6 -0
  43. package/dist/emit_auth.js +69 -0
  44. package/dist/emit_auth.js.map +1 -0
  45. package/dist/emit_capability.d.ts +13 -0
  46. package/dist/emit_capability.js +235 -125
  47. package/dist/emit_capability.js.map +1 -1
  48. package/dist/emit_database.d.ts +7 -0
  49. package/dist/emit_database.js +74 -0
  50. package/dist/emit_database.js.map +1 -0
  51. package/dist/emit_deploy.js +162 -162
  52. package/dist/emit_events.js +274 -274
  53. package/dist/emit_full.js +102 -95
  54. package/dist/emit_full.js.map +1 -1
  55. package/dist/emit_index.d.ts +6 -0
  56. package/dist/emit_index.js +157 -0
  57. package/dist/emit_index.js.map +1 -0
  58. package/dist/emit_maintenance.js +249 -249
  59. package/dist/emit_package.d.ts +7 -0
  60. package/dist/emit_package.js +70 -0
  61. package/dist/emit_package.js.map +1 -0
  62. package/dist/emit_router.d.ts +12 -0
  63. package/dist/emit_router.js +375 -0
  64. package/dist/emit_router.js.map +1 -0
  65. package/dist/emit_runtime.d.ts +17 -11
  66. package/dist/emit_runtime.js +29 -686
  67. package/dist/emit_runtime.js.map +1 -1
  68. package/dist/emit_sourcemap.js +66 -66
  69. package/dist/extension_manager.d.ts +2 -2
  70. package/dist/extension_manager.js +6 -3
  71. package/dist/extension_manager.js.map +1 -1
  72. package/dist/lowering.d.ts +5 -14
  73. package/dist/lowering.js +32 -417
  74. package/dist/lowering.js.map +1 -1
  75. package/dist/lowering_channels.d.ts +11 -0
  76. package/dist/lowering_channels.js +102 -0
  77. package/dist/lowering_channels.js.map +1 -0
  78. package/dist/lowering_entities.d.ts +11 -0
  79. package/dist/lowering_entities.js +222 -0
  80. package/dist/lowering_entities.js.map +1 -0
  81. package/dist/lowering_helpers.d.ts +13 -0
  82. package/dist/lowering_helpers.js +76 -0
  83. package/dist/lowering_helpers.js.map +1 -0
  84. package/dist/module_loader.d.ts +2 -2
  85. package/dist/module_loader.js +20 -23
  86. package/dist/module_loader.js.map +1 -1
  87. package/dist/scaffold.d.ts +2 -2
  88. package/dist/scaffold.js +316 -319
  89. package/dist/scaffold.js.map +1 -1
  90. package/package.json +62 -52
  91. package/src/algorithm_catalog.ts +345 -345
  92. package/src/ast.ts +334 -334
  93. package/src/cli.ts +98 -624
  94. package/src/commands/check.ts +33 -0
  95. package/src/commands/compile.ts +160 -0
  96. package/src/commands/debug.ts +33 -0
  97. package/src/commands/diff.ts +108 -0
  98. package/src/commands/fmt.ts +22 -0
  99. package/src/commands/init.ts +46 -0
  100. package/src/commands/ir.ts +23 -0
  101. package/src/commands/lex.ts +17 -0
  102. package/src/commands/parse.ts +24 -0
  103. package/src/commands/test.ts +36 -0
  104. package/src/commands/verify_determinism.ts +66 -0
  105. package/src/commands/watch.ts +25 -0
  106. package/src/emit_auth.ts +67 -0
  107. package/src/emit_batch.ts +140 -140
  108. package/src/emit_capability.ts +562 -436
  109. package/src/emit_composition.ts +196 -196
  110. package/src/emit_database.ts +75 -0
  111. package/src/emit_deploy.ts +190 -190
  112. package/src/emit_events.ts +307 -307
  113. package/src/emit_extras.ts +240 -240
  114. package/src/emit_full.ts +316 -309
  115. package/src/emit_index.ts +161 -0
  116. package/src/emit_maintenance.ts +459 -459
  117. package/src/emit_package.ts +69 -0
  118. package/src/emit_router.ts +395 -0
  119. package/src/emit_runtime.ts +17 -728
  120. package/src/emit_sourcemap.ts +140 -140
  121. package/src/emit_tests.ts +205 -205
  122. package/src/emit_websocket.ts +229 -229
  123. package/src/emitter.ts +566 -566
  124. package/src/extension_manager.ts +189 -187
  125. package/src/formatter.ts +297 -297
  126. package/src/index.ts +88 -88
  127. package/src/ir.ts +215 -215
  128. package/src/lexer.ts +630 -630
  129. package/src/lowering.ts +124 -556
  130. package/src/lowering_channels.ts +107 -0
  131. package/src/lowering_entities.ts +248 -0
  132. package/src/lowering_helpers.ts +75 -0
  133. package/src/module_loader.ts +112 -114
  134. package/src/optimizer.ts +196 -196
  135. package/src/parse_decls.ts +409 -409
  136. package/src/parse_decls2.ts +244 -244
  137. package/src/parse_expr.ts +197 -197
  138. package/src/parse_types.ts +54 -54
  139. package/src/parser.ts +1 -1
  140. package/src/parser_base.ts +57 -57
  141. package/src/parser_recovery.ts +153 -153
  142. package/src/scaffold.ts +372 -375
  143. package/src/solver.ts +330 -330
  144. package/src/typechecker.ts +591 -591
  145. package/src/types.ts +122 -122
  146. package/src/verifier.ts +348 -348
@@ -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
+ }