@hew-lang/tree-sitter-hew 1.0.2 → 1.1.1

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,12 +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],
41
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],
42
72
  ],
43
73
 
44
74
  rules: {
@@ -48,18 +78,19 @@ export default grammar({
48
78
 
49
79
  _item: $ => seq(
50
80
  repeat($.attribute),
81
+ // Visibility is hoisted to item level per spec grammar.ebnf:27
82
+ // Item = Attribute* Visibility? ( Import | ConstDecl | ... )
83
+ optional($.visibility),
51
84
  choice(
52
85
  $.import_declaration,
53
86
  $.const_declaration,
54
87
  $.struct_declaration,
88
+ $.tuple_type_declaration,
55
89
  $.enum_declaration,
56
- $.wire_declaration,
57
90
  $.trait_declaration,
58
91
  $.impl_declaration,
59
92
  $.function_declaration,
60
93
  $.gen_function_declaration,
61
- $.async_gen_function_declaration,
62
- $.async_function_declaration,
63
94
  $.extern_block,
64
95
  $.actor_declaration,
65
96
  $.supervisor_declaration,
@@ -68,25 +99,56 @@ export default grammar({
68
99
  ),
69
100
  ),
70
101
 
71
- 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
+ ),
72
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.
73
127
  import_declaration: $ => seq(
74
128
  'import',
75
- $.module_path,
76
- 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))),
77
136
  ';',
78
137
  ),
79
138
 
80
- module_path: $ => prec.left(sep1($.identifier, '::')),
81
-
82
- _import_spec: $ => choice(
139
+ module_path: $ => seq(
83
140
  $.identifier,
84
- seq('{', sep1($.identifier, ','), '}'),
85
- '*',
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))),
86
149
  ),
87
150
 
