@hew-lang/tree-sitter-hew 1.0.1 → 1.1.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.
package/grammar.js CHANGED
@@ -7,23 +7,24 @@
7
7
  /// <reference types="tree-sitter-cli/dsl" />
8
8
  // @ts-check
9
9
 
10
- // Precedence levels (from Pratt parser in hew-parser/src/parser.rs)
10
+ // Binary tiers follow hew-parser precedence.rs (loosest first). All sit above
11
+ // lambda bodies (2) and `handle` (1).
11
12
  const PREC = {
12
- SEND: 1, // <-
13
- OR: 3, // || or
14
- BIT_OR: 4, // |
15
- AND: 5, // && and
16
- BIT_AND: 6, // &
17
- BIT_XOR: 6, // ^
18
- EQ: 7, // == != =~ !~
19
- REL: 9, // < <= > >=
20
- RANGE: 11, // .. ..=
13
+ COALESCE: 3, // ?? (right-associative)
14
+ RANGE: 4, // .. ..=
15
+ OR: 5, // ||
16
+ AND: 6, // &&
17
+ EQ: 7, // == != is
18
+ REL: 8, // < <= > >=
19
+ BIT_OR: 9, // |
20
+ BIT_XOR: 10, // ^
21
+ BIT_AND: 11, // &
21
22
  SHIFT: 12, // << >>
22
- ADD: 13, // + -
23
- MUL: 15, // * / %
23
+ ADD: 13, // + - &+ &-
24
+ MUL: 15, // * / % &*
24
25
  UNARY: 17, // ! - ~ await
25
26
  POSTFIX: 19, // . () [] ?
26
- FIELD: 20, // field access
27
+ FIELD: 19, // field access (same left-associative postfix tier)
27
28
  };
28
29
 
