bonescript-compiler 0.5.3 → 0.5.4

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 (194) hide show
  1. package/LICENSE +21 -21
  2. package/dist/algorithm_catalog.js +166 -166
  3. package/dist/cli.d.ts +1 -2
  4. package/dist/cli.js +543 -75
  5. package/dist/cli.js.map +1 -1
  6. package/dist/emit_capability.d.ts +0 -13
  7. package/dist/emit_capability.js +134 -296
  8. package/dist/emit_capability.js.map +1 -1
  9. package/dist/emit_composition.js +3 -37
  10. package/dist/emit_composition.js.map +1 -1
  11. package/dist/emit_deploy.js +167 -165
  12. package/dist/emit_deploy.js.map +1 -1
  13. package/dist/emit_events.d.ts +0 -1
  14. package/dist/emit_events.js +275 -325
  15. package/dist/emit_events.js.map +1 -1
  16. package/dist/emit_extras.js +5 -3
  17. package/dist/emit_extras.js.map +1 -1
  18. package/dist/emit_full.js +112 -272
  19. package/dist/emit_full.js.map +1 -1
  20. package/dist/emit_maintenance.js +249 -249
  21. package/dist/emit_runtime.d.ts +11 -17
  22. package/dist/emit_runtime.js +688 -29
  23. package/dist/emit_runtime.js.map +1 -1
  24. package/dist/emit_sourcemap.js +66 -66
  25. package/dist/emit_tests.js +12 -47
  26. package/dist/emit_tests.js.map +1 -1
  27. package/dist/emit_websocket.js +3 -0
  28. package/dist/emit_websocket.js.map +1 -1
  29. package/dist/emitter.js +49 -94
  30. package/dist/emitter.js.map +1 -1
  31. package/dist/extension_manager.d.ts +2 -2
  32. package/dist/extension_manager.js +20 -9
  33. package/dist/extension_manager.js.map +1 -1
  34. package/dist/ir.d.ts +0 -4
  35. package/dist/lowering.d.ts +14 -5
  36. package/dist/lowering.js +417 -66
  37. package/dist/lowering.js.map +1 -1
  38. package/dist/module_loader.d.ts +2 -2
  39. package/dist/module_loader.js +23 -20
  40. package/dist/module_loader.js.map +1 -1
  41. package/dist/optimizer.js +3 -6
  42. package/dist/optimizer.js.map +1 -1
  43. package/dist/scaffold.d.ts +2 -2
  44. package/dist/scaffold.js +319 -315
  45. package/dist/scaffold.js.map +1 -1
  46. package/dist/solver.js +1 -1
  47. package/dist/solver.js.map +1 -1
  48. package/dist/source_map.js.map +1 -0
  49. package/dist/test.js.map +1 -0
  50. package/dist/test_typechecker.d.ts +5 -0
  51. package/dist/test_typechecker.js +126 -0
  52. package/dist/test_typechecker.js.map +1 -0
  53. package/dist/typechecker.d.ts +0 -7
  54. package/dist/typechecker.js +16 -103
  55. package/dist/typechecker.js.map +1 -1
  56. package/dist/verifier.d.ts +1 -5
  57. package/dist/verifier.js +38 -142
  58. package/dist/verifier.js.map +1 -1
  59. package/package.json +52 -62
  60. package/src/algorithm_catalog.ts +345 -345
  61. package/src/ast.d.ts +244 -0
  62. package/src/ast.ts +334 -334
  63. package/src/cli.ts +624 -98
  64. package/src/emit_batch.ts +140 -140
  65. package/src/emit_capability.ts +436 -613
  66. package/src/emit_composition.ts +196 -229
  67. package/src/emit_deploy.ts +190 -187
  68. package/src/emit_events.ts +307 -362
  69. package/src/emit_extras.ts +240 -237
  70. package/src/emit_full.ts +309 -472
  71. package/src/emit_maintenance.ts +459 -459
  72. package/src/emit_runtime.ts +730 -17
  73. package/src/emit_sourcemap.ts +140 -140
  74. package/src/emit_tests.ts +205 -243
  75. package/src/emit_websocket.ts +229 -226
  76. package/src/emitter.ts +578 -626
  77. package/src/extension_manager.ts +187 -177
  78. package/src/formatter.ts +297 -297
  79. package/src/index.ts +88 -88
  80. package/src/ir.ts +215 -216
  81. package/src/lexer.d.ts +195 -0
  82. package/src/lexer.ts +630 -630
  83. package/src/lowering.ts +556 -168
  84. package/src/module_loader.ts +114 -112
  85. package/src/optimizer.ts +196 -199
  86. package/src/parse_decls.d.ts +13 -0
  87. package/src/parse_decls.ts +409 -409
  88. package/src/parse_decls2.d.ts +13 -0
  89. package/src/parse_decls2.ts +244 -244
  90. package/src/parse_expr.d.ts +7 -0
  91. package/src/parse_expr.ts +197 -197
  92. package/src/parse_types.d.ts +6 -0
  93. package/src/parse_types.ts +54 -54
  94. package/src/parser.d.ts +10 -0
  95. package/src/parser.ts +1 -1
  96. package/src/parser_base.d.ts +19 -0
  97. package/src/parser_base.ts +57 -57
  98. package/src/parser_recovery.ts +153 -153
  99. package/src/scaffold.ts +375 -371
  100. package/src/solver.ts +330 -330
  101. package/src/typechecker.d.ts +52 -0
  102. package/src/typechecker.ts +591 -700
  103. package/src/types.d.ts +38 -0
  104. package/src/types.ts +122 -122
  105. package/src/verifier.ts +49 -154
  106. package/README.md +0 -382
  107. package/dist/commands/check.d.ts +0 -5
  108. package/dist/commands/check.js +0 -34
  109. package/dist/commands/check.js.map +0 -1
  110. package/dist/commands/compile.d.ts +0 -5
  111. package/dist/commands/compile.js +0 -215
  112. package/dist/commands/compile.js.map +0 -1
  113. package/dist/commands/debug.d.ts +0 -5
  114. package/dist/commands/debug.js +0 -59
  115. package/dist/commands/debug.js.map +0 -1
  116. package/dist/commands/diff.d.ts +0 -5
  117. package/dist/commands/diff.js +0 -123
  118. package/dist/commands/diff.js.map +0 -1
  119. package/dist/commands/fmt.d.ts +0 -5
  120. package/dist/commands/fmt.js +0 -49
  121. package/dist/commands/fmt.js.map +0 -1
  122. package/dist/commands/init.d.ts +0 -5
  123. package/dist/commands/init.js +0 -96
  124. package/dist/commands/init.js.map +0 -1
  125. package/dist/commands/ir.d.ts +0 -5
  126. package/dist/commands/ir.js +0 -27
  127. package/dist/commands/ir.js.map +0 -1
  128. package/dist/commands/lex.d.ts +0 -5
  129. package/dist/commands/lex.js +0 -21
  130. package/dist/commands/lex.js.map +0 -1
  131. package/dist/commands/parse.d.ts +0 -5
  132. package/dist/commands/parse.js +0 -30
  133. package/dist/commands/parse.js.map +0 -1
  134. package/dist/commands/test.d.ts +0 -5
  135. package/dist/commands/test.js +0 -61
  136. package/dist/commands/test.js.map +0 -1
  137. package/dist/commands/verify_determinism.d.ts +0 -5
  138. package/dist/commands/verify_determinism.js +0 -64
  139. package/dist/commands/verify_determinism.js.map +0 -1
  140. package/dist/commands/watch.d.ts +0 -5
  141. package/dist/commands/watch.js +0 -50
  142. package/dist/commands/watch.js.map +0 -1
  143. package/dist/emit_auth.d.ts +0 -18
  144. package/dist/emit_auth.js +0 -507
  145. package/dist/emit_auth.js.map +0 -1
  146. package/dist/emit_database.d.ts +0 -7
  147. package/dist/emit_database.js +0 -72
  148. package/dist/emit_database.js.map +0 -1
  149. package/dist/emit_index.d.ts +0 -6
  150. package/dist/emit_index.js +0 -202
  151. package/dist/emit_index.js.map +0 -1
  152. package/dist/emit_models.d.ts +0 -12
  153. package/dist/emit_models.js +0 -171
  154. package/dist/emit_models.js.map +0 -1
  155. package/dist/emit_openapi.d.ts +0 -9
  156. package/dist/emit_openapi.js +0 -306
  157. package/dist/emit_openapi.js.map +0 -1
  158. package/dist/emit_package.d.ts +0 -7
  159. package/dist/emit_package.js +0 -68
  160. package/dist/emit_package.js.map +0 -1
  161. package/dist/emit_router.d.ts +0 -12
  162. package/dist/emit_router.js +0 -389
  163. package/dist/emit_router.js.map +0 -1
  164. package/dist/lowering_channels.d.ts +0 -11
  165. package/dist/lowering_channels.js +0 -103
  166. package/dist/lowering_channels.js.map +0 -1
  167. package/dist/lowering_entities.d.ts +0 -11
  168. package/dist/lowering_entities.js +0 -232
  169. package/dist/lowering_entities.js.map +0 -1
  170. package/dist/lowering_helpers.d.ts +0 -13
  171. package/dist/lowering_helpers.js +0 -76
  172. package/dist/lowering_helpers.js.map +0 -1
  173. package/src/commands/check.ts +0 -33
  174. package/src/commands/compile.ts +0 -191
  175. package/src/commands/debug.ts +0 -33
  176. package/src/commands/diff.ts +0 -105
  177. package/src/commands/fmt.ts +0 -22
  178. package/src/commands/init.ts +0 -72
  179. package/src/commands/ir.ts +0 -23
  180. package/src/commands/lex.ts +0 -17
  181. package/src/commands/parse.ts +0 -24
  182. package/src/commands/test.ts +0 -36
  183. package/src/commands/verify_determinism.ts +0 -66
  184. package/src/commands/watch.ts +0 -25
  185. package/src/emit_auth.ts +0 -513
  186. package/src/emit_database.ts +0 -72
  187. package/src/emit_index.ts +0 -210
  188. package/src/emit_models.ts +0 -176
  189. package/src/emit_openapi.ts +0 -315
  190. package/src/emit_package.ts +0 -66
  191. package/src/emit_router.ts +0 -408
  192. package/src/lowering_channels.ts +0 -108
  193. package/src/lowering_entities.ts +0 -258
  194. package/src/lowering_helpers.ts +0 -75
@@ -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
+ }