@hew-lang/tree-sitter-hew 1.1.1 → 1.2.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/README.md CHANGED
@@ -2,6 +2,12 @@
2
2
 
3
3
  [Tree-sitter](https://tree-sitter.github.io/tree-sitter/) grammar for the [Hew](https://hew.sh) programming language.
4
4
 
5
+ The grammar includes Hew v0.6.1 generic closures and callable types:
6
+ `|<T: Eq> left: T, right: T| -> bool { left == right }` and
7
+ `fn<T: Eq>(T, T) -> bool`. Use the
8
+ [released SDK](https://github.com/hew-lang/hew/releases/tag/v0.6.1) for type
9
+ checking and execution; an editor parse is not a compiler acceptance check.
10
+
5
11
  ## Usage
6
12
 
7
13
  ### Neovim (nvim-treesitter)
package/grammar.js CHANGED
@@ -22,9 +22,11 @@ const PREC = {
22
22
  SHIFT: 12, // << >>
23
23
  ADD: 13, // + - &+ &-
24
24
  MUL: 15, // * / % &*
25
- UNARY: 17, // ! - ~ await
26
- POSTFIX: 19, // . () [] ?
27
- FIELD: 19, // field access (same left-associative postfix tier)
25
+ UNARY: 17, // ! - ~ clone
26
+ TRY: 18, // ?
27
+ TASK: 19, // await fork
28
+ POSTFIX: 20, // . () []
29
+ FIELD: 20, // field access (same left-associative postfix tier)
28
30
  };
29
31
 
30
32
  export default grammar({
@@ -38,25 +40,32 @@ export default grammar({
38
40
  inline: $ => [$._binary_operand],
39
41
 
40
42
  conflicts: $ => [
41
- [$._block_arm_value, $.expression],
43
+ [$._block_arm_value, $._callable_expression],
42
44
  [$._block_arm_value, $.fork_expression],
43
- [$.if_statement, $.expression],
44
- [$.match_statement, $.expression],
45
- [$.block_like_statement, $.expression],
45
+ [$.if_statement, $._callable_expression],
46
+ [$.match_statement, $._callable_expression],
47
+ [$.block_like_statement, $._callable_expression],
46
48
  [$.block_like_statement, $.fork_expression],
47
- [$.expression, $.struct_init],
49
+ [$._callable_expression, $.struct_init],
48
50
  [$.block, $.map_literal],
49
- [$.expression, $._member_object, $.path_expression],
50
- [$.expression, $._member_object],
51
+ [$._callable_expression, $._member_object, $.path_expression],
52
+ [$._callable_expression, $._member_object],
51
53
  [$.qualified_expression, $.field_expression],
52
54
  [$.generic_apply_expression, $.bare_generic_call_expression],
53
- [$.expression, $.private_capture_list],
55
+ [$._callable_expression, $.private_capture_list],
54
56
  [$.actor_spawn],
55
57
  [$.trait_bound],
58
+ // `-> fn() fails E` — the short `fails E` may belong to the inner callable
59
+ // type or to the enclosing declaration's return clause; GLR keeps both.
60
+ [$.function_type],
61
+ [$.actor_type],
56
62
  // `where P , (` — after a where-predicate's trailing comma, a `(` may begin
57
63
  // either another predicate (a parenthesized/tuple type) or a record's tuple
58
64
  // body `( T, … )`; GLR explores both and only the valid continuation lives.
59
65
  [$.where_clause],
66
+ // `return error` — a failure return or a binding named `error`; the next
67
+ // token decides, and a payload that fits both reads as the failure return.
68
+ [$.return_error_expression, $._callable_expression],
60
69
  // `import a.b.c` — at each `.` the parser cannot tell (within LR(1))
61
70
  // whether another path segment or the import selection follows; GLR explores
62
71
  // both and only the valid continuation survives.
@@ -96,24 +105,69 @@ export default grammar({
96
105
  $.supervisor_declaration,
97
106
  $.machine_declaration,
98
107
  $.type_alias,
108
+ $.macro_declaration,
109
+ $.macro_invocation,
99
110
  ),
100
111
  ),
101
112
 
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, ',')), ')')), ']'),
113
+ attribute: $ => seq(
114
+ '#', '[', field('name', $.identifier),
115
+ repeat(seq('.', field('name', $.identifier))),
116
+ optional(seq('(', repeat($._macro_token), ')')), ']',
117
+ ),
107
118
 
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,
119
+ macro_declaration: $ => seq(
120
+ 'macro', field('name', $.identifier), '{', repeat1($.macro_rule), '}',
121
+ ),
122
+
123
+ macro_rule: $ => seq(
124
+ choice(
125
+ field('pattern', $.macro_pattern),
126
+ seq('#', '[', field('pattern', $.macro_pattern), ']', $.macro_function_capture),
127
+ ),
128
+ '=>', field('category', choice('expr', 'items')),
129
+ field('template', $.macro_template),
130
+ ),
131
+
132
+ macro_pattern: $ => seq('(', repeat($._macro_token), ')'),
133
+ macro_template: $ => seq('{', repeat($._macro_token), '}'),
134
+
135
+ macro_function_capture: $ => seq(
136
+ $.macro_variable, ':', choice('fn', 'method'), optional('fails'),
137
+ ),
138
+
139
+ macro_variable: $ => prec(1, seq('$', $.identifier)),
140
+
141
+ token_tree: $ => choice(
142
+ seq('(', repeat($._macro_token), ')'),
143
+ seq('[', repeat($._macro_token), ']'),
144
+ seq('{', repeat($._macro_token), '}'),
145
+ ),
146
+
147
+ // Expansion owns fragment and repetition checks; these remain balanced tokens.
148
+ _macro_token: $ => choice(
149
+ $.token_tree, $.macro_variable, $.identifier, $._literal,
150
+ alias($.macro_interpolated_string, $.interpolated_string),
151
+ ',', ';', ':', '.', '@', '#', '$',
152
+ '+', '-', '*', '/', '%', '!', '~', '&', '|', '^',
153
+ '=', '<', '>', '?', '??', '?.', '=>', '->', '..', '..=',
154
+ '==', '!=', '<=', '>=', '&&', '||', '<<', '>>',
155
+ '+=', '-=', '*=', '/=', '%=', '&=', '|=', '^=', '<<=', '>>=',
156
+ '&+', '&-', '&*',
157
+ ),
158
+
159
+ macro_interpolated_string: $ => seq(
160
+ $.fstring_start,
161
+ repeat(choice(
162
+ alias($.interpolated_string_content, $.string_content),
163
+ $.escape_sequence,
164
+ alias($.macro_interpolation, $.interpolation),
165
+ )),
166
+ '"',
115
167
  ),
116
168
 
169
+ macro_interpolation: $ => seq(token.immediate('{'), repeat($._macro_token), '}'),
170
+
117
171
  // Import (hew-parser: import item):
118
172
  // Import = "import" ( StringLit | ModulePath ( "." ImportSelection )? ) ";"
119
173
  // ModulePath = Ident { "." Ident }
@@ -224,15 +278,20 @@ export default grammar({
224
278
 
225
279
  enum_body: $ => seq('{', repeat($.variant), '}'),
226
280
 
281
+ // A `#[wire]` / `#[serial]` enum tags each variant with `@N` after its
282
+ // payload: `Pending @0;`, `Push(i64) @1;`, `Move { x: i32; } @2;`.
283
+ // A struct variant ends at `}` unless it carries a tag.
227
284
  variant: $ => seq(
228
285
  field('name', $.identifier),
229
286
  choice(
230
- ';',
231
- seq('(', sep1($._type, ','), optional(','), ')', ';'),
232
- seq('{', repeat($.struct_field), '}'),
287
+ seq(optional($.variant_tag), ';'),
288
+ seq('(', sep1($._type, ','), optional(','), ')', optional($.variant_tag), ';'),
289
+ seq('{', repeat($.struct_field), '}', optional(seq($.variant_tag, ';'))),
233
290
  ),
234
291
  ),
235
292
 
293
+ variant_tag: $ => seq('@', $.integer_literal),
294
+
236
295
  // NOTE: the legacy bare `wire` keyword item form (`wire struct/enum/type
237
296
  // Name { … }`) was removed from the compiler in hew commit 60c50daef
238
297
  // ("refactor(parser): remove the wire keyword surface in favor of #[wire]").
@@ -281,6 +340,7 @@ export default grammar({
281
340
 
282
341
  trait_function_signature: $ => seq(
283
342
  'fn',
343
+ optional($.trait_method_effect),
284
344
  field('name', $.identifier),
285
345
  optional($.type_parameters),
286
346
  '(',
@@ -291,6 +351,8 @@ export default grammar({
291
351
  choice(';', field('body', $.block)),
292
352
  ),
293
353
 
354
+ trait_method_effect: $ => seq('[', 'suspends', ']'),
355
+
294
356
  associated_type: $ => seq(
295
357
  'type',
296
358
  field('name', $.identifier),
@@ -378,7 +440,11 @@ export default grammar({
378
440
  $.self,
379
441
  ),
380
442
 
381
- return_type: $ => prec.right(seq('->', $._type, optional($.failure_return))),
443
+ // `-> T`, `-> T fails E`, or the short `fails E` (a `() fails E` function).
444
+ return_type: $ => prec.right(choice(
445
+ seq('->', $._type, optional($.failure_return)),
446
+ $.failure_return,
447
+ )),
382
448
 
383
449
  failure_return: $ => seq('fails', field('error', $._type)),
384
450
 
@@ -507,7 +573,7 @@ export default grammar({
507
573
  field('name', $.identifier),
508
574
  ':',
509
575
  field('actor', $._type),
510
- optional(seq('(', optional(sep1($.call_argument, ',')), optional(','), ')')),
576
+ optional($.spawn_keys),
511
577
  repeat($.child_clause),
512
578
  ';',
513
579
  ),
@@ -581,18 +647,18 @@ export default grammar({
581
647
  ),
582
648
  ),
583
649
 
584
- // [ "initial" ] StateDecl — depth-1 composite substate
650
+ // [ "initial" ] StateDecl — direct leaf or compound child
585
651
  machine_composite_member: $ => seq(
586
652
  optional('initial'),
587
653
  $.machine_state,
588
654
  ),
589
655
 
590
- // on EventIdent [ "(" Ident { "," Ident } ")" ] : Source => Target
656
+ // on EventIdent [ "{" Ident { "," Ident } "}" ] : Source => Target
591
657
  // [ "reenter" ] [ "when" Expr ] TransitionBody
592
658
  // Source = Ident ( "." Ident )* | "_" (hew-parser/src/parser/actor_machine_supervisor.rs
593
659
  // 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
660
+ // qualified ancestry is retained and checked)
661
+ // Target = Ident ( "." Ident )* | "." Ident | "_" (the contextual
596
662
  // form is the bare-variant target the checker's fix-it steers authors
597
663
  // towards; "._" is rejected, so the wildcard stays plain "_")
598
664
  // TransitionBody = ";" | "{" FieldInitList "}" | Block
@@ -601,20 +667,33 @@ export default grammar({
601
667
  machine_transition: $ => seq(
602
668
  'on',
603
669
  field('event', $.identifier),
604
- optional(seq('(', field('payload_bindings', sep1($.identifier, ',')), ')')),
670
+ optional(seq('{', field('payload_bindings', sep1($.identifier, ',')), optional(','), '}')),
605
671
  ':',
606
672
  field('source', choice(seq($.identifier, repeat(seq('.', $.identifier))), '_')),
607
673
  '=>',
608
- field('target', choice($.identifier, $.contextual_variant_expression, '_')),
674
+ field('target', choice(seq($.identifier, repeat(seq('.', $.identifier))), $.contextual_variant_expression, '_')),
609
675
  optional('reenter'),
610
676
  optional(seq('when', field('guard', $.expression))),
611
677
  // A bodyless transition needs a semicolon; a body ends at `}`.
612
678
  choice(
613
679
  ';',
680
+ // A lone shorthand `{ name }` is the block yielding `name`, so a
681
+ // payload opens with a written field or with a shorthand field and
682
+ // `,` (hew-parser `is_struct_init_body`).
614
683
  field('payload', seq(
615
684
  '{',
616
- sep1($.field_initializer, ','),
617
- optional(','),
685
+ choice(
686
+ seq(
687
+ alias($._written_field_initializer, $.field_initializer),
688
+ repeat(seq(',', $.field_initializer)),
689
+ optional(','),
690
+ ),
691
+ seq(
692
+ alias($._shorthand_field_initializer, $.field_initializer),
693
+ ',',
694
+ optional(seq(sep1($.field_initializer, ','), optional(','))),
695
+ ),
696
+ ),
618
697
  '}',
619
698
  )),
620
699
  field('body', $.block),
@@ -686,7 +765,12 @@ export default grammar({
686
765
 
687
766
  trait_bounds: $ => sep1($.trait_bound, '+'),
688
767
 
689
- trait_bound: $ => seq($.identifier, optional($.type_arguments)),
768
+ // A trait is named by a bare or module-qualified path: `Show`, `alpha.Show`.
769
+ trait_bound: $ => seq(
770
+ $.identifier,
771
+ repeat(seq('.', $.identifier)),
772
+ optional($.type_arguments),
773
+ ),
690
774
 
691
775
  where_clause: $ => seq(
692
776
  'where',
@@ -724,8 +808,9 @@ export default grammar({
724
808
  // Qualified type name: `mod.Type` or `a.b.C`, with an optional trailing
725
809
  // type-arg list (e.g. `mod.Worker<T>`). Dotted paths are used throughout
726
810
  // the current surface for cross-module types (`net.NetError`, `fs.IoError`).
811
+ // A module can share a primitive spelling, as in `string.NumberError`.
727
812
  scoped_type: $ => seq(
728
- $.identifier,
813
+ choice($.identifier, alias($.primitive_type, $.identifier)),
729
814
  repeat1(seq('.', $.identifier)),
730
815
  optional($.type_arguments),
731
816
  ),
@@ -742,6 +827,7 @@ export default grammar({
742
827
 
743
828
  function_type: $ => prec(1, seq(
744
829
  'fn', optional($.callable_capabilities),
830
+ optional($.type_parameters),
745
831
  '(', optional(sep1($._type, ',')), ')', optional($.return_type),
746
832
  )),
747
833
 
@@ -772,7 +858,9 @@ export default grammar({
772
858
 
773
859
  // ---- Statements ----
774
860
 
775
- block: $ => seq('{', repeat($._statement), '}'),
861
+ block: $ => seq('{', repeat($._statement), optional(alias($._tail_expression, $.expression_statement)), '}'),
862
+
863
+ _tail_expression: $ => $.expression,
776
864
 
777
865
  _statement: $ => choice(
778
866
  // A stray `;` after a statement is accepted by the compiler with an
@@ -797,9 +885,6 @@ export default grammar({
797
885
  $.block_like_statement,
798
886
  ),
799
887
 
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).
803
888
  let_statement: $ => seq(
804
889
  'let',
805
890
  field('pattern', $.pattern),
@@ -816,9 +901,10 @@ export default grammar({
816
901
  var_statement: $ => seq(
817
902
  'var',
818
903
  field('name', $.identifier),
819
- optional(seq(':', field('type', $._type))),
820
- '=',
821
- field('value', $.expression),
904
+ choice(
905
+ seq(':', field('type', $._type), optional(seq('=', field('value', $.expression)))),
906
+ seq('=', field('value', $.expression)),
907
+ ),
822
908
  ';',
823
909
  ),
824
910
 
@@ -853,7 +939,7 @@ export default grammar({
853
939
  $.scope_expression,
854
940
  $.unsafe_expression,
855
941
  $.select_expression,
856
- seq('fork', $.block),
942
+ prec(PREC.TASK, seq('fork', $.block)),
857
943
  ),
858
944
 
859
945
 
@@ -866,13 +952,21 @@ export default grammar({
866
952
  // it is valid in tail position without `;` and inside larger expressions
867
953
  // (`b || return 0`, `g(if c { 1 } else { return 0 })`). See hew commit
868
954
  // 480456ec5 (return-as-expression) and hew-parser expressions.rs:1125-1150.
869
- // Statement position is handled by `expression_statement` (optional `;`).
955
+ // Statement position is handled by `expression_statement`; a tail omits `;`.
870
956
  return_expression: $ => prec.right(seq('return', optional($.expression))),
871
957
 
958
+ // `return error <expr>` returns the failure of a `fails E` function. After
959
+ // `return`, `error` begins a failure return unless an operator, call, index,
960
+ // `?`, `as` or terminator follows, in which case it stays an ordinary
961
+ // binding (hew-parser core.rs eat_error_return_marker). The grammar cannot see
962
+ // whitespace, so an attached `return error.f()` or `return error(x)` also
963
+ // reads as a failure return; the compiler refuses the attached forms.
964
+ return_error_expression: $ => prec.dynamic(1, prec.right(seq('return', 'error', choice($.expression, $.block)))),
965
+
872
966
  // Deferred cleanup may be an expression statement or a scoped block.
873
967
  // `defer <expr>;` or `defer { … }` — a block body needs no `;`
874
968
  // (hew-parser statements.rs `Token::Defer`).
875
- defer_statement: $ => seq('defer', choice(seq($.expression, ';'), $.block)),
969
+ defer_statement: $ => seq('defer', optional('err'), choice(seq($.expression, ';'), $.block)),
876
970
 
877
971
  // emit EventName { field: value, … } ; — Mealy output inside a transition body
878
972
  emit_statement: $ => seq(
@@ -882,9 +976,8 @@ export default grammar({
882
976
  ';',
883
977
  ),
884
978
 
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(';'))),
979
+ // Ordinary expressions omit their semicolon only at the end of a block.
980
+ expression_statement: $ => seq($.expression, ';'),
888
981
 
889
982
  empty_statement: $ => ';',
890
983
 
@@ -898,8 +991,7 @@ export default grammar({
898
991
  $.unsafe_expression,
899
992
  $.scope_expression,
900
993
  $.select_expression,
901
- $.race_expression,
902
- alias(seq('fork', $.block), $.fork_expression),
994
+ alias(prec(PREC.TASK, seq('fork', $.block)), $.fork_expression),
903
995
  $.gen_block_expression,
904
996
  )),
905
997
 
@@ -912,21 +1004,36 @@ export default grammar({
912
1004
  // ---- Expressions ----
913
1005
 
914
1006
  expression: $ => choice(
1007
+ $._callable_expression,
1008
+ $.spawn_expression,
1009
+ $.unary_expression,
1010
+ $.binary_expression,
1011
+ $.await_expression,
1012
+ $.clone_expression,
1013
+ $.lambda,
1014
+ $.fork_expression,
1015
+ $.handle_expression,
1016
+ $.yield_expression,
1017
+ $.return_expression,
1018
+ $.return_error_expression,
1019
+ ),
1020
+
1021
+ // Postfix calls bind inside prefix and infix operands. In particular,
1022
+ // `return spawn Worker()` cannot become a call on `return spawn Worker`.
1023
+ // A bare spawn ends after its actor and optional brace-delimited keys.
1024
+ _callable_expression: $ => choice(
915
1025
  $.identifier,
916
1026
  alias('capture', $.identifier),
917
1027
  $.self,
918
1028
  $._literal,
919
1029
  $.interpolated_string,
920
- $.unary_expression,
921
- $.binary_expression,
922
1030
  $.call_expression,
1031
+ $.macro_invocation,
923
1032
  $.method_call_expression,
924
1033
  $.field_expression,
925
1034
  $.index_expression,
926
1035
  $.try_expression,
927
1036
  $.cast_expression,
928
- $.await_expression,
929
- $.clone_expression,
930
1037
  $.struct_init,
931
1038
  $.generic_struct_init,
932
1039
  $.contextual_variant_expression,
@@ -940,16 +1047,11 @@ export default grammar({
940
1047
  $.parenthesized_expression,
941
1048
  $.if_expression,
942
1049
  $.match_expression,
943
- $.lambda,
944
1050
  $.actor_expression,
945
- $.spawn_expression,
946
1051
  $.select_expression,
947
1052
  $.race_expression,
948
- $.fork_expression,
949
- $.handle_expression,
950
1053
  $.scope_expression,
951
- $.yield_expression,
952
- $.return_expression,
1054
+ alias('error', $.identifier),
953
1055
  $.gen_block_expression,
954
1056
  $.qualified_expression,
955
1057
  $.generic_call_expression,
@@ -998,13 +1100,18 @@ export default grammar({
998
1100
  // Left associativity keeps postfix calls composable after a completed
999
1101
  // field/call chain (`value.slice(...).to_lower()`).
1000
1102
  call_expression: $ => prec.left(PREC.FIELD + 1, seq(
1001
- field('function', $.expression),
1103
+ field('function', alias($._callable_expression, $.expression)),
1002
1104
  '(',
1003
1105
  optional(sep1($.call_argument, ',')),
1004
1106
  optional(','),
1005
1107
  ')',
1006
1108
  )),
1007
1109
 
1110
+ macro_invocation: $ => prec(PREC.POSTFIX, seq(
1111
+ field('name', choice($.identifier, alias('capture', $.identifier), $.path_expression)), '!',
1112
+ field('arguments', alias($.macro_pattern, $.token_tree)),
1113
+ )),
1114
+
1008
1115
  // A member call must remain a single postfix expression even where `(`
1009
1116
  // could begin the following match pattern. Its function is structurally a
1010
1117
  // field expression, so this does not overlap plain calls or grouping.
@@ -1080,14 +1187,23 @@ export default grammar({
1080
1187
  // general expression set so a statement-level `{` still opens a block
1081
1188
  // statement.
1082
1189
  call_argument: $ => choice(
1083
- seq(field('name', $.identifier), ':', field('value', choice($.expression, $.block))),
1190
+ seq(field('name', $.identifier), ':', field('value', choice($.expression, $.block, $.range_argument))),
1084
1191
  $.expression,
1085
1192
  $.block,
1193
+ $.range_argument,
1194
+ ),
1195
+
1196
+ // Closed ranges remain ordinary binary expressions. Open bounds are
1197
+ // accepted as arguments; an inclusive range still requires its end.
1198
+ range_argument: $ => choice(
1199
+ seq('..', optional(field('end', $.expression))),
1200
+ seq('..=', field('end', $.expression)),
1201
+ seq(field('start', $.expression), '..'),
1086
1202
  ),
1087
1203
 
1088
1204
  field_expression: $ => prec(PREC.FIELD, seq(
1089
1205
  field('object', $.expression),
1090
- '.',
1206
+ choice('.', '?.'),
1091
1207
  field('field', choice($.identifier, $.integer_literal)),
1092
1208
  )),
1093
1209
 
@@ -1103,7 +1219,7 @@ export default grammar({
1103
1219
  ']',
1104
1220
  )),
1105
1221
 
1106
- try_expression: $ => prec(PREC.POSTFIX, seq(
1222
+ try_expression: $ => prec(PREC.TRY, seq(
1107
1223
  $.expression,
1108
1224
  '?',
1109
1225
  )),
@@ -1114,7 +1230,7 @@ export default grammar({
1114
1230
  field('type', $._type),
1115
1231
  )),
1116
1232
 
1117
- await_expression: $ => prec(PREC.UNARY, seq(
1233
+ await_expression: $ => prec(PREC.TASK, seq(
1118
1234
  'await',
1119
1235
  choice($.expression, $.block),
1120
1236
  )),
@@ -1162,13 +1278,22 @@ export default grammar({
1162
1278
  '}',
1163
1279
  )),
1164
1280
 
1165
- field_initializer: $ => seq(
1166
- choice(
1167
- seq(field('name', $.identifier), ':', field('value', $.expression)),
1168
- seq('..', field('spread', $.expression)),
1169
- ),
1281
+ field_initializer: $ => choice(
1282
+ $._written_field_initializer,
1283
+ $._shorthand_field_initializer,
1284
+ ),
1285
+
1286
+ // `error` lexes as a keyword where a statement could start (`return
1287
+ // error`, bare `error` expression); alias it so `{ error: v }` and
1288
+ // `{ error }` still read as field initializers.
1289
+ _written_field_initializer: $ => choice(
1290
+ seq(field('name', choice($.identifier, alias('error', $.identifier))), ':', field('value', $.expression)),
1291
+ seq('..', field('spread', $.expression)),
1170
1292
  ),
1171
1293
 
1294
+ // Shorthand `name` stands for `name: name`.
1295
+ _shorthand_field_initializer: $ => field('name', choice($.identifier, alias('error', $.identifier))),
1296
+
1172
1297
  array_expression: $ => seq(
1173
1298
  '[',
1174
1299
  optional(seq(sep1($._array_element, ','), optional(','))),
@@ -1245,7 +1370,7 @@ export default grammar({
1245
1370
  // Closures are PIPE-style (spec grammar.ebnf:288-295):
1246
1371
  // Lambda = "move"? "|" LambdaParams? "|" Expr
1247
1372
  // | "move"? "||" Expr
1248
- // | "move"? "|" LambdaParams? "|" RetType Block ;
1373
+ // | "move"? "|" TypeParams? LambdaParams? "|" RetType Block ;
1249
1374
  // The paren-arrow `(params) => body` form was removed in v0.5
1250
1375
  // (compiler emits E_CLOSURE_PIPE_SYNTAX). An empty parameter list is the
1251
1376
  // single `||` token because the lexer fuses adjacent pipes; the bracketed
@@ -1256,7 +1381,7 @@ export default grammar({
1256
1381
  optional($.private_capture_list),
1257
1382
  field('parameters', choice(
1258
1383
  '||',
1259
- seq('|', optional(sep1($.lambda_parameter, ',')), '|'),
1384
+ seq('|', optional($.type_parameters), optional(sep1($.lambda_parameter, ',')), '|'),
1260
1385
  )),
1261
1386
  choice(
1262
1387
  seq($.return_type, field('body', $.block)),
@@ -1296,12 +1421,16 @@ export default grammar({
1296
1421
  actor_spawn: $ => seq(
1297
1422
  field('actor', $._type),
1298
1423
  optional($.type_arguments),
1299
- optional(seq(
1300
- '(',
1301
- optional(sep1($.call_argument, ',')),
1302
- optional(','),
1303
- ')',
1304
- )),
1424
+ optional($.spawn_keys),
1425
+ ),
1426
+
1427
+ // Keys of a spawn or supervisor child, keyed like a record literal:
1428
+ // `{ key: value, key }`. An empty list is written without braces.
1429
+ spawn_keys: $ => seq(
1430
+ '{',
1431
+ sep1($.field_initializer, ','),
1432
+ optional(','),
1433
+ '}',
1305
1434
  ),
1306
1435
 
1307
1436
  select_expression: $ => seq(
@@ -1358,10 +1487,10 @@ export default grammar({
1358
1487
  ),
1359
1488
 
1360
1489
  race_expression: $ => seq(
1361
- 'race', '{', optional(seq(sep1($.expression, ','), optional(','))), '}',
1490
+ 'race', '[', optional(seq(sep1($.expression, ','), optional(','))), ']',
1362
1491
  ),
1363
1492
 
1364
- fork_expression: $ => prec.right(seq('fork', choice($.expression, $.block))),
1493
+ fork_expression: $ => prec.right(PREC.TASK, seq('fork', choice($.expression, $.block))),
1365
1494
 
1366
1495
  handle_expression: $ => prec.left(1, seq(
1367
1496
  field('operand', $.expression), 'handle', field('error', $.identifier),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hew-lang/tree-sitter-hew",
3
- "version": "1.1.1",
3
+ "version": "1.2.1",
4
4
  "description": "Hew grammar for tree-sitter",
5
5
  "type": "module",
6
6
  "repository": {
package/queries/folds.scm CHANGED
@@ -13,6 +13,8 @@
13
13
  (supervisor_declaration)
14
14
  (machine_declaration)
15
15
  (extern_block)
16
+ (macro_declaration)
17
+ (macro_template)
16
18
  (receive_function)
17
19
  (receive_gen_function)
18
20
  (if_expression)