bonescript-compiler 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.
Files changed (146) hide show
  1. package/LICENSE +21 -0
  2. package/dist/algorithm_catalog.d.ts +32 -0
  3. package/dist/algorithm_catalog.js +323 -0
  4. package/dist/algorithm_catalog.js.map +1 -0
  5. package/dist/ast.d.ts +244 -0
  6. package/dist/ast.js +8 -0
  7. package/dist/ast.js.map +1 -0
  8. package/dist/cli.d.ts +4 -0
  9. package/dist/cli.js +605 -0
  10. package/dist/cli.js.map +1 -0
  11. package/dist/emit_batch.d.ts +7 -0
  12. package/dist/emit_batch.js +133 -0
  13. package/dist/emit_batch.js.map +1 -0
  14. package/dist/emit_capability.d.ts +7 -0
  15. package/dist/emit_capability.js +376 -0
  16. package/dist/emit_capability.js.map +1 -0
  17. package/dist/emit_composition.d.ts +22 -0
  18. package/dist/emit_composition.js +184 -0
  19. package/dist/emit_composition.js.map +1 -0
  20. package/dist/emit_deploy.d.ts +9 -0
  21. package/dist/emit_deploy.js +191 -0
  22. package/dist/emit_deploy.js.map +1 -0
  23. package/dist/emit_events.d.ts +14 -0
  24. package/dist/emit_events.js +305 -0
  25. package/dist/emit_events.js.map +1 -0
  26. package/dist/emit_extras.d.ts +12 -0
  27. package/dist/emit_extras.js +234 -0
  28. package/dist/emit_extras.js.map +1 -0
  29. package/dist/emit_full.d.ts +13 -0
  30. package/dist/emit_full.js +273 -0
  31. package/dist/emit_full.js.map +1 -0
  32. package/dist/emit_maintenance.d.ts +16 -0
  33. package/dist/emit_maintenance.js +442 -0
  34. package/dist/emit_maintenance.js.map +1 -0
  35. package/dist/emit_runtime.d.ts +13 -0
  36. package/dist/emit_runtime.js +691 -0
  37. package/dist/emit_runtime.js.map +1 -0
  38. package/dist/emit_sourcemap.d.ts +29 -0
  39. package/dist/emit_sourcemap.js +123 -0
  40. package/dist/emit_sourcemap.js.map +1 -0
  41. package/dist/emit_tests.d.ts +15 -0
  42. package/dist/emit_tests.js +185 -0
  43. package/dist/emit_tests.js.map +1 -0
  44. package/dist/emit_websocket.d.ts +6 -0
  45. package/dist/emit_websocket.js +223 -0
  46. package/dist/emit_websocket.js.map +1 -0
  47. package/dist/emitter.d.ts +25 -0
  48. package/dist/emitter.js +511 -0
  49. package/dist/emitter.js.map +1 -0
  50. package/dist/extension_manager.d.ts +38 -0
  51. package/dist/extension_manager.js +170 -0
  52. package/dist/extension_manager.js.map +1 -0
  53. package/dist/formatter.d.ts +34 -0
  54. package/dist/formatter.js +317 -0
  55. package/dist/formatter.js.map +1 -0
  56. package/dist/index.d.ts +42 -0
  57. package/dist/index.js +113 -0
  58. package/dist/index.js.map +1 -0
  59. package/dist/ir.d.ts +168 -0
  60. package/dist/ir.js +10 -0
  61. package/dist/ir.js.map +1 -0
  62. package/dist/lexer.d.ts +195 -0
  63. package/dist/lexer.js +619 -0
  64. package/dist/lexer.js.map +1 -0
  65. package/dist/lowering.d.ts +25 -0
  66. package/dist/lowering.js +500 -0
  67. package/dist/lowering.js.map +1 -0
  68. package/dist/module_loader.d.ts +25 -0
  69. package/dist/module_loader.js +126 -0
  70. package/dist/module_loader.js.map +1 -0
  71. package/dist/optimizer.d.ts +26 -0
  72. package/dist/optimizer.js +158 -0
  73. package/dist/optimizer.js.map +1 -0
  74. package/dist/parse_decls.d.ts +13 -0
  75. package/dist/parse_decls.js +442 -0
  76. package/dist/parse_decls.js.map +1 -0
  77. package/dist/parse_decls2.d.ts +13 -0
  78. package/dist/parse_decls2.js +295 -0
  79. package/dist/parse_decls2.js.map +1 -0
  80. package/dist/parse_expr.d.ts +7 -0
  81. package/dist/parse_expr.js +197 -0
  82. package/dist/parse_expr.js.map +1 -0
  83. package/dist/parse_types.d.ts +6 -0
  84. package/dist/parse_types.js +51 -0
  85. package/dist/parse_types.js.map +1 -0
  86. package/dist/parser.d.ts +10 -0
  87. package/dist/parser.js +62 -0
  88. package/dist/parser.js.map +1 -0
  89. package/dist/parser_base.d.ts +19 -0
  90. package/dist/parser_base.js +50 -0
  91. package/dist/parser_base.js.map +1 -0
  92. package/dist/parser_recovery.d.ts +26 -0
  93. package/dist/parser_recovery.js +140 -0
  94. package/dist/parser_recovery.js.map +1 -0
  95. package/dist/scaffold.d.ts +13 -0
  96. package/dist/scaffold.js +376 -0
  97. package/dist/scaffold.js.map +1 -0
  98. package/dist/solver.d.ts +26 -0
  99. package/dist/solver.js +281 -0
  100. package/dist/solver.js.map +1 -0
  101. package/dist/typechecker.d.ts +52 -0
  102. package/dist/typechecker.js +534 -0
  103. package/dist/typechecker.js.map +1 -0
  104. package/dist/types.d.ts +38 -0
  105. package/dist/types.js +85 -0
  106. package/dist/types.js.map +1 -0
  107. package/dist/verifier.d.ts +46 -0
  108. package/dist/verifier.js +307 -0
  109. package/dist/verifier.js.map +1 -0
  110. package/package.json +52 -0
  111. package/src/algorithm_catalog.ts +345 -0
  112. package/src/ast.ts +334 -0
  113. package/src/cli.ts +624 -0
  114. package/src/emit_batch.ts +140 -0
  115. package/src/emit_capability.ts +436 -0
  116. package/src/emit_composition.ts +196 -0
  117. package/src/emit_deploy.ts +190 -0
  118. package/src/emit_events.ts +307 -0
  119. package/src/emit_extras.ts +240 -0
  120. package/src/emit_full.ts +309 -0
  121. package/src/emit_maintenance.ts +459 -0
  122. package/src/emit_runtime.ts +731 -0
  123. package/src/emit_sourcemap.ts +140 -0
  124. package/src/emit_tests.ts +205 -0
  125. package/src/emit_websocket.ts +229 -0
  126. package/src/emitter.ts +566 -0
  127. package/src/extension_manager.ts +187 -0
  128. package/src/formatter.ts +297 -0
  129. package/src/index.ts +88 -0
  130. package/src/ir.ts +215 -0
  131. package/src/lexer.ts +630 -0
  132. package/src/lowering.ts +556 -0
  133. package/src/module_loader.ts +114 -0
  134. package/src/optimizer.ts +196 -0
  135. package/src/parse_decls.ts +409 -0
  136. package/src/parse_decls2.ts +244 -0
  137. package/src/parse_expr.ts +197 -0
  138. package/src/parse_types.ts +54 -0
  139. package/src/parser.ts +64 -0
  140. package/src/parser_base.ts +57 -0
  141. package/src/parser_recovery.ts +153 -0
  142. package/src/scaffold.ts +375 -0
  143. package/src/solver.ts +330 -0
  144. package/src/typechecker.ts +591 -0
  145. package/src/types.ts +122 -0
  146. package/src/verifier.ts +348 -0