29
30
  export default grammar({
@@ -33,11 +34,41 @@ export default grammar({
33
34
 
34
35
  word: $ => $.identifier,
35
36
 
37
+ // Inlining keeps each operator's precedence on its right operand.
38
+ inline: $ => [$._binary_operand],
39
+
36
40
  conflicts: $ => [
37
- [$.expression, $.lambda_parameter],
41
+ [$._block_arm_value, $.expression],
42
+ [$._block_arm_value, $.fork_expression],
38
43
  [$.if_statement, $.expression],
39
44
  [$.match_statement, $.expression],
45
+ [$.block_like_statement, $.expression],
46
+ [$.block_like_statement, $.fork_expression],
40
47
  [$.expression, $.struct_init],
48
+ [$.block, $.map_literal],
49
+ [$.expression, $._member_object, $.path_expression],
50
+ [$.expression, $._member_object],
51
+ [$.qualified_expression, $.field_expression],
52
+ [$.generic_apply_expression, $.bare_generic_call_expression],
53
+ [$.expression, $.private_capture_list],
54
+ [$.actor_spawn],
55
+ [$.trait_bound],
56
+ // `where P , (` — after a where-predicate's trailing comma, a `(` may begin
57
+ // either another predicate (a parenthesized/tuple type) or a record's tuple
58
+ // body `( T, … )`; GLR explores both and only the valid continuation lives.
59
+ [$.where_clause],
60
+ // `import a.b.c` — at each `.` the parser cannot tell (within LR(1))
61
+ // whether another path segment or the import selection follows; GLR explores
62
+ // both and only the valid continuation survives.
63
+ [$.module_path],
64
+ // `expr as Name.Field` — the `.` may extend a qualified (scoped) type or be
65
+ // a field access on the cast result. The real parser parses the type
66
+ // greedily (`x as net.NetError`); field access on a cast needs parens.
67
+ [$._type, $.scoped_type],
68
+ // `expr as Name.Field<...>` — `<` may open the scoped type's type-arg list
69
+ // or be a comparison on the cast result; the real parser takes type args
70
+ // greedily (mirrors generic_type's prec(1)).
71
+ [$.scoped_type],
41
72
  ],
42
73
 
43
74
  rules: {
@@ -47,44 +78,77 @@ export default grammar({
47
78
 
48
79
  _item: $ => seq(
49
80
  repeat($.attribute),
81
+ // Visibility is hoisted to item level per spec grammar.ebnf:27
82
+ // Item = Attribute* Visibility? ( Import | ConstDecl | ... )
83
+ optional($.visibility),
50
84
  choice(
51
85
  $.import_declaration,
52
86
  $.const_declaration,
53
87
  $.struct_declaration,
88
+ $.tuple_type_declaration,
54
89
  $.enum_declaration,
55
- $.wire_declaration,
56
90
  $.trait_declaration,
57
91
  $.impl_declaration,
58
92
  $.function_declaration,
59
93
  $.gen_function_declaration,
60
- $.async_gen_function_declaration,
61
- $.async_function_declaration,
62
94
  $.extern_block,
63
95
  $.actor_declaration,
64
96
  $.supervisor_declaration,
97
+ $.machine_declaration,
65
98
  $.type_alias,
66
99
  ),
67
100
  ),
68
101
 
69
- attribute: $ => seq('#', '[', $.identifier, optional(seq('(', optional(sep1(choice($.identifier, $._literal), ',')), ')')), ']'),
102
+ // Attribute args accept both a positional list (`#[derive(Debug, Clone)]`)
103
+ // and `key = value` pairs (`#[wire(version = 2, min_version = 1)]`), matching
104
+ // the real parser's AttributeArg forms (hew-parser wire.rs
105
+ // reads `version`/`min_version` as KeyValue args).
106
+ attribute: $ => seq('#', '[', $.identifier, optional(seq('(', optional(sep1($._attribute_arg, ',')), ')')), ']'),
107
+
108
+ _attribute_arg: $ => choice(
109
+ seq($.identifier, '=', choice($.identifier, $._literal)),
110
+ // Unit-bearing limits such as `#[max_heap(64 kb)]` are accepted by the
111
+ // compiler as one attribute argument.
112
+ seq($.integer_literal, $.identifier),
113
+ $.identifier,
114
+ $._literal,
115
+ ),
70
116
 
117
+ // Import (hew-parser: import item):
118
+ // Import = "import" ( StringLit | ModulePath ( "." ImportSelection )? ) ";"
119
+ // ModulePath = Ident { "." Ident }
120
+ // ImportSelection = "{" ImportName { "," ImportName } "}"
121
+ // ImportName = Ident ( "as" Ident )?
122
+ // A bare trailing Ident is NOT a valid spec — a single name uses the brace
123
+ // form `import m.{ Name };`. The path/selection tail is right-recursive so the
124
+ // token after each `.` (another Ident vs `{`) disambiguates within
125
+ // LR(1); a left-recursive sep1 reduces the path too early and breaks
126
+ // N-segment (>=3) paths.
71
127
  import_declaration: $ => seq(
72
128
  'import',
73
- $.module_path,
74
- optional(seq('::', $._import_spec)),
129
+ choice(
130
+ $.string_literal,
131
+ seq($.module_path, optional(seq('.', $._import_selection))),
132
+ ),
133
+ // A complete module path may carry an alias (`import a.b as c`).
134
+ // Explicit selection members carry their own aliases instead.
135
+ optional(seq('as', field('alias', $.identifier))),
75
136
  ';',
76
137
  ),
77
138
 
78
- module_path: $ => prec.left(sep1($.identifier, '::')),
79
-
80
- _import_spec: $ => choice(
139
+ module_path: $ => seq(
81
140
  $.identifier,
82
- seq('{', sep1($.identifier, ','), '}'),
83
- '*',
141
+ repeat(seq('.', $.identifier)),
142
+ ),
143
+
144
+ _import_selection: $ => seq('{', sep1($.import_name, ','), optional(','), '}'),
145
+
146
+ import_name: $ => seq(
147
+ field('name', $.identifier),
148
+ optional(seq('as', field('alias', $.identifier))),
84
149
  ),
85
150
 
86
151
  const_declaration: $ => seq(
87
- optional($.visibility),
88
152
  'const',
89
153
  field('name', $.identifier),
90
154
  ':',
@@ -103,8 +167,10 @@ export default grammar({
103
167
  ';',
104
168
  ),
105
169
 
170
+ // TypeDecl (grammar.ebnf:47, 65): `type Name { ... }` whose body may hold
171
+ // struct fields, variants, AND fn declarations (TypeBody). The alias form
172
+ // `type Name = Type;` is handled separately by `type_alias`.
106
173
  struct_declaration: $ => seq(
107
- optional($.visibility),
108
174
  'type',
109
175
  field('name', $.identifier),
110
176
  optional($.type_parameters),
@@ -112,18 +178,43 @@ export default grammar({
112
178
  field('body', $.struct_body),
113
179
  ),
114
180
 
115
- struct_body: $ => seq('{', repeat($.struct_field), '}'),
181
+ struct_body: $ => seq('{', repeat(choice(
182
+ $.struct_field,
183
+ $.reserved_declaration,
184
+ seq(optional($.visibility), $.function_declaration),
185
+ )), '}'),
116
186
 
187
+ // StructFieldDecl / WireStructFieldDecl (grammar.ebnf:68, 76):
188
+ // `Attribute* Ident ":" Type ("@" IntLit)? WireAttr* ";"`.
189
+ // The real parser attaches `@N` tags and trailing wire modifiers to a normal
190
+ // `type` declaration when a `#[wire]` attribute is present.
117
191
  struct_field: $ => seq(
118
- optional('var'),
192
+ repeat($.attribute),
119
193
  field('name', $.identifier),
120
194
  ':',
121
195
  field('type', $._type),
122
- optional(choice(',', ';')),
196
+ optional(seq('@', $.integer_literal)),
197
+ repeat($.wire_attribute),
198
+ ';',
199
+ ),
200
+
201
+ // `type Name(T, T, ...);` — positional/tuple record. The bare `record`
202
+ // keyword form (both named- and tuple-body) was removed from the
203
+ // compiler ("record has been removed; use `type Name { ... }` instead");
204
+ // the tuple-positional shape survives under the `type` keyword, dispatched
205
+ // by the parser's `is_tuple_type_lookahead` (hew-parser items.rs:441-457,
206
+ // parse_record_decl at items.rs:941-983). The named-body form is just
207
+ // `struct_declaration` (`type Name { field: T, ... }`).
208
+ tuple_type_declaration: $ => seq(
209
+ 'type',
210
+ field('name', $.identifier),
211
+ optional($.type_parameters),
212
+ optional($.where_clause),
213
+ '(', sep1($._type, ','), optional(','), ')', ';',
123
214
  ),
124
215
 
125
216
  enum_declaration: $ => seq(
126
- optional($.visibility),
217
+ optional('indirect'),
127
218
  'enum',
128
219
  field('name', $.identifier),
129
220
  optional($.type_parameters),
@@ -135,52 +226,39 @@ export default grammar({
135
226
 
136
227
  variant: $ => seq(
137
228
  field('name', $.identifier),
138
- optional(choice(
139
- seq('(', sep1($._type, ','), ')'),
229
+ choice(
230
+ ';',
231
+ seq('(', sep1($._type, ','), optional(','), ')', ';'),
140
232
  seq('{', repeat($.struct_field), '}'),
141
- )),
142
- optional(choice(',', ';')),
143
- ),
144
-
145
- // "type X { ... }" is a struct alias form
146
- // handled by struct_declaration with choice('struct', 'type')
147
-
148
- wire_declaration: $ => seq(
149
- 'wire',
150
- choice('struct', 'enum'),
151
- field('name', $.identifier),
152
- field('body', $.wire_body),
233
+ ),
153
234
  ),
154
235
 
155
- wire_body: $ => seq('{', repeat(choice($.wire_field, $.variant, $.reserved_declaration)), '}'),
156
-
157
- wire_field: $ => prec.left(seq(
158
- field('name', $.identifier),
159
- ':',
160
- field('type', $.wire_type),
161
- optional(seq('@', $.integer_literal)),
162
- repeat($.wire_attribute),
163
- optional(choice(',', ';')),
164
- )),
165
-
166
- wire_type: $ => choice(
167
- 'u8', 'u16', 'u32', 'u64',
168
- 'i8', 'i16', 'i32', 'i64',
169
- 'f32', 'f64',
170
- 'bool', 'bytes', 'string',
171
- $.identifier,
172
- seq('list', '[', $.wire_type, ']'),
173
- ),
236
+ // NOTE: the legacy bare `wire` keyword item form (`wire struct/enum/type
237
+ // Name { … }`) was removed from the compiler in hew commit 60c50daef
238
+ // ("refactor(parser): remove the wire keyword surface in favor of #[wire]").
239
+ // The current surface is `#[wire] type Name { … }` / `#[wire] enum Name { … }`:
240
+ // both struct-shaped and enum-shaped wire types use the generic `#[wire]`
241
+ // attribute on ordinary `type` / `enum` declarations.
174
242
 
243
+ // `reserved @N, @M;` inside a `#[wire] type` body reserves wire field
244
+ // numbers (hew-parser wire.rs:222-248 expects `@` markers, comma-separated,
245
+ // terminated by `;`). The old parenthesised `reserved(N, M);` form is not
246
+ // accepted by the compiler.
175
247
  reserved_declaration: $ => seq(
176
- 'reserved', '(', sep1($.integer_literal, ','), ')', ';',
248
+ 'reserved', sep1(seq('@', $.integer_literal), ','), ';',
177
249
  ),
178
250
 
251
+ // @sync:wire_attributes — per-field modifiers on a `#[wire] type` field
252
+ // (hew-parser wire.rs:53-117). `optional`/`deprecated` are real keyword
253
+ // tokens; `repeated`/`since`/`json`/`yaml`/`json_name`/`yaml_name` are
254
+ // contextual identifiers recognised only in wire-field modifier position.
179
255
  wire_attribute: $ => choice(
180
- 'optional',
181
- 'deprecated',
182
- seq('default', '(', $.expression, ')'),
183
- seq('reserved', '(', sep1($.integer_literal, ','), ')'),
256
+ 'optional', 'deprecated', 'repeated',
257
+ seq('since', $.integer_literal),
258
+ seq('json', '(', $.string_literal, ')'),
259
+ seq('yaml', '(', $.string_literal, ')'),
260
+ seq('json_name', '=', $.string_literal),
261
+ seq('yaml_name', '=', $.string_literal),
184
262
  ),
185
263
 
186
264
  // ---- Traits & Impls ----
@@ -192,7 +270,7 @@ export default grammar({
192
270
  optional(seq(':', $.trait_bounds)),
193
271
  optional($.where_clause),
194
272
  '{',
195
- repeat($._trait_item),
273
+ repeat(seq(repeat($.attribute), $._trait_item)),
196
274
  '}',
197
275
  ),
198
276
 
@@ -202,7 +280,6 @@ export default grammar({
202
280
  ),
203
281
 
204
282
  trait_function_signature: $ => seq(
205
- optional('pure'),
206
283
  'fn',
207
284
  field('name', $.identifier),
208
285
  optional($.type_parameters),
@@ -210,6 +287,7 @@ export default grammar({
210
287
  optional($.parameters),
211
288
  ')',
212
289
  optional($.return_type),
290
+ optional($.where_clause),
213
291
  choice(';', field('body', $.block)),
214
292
  ),
215
293
 
@@ -221,16 +299,24 @@ export default grammar({
221
299
  ';',
222
300
  ),
223
301
 
302
+ // ImplDecl (spec grammar.ebnf:119-120) — trait impls AND inherent impls:
303
+ // "impl" TypeParams? TraitBound "for" Type WhereClause? "{" ... "}"
304
+ // "impl" TypeParams? Type WhereClause? "{" ... "}"
305
+ // When `for` is present the first type is the trait and the second is the
306
+ // self type; otherwise the single type is the inherent self type. Impl-body
307
+ // fns may carry visibility (hew-parser/src/parser.rs:2939 parse_impl_decl).
224
308
  impl_declaration: $ => seq(
225
309
  'impl',
226
310
  optional($.type_parameters),
227
- field('trait', $.identifier),
228
- optional($.type_arguments),
229
- 'for',
230
311
  field('type', $._type),
312
+ optional(seq('for', field('self_type', $._type))),
231
313
  optional($.where_clause),
232
314
  '{',
233
- repeat(choice($.function_declaration, $.associated_type_impl)),
315
+ repeat(seq(
316
+ repeat($.attribute),
317
+ optional($.visibility),
318
+ choice($.function_declaration, $.associated_type_impl),
319
+ )),
234
320
  '}',
235
321
  ),
236
322
 
@@ -239,8 +325,6 @@ export default grammar({
239
325
  // ---- Functions ----
240
326
 
241
327
  function_declaration: $ => seq(
242
- optional($.visibility),
243
- optional('pure'),
244
328
  'fn',
245
329
  field('name', $.identifier),
246
330
  optional($.type_parameters),
@@ -253,7 +337,6 @@ export default grammar({
253
337
  ),
254
338
 
255
339
  gen_function_declaration: $ => seq(
256
- optional('pure'),
257
340
  'gen',
258
341
  'fn',
259
342
  field('name', $.identifier),
@@ -267,45 +350,41 @@ export default grammar({
267
350
  field('body', $.block),
268
351
  ),
269
352
 
270
- async_function_declaration: $ => seq(
271
- 'async',
272
- 'fn',
273
- field('name', $.identifier),
274
- optional($.type_parameters),
275
- '(',
276
- optional($.parameters),
277
- ')',
278
- optional($.return_type),
279
- optional($.where_clause),
280
- field('body', $.block),
281
- ),
353
+ parameters: $ => sep1($.parameter, ','),
282
354
 
283
- async_gen_function_declaration: $ => seq(
284
- 'async',
285
- 'gen',
286
- 'fn',
287
- field('name', $.identifier),
288
- optional($.type_parameters),
289
- '(',
290
- optional($.parameters),
291
- ')',
292
- '->',
293
- field('yield_type', $._type),
294
- optional($.where_clause),
295
- field('body', $.block),
355
+ // Param (hew-parser `parse_params_with_implicit_self_and_context`) admits
356
+ // `consume` and then `var`, independently and in that order. `consume` is
357
+ // the affine FFI/resource ownership modifier, e.g.
358
+ // `fn hew_handle_free(consume h: Handle);`, and `consume var items: T`
359
+ // is accepted — `var consume` is not.
360
+ // In trait/impl method position a leading bare receiver is also accepted:
361
+ // `self`, `var self`, or `consume self` — all without a type annotation, as
362
+ // `Self`-typed receiver sugar (hew-parser/src/parser/types.rs
363
+ // `eat_consume_self_receiver`). `consuming self` is refused by the compiler
364
+ // with a fix-it and `this` is an ordinary identifier.
365
+ parameter: $ => choice(
366
+ $.self_parameter,
367
+ seq(
368
+ optional('consume'),
369
+ optional('var'),
370
+ field('name', $.identifier),
371
+ ':',
372
+ field('type', $._type),
373
+ ),
296
374
  ),
297
375
 
298
- parameters: $ => sep1($.parameter, ','),
299
-
300
- parameter: $ => seq(
301
- field('name', choice($.identifier, $.self)),
302
- ':',
303
- field('type', $._type),
376
+ self_parameter: $ => seq(
377
+ optional(choice('var', 'consume')),
378
+ $.self,
304
379
  ),
305
380
 
306
- return_type: $ => seq('->', $._type),
381
+ return_type: $ => prec.right(seq('->', $._type, optional($.failure_return))),
307
382
 
308
- visibility: $ => seq('pub', optional(seq('(', choice('package', 'super'), ')'))),
383
+ failure_return: $ => seq('fails', field('error', $._type)),
384
+
385
+ // Visibility = "pub" | "package" (spec grammar.ebnf:53; both standalone
386
+ // keywords per hew-parser/src/parser.rs:1863 parse_visibility).
387
+ visibility: $ => choice('pub', 'package'),
309
388
 
310
389
  // ---- Actors ----
311
390
 
@@ -316,14 +395,19 @@ export default grammar({
316
395
  optional(seq(':', $.trait_bounds)),
317
396
  optional($.where_clause),
318
397
  '{',
319
- optional($.actor_init),
320
398
  repeat(choice(
399
+ $.actor_init,
321
400
  $.actor_field,
322
401
  $.mailbox_declaration,
323
- $.receive_function,
324
- $.receive_gen_function,
325
- $.function_declaration,
326
- $.gen_function_declaration,
402
+ // Receive handlers carry the same declaration attributes as actor
403
+ // methods (for example, `#[every(1s)] receive fn tick() { ... }`).
404
+ // Keep the attributes at the actor-member boundary: the real parser
405
+ // stores them on the handler declaration rather than treating them as
406
+ // standalone items.
407
+ seq(repeat($.attribute), $.receive_function),
408
+ seq(repeat($.attribute), $.receive_gen_function),
409
+ seq(repeat($.attribute), $.function_declaration),
410
+ seq(repeat($.attribute), $.gen_function_declaration),
327
411
  )),
328
412
  '}',
329
413
  ),
@@ -348,14 +432,13 @@ export default grammar({
348
432
  $.overflow_kind,
349
433
  ),
350
434
 
435
+ // @sync:overflow_kinds
351
436
  overflow_kind: $ => choice(
352
- 'block',
353
- 'drop_new',
354
- 'drop_old',
355
- 'fail',
437
+ 'block', 'drop_new', 'drop_old', 'fail',
356
438
  seq('coalesce', '(', $.identifier, ')', optional(seq('fallback', $.overflow_kind))),
357
439
  ),
358
440
 
441
+ // Actor state uses the same declaration terminator as record fields.
359
442
  actor_field: $ => seq(
360
443
  choice('let', 'var'),
361
444
  field('name', $.identifier),
@@ -366,7 +449,6 @@ export default grammar({
366
449
  ),
367
450
 
368
451
  receive_function: $ => seq(
369
- optional('pure'),
370
452
  'receive',
371
453
  'fn',
372
454
  field('name', $.identifier),
@@ -380,7 +462,6 @@ export default grammar({
380
462
  ),
381
463
 
382
464
  receive_gen_function: $ => seq(
383
- optional('pure'),
384
465
  'receive',
385
466
  'gen',
386
467
  'fn',
@@ -400,45 +481,164 @@ export default grammar({
400
481
  supervisor_declaration: $ => seq(
401
482
  'supervisor',
402
483
  field('name', $.identifier),
484
+ optional($.type_parameters),
485
+ optional(seq(
486
+ '(',
487
+ optional($.parameters),
488
+ ')',
489
+ )),
403
490
  '{',
404
491
  repeat(choice($.child_spec, $.supervisor_field)),
405
492
  '}',
406
493
  ),
407
494
 
408
- supervisor_field: $ => prec(5, seq(
409
- field('name', $.identifier),
410
- ':',
411
- field('value', $.expression),
412
- optional(choice(',', ';')),
413
- )),
495
+ // SupervisorField (spec grammar.ebnf:142-144):
496
+ // "strategy" ":" SupervisorStrategy
497
+ // "intensity" ":" IntLit "within" DurationLit
498
+ // `within` is a contextual keyword used only here. DurationLit is reused.
499
+ supervisor_field: $ => choice(
500
+ seq('strategy', ':', field('strategy', $.supervisor_strategy_value), ';'),
501
+ seq('intensity', ':', field('restarts', $.integer_literal), 'within', field('window', $.duration_literal), ';'),
502
+ ),
414
503
 
415
504
  child_spec: $ => seq(
416
- 'child',
505
+ // @sync:child_kinds
506
+ choice('child', 'pool'),
417
507
  field('name', $.identifier),
418
508
  ':',
419
- field('actor', $.identifier),
420
- optional($.restart_spec),
509
+ field('actor', $._type),
510
+ optional(seq('(', optional(sep1($.call_argument, ',')), optional(','), ')')),
511
+ repeat($.child_clause),
421
512
  ';',
422
513
  ),
423
514
 
424
- restart_spec: $ => seq(
425
- 'restart',
426
- '(',
427
- choice('permanent', 'transient', 'temporary'),
428
- ')',
429
- optional(seq('budget', '(', $.integer_literal, ',', $.duration_literal, ')')),
430
- optional(seq('strategy', '(', choice('one_for_one', 'one_for_all', 'rest_for_one'), ')')),
515
+ child_clause: $ => choice(
516
+ seq('count', ':', $.integer_literal),
517
+ seq('restart', ':', choice('permanent', 'transient', 'temporary')),
518
+ seq('stop', ':', $.expression),
519
+ seq('wired_to', ':', '{', repeat(seq($.identifier, optional(seq(':', $.identifier)), optional(','))), '}'),
431
520
  ),
432
521
 
522
+ // @sync:duration_suffixes
433
523
  duration_literal: $ => seq($.integer_literal, choice('ns', 'us', 'ms', 's', 'm', 'h')),
434
524
 
525
+ // ---- Machines ----
526
+
527
+ machine_declaration: $ => seq(
528
+ 'machine',
529
+ field('name', $.identifier),
530
+ optional($.machine_type_parameters),
531
+ optional($.where_clause),
532
+ '{',
533
+ field('events_header', $.machine_events_header),
534
+ optional(field('emits_header', $.machine_emits_header)),
535
+ repeat(choice(
536
+ $.machine_state,
537
+ $.machine_transition,
538
+ $.machine_default,
539
+ )),
540
+ '}',
541
+ ),
542
+
543
+ // Bodyless event and state declarations end with semicolons.
544
+ machine_events_header: $ => seq(
545
+ 'events',
546
+ '{',
547
+ repeat($.machine_event_decl),
548
+ '}',
549
+ ),
550
+
551
+ machine_event_decl: $ => seq(
552
+ field('name', $.identifier),
553
+ choice(';', seq('{', repeat($.struct_field), '}')),
554
+ ),
555
+
556
+ // Output events use the same declaration terminators as inputs.
557
+ // Output events carry the same optional payload shape as inputs
558
+ // (hew-parser actor_machine_supervisor.rs `parse_machine_event_fields`).
559
+ machine_emits_header: $ => seq(
560
+ 'emits',
561
+ '{',
562
+ repeat($.machine_event_decl),
563
+ '}',
564
+ ),
565
+
566
+ // States may contain fields, hooks, substates and transition rules.
567
+ machine_state: $ => seq(
568
+ 'state',
569
+ field('name', $.identifier),
570
+ choice(
571
+ ';',
572
+ seq(
573
+ '{',
574
+ repeat($.struct_field),
575
+ optional(seq('entry', field('entry_body', $.block))),
576
+ optional(seq('exit', field('exit_body', $.block))),
577
+ repeat($.machine_composite_member),
578
+ repeat($.machine_transition),
579
+ '}',
580
+ ),
581
+ ),
582
+ ),
583
+
584
+ // [ "initial" ] StateDecl — depth-1 composite substate
585
+ machine_composite_member: $ => seq(
586
+ optional('initial'),
587
+ $.machine_state,
588
+ ),
589
+
590
+ // on EventIdent [ "(" Ident { "," Ident } ")" ] : Source => Target
591
+ // [ "reenter" ] [ "when" Expr ] TransitionBody
592
+ // Source = Ident ( "." Ident )* | "_" (hew-parser/src/parser/actor_machine_supervisor.rs
593
+ // parse_state_pattern: a leading "." is rejected on the source; a
594
+ // "Composite.Leaf" qualifier strips to "Leaf")
595
+ // Target = Ident | "." Ident | "_" (same fn: the "." Ident contextual
596
+ // form is the bare-variant target the checker's fix-it steers authors
597
+ // towards; "._" is rejected, so the wildcard stays plain "_")
598
+ // TransitionBody = ";" | "{" FieldInitList "}" | Block
599
+ // The `{ FieldInitList }` form supplies the target state's payload, e.g.
600
+ // `=> Holding { handle: handle }` or `=> .Holding { handle: handle }`.
601
+ machine_transition: $ => seq(
602
+ 'on',
603
+ field('event', $.identifier),
604
+ optional(seq('(', field('payload_bindings', sep1($.identifier, ',')), ')')),
605
+ ':',
606
+ field('source', choice(seq($.identifier, repeat(seq('.', $.identifier))), '_')),
607
+ '=>',
608
+ field('target', choice($.identifier, $.contextual_variant_expression, '_')),
609
+ optional('reenter'),
610
+ optional(seq('when', field('guard', $.expression))),
611
+ // A bodyless transition needs a semicolon; a body ends at `}`.
612
+ choice(
613
+ ';',
614
+ field('payload', seq(
615
+ '{',
616
+ sep1($.field_initializer, ','),
617
+ optional(','),
618
+ '}',
619
+ )),
620
+ field('body', $.block),
621
+ ),
622
+ ),
623
+
624
+ // default { state } — covers all remaining uncovered (state, event) cells
625
+ machine_default: $ => seq(
626
+ 'default',
627
+ '{',
628
+ 'state',
629
+ '}',
630
+ ),
631
+
435
632
  // ---- Extern ----
436
633
 
437
634
  extern_block: $ => seq(
438
635
  'extern',
439
636
  $.string_literal,
440
637
  '{',
441
- repeat($.extern_function),
638
+ // Current stdlib extern blocks permit function attributes such as
639
+ // `#[runtime_capability(...)]`; keeping attributes inside the block is
640
+ // necessary for ast-grep's dialect to parse the generated FFI surface.
641
+ repeat(seq(repeat($.attribute), $.extern_function)),
442
642
  '}',
443
643
  ),
444
644
 
@@ -455,14 +655,34 @@ export default grammar({
455
655
 
456
656
  // ---- Types ----
457
657
 
658
+ machine_type_parameters: $ => seq('<', choice(
659
+ sep1($.const_parameter, ','),
660
+ seq(sep1($.type_parameter, ','), optional(seq(',', sep1($.const_parameter, ',')))),
661
+ ), '>'),
662
+
663
+ const_parameter: $ => seq('const', field('name', $.identifier), ':', 'usize', optional(seq('=', $.integer_literal))),
664
+
458
665
  type_parameters: $ => seq('<', sep1($.type_parameter, ','), '>'),
459
666
 
460
- type_parameter: $ => seq(
461
- $.identifier,
462
- optional(seq(':', $.trait_bounds)),
667
+ type_parameter: $ => seq($.identifier, optional(seq(':', $.trait_bounds))),
668
+
669
+ type_arguments: $ => seq('<', sep1($._type_argument, ','), '>'),
670
+
671
+ // A type argument is a type, or an associated-type binding `Ident = Type`
672
+ // (e.g. `Iterator<Item = A>`). The binding form is accepted by the real
673
+ // parser's parse_trait_bound_args (hew-parser/src/parser.rs) for trait
674
+ // bounds; allowing it in any type-arg list is harmlessly permissive.
675
+ _type_argument: $ => choice(
676
+ $._type,
677
+ $.integer_literal,
678
+ $.associated_type_binding,
463
679
  ),
464
680
 
465
- type_arguments: $ => seq('<', sep1($._type, ','), '>'),
681
+ associated_type_binding: $ => seq(
682
+ field('name', $.identifier),
683
+ '=',
684
+ field('type', $._type),
685
+ ),
466
686
 
467
687
  trait_bounds: $ => sep1($.trait_bound, '+'),
468
688
 
@@ -471,6 +691,7 @@ export default grammar({
471
691
  where_clause: $ => seq(
472
692
  'where',
473
693
  sep1($.where_predicate, ','),
694
+ optional(','),
474
695
  ),
475
696
 
476
697
  where_predicate: $ => seq($._type, ':', $.trait_bounds),
@@ -479,25 +700,38 @@ export default grammar({
479
700
  $.primitive_type,
480
701
  $.identifier,
481
702
  $.generic_type,
703
+ $.scoped_type,
704
+ $.associated_type_projection,
482
705
  $.tuple_type,
483
706
  $.array_type,
484
707
  $.slice_type,
485
708
  $.function_type,
709
+ $.actor_type,
486
710
  $.pointer_type,
711
+ $.borrow_type,
487
712
  $.trait_object_type,
488
713
  $.unit_type,
489
714
  ),
490
715
 
716
+ // @sync:primitive_types
491
717
  primitive_type: $ => choice(
492
- 'i8', 'i16', 'i32', 'i64',
493
- 'u8', 'u16', 'u32', 'u64',
494
- 'f32', 'f64',
495
- 'isize', 'usize',
496
- 'bool', 'char', 'string', 'bytes', 'void', 'duration',
718
+ 'i8', 'i16', 'i32', 'i64', 'u8', 'u16', 'u32', 'u64', 'isize', 'usize',
719
+ 'f32', 'f64', 'bool', 'char', 'string', 'bytes', 'duration', 'instant',
497
720
  ),
498
721
 
499
722
  generic_type: $ => prec(1, seq($.identifier, $.type_arguments)),
500
723
 
724
+ // Qualified type name: `mod.Type` or `a.b.C`, with an optional trailing
725
+ // type-arg list (e.g. `mod.Worker<T>`). Dotted paths are used throughout
726
+ // the current surface for cross-module types (`net.NetError`, `fs.IoError`).
727
+ scoped_type: $ => seq(
728
+ $.identifier,
729
+ repeat1(seq('.', $.identifier)),
730
+ optional($.type_arguments),
731
+ ),
732
+
733
+ associated_type_projection: $ => seq('<', $._type, 'as', $.trait_bound, '>', '.', $.identifier),
734
+
501
735
  tuple_type: $ => seq('(', sep1($._type, ','), ')'),
502
736
 
503
737
  unit_type: $ => seq('(', ')'),
@@ -506,9 +740,30 @@ export default grammar({
506
740
 
507
741
  slice_type: $ => seq('[', $._type, ']'),
508
742
 
509
- function_type: $ => prec(1, seq('fn', '(', optional(sep1($._type, ',')), ')', optional($.return_type))),
743
+ function_type: $ => prec(1, seq(
744
+ 'fn', optional($.callable_capabilities),
745
+ '(', optional(sep1($._type, ',')), ')', optional($.return_type),
746
+ )),
747
+
748
+ // actor(M) [-> R] — the type of an anonymous actor handle
749
+ // (hew-parser/src/parser/types.rs, `Token::Actor` in the type parser); a named actor's
750
+ // handle is spelled by the actor's own name.
751
+ actor_type: $ => prec(1, seq(
752
+ 'actor', '(', optional(sep1($._type, ',')), ')', optional($.return_type),
753
+ )),
754
+
755
+ // Bracketed callable qualifiers (hew-parser/src/parser/types.rs
756
+ // `parse_callable_qualifiers`): `var` or `once` (call mode, at most one),
757
+ // `clone` and `suspends`, comma-separated in any order, e.g.
758
+ // `fn[once, suspends](i64) -> i64`. Only `var` is reserved elsewhere.
759
+ callable_capabilities: $ => seq('[', sep1(choice('var', 'once', 'clone', 'suspends'), ','), ']'),
760
+
761
+ // *const T / *mut T — raw pointer types. Real but FFI/unsafe-scoped; *var T is legacy and rejected.
762
+ pointer_type: $ => seq('*', choice('const', 'mut'), $._type),
510
763
 
511
- pointer_type: $ => seq('*', optional('var'), $._type),
764
+ // &T — immutable non-owning borrow marker (hew-parser/src/parser.rs:5126,
765
+ // Token::Ampersand → TypeExpr::Borrow). `&mut T` / `&var T` are rejected.
766
+ borrow_type: $ => seq('&', $._type),
512
767
 
513
768
  trait_object_type: $ => seq('dyn', choice(
514
769
  $.trait_bound,
@@ -520,29 +775,41 @@ export default grammar({
520
775
  block: $ => seq('{', repeat($._statement), '}'),
521
776
 
522
777
  _statement: $ => choice(
778
+ // A stray `;` after a statement is accepted by the compiler with an
779
+ // "unnecessary semicolon" warning (hew-parser statements.rs), so
780
+ // `defer { … };` and `};` stay ERROR-free here.
781
+ $.empty_statement,
523
782
  $.let_statement,
524
783
  $.var_statement,
525
784
  $.assignment_statement,
526
785
  $.defer_statement,
786
+ $.emit_statement,
527
787
 
528
788
  $.for_statement,
529
789
  $.while_statement,
530
790
  $.loop_statement,
531
791
  $.break_statement,
532
792
  $.continue_statement,
533
- $.return_statement,
534
793
  $.expression_statement,
535
794
  $.if_statement,
536
795
  $.match_statement,
537
796
  $.block_statement,
797
+ $.block_like_statement,
538
798
  ),
539
799
 
800
+ // LetStmt (grammar.ebnf): the initializer is optional — `let x: i64;` is a
801
+ // bare declaration whose definite-assignment is checked later by the move
802
+ // checker (examples/v05/checked-mir/reject/init_before_use.hew).
540
803
  let_statement: $ => seq(
541
804
  'let',
542
805
  field('pattern', $.pattern),
806
+ // `let r? = expr;` was removed from the compiler (statements.rs refuses it
807
+ // with a hint); only `let r = expr?;` remains.
543
808
  optional(seq(':', field('type', $._type))),
544
- '=',
545
- field('value', $.expression),
809
+ optional(seq('=', field('value', choice($.expression, $.block)))),
810
+ // let-else: `let Pat = expr else { <diverging block> };`
811
+ // (hew-parser statements.rs:312-341).
812
+ optional(seq('else', field('else', $.block))),
546
813
  ';',
547
814
  ),
548
815
 
@@ -557,36 +824,87 @@ export default grammar({
557
824
 
558
825
  assignment_statement: $ => seq(
559
826
  field('left', $.expression),
827
+ // @sync:assignment_operators
560
828
  field('operator', choice('=', '+=', '-=', '*=', '/=', '%=', '&=', '|=', '^=', '<<=', '>>=')),
561
829
  field('right', $.expression),
562
830
  ';',
563
831
  ),
564
832
 
565
- match_arm: $ => prec(3, seq(
833
+ _match_arm_head: $ => seq(
566
834
  field('pattern', $.pattern),
567
- optional(seq('if', field('guard', $.expression))),
835
+ // The compiler accepts a block-valued, including diverging, match guard.
836
+ optional(seq('if', field('guard', choice($.expression, $.block)))),
568
837
  '=>',
569
- field('value', choice(
570
- seq($.block, optional(',')),
571
- seq($.expression, optional(',')),
572
- )),
838
+ ),
839
+
840
+ match_arm: $ => prec(3, seq(
841
+ $._match_arm_head,
842
+ field('value', choice($._block_arm_value, seq($.expression, ','))),
573
843
  )),
574
844
 
845
+ _match_final_arm: $ => prec(3, seq($._match_arm_head, field('value', $.expression))),
846
+
847
+ // Match and select share the compiler's source-token classification:
848
+ // brace-bodied expressions end at `}`, while other expressions need `,`.
849
+ _block_arm_value: $ => choice(
850
+ $.block,
851
+ $.if_expression,
852
+ $.match_expression,
853
+ $.scope_expression,
854
+ $.unsafe_expression,
855
+ $.select_expression,
856
+ seq('fork', $.block),
857
+ ),
858
+
859
+
575
860
 
861
+ break_statement: $ => prec.right(seq('break', optional($.label), optional(';'))),
576
862
 
577
- break_statement: $ => seq('break', optional($.label), optional($.expression), ';'),
863
+ continue_statement: $ => prec.right(seq('continue', optional($.label), optional(';'))),
578
864
 
579
- continue_statement: $ => seq('continue', optional($.label), ';'),
865
+ // `return [expr]` is an expression (Never-typed) in Hew, not a statement:
866
+ // it is valid in tail position without `;` and inside larger expressions
867
+ // (`b || return 0`, `g(if c { 1 } else { return 0 })`). See hew commit
868
+ // 480456ec5 (return-as-expression) and hew-parser expressions.rs:1125-1150.
869
+ // Statement position is handled by `expression_statement` (optional `;`).
870
+ return_expression: $ => prec.right(seq('return', optional($.expression))),
580
871
 
581
- return_statement: $ => seq('return', optional($.expression), ';'),
872
+ // Deferred cleanup may be an expression statement or a scoped block.
873
+ // `defer <expr>;` or `defer { … }` — a block body needs no `;`
874
+ // (hew-parser statements.rs `Token::Defer`).
875
+ defer_statement: $ => seq('defer', choice(seq($.expression, ';'), $.block)),
582
876
 
583
- defer_statement: $ => seq('defer', $.expression, ';'),
877
+ // emit EventName { field: value, … } ; — Mealy output inside a transition body
878
+ emit_statement: $ => seq(
879
+ 'emit',
880
+ field('event', $.identifier),
881
+ optional(seq('{', optional(seq(sep1($.field_initializer, ','), optional(','))), '}')),
882
+ ';',
883
+ ),
584
884
 
585
- expression_statement: $ => seq($.expression, optional(';')),
885
+ // prec.right keeps the `;` attached to the expression it ends instead of
886
+ // parsing it as an empty statement.
887
+ expression_statement: $ => prec.right(seq($.expression, optional(';'))),
586
888
 
587
- if_statement: $ => $.if_expression,
588
- match_statement: $ => $.match_expression,
889
+ empty_statement: $ => ';',
589
890
 
891
+ // A block-like form at the start of a statement ends the statement at its
892
+ // `}` (HEW-SPEC-2026 §12.2): a `.Ok(x)` on the next line is a new
893
+ // expression, never a method call on the block. The dynamic precedence
894
+ // makes the statement reading win over the operand reading.
895
+ if_statement: $ => prec.dynamic(1, $.if_expression),
896
+ match_statement: $ => prec.dynamic(1, $.match_expression),
897
+ block_like_statement: $ => prec.dynamic(1, choice(
898
+ $.unsafe_expression,
899
+ $.scope_expression,
900
+ $.select_expression,
901
+ $.race_expression,
902
+ alias(seq('fork', $.block), $.fork_expression),
903
+ $.gen_block_expression,
904
+ )),
905
+
906
+ // A block used in statement position may carry a trailing `;`
907
+ // (examples/test_block.hew, examples/lambda_actors.hew).
590
908
  block_statement: $ => $.block,
591
909
 
592
910
  label: $ => /@[a-zA-Z_][a-zA-Z0-9_]*/,
@@ -595,6 +913,7 @@ export default grammar({
595
913
 
596
914
  expression: $ => choice(
597
915
  $.identifier,
916
+ alias('capture', $.identifier),
598
917
  $.self,
599
918
  $._literal,
600
919
  $.interpolated_string,
@@ -605,46 +924,80 @@ export default grammar({
605
924
  $.field_expression,
606
925
  $.index_expression,
607
926
  $.try_expression,
927
+ $.cast_expression,
608
928
  $.await_expression,
929
+ $.clone_expression,
609
930
  $.struct_init,
931
+ $.generic_struct_init,
932
+ $.contextual_variant_expression,
933
+ $.contextual_variant_record_init,
934
+ $.generic_apply_expression,
610
935
  $.array_expression,
611
936
  $.array_repeat,
937
+ $.map_literal,
612
938
  $.tuple_expression,
939
+ $.unit_expression,
613
940
  $.parenthesized_expression,
614
941
  $.if_expression,
615
942
  $.match_expression,
616
943
  $.lambda,
944
+ $.actor_expression,
617
945
  $.spawn_expression,
618
946
  $.select_expression,
619
- $.join_expression,
947
+ $.race_expression,
948
+ $.fork_expression,
949
+ $.handle_expression,
620
950
  $.scope_expression,
621
- $.cooperate_expression,
622
951
  $.yield_expression,
623
- $.path_expression,
952
+ $.return_expression,
953
+ $.gen_block_expression,
954
+ $.qualified_expression,
955
+ $.generic_call_expression,
956
+ $.bare_generic_call_expression,
957
+ $.byte_array_expression,
624
958
  $.unsafe_expression,
625
959
  ),
626
960
 
961
+ // @sync:supervisor_strategies
962
+ // Real lexer keywords (hew-lexer lex:189-195); used as the value of the
963
+ // `strategy:` field in a supervisor declaration body.
964
+ supervisor_strategy_value: $ => choice(
965
+ 'one_for_one',
966
+ 'one_for_all',
967
+ 'rest_for_one',
968
+ 'simple_one_for_one',
969
+ ),
970
+
627
971
  unary_expression: $ => prec(PREC.UNARY, seq(
628
972
  field('operator', choice('!', '-', '~')),
629
973
  field('operand', $.expression),
630
974
  )),
631
975
 
976
+ // The right operand may be a block (`text + { text = other; ":tail" }`):
977
+ // the real parser treats a block as an ordinary primary expression.
632
978
  binary_expression: $ => choice(
633
- prec.right(PREC.SEND, seq($.expression, '<-', $.expression)),
634
- prec.left(PREC.OR, seq($.expression, '||', $.expression)),
635
- prec.left(PREC.BIT_OR, seq($.expression, '|', $.expression)),
636
- prec.left(PREC.BIT_XOR, seq($.expression, '^', $.expression)),
637
- prec.left(PREC.AND, seq($.expression, '&&', $.expression)),
638
- prec.left(PREC.BIT_AND, seq($.expression, '&', $.expression)),
639
- prec.left(PREC.EQ, seq($.expression, choice('==', '!=', '=~', '!~'), $.expression)),
640
- prec.left(PREC.REL, seq($.expression, choice('<', '<=', '>', '>='), $.expression)),
641
- prec.right(PREC.RANGE, seq($.expression, choice('..', '..='), $.expression)),
642
- prec.left(PREC.SHIFT, seq($.expression, choice('<<', '>>'), $.expression)),
643
- prec.left(PREC.ADD, seq($.expression, choice('+', '-'), $.expression)),
644
- prec.left(PREC.MUL, seq($.expression, choice('*', '/', '%'), $.expression)),
645
- ),
646
-
647
- call_expression: $ => prec(PREC.POSTFIX, seq(
979
+ prec.right(PREC.COALESCE, seq($.expression, '??', $._binary_operand)),
980
+ prec.left(PREC.OR, seq($.expression, '||', $._binary_operand)),
981
+ prec.left(PREC.BIT_OR, seq($.expression, '|', $._binary_operand)),
982
+ prec.left(PREC.BIT_XOR, seq($.expression, '^', $._binary_operand)),
983
+ prec.left(PREC.AND, seq($.expression, '&&', $._binary_operand)),
984
+ prec.left(PREC.BIT_AND, seq($.expression, '&', $._binary_operand)),
985
+ prec.left(PREC.EQ, seq($.expression, choice('==', '!=', 'is'), $._binary_operand)),
986
+ prec.left(PREC.REL, seq($.expression, choice('<', '<=', '>', '>='), $._binary_operand)),
987
+ prec.right(PREC.RANGE, seq($.expression, choice('..', '..='), $._binary_operand)),
988
+ prec.left(PREC.SHIFT, seq($.expression, choice('<<', '>>'), $._binary_operand)),
989
+ // '+' also concatenates strings; '&+'/'&-' are two's-complement wrapping
990
+ // forms (grammar.ebnf:233, Hew.g4:658).
991
+ prec.left(PREC.ADD, seq($.expression, choice('+', '-', '&+', '&-'), $._binary_operand)),
992
+ // '&*' is two's-complement wrapping multiply (grammar.ebnf:234, Hew.g4:663).
993
+ prec.left(PREC.MUL, seq($.expression, choice('*', '/', '%', '&*'), $._binary_operand)),
994
+ ),
995
+
996
+ _binary_operand: $ => choice($.expression, $.block),
997
+
998
+ // Left associativity keeps postfix calls composable after a completed
999
+ // field/call chain (`value.slice(...).to_lower()`).
1000
+ call_expression: $ => prec.left(PREC.FIELD + 1, seq(
648
1001
  field('function', $.expression),
649
1002
  '(',
650
1003
  optional(sep1($.call_argument, ',')),
@@ -652,21 +1005,86 @@ export default grammar({
652
1005
  ')',
653
1006
  )),
654
1007
 
655
- call_argument: $ => choice(
656
- seq(field('name', $.identifier), ':', field('value', $.expression)),
657
- $.expression,
1008
+ // A member call must remain a single postfix expression even where `(`
1009
+ // could begin the following match pattern. Its function is structurally a
1010
+ // field expression, so this does not overlap plain calls or grouping.
1011
+ method_call_expression: $ => prec.dynamic(2, prec.left(PREC.FIELD + 2, seq(
1012
+ field('object', $._member_object),
1013
+ '.',
1014
+ field('method', $.identifier),
1015
+ '(',
1016
+ optional(sep1($.call_argument, ',')),
1017
+ optional(','),
1018
+ ')',
1019
+ ))),
1020
+
1021
+ _member_object: $ => choice(
1022
+ $.identifier,
1023
+ $.self,
1024
+ $._literal,
1025
+ $.parenthesized_expression,
1026
+ $.qualified_expression,
1027
+ $.call_expression,
1028
+ $.field_expression,
1029
+ $.index_expression,
1030
+ $.method_call_expression,
1031
+ $.generic_apply_expression,
658
1032
  ),
659
1033
 
660
- method_call_expression: $ => prec(PREC.POSTFIX, seq(
661
- field('object', $.expression),
1034
+ // Dotted path expression `path.name`. It composes with calls and the generic
1035
+ // call form below while keeping path-headed struct inits separate.
1036
+ qualified_expression: $ => prec.left(PREC.POSTFIX, seq(
1037
+ field('path', $.expression),
662
1038
  '.',
663
- field('method', $.identifier),
1039
+ field('name', $.identifier),
1040
+ )),
1041
+
1042
+ // Generic call `path.method<Type, …>(args)`. Restrict the callee to a dotted
1043
+ // path so `a < b` stays unambiguously a comparison. Dynamic precedence commits
1044
+ // the `<` to type arguments when a closing `>` and call opener follow.
1045
+ generic_call_expression: $ => prec.dynamic(1, prec(PREC.POSTFIX, seq(
1046
+ field('function', $.qualified_expression),
1047
+ $.type_arguments,
1048
+ '(',
1049
+ optional(sep1($.call_argument, ',')),
1050
+ optional(','),
1051
+ ')',
1052
+ ))),
1053
+
1054
+ // The compiler accepts a type application before later postfixes, so
1055
+ // `Vec<i64>.new()` is distinct from a generic call such as `f<i64>()`.
1056
+ generic_apply_expression: $ => prec.dynamic(-1, prec(PREC.POSTFIX, seq(
1057
+ field('target', choice($.identifier, $.qualified_expression)),
1058
+ token.immediate('<'),
1059
+ sep1($._type, ','),
1060
+ '>',
1061
+ ))),
1062
+
1063
+ // A bare generic call is lexically ambiguous with comparison. Current
1064
+ // accepted source spells its type-argument opener adjacent to the callee,
1065
+ // which is also the compiler's canonical formatter output. Requiring that
1066
+ // adjacency keeps `i < n` and `a < b` unambiguously binary expressions.
1067
+ bare_generic_call_expression: $ => prec(PREC.POSTFIX, seq(
1068
+ field('function', $.identifier),
1069
+ token.immediate('<'),
1070
+ sep1($._type, ','),
1071
+ '>',
664
1072
  '(',
665
- optional(sep1($.expression, ',')),
1073
+ optional(sep1($.call_argument, ',')),
666
1074
  optional(','),
667
1075
  ')',
668
1076
  )),
669
1077
 
1078
+ // A block is an ordinary argument expression to the real parser
1079
+ // (`f({ side_effect(); value })`); it is admitted here rather than in the
1080
+ // general expression set so a statement-level `{` still opens a block
1081
+ // statement.
1082
+ call_argument: $ => choice(
1083
+ seq(field('name', $.identifier), ':', field('value', choice($.expression, $.block))),
1084
+ $.expression,
1085
+ $.block,
1086
+ ),
1087
+
670
1088
  field_expression: $ => prec(PREC.FIELD, seq(
671
1089
  field('object', $.expression),
672
1090
  '.',
@@ -676,7 +1094,12 @@ export default grammar({
676
1094
  index_expression: $ => prec(PREC.POSTFIX, seq(
677
1095
  field('object', $.expression),
678
1096
  '[',
679
- field('index', $.expression),
1097
+ // Indexing and range slicing share the postfix brackets. Both range
1098
+ // endpoints are optional (`[..]`, `[..end]`, `[start..]`).
1099
+ choice(
1100
+ field('index', $.expression),
1101
+ seq(optional(field('start', $.expression)), choice('..', '..='), optional(field('end', $.expression))),
1102
+ ),
680
1103
  ']',
681
1104
  )),
682
1105
 
@@ -685,40 +1108,125 @@ export default grammar({
685
1108
  '?',
686
1109
  )),
687
1110
 
1111
+ cast_expression: $ => prec(PREC.POSTFIX, seq(
1112
+ field('value', $.expression),
1113
+ 'as',
1114
+ field('type', $._type),
1115
+ )),
1116
+
688
1117
  await_expression: $ => prec(PREC.UNARY, seq(
689
1118
  'await',
1119
+ choice($.expression, $.block),
1120
+ )),
1121
+
1122
+ // `clone <operand>` prefix duplication (grammar.ebnf:242-248). Contextual:
1123
+ // `clone` is only a prefix when an operand follows; `x.clone()`, `fn clone()`,
1124
+ // and `clone(args)` keep `clone` as an ordinary identifier (tree-sitter keyword
1125
+ // extraction via word:$.identifier preserves identifier use elsewhere).
1126
+ clone_expression: $ => prec(PREC.UNARY, seq(
1127
+ 'clone',
690
1128
  $.expression,
691
1129
  )),
692
1130
 
693
1131
  struct_init: $ => prec.dynamic(1, seq(
1132
+ field('name', choice($.identifier, $.path_expression)),
1133
+ '{',
1134
+ optional(seq(sep1($.field_initializer, ','), optional(','))),
1135
+ '}',
1136
+ )),
1137
+
1138
+ // As with a bare generic call, adjacency of `<` distinguishes a generic
1139
+ // constructor from comparison while preserving `i < n`.
1140
+ generic_struct_init: $ => prec.dynamic(2, seq(
1141
+ field('name', choice($.identifier, $.path_expression)),
1142
+ token.immediate('<'),
1143
+ sep1($._type, ','),
1144
+ '>',
1145
+ '{',
1146
+ optional(seq(sep1($.field_initializer, ','), optional(','))),
1147
+ '}',
1148
+ )),
1149
+
1150
+ // Expected-type contextual enum constructors are valid expressions in all
1151
+ // three payload forms: `.None`, `.Some(value)`, and `.Ready { value }`.
1152
+ contextual_variant_expression: $ => prec(PREC.POSTFIX, seq(
1153
+ '.',
1154
+ field('name', $.identifier),
1155
+ )),
1156
+
1157
+ contextual_variant_record_init: $ => prec.dynamic(2, seq(
1158
+ '.',
694
1159
  field('name', $.identifier),
695
1160
  '{',
696
- sep1($.field_initializer, ','),
697
- optional(','),
1161
+ optional(seq(sep1($.field_initializer, ','), optional(','))),
698
1162
  '}',
699
1163
  )),
700
1164
 
701
1165
  field_initializer: $ => seq(
702
- field('name', $.identifier),
703
- ':',
704
- field('value', $.expression),
1166
+ choice(
1167
+ seq(field('name', $.identifier), ':', field('value', $.expression)),
1168
+ seq('..', field('spread', $.expression)),
1169
+ ),
705
1170
  ),
706
1171
 
707
1172
  array_expression: $ => seq(
708
1173
  '[',
1174
+ optional(seq(sep1($._array_element, ','), optional(','))),
1175
+ ']',
1176
+ ),
1177
+
1178
+ _array_element: $ => choice($.expression, seq('..', field('spread', $.expression))),
1179
+
1180
+ // Byte array literal `bytes[0x41, 0x42]` (grammar.ebnf:260
1181
+ // Primary `"bytes" "[" ExprList? "]"`). The opener is a single merged
1182
+ // `bytes[` token (not a bare `bytes` keyword) so the lexeme reserved is
1183
+ // `bytes[`, never bare `bytes`; this keeps `bytes` usable as a value path
1184
+ // (`bytes.new()`, 35× in std) and as the `bytes` primitive type. Requires
1185
+ // `[` adjacent to `bytes`, which is the only literal form (0 spaced uses).
1186
+ // The compiler also accepts ordinary whitespace between the keyword and
1187
+ // opener. Keep the opener merged so bare `bytes` remains usable as a path
1188
+ // or primitive type; splitting it into `bytes`, `[` would reserve every
1189
+ // `bytes.new()` expression as the start of this literal.
1190
+ byte_array_expression: $ => seq(
1191
+ token(choice(seq('bytes', '['), /bytes[ \t\r\n]+\[/)),
709
1192
  optional(seq(sep1($.expression, ','), optional(','))),
710
1193
  ']',
711
1194
  ),
712
1195
 
713
1196
  array_repeat: $ => seq('[', $.expression, ';', $.expression, ']'),
714
1197
 
1198
+ map_literal: $ => seq(
1199
+ '{',
1200
+ optional(seq(sep1($.map_entry, ','), optional(','))),
1201
+ '}',
1202
+ ),
1203
+
1204
+ map_entry: $ => seq(
1205
+ field('key', $.expression),
1206
+ ':',
1207
+ field('value', $.expression),
1208
+ ),
1209
+
715
1210
  tuple_expression: $ => seq('(', $.expression, ',', optional(sep1($.expression, ',')), ')'),
716
1211
 
717
1212
  parenthesized_expression: $ => seq('(', $.expression, ')'),
718
1213
 
1214
+ // Unit value `()` — the empty tuple expression (hew-parser/src/parser.rs
1215
+ // parse_primary: `( )` → Expr::Tuple(Vec::new())). Distinct from the unit
1216
+ // *type* `()` which appears only in type position.
1217
+ unit_expression: $ => seq('(', ')'),
1218
+
1219
+ _condition: $ => choice($.expression, $.block, $.let_condition, $.condition_chain),
1220
+
1221
+ let_condition: $ => prec(PREC.AND + 1, seq('let', field('pattern', $.pattern), '=', field('value', $.expression))),
1222
+
1223
+ condition_chain: $ => prec.left(PREC.AND + 1, seq(
1224
+ choice($.let_condition, $.expression, $.block),
1225
+ repeat1(prec.left(PREC.AND + 1, seq('&&', choice($.let_condition, $.expression, $.block)))),
1226
+ )),
1227
+
719
1228
  if_expression: $ => prec.right(seq(
720
- 'if',
721
- field('condition', $.expression),
1229
+ 'if', field('condition', $._condition),
722
1230
  field('consequence', $.block),
723
1231
  optional(field('alternative', $.else_clause)),
724
1232
  )),
@@ -727,73 +1235,100 @@ export default grammar({
727
1235
 
728
1236
  match_expression: $ => seq(
729
1237
  'match',
730
- field('value', $.expression),
1238
+ field('value', choice($.expression, $.block)),
731
1239
  '{',
732
1240
  repeat($.match_arm),
1241
+ optional(alias($._match_final_arm, $.match_arm)),
733
1242
  '}',
734
1243
  ),
735
1244
 
736
- lambda: $ => prec(2, seq(
1245
+ // Closures are PIPE-style (spec grammar.ebnf:288-295):
1246
+ // Lambda = "move"? "|" LambdaParams? "|" Expr
1247
+ // | "move"? "||" Expr
1248
+ // | "move"? "|" LambdaParams? "|" RetType Block ;
1249
+ // The paren-arrow `(params) => body` form was removed in v0.5
1250
+ // (compiler emits E_CLOSURE_PIPE_SYNTAX). An empty parameter list is the
1251
+ // single `||` token because the lexer fuses adjacent pipes; the bracketed
1252
+ // form `|params|` uses two distinct `|` tokens. A return type forces a
1253
+ // braced body (hew-parser/src/parser.rs:7691 parse_pipe_lambda).
1254
+ lambda: $ => prec.right(2, seq(
737
1255
  optional('move'),
738
- '(',
739
- optional(sep1($.lambda_parameter, ',')),
740
- ')',
741
- optional($.return_type),
742
- '=>',
743
- choice($.expression, $.block),
1256
+ optional($.private_capture_list),
1257
+ field('parameters', choice(
1258
+ '||',
1259
+ seq('|', optional(sep1($.lambda_parameter, ',')), '|'),
1260
+ )),
1261
+ choice(
1262
+ seq($.return_type, field('body', $.block)),
1263
+ field('body', $.block),
1264
+ field('body', $.expression),
1265
+ ),
744
1266
  )),
745
1267
 
1268
+ private_capture_list: $ => seq(
1269
+ 'capture', '(', sep1($.private_capture, ','), ')',
1270
+ ),
1271
+
1272
+ private_capture: $ => seq('var', field('name', $.identifier)),
1273
+
746
1274
  lambda_parameter: $ => seq(
747
1275
  field('name', $.identifier),
748
1276
  optional(seq(':', field('type', $._type))),
749
1277
  ),
750
1278
 
1279
+ // actor [move] |params| [-> Ret] { body } — inline actor literal (v0.5)
1280
+ // Replaces the removed `spawn (...) => ...` lambda-actor form.
1281
+ actor_expression: $ => prec(2, seq(
1282
+ 'actor',
1283
+ optional('move'),
1284
+ '|',
1285
+ optional(sep1($.lambda_parameter, ',')),
1286
+ '|',
1287
+ optional($.return_type),
1288
+ field('body', $.block),
1289
+ )),
1290
+
751
1291
  spawn_expression: $ => seq(
752
1292
  'spawn',
753
- choice(
754
- $.actor_spawn,
755
- $.lambda_actor,
756
- ),
1293
+ $.actor_spawn,
757
1294
  ),
758
1295
 
759
1296
  actor_spawn: $ => seq(
760
- field('actor', $.identifier),
1297
+ field('actor', $._type),
761
1298
  optional($.type_arguments),
762
- '(',
763
- optional(sep1($.call_argument, ',')),
764
- ')',
1299
+ optional(seq(
1300
+ '(',
1301
+ optional(sep1($.call_argument, ',')),
1302
+ optional(','),
1303
+ ')',
1304
+ )),
765
1305
  ),
766
1306
 
767
- lambda_actor: $ => prec(2, seq(
768
- optional('move'),
769
- '(',
770
- optional(sep1($.lambda_parameter, ',')),
771
- ')',
772
- optional($.return_type),
773
- '=>',
774
- choice($.expression, $.block),
775
- )),
776
-
777
1307
  select_expression: $ => seq(
778
1308
  'select',
779
1309
  '{',
780
1310
  repeat($.select_arm),
1311
+ optional(alias($._select_final_arm, $.select_arm)),
781
1312
  '}',
782
1313
  ),
783
1314
 
784
- select_arm: $ => choice(
785
- seq($.identifier, 'from', $.expression, '=>', $.expression, ','),
786
- seq('after', $.expression, '=>', $.expression, ','),
787
- ),
1315
+ // Selection binds the result of an await expression or a timer arm.
1316
+ // The source clause is spelled `from`, a contextual identifier rather
1317
+ // than a keyword (hew-parser patterns.rs `parse_select_arm`), so `from`
1318
+ // stays usable as an ordinary name everywhere else.
1319
+ select_arm: $ => prec(3, seq(
1320
+ $._select_arm_head,
1321
+ field('body', choice($._block_arm_value, seq($.expression, ','))),
1322
+ )),
788
1323
 
789
- join_expression: $ => seq(
790
- 'join',
791
- '{',
792
- sep1($.expression, ','),
793
- optional(','),
794
- '}',
1324
+ _select_arm_head: $ => choice(
1325
+ seq(field('binding', $.pattern), 'from', field('source', $.expression), '=>'),
1326
+ seq('after', field('duration', $.expression), '=>'),
795
1327
  ),
796
1328
 
1329
+ // The last expression arm may omit its comma, as in `match`.
1330
+ _select_final_arm: $ => prec(3, seq($._select_arm_head, field('body', $.expression))),
1331
+
797
1332
  loop_statement: $ => prec(10, seq(
798
1333
  optional(seq($.label, ':')),
799
1334
  'loop',
@@ -803,31 +1338,59 @@ export default grammar({
803
1338
  for_statement: $ => prec(10, seq(
804
1339
  optional(seq($.label, ':')),
805
1340
  'for',
806
- optional('await'),
807
1341
  field('pattern', $.pattern),
808
1342
  'in',
809
- field('iterable', $.expression),
1343
+ field('iterable', choice($.expression, $.block)),
810
1344
  field('body', $.block),
811
1345
  )),
812
1346
 
813
1347
  while_statement: $ => prec(10, seq(
814
1348
  optional(seq($.label, ':')),
815
1349
  'while',
816
- field('condition', $.expression),
1350
+ field('condition', $._condition),
817
1351
  field('body', $.block),
818
1352
  )),
819
1353
 
820
1354
  scope_expression: $ => seq(
821
1355
  'scope',
822
- optional(seq('|', field('binding', $.identifier), '|')),
823
- $.block,
1356
+ optional(seq('within', field('duration', $.expression))),
1357
+ field('body', $.block),
1358
+ ),
1359
+
1360
+ race_expression: $ => seq(
1361
+ 'race', '{', optional(seq(sep1($.expression, ','), optional(','))), '}',
1362
+ ),
1363
+
1364
+ fork_expression: $ => prec.right(seq('fork', choice($.expression, $.block))),
1365
+
1366
+ handle_expression: $ => prec.left(1, seq(
1367
+ field('operand', $.expression), 'handle', field('error', $.identifier),
1368
+ field('body', $.block),
1369
+ )),
1370
+
1371
+ // Gen-block expression `gen { … }` (hew-parser/src/parser.rs:7433
1372
+ // `Token::Gen` immediately followed by `{` → `Expr::GenBlock`). Distinct
1373
+ // from the item-level `gen fn`; the body is an ordinary block whose
1374
+ // statements may include `yield`. Corpus: examples/machine/
1375
+ // reject_gen_in_transition.hew, tests/vertical-slice/accept/gen_block_*.hew.
1376
+ gen_block_expression: $ => seq(
1377
+ 'gen',
1378
+ field('body', $.block),
824
1379
  ),
825
- cooperate_expression: $ => 'cooperate',
826
- yield_expression: $ => seq('yield', $.expression),
1380
+
1381
+ // YieldExpr (hew-parser: yield expression, operand optional): "yield" Expr? — the operand is optional, so
1382
+ // bare `yield;` is valid (yields Unit). `prec.right` makes `yield expr`
1383
+ // greedily consume the operand rather than reduce `yield` on its own.
1384
+ yield_expression: $ => prec.right(seq('yield', optional($.expression))),
827
1385
 
828
1386
  unsafe_expression: $ => seq('unsafe', $.block),
829
1387
 
830
- path_expression: $ => seq($.identifier, '::', $.identifier),
1388
+ // Path expression — N-segment `a.b.C`. Used as a value path, in patterns,
1389
+ // and as a struct-init head (`Enum.Variant { .. }`, `mod.Type.Variant { .. }`).
1390
+ path_expression: $ => prec.left(seq(
1391
+ $.identifier,
1392
+ repeat1(seq('.', $.identifier)),
1393
+ )),
831
1394
 
832
1395
 
833
1396
 
@@ -838,21 +1401,56 @@ export default grammar({
838
1401
  $.identifier,
839
1402
  $.path_expression,
840
1403
  $._literal,
1404
+ seq('-', $.integer_literal),
1405
+ '()',
841
1406
  $.tuple_pattern,
842
1407
  $.struct_pattern,
1408
+ $.record_pattern,
1409
+ $.variant_pattern,
843
1410
  $.constructor_pattern,
844
1411
  $.or_pattern,
845
1412
  ),
846
1413
 
847
1414
  tuple_pattern: $ => seq('(', sep1($.pattern, ','), ')'),
848
1415
 
849
- struct_pattern: $ => seq(
1416
+ // Shorthand record destructure `{ a, b }` / `{ a: p, b }` with no type name
1417
+ // (hew commit 6098d7ef9; hew-parser patterns.rs:190-213). The record type is
1418
+ // inferred from the scrutinee.
1419
+ record_pattern: $ => seq(
1420
+ '{',
1421
+ optional($._record_pattern_fields),
1422
+ '}',
1423
+ ),
1424
+
1425
+ // Leading-dot variant pattern `.Variant`, `.Variant(p, …)`, `.Variant { f: p }`
1426
+ // (hew commit 99913e044; hew-parser patterns.rs:67-113). The enum type is left
1427
+ // implicit and resolved against the match scrutinee.
1428
+ variant_pattern: $ => seq(
1429
+ '.',
850
1430
  field('name', $.identifier),
1431
+ optional(choice(
1432
+ seq('(', optional(seq(sep1($.pattern, ','), optional(','))), ')'),
1433
+ seq('{', optional($._record_pattern_fields), '}'),
1434
+ )),
1435
+ ),
1436
+
1437
+ struct_pattern: $ => seq(
1438
+ field('name', choice($.identifier, $.path_expression)),
851
1439
  '{',
852
- optional(seq(sep1($.pattern_field, ','), optional(','))),
1440
+ optional($._record_pattern_fields),
853
1441
  '}',
854
1442
  ),
855
1443
 
1444
+ // A record-like pattern may end with `..` to ignore all unlisted fields.
1445
+ // Keeping rest terminal-only matches the compiler's `Pattern::{Struct,
1446
+ // RecordShorthand}` parser and admits nested/rest-only forms through the
1447
+ // recursive `pattern_field` production.
1448
+ _record_pattern_fields: $ => choice(
1449
+ '..',
1450
+ seq(sep1($.pattern_field, ','), ',', '..', optional(',')),
1451
+ seq(sep1($.pattern_field, ','), optional(',')),
1452
+ ),
1453
+
856
1454
  pattern_field: $ => seq(
857
1455
  field('name', $.identifier),
858
1456
  optional(seq(':', field('pattern', $.pattern))),
@@ -875,16 +1473,22 @@ export default grammar({
875
1473
  $.float_literal,
876
1474
  $.string_literal,
877
1475
  $.raw_string_literal,
1476
+ $.byte_string_literal,
1477
+ $.char_literal,
878
1478
  $.regex_literal,
879
1479
  $.boolean_literal,
880
1480
  $.none_literal,
881
1481
  ),
882
1482
 
1483
+ // The active interpolation suite exercises integer-width spellings (for
1484
+ // example `f"{255u8}"`). Treat each as one literal token so highlighting
1485
+ // and ast-grep preserve the expression boundary instead of showing a
1486
+ // literal followed by an identifier.
883
1487
  integer_literal: $ => token(choice(
884
- /0[xX][0-9a-fA-F][0-9a-fA-F_]*/,
885
- /0[bB][01][01_]*/,
886
- /0[oO][0-7][0-7_]*/,
887
- /[0-9][0-9_]*/,
1488
+ /0[xX][0-9a-fA-F][0-9a-fA-F_]*(i8|i16|i32|i64|isize|u8|u16|u32|u64|usize)?/,
1489
+ /0[bB][01][01_]*(i8|i16|i32|i64|isize|u8|u16|u32|u64|usize)?/,
1490
+ /0[oO][0-7][0-7_]*(i8|i16|i32|i64|isize|u8|u16|u32|u64|usize)?/,
1491
+ /[0-9][0-9_]*(i8|i16|i32|i64|isize|u8|u16|u32|u64|usize)?/,
888
1492
  )),
889
1493
 
890
1494
  float_literal: $ => token(
@@ -902,11 +1506,17 @@ export default grammar({
902
1506
 
903
1507
  string_content: $ => token.immediate(prec(1, /[^"\\]+/)),
904
1508
 
1509
+ // Escapes mirror the real lexer, which accepts `\` + any char (hew-lexer
1510
+ // regex `\\.` and the interpolated-string scanner's `\\` => skip-2). The
1511
+ // `\u{..}` and `\xHH` branches are split out so they form one token (and so
1512
+ // an `\u{..}` inside an f-string is not mistaken for an interpolation); the
1513
+ // `/./` fallback covers `\n \r \t \\ \" \' \0 \{ \}` and any other escape.
905
1514
  escape_sequence: $ => token.immediate(seq(
906
1515
  '\\',
907
1516
  choice(
908
- /[nrt\\"0]/,
1517
+ /u\{[0-9a-fA-F]{1,6}\}/,
909
1518
  /x[0-9a-fA-F]{2}/,
1519
+ /./,
910
1520
  ),
911
1521
  )),
912
1522
 
@@ -922,11 +1532,19 @@ export default grammar({
922
1532
 
923
1533
  fstring_start: $ => token(prec(2, /f"/)),
924
1534
 
925
- interpolated_string_content: $ => token.immediate(prec(1, /[^"\{]+/)),
1535
+ // Exclude `\` (as well as `"` and `{`) so escape sequences like `\"` are
1536
+ // not swallowed into content — this is what lets escapes resume correctly
1537
+ // after an interpolation closes (spec grammar.ebnf:385-386, InterpPart).
1538
+ interpolated_string_content: $ => token.immediate(prec(1, /[^"\\{]+/)),
926
1539
 
1540
+ // `{expr:?}` requests explicit structural/debug rendering (StringPart::
1541
+ // StructuralExpr, hew-parser parser/mod.rs:547-552 — a literal `:?`
1542
+ // suffix stripped from the trimmed sub-expression text, not a general
1543
+ // format-spec mini-language).
927
1544
  interpolation: $ => seq(
928
1545
  token.immediate('{'),
929
1546
  $.expression,
1547
+ optional(seq(':', '?')),
930
1548
  '}',
931
1549
  ),
932
1550
 
@@ -936,12 +1554,23 @@ export default grammar({
936
1554
  '"',
937
1555
  )),
938
1556
 
1557
+ byte_string_literal: $ => token(seq(
1558
+ 'b"',
1559
+ /([^"\\]|\\.)*/,
1560
+ '"',
1561
+ )),
1562
+
1563
+ // Char literal — ported from the hew-lexer regex (hew-lexer/src/lib.rs:502):
1564
+ // '([^'\\]|\\u\{[0-9a-fA-F]{1,6}\}|\\x[0-9a-fA-F][0-9a-fA-F]|\\.)'
1565
+ char_literal: $ => token(/'([^'\\]|\\u\{[0-9a-fA-F]{1,6}\}|\\x[0-9a-fA-F][0-9a-fA-F]|\\.)'/),
1566
+
939
1567
  regex_literal: $ => token(seq(
940
1568
  're"',
941
1569
  /[^"]*/,
942
1570
  '"',
943
1571
  )),
944
1572
 
1573
+ // @sync:boolean_literals
945
1574
  boolean_literal: $ => choice('true', 'false'),
946
1575
 
947
1576
  none_literal: $ => 'None',