@hew-lang/tree-sitter-hew 1.1.0 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/grammar.js CHANGED
@@ -53,10 +53,17 @@ export default grammar({
53
53
  [$.expression, $.private_capture_list],
54
54
  [$.actor_spawn],
55
55
  [$.trait_bound],
56
+ // `-> fn() fails E` — the short `fails E` may belong to the inner callable
57
+ // type or to the enclosing declaration's return clause; GLR keeps both.
58
+ [$.function_type],
59
+ [$.actor_type],
56
60
  // `where P , (` — after a where-predicate's trailing comma, a `(` may begin
57
61
  // either another predicate (a parenthesized/tuple type) or a record's tuple
58
62
  // body `( T, … )`; GLR explores both and only the valid continuation lives.
59
63
  [$.where_clause],
64
+ // `return error` — a failure return or a binding named `error`; the next
65
+ // token decides, and a payload that fits both reads as the failure return.
66
+ [$.return_error_expression, $.expression],
60
67
  // `import a.b.c` — at each `.` the parser cannot tell (within LR(1))
61
68
  // whether another path segment or the import selection follows; GLR explores
62
69
  // both and only the valid continuation survives.
@@ -224,15 +231,20 @@ export default grammar({
224
231
 
225
232
  enum_body: $ => seq('{', repeat($.variant), '}'),
226
233
 
234
+ // A `#[wire]` / `#[serial]` enum tags each variant with `@N` after its
235
+ // payload: `Pending @0;`, `Push(i64) @1;`, `Move { x: i32; } @2;`.
236
+ // A struct variant ends at `}` unless it carries a tag.
227
237
  variant: $ => seq(
228
238
  field('name', $.identifier),
229
239
  choice(
230
- ';',
231
- seq('(', sep1($._type, ','), optional(','), ')', ';'),
232
- seq('{', repeat($.struct_field), '}'),
240
+ seq(optional($.variant_tag), ';'),
241
+ seq('(', sep1($._type, ','), optional(','), ')', optional($.variant_tag), ';'),
242
+ seq('{', repeat($.struct_field), '}', optional(seq($.variant_tag, ';'))),
233
243
  ),
234
244
  ),
235
245
 
246
+ variant_tag: $ => seq('@', $.integer_literal),
247
+
236
248
  // NOTE: the legacy bare `wire` keyword item form (`wire struct/enum/type
237
249
  // Name { … }`) was removed from the compiler in hew commit 60c50daef
238
250
  // ("refactor(parser): remove the wire keyword surface in favor of #[wire]").
@@ -378,7 +390,11 @@ export default grammar({
378
390
  $.self,
379
391
  ),
380
392
 
381
- return_type: $ => prec.right(seq('->', $._type, optional($.failure_return))),
393
+ // `-> T`, `-> T fails E`, or the short `fails E` (a `() fails E` function).
394
+ return_type: $ => prec.right(choice(
395
+ seq('->', $._type, optional($.failure_return)),
396
+ $.failure_return,
397
+ )),
382
398
 
383
399
  failure_return: $ => seq('fails', field('error', $._type)),
384
400
 
@@ -686,7 +702,12 @@ export default grammar({
686
702
 
687
703
  trait_bounds: $ => sep1($.trait_bound, '+'),
688
704
 
689
- trait_bound: $ => seq($.identifier, optional($.type_arguments)),
705
+ // A trait is named by a bare or module-qualified path: `Show`, `alpha.Show`.
706
+ trait_bound: $ => seq(
707
+ $.identifier,
708
+ repeat(seq('.', $.identifier)),
709
+ optional($.type_arguments),
710
+ ),
690
711
 
691
712
  where_clause: $ => seq(
692
713
  'where',
@@ -869,6 +890,14 @@ export default grammar({
869
890
  // Statement position is handled by `expression_statement` (optional `;`).
870
891
  return_expression: $ => prec.right(seq('return', optional($.expression))),
871
892
 
893
+ // `return error <expr>` returns the failure of a `fails E` function. After
894
+ // `return`, `error` begins a failure return unless an operator, call, index,
895
+ // `?`, `as` or terminator follows, in which case it stays an ordinary
896
+ // binding (hew-parser core.rs eat_error_return_marker). The grammar cannot see
897
+ // whitespace, so an attached `return error.f()` or `return error(x)` also
898
+ // reads as a failure return; the compiler refuses the attached forms.
899
+ return_error_expression: $ => prec.dynamic(1, prec.right(seq('return', 'error', choice($.expression, $.block)))),
900
+
872
901
  // Deferred cleanup may be an expression statement or a scoped block.
873
902
  // `defer <expr>;` or `defer { … }` — a block body needs no `;`
874
903
  // (hew-parser statements.rs `Token::Defer`).
@@ -950,6 +979,8 @@ export default grammar({
950
979
  $.scope_expression,
951
980
  $.yield_expression,
952
981
  $.return_expression,
982
+ $.return_error_expression,
983
+ alias('error', $.identifier),
953
984
  $.gen_block_expression,
954
985
  $.qualified_expression,
955
986
  $.generic_call_expression,
@@ -1164,7 +1195,10 @@ export default grammar({
1164
1195
 
1165
1196
  field_initializer: $ => seq(
1166
1197
  choice(
1167
- seq(field('name', $.identifier), ':', field('value', $.expression)),
1198
+ // `error` lexes as a keyword where a statement could start (`return
1199
+ // error`, bare `error` expression); alias it so `{ error: v }` still
1200
+ // reads as a field initializer.
1201
+ seq(field('name', choice($.identifier, alias('error', $.identifier))), ':', field('value', $.expression)),
1168
1202
  seq('..', field('spread', $.expression)),
1169
1203
  ),
1170
1204
  ),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hew-lang/tree-sitter-hew",
3
- "version": "1.1.0",
3
+ "version": "1.2.0",
4
4
  "description": "Hew grammar for tree-sitter",
5
5
  "type": "module",
6
6
  "repository": {
@@ -23,6 +23,7 @@
23
23
  (private_capture_list "capture" @keyword)
24
24
  (private_capture (identifier) @variable)
25
25
  (failure_return "fails" @keyword)
26
+ (return_error_expression "error" @keyword)
26
27
  (child_clause ["count" "stop"] @keyword)
27
28
 
28
29
  "receive" @keyword
@@ -196,6 +197,14 @@
196
197
 
197
198
  (variant
198
199
  name: (identifier) @constant)
200
+ (variant_tag "@" @punctuation.special)
201
+ (variant_tag (integer_literal) @number)
202
+
203
+ ; `.Variant` and `.Variant { .. }` take their enum from the expected type.
204
+ (contextual_variant_expression
205
+ name: (identifier) @constant)
206
+ (contextual_variant_record_init
207
+ name: (identifier) @constant)
199
208
 
200
209
  ; ---- Struct init ----
201
210
  (struct_init
package/src/grammar.json CHANGED
@@ -851,8 +851,25 @@
851
851
  "type": "CHOICE",
852
852
  "members": [
853
853
  {
854
- "type": "STRING",
855
- "value": ";"
854
+ "type": "SEQ",
855
+ "members": [
856
+ {
857
+ "type": "CHOICE",
858
+ "members": [
859
+ {
860
+ "type": "SYMBOL",
861
+ "name": "variant_tag"
862
+ },
863
+ {
864
+ "type": "BLANK"
865
+ }
866
+ ]
867
+ },
868
+ {
869
+ "type": "STRING",
870
+ "value": ";"
871
+ }
872
+ ]
856
873
  },
857
874
  {
858
875
  "type": "SEQ",
@@ -902,6 +919,18 @@
902
919
  "type": "STRING",
903
920
  "value": ")"
904
921
  },
922
+ {
923
+ "type": "CHOICE",
924
+ "members": [
925
+ {
926
+ "type": "SYMBOL",
927
+ "name": "variant_tag"
928
+ },
929
+ {
930
+ "type": "BLANK"
931
+ }
932
+ ]
933
+ },
905
934
  {
906
935
  "type": "STRING",
907
936
  "value": ";"
@@ -925,6 +954,27 @@
925
954
  {
926
955
  "type": "STRING",
927
956
  "value": "}"
957
+ },
958
+ {
959
+ "type": "CHOICE",
960
+ "members": [
961
+ {
962
+ "type": "SEQ",
963
+ "members": [
964
+ {
965
+ "type": "SYMBOL",
966
+ "name": "variant_tag"
967
+ },
968
+ {
969
+ "type": "STRING",
970
+ "value": ";"
971
+ }
972
+ ]
973
+ },
974
+ {
975
+ "type": "BLANK"
976
+ }
977
+ ]
928
978
  }
929
979
  ]
930
980
  }
@@ -932,6 +982,19 @@
932
982
  }
933
983
  ]
934
984
  },
985
+ "variant_tag": {
986
+ "type": "SEQ",
987
+ "members": [
988
+ {
989
+ "type": "STRING",
990
+ "value": "@"
991
+ },
992
+ {
993
+ "type": "SYMBOL",
994
+ "name": "integer_literal"
995
+ }
996
+ ]
997
+ },
935
998
  "reserved_declaration": {
936
999
  "type": "SEQ",
937
1000
  "members": [
@@ -1771,27 +1834,36 @@
1771
1834
  "type": "PREC_RIGHT",
1772
1835
  "value": 0,
1773
1836
  "content": {
1774
- "type": "SEQ",
1837
+ "type": "CHOICE",
1775
1838
  "members": [
1776
1839
  {
1777
- "type": "STRING",
1778
- "value": "->"
1779
- },
1780
- {
1781
- "type": "SYMBOL",
1782
- "name": "_type"
1783
- },
1784
- {
1785
- "type": "CHOICE",
1840
+ "type": "SEQ",
1786
1841
  "members": [
1842
+ {
1843
+ "type": "STRING",
1844
+ "value": "->"
1845
+ },
1787
1846
  {
1788
1847
  "type": "SYMBOL",
1789
- "name": "failure_return"
1848
+ "name": "_type"
1790
1849
  },
1791
1850
  {
1792
- "type": "BLANK"
1851
+ "type": "CHOICE",
1852
+ "members": [
1853
+ {
1854
+ "type": "SYMBOL",
1855
+ "name": "failure_return"
1856
+ },
1857
+ {
1858
+ "type": "BLANK"
1859
+ }
1860
+ ]
1793
1861
  }
1794
1862
  ]
1863
+ },
1864
+ {
1865
+ "type": "SYMBOL",
1866
+ "name": "failure_return"
1795
1867
  }
1796
1868
  ]
1797
1869
  }
@@ -3819,6 +3891,22 @@
3819
3891
  "type": "SYMBOL",
3820
3892
  "name": "identifier"
3821
3893
  },
3894
+ {
3895
+ "type": "REPEAT",
3896
+ "content": {
3897
+ "type": "SEQ",
3898
+ "members": [
3899
+ {
3900
+ "type": "STRING",
3901
+ "value": "."
3902
+ },
3903
+ {
3904
+ "type": "SYMBOL",
3905
+ "name": "identifier"
3906
+ }
3907
+ ]
3908
+ }
3909
+ },
3822
3910
  {
3823
3911
  "type": "CHOICE",
3824
3912
  "members": [
@@ -5086,6 +5174,40 @@
5086
5174
  ]
5087
5175
  }
5088
5176
  },
5177
+ "return_error_expression": {
5178
+ "type": "PREC_DYNAMIC",
5179
+ "value": 1,
5180
+ "content": {
5181
+ "type": "PREC_RIGHT",
5182
+ "value": 0,
5183
+ "content": {
5184
+ "type": "SEQ",
5185
+ "members": [
5186
+ {
5187
+ "type": "STRING",
5188
+ "value": "return"
5189
+ },
5190
+ {
5191
+ "type": "STRING",
5192
+ "value": "error"
5193
+ },
5194
+ {
5195
+ "type": "CHOICE",
5196
+ "members": [
5197
+ {
5198
+ "type": "SYMBOL",
5199
+ "name": "expression"
5200
+ },
5201
+ {
5202
+ "type": "SYMBOL",
5203
+ "name": "block"
5204
+ }
5205
+ ]
5206
+ }
5207
+ ]
5208
+ }
5209
+ }
5210
+ },
5089
5211
  "defer_statement": {
5090
5212
  "type": "SEQ",
5091
5213
  "members": [
@@ -5469,6 +5591,19 @@
5469
5591
  "type": "SYMBOL",
5470
5592
  "name": "return_expression"
5471
5593
  },
5594
+ {
5595
+ "type": "SYMBOL",
5596
+ "name": "return_error_expression"
5597
+ },
5598
+ {
5599
+ "type": "ALIAS",
5600
+ "content": {
5601
+ "type": "STRING",
5602
+ "value": "error"
5603
+ },
5604
+ "named": true,
5605
+ "value": "identifier"
5606
+ },
5472
5607
  {
5473
5608
  "type": "SYMBOL",
5474
5609
  "name": "gen_block_expression"
@@ -6965,8 +7100,22 @@
6965
7100
  "type": "FIELD",
6966
7101
  "name": "name",
6967
7102
  "content": {
6968
- "type": "SYMBOL",
6969
- "name": "identifier"
7103
+ "type": "CHOICE",
7104
+ "members": [
7105
+ {
7106
+ "type": "SYMBOL",
7107
+ "name": "identifier"
7108
+ },
7109
+ {
7110
+ "type": "ALIAS",
7111
+ "content": {
7112
+ "type": "STRING",
7113
+ "value": "error"
7114
+ },
7115
+ "named": true,
7116
+ "value": "identifier"
7117
+ }
7118
+ ]
6970
7119
  }
6971
7120
  },
6972
7121
  {
@@ -9557,9 +9706,19 @@
9557
9706
  [
9558
9707
  "trait_bound"
9559
9708
  ],
9709
+ [
9710
+ "function_type"
9711
+ ],
9712
+ [
9713
+ "actor_type"
9714
+ ],
9560
9715
  [
9561
9716
  "where_clause"
9562
9717
  ],
9718
+ [
9719
+ "return_error_expression",
9720
+ "expression"
9721
+ ],
9563
9722
  [
9564
9723
  "module_path"
9565
9724
  ],
@@ -2053,6 +2053,10 @@
2053
2053
  "type": "regex_literal",
2054
2054
  "named": true
2055
2055
  },
2056
+ {
2057
+ "type": "return_error_expression",
2058
+ "named": true
2059
+ },
2056
2060
  {
2057
2061
  "type": "return_expression",
2058
2062
  "named": true
@@ -4831,6 +4835,25 @@
4831
4835
  ]
4832
4836
  }
4833
4837
  },
4838
+ {
4839
+ "type": "return_error_expression",
4840
+ "named": true,
4841
+ "fields": {},
4842
+ "children": {
4843
+ "multiple": false,
4844
+ "required": true,
4845
+ "types": [
4846
+ {
4847
+ "type": "block",
4848
+ "named": true
4849
+ },
4850
+ {
4851
+ "type": "expression",
4852
+ "named": true
4853
+ }
4854
+ ]
4855
+ }
4856
+ },
4834
4857
  {
4835
4858
  "type": "return_expression",
4836
4859
  "named": true,
@@ -6316,6 +6339,10 @@
6316
6339
  {
6317
6340
  "type": "unit_type",
6318
6341
  "named": true
6342
+ },
6343
+ {
6344
+ "type": "variant_tag",
6345
+ "named": true
6319
6346
  }
6320
6347
  ]
6321
6348
  }
@@ -6350,6 +6377,21 @@
6350
6377
  ]
6351
6378
  }
6352
6379
  },
6380
+ {
6381
+ "type": "variant_tag",
6382
+ "named": true,
6383
+ "fields": {},
6384
+ "children": {
6385
+ "multiple": false,
6386
+ "required": true,
6387
+ "types": [
6388
+ {
6389
+ "type": "integer_literal",
6390
+ "named": true
6391
+ }
6392
+ ]
6393
+ }
6394
+ },
6353
6395
  {
6354
6396
  "type": "visibility",
6355
6397
  "named": true,
@@ -6852,6 +6894,10 @@
6852
6894
  "type": "enum",
6853
6895
  "named": false
6854
6896
  },
6897
+ {
6898
+ "type": "error",
6899
+ "named": false
6900
+ },
6855
6901
  {
6856
6902
  "type": "escape_sequence",
6857
6903
  "named": true