88
151
  const_declaration: $ => seq(
89
- optional($.visibility),
90
152
  'const',
91
153
  field('name', $.identifier),
92
154
  ':',
@@ -105,8 +167,10 @@ export default grammar({
105
167
  ';',
106
168
  ),
107
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`.
108
173
  struct_declaration: $ => seq(
109
- optional($.visibility),
110
174
  'type',
111
175
  field('name', $.identifier),
112
176
  optional($.type_parameters),
@@ -114,18 +178,43 @@ export default grammar({
114
178
  field('body', $.struct_body),
115
179
  ),
116
180
 
117
- 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
+ )), '}'),
118
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.
119
191
  struct_field: $ => seq(
120
- optional('var'),
192
+ repeat($.attribute),
121
193
  field('name', $.identifier),
122
194
  ':',
123
195
  field('type', $._type),
124
- 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(','), ')', ';',
125
214
  ),
126
215
 
127
216
  enum_declaration: $ => seq(
128
- optional($.visibility),
217
+ optional('indirect'),
129
218
  'enum',
130
219
  field('name', $.identifier),
131
220
  optional($.type_parameters),
@@ -137,51 +226,39 @@ export default grammar({
137
226
 
138
227
  variant: $ => seq(
139
228
  field('name', $.identifier),
140
- optional(choice(
141
- seq('(', sep1($._type, ','), ')'),
229
+ choice(
230
+ ';',
231
+ seq('(', sep1($._type, ','), optional(','), ')', ';'),
142
232
  seq('{', repeat($.struct_field), '}'),
143
- )),
144
- optional(choice(',', ';')),
145
- ),
146
-
147
- // "type X { ... }" is a struct alias form
148
- // handled by struct_declaration with choice('struct', 'type')
149
-
150
- wire_declaration: $ => seq(
151
- 'wire',
152
- choice('struct', 'enum', 'type'),
153
- field('name', $.identifier),
154
- field('body', $.wire_body),
233
+ ),
155
234
  ),
156
235
 
157
- wire_body: $ => seq('{', repeat(choice($.wire_field, $.variant, $.reserved_declaration)), '}'),
158
-
159
- wire_field: $ => prec.left(seq(
160
- field('name', $.identifier),
161
- ':',
162
- field('type', $.wire_type),
163
- optional(seq('@', $.integer_literal)),
164
- repeat($.wire_attribute),
165
- optional(choice(',', ';')),
166
- )),
167
-
168
- // @sync:wire_types
169
- wire_type: $ => choice(
170
- 'u8', 'u16', 'u32', 'u64', 'i8', 'i16', 'i32', 'i64', 'f32', 'f64', 'bool',
171
- 'bytes', 'string',
172
- $.identifier,
173
- seq('list', '[', $.wire_type, ']'),
174
- ),
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.
175
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.
176
247
  reserved_declaration: $ => seq(
177
- 'reserved', '(', sep1($.integer_literal, ','), ')', ';',
248
+ 'reserved', sep1(seq('@', $.integer_literal), ','), ';',
178
249
  ),
179
250
 
180
- // @sync:wire_attributes
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.
181
255
  wire_attribute: $ => choice(
182
- 'optional', 'deprecated',
183
- seq('default', '(', $.expression, ')'),
184
- 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),
185
262
  ),
186
263
 
187
264
  // ---- Traits & Impls ----
@@ -193,7 +270,7 @@ export default grammar({
193
270
  optional(seq(':', $.trait_bounds)),
194
271
  optional($.where_clause),
195
272
  '{',
196
- repeat($._trait_item),
273
+ repeat(seq(repeat($.attribute), $._trait_item)),
197
274
  '}',
198
275
  ),
199
276
 
@@ -203,7 +280,6 @@ export default grammar({
203
280
  ),
204
281
 
205
282
  trait_function_signature: $ => seq(
206
- optional('pure'),
207
283
  'fn',
208
284
  field('name', $.identifier),
209
285
  optional($.type_parameters),
@@ -211,6 +287,7 @@ export default grammar({
211
287
  optional($.parameters),
212
288
  ')',
213
289
  optional($.return_type),
290
+ optional($.where_clause),
214
291
  choice(';', field('body', $.block)),
215
292
  ),
216
293
 
@@ -222,16 +299,24 @@ export default grammar({
222
299
  ';',
223
300
  ),
224
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).
225
308
  impl_declaration: $ => seq(
226
309
  'impl',
227
310
  optional($.type_parameters),
228
- field('trait', $.identifier),
229
- optional($.type_arguments),
230
- 'for',
231
311
  field('type', $._type),
312
+ optional(seq('for', field('self_type', $._type))),
232
313
  optional($.where_clause),
233
314
  '{',
234
- repeat(choice($.function_declaration, $.associated_type_impl)),
315
+ repeat(seq(
316
+ repeat($.attribute),
317
+ optional($.visibility),
318
+ choice($.function_declaration, $.associated_type_impl),
319
+ )),
235
320
  '}',
236
321
  ),
237
322
 
@@ -240,8 +325,6 @@ export default grammar({
240
325
  // ---- Functions ----
241
326
 
242
327
  function_declaration: $ => seq(
243
- optional($.visibility),
244
- optional('pure'),
245
328
  'fn',
246
329
  field('name', $.identifier),
247
330
  optional($.type_parameters),
@@ -254,7 +337,6 @@ export default grammar({
254
337
  ),
255
338
 
256
339
  gen_function_declaration: $ => seq(
257
- optional('pure'),
258
340
  'gen',
259
341
  'fn',
260
342
  field('name', $.identifier),
@@ -268,45 +350,41 @@ export default grammar({
268
350
  field('body', $.block),
269
351
  ),
270
352
 
271
- async_function_declaration: $ => seq(
272
- 'async',
273
- 'fn',
274
- field('name', $.identifier),
275
- optional($.type_parameters),
276
- '(',
277
- optional($.parameters),
278
- ')',
279
- optional($.return_type),
280
- optional($.where_clause),
281
- field('body', $.block),
282
- ),
353
+ parameters: $ => sep1($.parameter, ','),
283
354
 
284
- async_gen_function_declaration: $ => seq(
285
- 'async',
286
- 'gen',
287
- 'fn',
288
- field('name', $.identifier),
289
- optional($.type_parameters),
290
- '(',
291
- optional($.parameters),
292
- ')',
293
- '->',
294
- field('yield_type', $._type),
295
- optional($.where_clause),
296
- 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
+ ),
297
374
  ),
298
375
 
299
- parameters: $ => sep1($.parameter, ','),
300
-
301
- parameter: $ => seq(
302
- field('name', choice($.identifier, $.self)),
303
- ':',
304
- field('type', $._type),
376
+ self_parameter: $ => seq(
377
+ optional(choice('var', 'consume')),
378
+ $.self,
305
379
  ),
306
380
 
307
- return_type: $ => seq('->', $._type),
381
+ return_type: $ => prec.right(seq('->', $._type, optional($.failure_return))),
308
382
 
309
- 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'),
310
388
 
311
389
  // ---- Actors ----
312
390
 
@@ -317,14 +395,19 @@ export default grammar({
317
395
  optional(seq(':', $.trait_bounds)),
318
396
  optional($.where_clause),
319
397
  '{',
320
- optional($.actor_init),
321
398
  repeat(choice(
399
+ $.actor_init,
322
400
  $.actor_field,
323
401
  $.mailbox_declaration,
324
- $.receive_function,
325
- $.receive_gen_function,
326
- $.function_declaration,
327
- $.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),
328
411
  )),
329
412
  '}',
330
413
  ),
@@ -355,6 +438,7 @@ export default grammar({
355
438
  seq('coalesce', '(', $.identifier, ')', optional(seq('fallback', $.overflow_kind))),
356
439
  ),
357
440
 
441
+ // Actor state uses the same declaration terminator as record fields.
358
442
  actor_field: $ => seq(
359
443
  choice('let', 'var'),
360
444
  field('name', $.identifier),
@@ -365,7 +449,6 @@ export default grammar({
365
449
  ),
366
450
 
367
451
  receive_function: $ => seq(
368
- optional('pure'),
369
452
  'receive',
370
453
  'fn',
371
454
  field('name', $.identifier),
@@ -379,7 +462,6 @@ export default grammar({
379
462
  ),
380
463
 
381
464
  receive_gen_function: $ => seq(
382
- optional('pure'),
383
465
  'receive',
384
466
  'gen',
385
467
  'fn',
@@ -399,36 +481,42 @@ export default grammar({
399
481
  supervisor_declaration: $ => seq(
400
482
  'supervisor',
401
483
  field('name', $.identifier),
484
+ optional($.type_parameters),
485
+ optional(seq(
486
+ '(',
487
+ optional($.parameters),
488
+ ')',
489
+ )),
402
490
  '{',
403
491
  repeat(choice($.child_spec, $.supervisor_field)),
404
492
  '}',
405
493
  ),
406
494
 
407
- supervisor_field: $ => prec(5, seq(
408
- field('name', $.identifier),
409
- ':',
410
- field('value', $.expression),
411
- optional(choice(',', ';')),
412
- )),
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
+ ),
413
503
 
414
504
  child_spec: $ => seq(
415
- 'child',
505
+ // @sync:child_kinds
506
+ choice('child', 'pool'),
416
507
  field('name', $.identifier),
417
508
  ':',
418
- field('actor', $.identifier),
419
- optional($.restart_spec),
509
+ field('actor', $._type),
510
+ optional(seq('(', optional(sep1($.call_argument, ',')), optional(','), ')')),
511
+ repeat($.child_clause),
420
512
  ';',
421
513
  ),
422
514
 
423
- restart_spec: $ => seq(
424
- 'restart',
425
- '(',
426
- // @sync:restart_permanence
427
- choice('permanent', 'transient', 'temporary'),
428
- ')',
429
- optional(seq('budget', '(', $.integer_literal, ',', $.duration_literal, ')')),
430
- // @sync:restart_strategies
431
- 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(','))), '}'),
432
520
  ),
433
521
 
434
522
  // @sync:duration_suffixes
@@ -437,53 +525,108 @@ export default grammar({
437
525
  // ---- Machines ----
438
526
 
439
527
  machine_declaration: $ => seq(
440
- optional($.visibility),
441
528
  'machine',
442
529
  field('name', $.identifier),
530
+ optional($.machine_type_parameters),
531
+ optional($.where_clause),
443
532
  '{',
533
+ field('events_header', $.machine_events_header),
534
+ optional(field('emits_header', $.machine_emits_header)),
444
535
  repeat(choice(
445
536
  $.machine_state,
446
- $.machine_event,
447
537
  $.machine_transition,
448
538
  $.machine_default,
449
539
  )),
450
540
  '}',
451
541
  ),
452
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.
453
567
  machine_state: $ => seq(
454
568
  'state',
455
569
  field('name', $.identifier),
456
- optional(seq('{', repeat($.struct_field), '}')),
457
- optional(';'),
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
+ ),
458
582
  ),
459
583
 
460
- machine_event: $ => seq(
461
- 'event',
462
- field('name', $.identifier),
463
- optional(seq('{', repeat($.struct_field), '}')),
464
- optional(';'),
584
+ // [ "initial" ] StateDecl — depth-1 composite substate
585
+ machine_composite_member: $ => seq(
586
+ optional('initial'),
587
+ $.machine_state,
465
588
  ),
466
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 }`.
467
601
  machine_transition: $ => seq(
468
602
  'on',
469
603
  field('event', $.identifier),
604
+ optional(seq('(', field('payload_bindings', sep1($.identifier, ',')), ')')),
470
605
  ':',
471
- field('source', $.identifier),
472
- '->',
473
- field('target', $.identifier),
606
+ field('source', choice(seq($.identifier, repeat(seq('.', $.identifier))), '_')),
607
+ '=>',
608
+ field('target', choice($.identifier, $.contextual_variant_expression, '_')),
609
+ optional('reenter'),
474
610
  optional(seq('when', field('guard', $.expression))),
611
+ // A bodyless transition needs a semicolon; a body ends at `}`.
475
612
  choice(
476
613
  ';',
477
- seq('{', repeat(choice($.field_initializer, $._statement)), '}'),
614
+ field('payload', seq(
615
+ '{',
616
+ sep1($.field_initializer, ','),
617
+ optional(','),
618
+ '}',
619
+ )),
620
+ field('body', $.block),
478
621
  ),
479
622
  ),
480
623
 
624
+ // default { state } — covers all remaining uncovered (state, event) cells
481
625
  machine_default: $ => seq(
482
626
  'default',
483
- choice(
484
- seq('{', repeat($._statement), '}'),
485
- ';',
486
- ),
627
+ '{',
628
+ 'state',
629
+ '}',
487
630
  ),
488
631
 
489
632
  // ---- Extern ----
@@ -492,7 +635,10 @@ export default grammar({
492
635
  'extern',
493
636
  $.string_literal,
494
637
  '{',
495
- 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)),
496
642
  '}',
497
643
  ),
498
644
 
@@ -509,14 +655,34 @@ export default grammar({
509
655
 
510
656
  // ---- Types ----
511
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
+
512
665
  type_parameters: $ => seq('<', sep1($.type_parameter, ','), '>'),
513
666
 
514
- type_parameter: $ => seq(
515
- $.identifier,
516
- 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,
517
679
  ),
518
680
 
519
- type_arguments: $ => seq('<', sep1($._type, ','), '>'),
681
+ associated_type_binding: $ => seq(
682
+ field('name', $.identifier),
683
+ '=',
684
+ field('type', $._type),
685
+ ),
520
686
 
521
687
  trait_bounds: $ => sep1($.trait_bound, '+'),
522
688
 
@@ -525,6 +691,7 @@ export default grammar({
525
691
  where_clause: $ => seq(
526
692
  'where',
527
693
  sep1($.where_predicate, ','),
694
+ optional(','),
528
695
  ),
529
696
 
530
697
  where_predicate: $ => seq($._type, ':', $.trait_bounds),
@@ -533,11 +700,15 @@ export default grammar({
533
700
  $.primitive_type,
534
701
  $.identifier,
535
702
  $.generic_type,
703
+ $.scoped_type,
704
+ $.associated_type_projection,
536
705
  $.tuple_type,
537
706
  $.array_type,
538
707
  $.slice_type,
539
708
  $.function_type,
709
+ $.actor_type,
540
710
  $.pointer_type,
711
+ $.borrow_type,
541
712
  $.trait_object_type,
542
713
  $.unit_type,
543
714
  ),
@@ -545,11 +716,22 @@ export default grammar({
545
716
  // @sync:primitive_types
546
717
  primitive_type: $ => choice(
547
718
  'i8', 'i16', 'i32', 'i64', 'u8', 'u16', 'u32', 'u64', 'isize', 'usize',
548
- 'f32', 'f64', 'bool', 'char', 'string', 'bytes', 'void', 'duration',
719
+ 'f32', 'f64', 'bool', 'char', 'string', 'bytes', 'duration', 'instant',
549
720
  ),
550
721
 
551
722
  generic_type: $ => prec(1, seq($.identifier, $.type_arguments)),
552
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
+
553
735
  tuple_type: $ => seq('(', sep1($._type, ','), ')'),
554
736
 
555
737
  unit_type: $ => seq('(', ')'),
@@ -558,9 +740,30 @@ export default grammar({
558
740
 
559
741
  slice_type: $ => seq('[', $._type, ']'),
560
742
 
561
- 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),
562
763
 
563
- 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),
564
767
 
565
768
  trait_object_type: $ => seq('dyn', choice(
566
769
  $.trait_bound,
@@ -572,29 +775,41 @@ export default grammar({
572
775
  block: $ => seq('{', repeat($._statement), '}'),
573
776
 
574
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,
575
782
  $.let_statement,
576
783
  $.var_statement,
577
784
  $.assignment_statement,
578
785
  $.defer_statement,
786
+ $.emit_statement,
579
787
 
580
788
  $.for_statement,
581
789
  $.while_statement,
582
790
  $.loop_statement,
583
791
  $.break_statement,
584
792
  $.continue_statement,
585
- $.return_statement,
586
793
  $.expression_statement,
587
794
  $.if_statement,
588
795
  $.match_statement,
589
796
  $.block_statement,
797
+ $.block_like_statement,
590
798
  ),
591
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).
592
803
  let_statement: $ => seq(
593
804
  'let',
594
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.
595
808
  optional(seq(':', field('type', $._type))),
596
- '=',
597
- 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))),
598
813
  ';',
599
814
  ),
600
815
 
@@ -615,31 +830,81 @@ export default grammar({
615
830
  ';',
616
831
  ),
617
832
 
618
- match_arm: $ => prec(3, seq(
833
+ _match_arm_head: $ => seq(
619
834
  field('pattern', $.pattern),
620
- 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)))),
621
837
  '=>',
622
- field('value', choice(
623
- seq($.block, optional(',')),
624
- seq($.expression, optional(',')),
625
- )),
838
+ ),
839
+
840
+ match_arm: $ => prec(3, seq(
841
+ $._match_arm_head,
842
+ field('value', choice($._block_arm_value, seq($.expression, ','))),
626
843
  )),
627
844
 
845
+ _match_final_arm: $ => prec(3, seq($._match_arm_head, field('value', $.expression))),
628
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
+ ),
629
858
 
630
- break_statement: $ => seq('break', optional($.label), optional($.expression), ';'),
631
859
 
632
- continue_statement: $ => seq('continue', optional($.label), ';'),
633
860
 
634
- return_statement: $ => seq('return', optional($.expression), ';'),
861
+ break_statement: $ => prec.right(seq('break', optional($.label), optional(';'))),
635
862
 
636
- defer_statement: $ => seq('defer', $.expression, ';'),
863
+ continue_statement: $ => prec.right(seq('continue', optional($.label), optional(';'))),
637
864
 
638
- expression_statement: $ => seq($.expression, optional(';')),
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))),
639
871
 
640
- if_statement: $ => $.if_expression,
641
- match_statement: $ => $.match_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)),
642
876
 
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
+ ),
884
+
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(';'))),
888
+
889
+ empty_statement: $ => ';',
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).
643
908
  block_statement: $ => $.block,