@@ -0,0 +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
+ }
@@ -0,0 +1,244 @@
1
+ /**
2
+ * BoneScript Declaration Parsers — Channel, Store, Event, Constraint, Policy, Flow, Import
3
+ */
4
+
5
+ import { TokenKind } from "./lexer";
6
+ import { TokenStream, ParseError } from "./parser_base";
7
+ import * as AST from "./ast";
8
+ import { parseExpr } from "./parse_expr";
9
+ import { parseTypeExpr } from "./parse_types";
10
+ import { parseFieldList, parseDuration, parseIdentList } from "./parse_decls";
11
+
12
+ // ─── Channel ─────────────────────────────────────────────────────────────────
13
+
14
+ export function parseChannelDecl(s: TokenStream): AST.ChannelDeclNode {
15
+ const loc = s.peek().loc;
16
+ s.expect(TokenKind.KwChannel, "channel");
17
+ const name = s.expect(TokenKind.Identifier, "name").value;
18
+ s.expect(TokenKind.LBrace, "{");
19
+
20
+ const node: AST.ChannelDeclNode = {
21
+ kind: "ChannelDecl", loc, name,
22
+ transport: null, ordering: null, participants: null,
23
+ persistence: null, filter: null, maxSize: null,
24
+ };
25
+
26
+ while (!s.check(TokenKind.RBrace) && !s.check(TokenKind.EOF)) {
27
+ const tok = s.peek();
28
+ switch (tok.kind) {
29
+ case TokenKind.KwTransport: s.advance(); s.expect(TokenKind.Colon, ":"); node.transport = s.advance().value; break;
30
+ case TokenKind.KwOrdering: s.advance(); s.expect(TokenKind.Colon, ":"); node.ordering = s.advance().value; break;
31
+ case TokenKind.KwParticipants: s.advance(); s.expect(TokenKind.Colon, ":"); node.participants = parseTypeExpr(s); break;
32
+ case TokenKind.KwPersistence: s.advance(); s.expect(TokenKind.Colon, ":"); node.persistence = s.advance().value; break;
33
+ case TokenKind.KwFilter: s.advance(); s.expect(TokenKind.Colon, ":"); node.filter = parseExpr(s); break;
34
+ case TokenKind.KwMaxSize: s.advance(); s.expect(TokenKind.Colon, ":"); node.maxSize = parseInt(s.expect(TokenKind.IntLiteral, "n").value, 10); break;
35
+ default: throw new ParseError(`Unexpected in channel: ${tok.kind}`, tok.loc);
36
+ }
37
+ }
38
+ s.expect(TokenKind.RBrace, "}");
39
+ return node;
40
+ }
41
+
42
+ // ─── Store ───────────────────────────────────────────────────────────────────
43
+
44
+ export function parseStoreDecl(s: TokenStream): AST.StoreDeclNode {
45
+ const loc = s.peek().loc;
46
+ s.expect(TokenKind.KwStore, "store");
47
+ const name = s.expect(TokenKind.Identifier, "name").value;
48
+ s.expect(TokenKind.LBrace, "{");
49
+
50
+ const node: AST.StoreDeclNode = {
51
+ kind: "StoreDecl", loc, name,
52
+ engine: null, schema: [], retention: null, partition: null, replicas: null,
53
+ };
54
+
55
+ while (!s.check(TokenKind.RBrace) && !s.check(TokenKind.EOF)) {
56
+ const tok = s.peek();
57
+ switch (tok.kind) {
58
+ case TokenKind.KwEngine: s.advance(); s.expect(TokenKind.Colon, ":"); node.engine = s.advance().value; break;
59
+ case TokenKind.KwSchema:
60
+ s.advance(); s.expect(TokenKind.Colon, ":");
61
+ s.expect(TokenKind.LBrace, "{");
62
+ node.schema = parseFieldList(s);
63
+ s.expect(TokenKind.RBrace, "}");
64
+ break;
65
+ case TokenKind.KwRetention: s.advance(); s.expect(TokenKind.Colon, ":"); node.retention = parseDuration(s); break;
66
+ case TokenKind.KwPartition: s.advance(); s.expect(TokenKind.Colon, ":"); node.partition = s.expect(TokenKind.Identifier, "field").value; break;
67
+ case TokenKind.KwReplicas: s.advance(); s.expect(TokenKind.Colon, ":"); node.replicas = parseInt(s.expect(TokenKind.IntLiteral, "n").value, 10); break;
68
+ default: throw new ParseError(`Unexpected in store: ${tok.kind}`, tok.loc);
69
+ }
70
+ }
71
+ s.expect(TokenKind.RBrace, "}");
72
+ return node;
73
+ }
74
+
75
+ // ─── Event ───────────────────────────────────────────────────────────────────
76
+
77
+ export function parseEventDecl(s: TokenStream): AST.EventDeclNode {
78
+ const loc = s.peek().loc;
79
+ s.expect(TokenKind.KwEvent, "event");
80
+ const name = s.expect(TokenKind.Identifier, "name").value;
81
+ s.expect(TokenKind.LBrace, "{");
82
+
83
+ const node: AST.EventDeclNode = {
84
+ kind: "EventDecl", loc, name, payload: [], delivery: null, ttl: null,
85
+ };
86
+
87
+ while (!s.check(TokenKind.RBrace) && !s.check(TokenKind.EOF)) {
88
+ const tok = s.peek();
89
+ switch (tok.kind) {
90
+ case TokenKind.KwPayload:
91
+ s.advance(); s.expect(TokenKind.Colon, ":");
92
+ s.expect(TokenKind.LBrace, "{");
93
+ node.payload = parseFieldList(s);
94
+ s.expect(TokenKind.RBrace, "}");
95
+ break;
96
+ case TokenKind.KwDelivery: s.advance(); s.expect(TokenKind.Colon, ":"); node.delivery = s.advance().value; break;
97
+ case TokenKind.KwTtl: s.advance(); s.expect(TokenKind.Colon, ":"); node.ttl = parseDuration(s); break;
98
+ default: throw new ParseError(`Unexpected in event: ${tok.kind}`, tok.loc);
99
+ }
100
+ }
101
+ s.expect(TokenKind.RBrace, "}");
102
+ return node;
103
+ }
104
+
105
+ // ─── Constraint ──────────────────────────────────────────────────────────────
106
+
107
+ export function parseConstraintDecl(s: TokenStream): AST.ConstraintDeclNode {
108
+ const loc = s.peek().loc;
109
+ s.expect(TokenKind.KwConstraint, "constraint");
110
+ const name = s.expect(TokenKind.Identifier, "name").value;
111
+ s.expect(TokenKind.Colon, ":");
112
+ const expr = parseExpr(s);
113
+ return { kind: "ConstraintDecl", loc, name, expr };
114
+ }
115
+
116
+ // ─── Policy ──────────────────────────────────────────────────────────────────
117
+
118
+ export function parsePolicyDecl(s: TokenStream): AST.PolicyDeclNode {
119
+ const loc = s.peek().loc;
120
+ s.expect(TokenKind.KwPolicy, "policy");
121
+ const name = s.expect(TokenKind.Identifier, "name").value;
122
+ s.expect(TokenKind.LBrace, "{");
123
+
124
+ const node: AST.PolicyDeclNode = {
125
+ kind: "PolicyDecl", loc, name, rateLimit: null, access: [], audit: null, encryption: null,
126
+ };
127
+
128
+ while (!s.check(TokenKind.RBrace) && !s.check(TokenKind.EOF)) {
129
+ const tok = s.peek();
130
+ switch (tok.kind) {
131
+ case TokenKind.KwRateLimit:
132
+ s.advance(); s.expect(TokenKind.Colon, ":");
133
+ const count = parseInt(s.expect(TokenKind.IntLiteral, "count").value, 10);
134
+ s.expect(TokenKind.KwPer, "per");
135
+ const per = parseDuration(s);
136
+ node.rateLimit = { count, per };
137
+ break;
138
+ case TokenKind.KwAccess:
139
+ s.advance(); s.expect(TokenKind.Colon, ":");
140
+ s.expect(TokenKind.LBracket, "[");
141
+ node.access = parseIdentList(s);
142
+ s.expect(TokenKind.RBracket, "]");
143
+ break;
144
+ case TokenKind.KwAudit: s.advance(); s.expect(TokenKind.Colon, ":"); node.audit = s.advance().kind === TokenKind.KwTrue; break;
145
+ case TokenKind.KwEncryption: s.advance(); s.expect(TokenKind.Colon, ":"); node.encryption = s.advance().value; break;
146
+ default: throw new ParseError(`Unexpected in policy: ${tok.kind}`, tok.loc);
147
+ }
148
+ }
149
+ s.expect(TokenKind.RBrace, "}");
150
+ return node;
151
+ }
152
+
153
+ // ─── Flow ────────────────────────────────────────────────────────────────────
154
+
155
+ export function parseFlowDecl(s: TokenStream): AST.FlowDeclNode {
156
+ const loc = s.peek().loc;
157
+ s.expect(TokenKind.KwFlow, "flow");
158
+ const name = s.expect(TokenKind.Identifier, "name").value;
159
+ s.expect(TokenKind.LBrace, "{");
160
+
161
+ const steps: AST.FlowStepNode[] = [];
162
+ while (!s.check(TokenKind.RBrace) && !s.check(TokenKind.EOF)) {
163
+ const sloc = s.peek().loc;
164
+ s.expect(TokenKind.KwStep, "step");
165
+ const stepName = s.expect(TokenKind.Identifier, "step name").value;
166
+ s.expect(TokenKind.Colon, ":");
167
+ const action = parseCallExpr(s);
168
+ let compensate: AST.CallExprNode | null = null;
169
+ if (s.check(TokenKind.KwCompensate)) {
170
+ s.advance(); s.expect(TokenKind.Colon, ":");
171
+ compensate = parseCallExpr(s);
172
+ }
173
+ steps.push({ kind: "FlowStep", loc: sloc, name: stepName, action, compensate });
174
+ }
175
+ s.expect(TokenKind.RBrace, "}");
176
+ return { kind: "FlowDecl", loc, name, steps };
177
+ }
178
+
179
+ function parseCallExpr(s: TokenStream): AST.CallExprNode {
180
+ const loc = s.peek().loc;
181
+ const name = s.expect(TokenKind.Identifier, "call name").value;
182
+ s.expect(TokenKind.LParen, "(");
183
+ const args: AST.ExprNode[] = [];
184
+ if (!s.check(TokenKind.RParen)) {
185
+ do { args.push(parseExpr(s)); } while (s.match(TokenKind.Comma));
186
+ }
187
+ s.expect(TokenKind.RParen, ")");
188
+ return { kind: "CallExpr", loc, name, args };
189
+ }
190
+
191
+ // ─── Import ──────────────────────────────────────────────────────────────────
192
+
193
+ export function parseImportDecl(s: TokenStream): AST.ImportDeclNode {
194
+ const loc = s.peek().loc;
195
+ s.expect(TokenKind.KwImport, "import");
196
+ const name = s.expect(TokenKind.Identifier, "name").value;
197
+ s.expect(TokenKind.KwFrom, "from");
198
+ const from = s.expect(TokenKind.StringLiteral, "path").value;
199
+ return { kind: "ImportDecl", loc, name, from };
200
+ }
201
+
202
+ // ─── Extension Point ─────────────────────────────────────────────────────────
203
+
204
+ export function parseExtensionPointDecl(s: TokenStream): AST.ExtensionPointDeclNode {
205
+ const loc = s.peek().loc;
206
+ s.expect(TokenKind.KwExtensionPoint, "extension_point");
207
+ const name = s.expect(TokenKind.Identifier, "extension point name").value;
208
+ s.expect(TokenKind.LParen, "(");
209
+ const params: AST.ParamNode[] = [];
210
+ if (!s.check(TokenKind.RParen)) {
211
+ do {
212
+ const ploc = s.peek().loc;
213
+ // Allow keywords as param names
214
+ const pname = s.peek().kind === TokenKind.Identifier ? s.advance().value : s.advance().value;
215
+ s.expect(TokenKind.Colon, ":");
216
+ const ptype = parseTypeExpr(s);
217
+ params.push({ kind: "Param", loc: ploc, name: pname, type: ptype });
218
+ } while (s.match(TokenKind.Comma));
219
+ }
220
+ s.expect(TokenKind.RParen, ")");
221
+
222
+ let returns: AST.TypeExprNode | null = null;
223
+ let stable = false;
224
+
225
+ s.expect(TokenKind.LBrace, "{");
226
+ while (!s.check(TokenKind.RBrace) && !s.check(TokenKind.EOF)) {
227
+ const tok = s.peek();
228
+ if (tok.kind === TokenKind.KwReturns) {
229
+ s.advance(); s.expect(TokenKind.Colon, ":");
230
+ returns = parseTypeExpr(s);
231
+ } else if (tok.kind === TokenKind.KwStable) {
232
+ s.advance(); s.expect(TokenKind.Colon, ":");
233
+ stable = s.advance().kind === TokenKind.KwTrue;
234
+ } else if (tok.kind === TokenKind.KwLanguage) {
235
+ // language: typescript — consume and ignore (only TS supported)
236
+ s.advance(); s.expect(TokenKind.Colon, ":"); s.advance();
237
+ } else {
238
+ throw new ParseError(`Unexpected in extension_point: ${tok.kind}`, tok.loc);
239
+ }
240
+ }
241
+ s.expect(TokenKind.RBrace, "}");
242
+
243
+ return { kind: "ExtensionPointDecl", loc, name, params, returns, stable };
244
+ }