644
909
 
645
910
  label: $ => /@[a-zA-Z_][a-zA-Z0-9_]*/,
@@ -648,6 +913,7 @@ export default grammar({
648
913
 
649
914
  expression: $ => choice(
650
915
  $.identifier,
916
+ alias('capture', $.identifier),
651
917
  $.self,
652
918
  $._literal,
653
919
  $.interpolated_string,
@@ -658,47 +924,80 @@ export default grammar({
658
924
  $.field_expression,
659
925
  $.index_expression,
660
926
  $.try_expression,
927
+ $.cast_expression,
661
928
  $.await_expression,
929
+ $.clone_expression,
662
930
  $.struct_init,
931
+ $.generic_struct_init,
932
+ $.contextual_variant_expression,
933
+ $.contextual_variant_record_init,
934
+ $.generic_apply_expression,
663
935
  $.array_expression,
664
936
  $.array_repeat,
665
937
  $.map_literal,
666
938
  $.tuple_expression,
939
+ $.unit_expression,
667
940
  $.parenthesized_expression,
668
941
  $.if_expression,
669
942
  $.match_expression,
670
943
  $.lambda,
944
+ $.actor_expression,
671
945
  $.spawn_expression,
672
946
  $.select_expression,
673
- $.join_expression,
947
+ $.race_expression,
948
+ $.fork_expression,
949
+ $.handle_expression,
674
950
  $.scope_expression,
675
- $.cooperate_expression,
676
951
  $.yield_expression,
677
- $.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,
678
958
  $.unsafe_expression,
679
959
  ),
680
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
+
681
971
  unary_expression: $ => prec(PREC.UNARY, seq(
682
972
  field('operator', choice('!', '-', '~')),
683
973
  field('operand', $.expression),
684
974
  )),
685
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.
686
978
  binary_expression: $ => choice(
687
- prec.right(PREC.SEND, seq($.expression, '<-', $.expression)),
688
- prec.left(PREC.OR, seq($.expression, '||', $.expression)),
689
- prec.left(PREC.BIT_OR, seq($.expression, '|', $.expression)),
690
- prec.left(PREC.BIT_XOR, seq($.expression, '^', $.expression)),
691
- prec.left(PREC.AND, seq($.expression, '&&', $.expression)),
692
- prec.left(PREC.BIT_AND, seq($.expression, '&', $.expression)),
693
- prec.left(PREC.EQ, seq($.expression, choice('==', '!=', '=~', '!~'), $.expression)),
694
- prec.left(PREC.REL, seq($.expression, choice('<', '<=', '>', '>='), $.expression)),
695
- prec.right(PREC.RANGE, seq($.expression, choice('..', '..='), $.expression)),
696
- prec.left(PREC.SHIFT, seq($.expression, choice('<<', '>>'), $.expression)),
697
- prec.left(PREC.ADD, seq($.expression, choice('+', '-'), $.expression)),
698
- prec.left(PREC.MUL, seq($.expression, choice('*', '/', '%'), $.expression)),
699
- ),
700
-
701
- 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(
702
1001
  field('function', $.expression),
703
1002
  '(',
704
1003
  optional(sep1($.call_argument, ',')),
@@ -706,21 +1005,86 @@ export default grammar({
706
1005
  ')',
707
1006
  )),
708
1007
 
709
- call_argument: $ => choice(
710
- seq(field('name', $.identifier), ':', field('value', $.expression)),
711
- $.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,
712
1032
  ),
713
1033
 
714
- method_call_expression: $ => prec(PREC.POSTFIX, seq(
715
- 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),
716
1038
  '.',
717
- 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
+ '>',
718
1072
  '(',
719
- optional(sep1($.expression, ',')),
1073
+ optional(sep1($.call_argument, ',')),
720
1074
  optional(','),
721
1075
  ')',
722
1076
  )),
723
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
+
724
1088
  field_expression: $ => prec(PREC.FIELD, seq(
725
1089
  field('object', $.expression),
726
1090
  '.',
@@ -730,7 +1094,12 @@ export default grammar({
730
1094
  index_expression: $ => prec(PREC.POSTFIX, seq(
731
1095
  field('object', $.expression),
732
1096
  '[',
733
- 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
+ ),
734
1103
  ']',
735
1104
  )),
736
1105
 
@@ -739,27 +1108,87 @@ export default grammar({
739
1108
  '?',
740
1109
  )),
741
1110
 
1111
+ cast_expression: $ => prec(PREC.POSTFIX, seq(
1112
+ field('value', $.expression),
1113
+ 'as',
1114
+ field('type', $._type),
1115
+ )),
1116
+
742
1117
  await_expression: $ => prec(PREC.UNARY, seq(
743
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',
744
1128
  $.expression,
745
1129
  )),
746
1130
 
747
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
+ '.',
748
1159
  field('name', $.identifier),
749
1160
  '{',
750
- sep1($.field_initializer, ','),
751
- optional(','),
1161
+ optional(seq(sep1($.field_initializer, ','), optional(','))),
752
1162
  '}',
753
1163
  )),
754
1164
 
755
1165
  field_initializer: $ => seq(
756
- field('name', $.identifier),
757
- ':',
758
- field('value', $.expression),
1166
+ choice(
1167
+ seq(field('name', $.identifier), ':', field('value', $.expression)),
1168
+ seq('..', field('spread', $.expression)),
1169
+ ),
759
1170
  ),
760
1171
 
761
1172
  array_expression: $ => seq(
762
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]+\[/)),
763
1192
  optional(seq(sep1($.expression, ','), optional(','))),
764
1193
  ']',
765
1194
  ),
@@ -768,8 +1197,7 @@ export default grammar({
768
1197
 
769
1198
  map_literal: $ => seq(
770
1199
  '{',
771
- sep1($.map_entry, ','),
772
- optional(','),
1200
+ optional(seq(sep1($.map_entry, ','), optional(','))),
773
1201
  '}',
774
1202
  ),
775
1203
 
@@ -783,9 +1211,22 @@ export default grammar({
783
1211
 
784
1212
  parenthesized_expression: $ => seq('(', $.expression, ')'),
785
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
+
786
1228
  if_expression: $ => prec.right(seq(
787
- 'if',
788
- field('condition', $.expression),
1229
+ 'if', field('condition', $._condition),
789
1230
  field('consequence', $.block),
790
1231
  optional(field('alternative', $.else_clause)),
791
1232
  )),
@@ -794,75 +1235,100 @@ export default grammar({
794
1235
 
795
1236
  match_expression: $ => seq(
796
1237
  'match',
797
- field('value', $.expression),
1238
+ field('value', choice($.expression, $.block)),
798
1239
  '{',
799
1240
  repeat($.match_arm),
1241
+ optional(alias($._match_final_arm, $.match_arm)),
800
1242
  '}',
801
1243
  ),
802
1244
 
803
- 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(
804
1255
  optional('move'),
805
- '(',
806
- optional(sep1($.lambda_parameter, ',')),
807
- ')',
808
- optional($.return_type),
809
- '=>',
810
- 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
+ ),
811
1266
  )),
812
1267
 
1268
+ private_capture_list: $ => seq(
1269
+ 'capture', '(', sep1($.private_capture, ','), ')',
1270
+ ),
1271
+
1272
+ private_capture: $ => seq('var', field('name', $.identifier)),
1273
+
813
1274
  lambda_parameter: $ => seq(
814
1275
  field('name', $.identifier),
815
1276
  optional(seq(':', field('type', $._type))),
816
1277
  ),
817
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
+
818
1291
  spawn_expression: $ => seq(
819
1292
  'spawn',
820
- choice(
821
- $.actor_spawn,
822
- $.lambda_actor,
823
- ),
1293
+ $.actor_spawn,
824
1294
  ),
825
1295
 
826
1296
  actor_spawn: $ => seq(
827
- field('actor', $.identifier),
1297
+ field('actor', $._type),
828
1298
  optional($.type_arguments),
829
1299
  optional(seq(
830
1300
  '(',
831
1301
  optional(sep1($.call_argument, ',')),
1302
+ optional(','),
832
1303
  ')',
833
1304
  )),
834
1305
  ),
835
1306
 
836
- lambda_actor: $ => prec(2, seq(
837
- optional('move'),
838
- '(',
839
- optional(sep1($.lambda_parameter, ',')),
840
- ')',
841
- optional($.return_type),
842
- '=>',
843
- choice($.expression, $.block),
844
- )),
845
-
846
1307
  select_expression: $ => seq(
847
1308
  'select',
848
1309
  '{',
849
1310
  repeat($.select_arm),
1311
+ optional(alias($._select_final_arm, $.select_arm)),
850
1312
  '}',
851
1313
  ),
852
1314
 
853
- select_arm: $ => choice(
854
- seq($.identifier, 'from', $.expression, '=>', $.expression, ','),
855
- seq('after', $.expression, '=>', $.expression, ','),
856
- ),
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
+ )),
857
1323
 
858
- join_expression: $ => seq(
859
- 'join',
860
- '{',
861
- sep1($.expression, ','),
862
- optional(','),
863
- '}',
1324
+ _select_arm_head: $ => choice(
1325
+ seq(field('binding', $.pattern), 'from', field('source', $.expression), '=>'),
1326
+ seq('after', field('duration', $.expression), '=>'),
864
1327
  ),
865
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
+
866
1332
  loop_statement: $ => prec(10, seq(
867
1333
  optional(seq($.label, ':')),
868
1334
  'loop',
@@ -872,31 +1338,59 @@ export default grammar({
872
1338
  for_statement: $ => prec(10, seq(
873
1339
  optional(seq($.label, ':')),
874
1340
  'for',
875
- optional('await'),
876
1341
  field('pattern', $.pattern),
877
1342
  'in',
878
- field('iterable', $.expression),
1343
+ field('iterable', choice($.expression, $.block)),
879
1344
  field('body', $.block),
880
1345
  )),
881
1346
 
882
1347
  while_statement: $ => prec(10, seq(
883
1348
  optional(seq($.label, ':')),
884
1349
  'while',
885
- field('condition', $.expression),
1350
+ field('condition', $._condition),
886
1351
  field('body', $.block),
887
1352
  )),
888
1353
 
889
1354
  scope_expression: $ => seq(
890
1355
  'scope',
891
- optional(seq('|', field('binding', $.identifier), '|')),
892
- $.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),
893
1379
  ),
894
- cooperate_expression: $ => 'cooperate',
895
- 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))),
896
1385
 
897
1386
  unsafe_expression: $ => seq('unsafe', $.block),
898
1387
 
899
- 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
+ )),
900
1394
 
901
1395
 
902
1396
 
@@ -907,21 +1401,56 @@ export default grammar({
907
1401
  $.identifier,
908
1402
  $.path_expression,
909
1403
  $._literal,
1404
+ seq('-', $.integer_literal),
1405
+ '()',
910
1406
  $.tuple_pattern,
911
1407
  $.struct_pattern,
1408
+ $.record_pattern,
1409
+ $.variant_pattern,
912
1410
  $.constructor_pattern,
913
1411
  $.or_pattern,
914
1412
  ),
915
1413
 
916
1414
  tuple_pattern: $ => seq('(', sep1($.pattern, ','), ')'),
917
1415
 
918
- 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
+ '.',
919
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)),
920
1439
  '{',
921
- optional(seq(sep1($.pattern_field, ','), optional(','))),
1440
+ optional($._record_pattern_fields),
922
1441
  '}',
923
1442
  ),
924
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
+
925
1454
  pattern_field: $ => seq(
926
1455
  field('name', $.identifier),
927
1456
  optional(seq(':', field('pattern', $.pattern))),
@@ -945,16 +1474,21 @@ export default grammar({
945
1474
  $.string_literal,
946
1475
  $.raw_string_literal,
947
1476
  $.byte_string_literal,
1477
+ $.char_literal,
948
1478
  $.regex_literal,
949
1479
  $.boolean_literal,
950
1480
  $.none_literal,
951
1481
  ),
952
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.
953
1487
  integer_literal: $ => token(choice(
954
- /0[xX][0-9a-fA-F][0-9a-fA-F_]*/,
955
- /0[bB][01][01_]*/,
956
- /0[oO][0-7][0-7_]*/,
957
- /[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)?/,
958
1492
  )),
959
1493
 
960
1494
  float_literal: $ => token(
@@ -972,11 +1506,17 @@ export default grammar({
972
1506
 
973
1507
  string_content: $ => token.immediate(prec(1, /[^"\\]+/)),
974
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.
975
1514
  escape_sequence: $ => token.immediate(seq(
976
1515
  '\\',
977
1516
  choice(
978
- /[nrt\\"0]/,
1517
+ /u\{[0-9a-fA-F]{1,6}\}/,
979
1518
  /x[0-9a-fA-F]{2}/,
1519
+ /./,
980
1520
  ),
981
1521
  )),
982
1522
 
@@ -992,11 +1532,19 @@ export default grammar({
992
1532
 
993
1533
  fstring_start: $ => token(prec(2, /f"/)),
994
1534
 
995
- 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, /[^"\\{]+/)),
996
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).
997
1544
  interpolation: $ => seq(
998
1545
  token.immediate('{'),
999
1546
  $.expression,
1547
+ optional(seq(':', '?')),
1000
1548
  '}',
1001
1549
  ),
1002
1550
 
@@ -1012,6 +1560,10 @@ export default grammar({
1012
1560
  '"',
1013
1561
  )),
1014
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
+
1015
1567
  regex_literal: $ => token(seq(
1016
1568
  're"',
1017
1569
  /[^"]*/,