@willbooster/tree-sitter-rust 4.0.0 → 5.0.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/README.md CHANGED
@@ -73,7 +73,7 @@ fuzzed with. The parser loads only with that runtime, version 2.0.0 or later; up
73
73
  ```toml
74
74
  [dependencies]
75
75
  tree-sitter = { package = "willbooster-tree-sitter", version = "2" }
76
- tree-sitter-rust = { package = "willbooster-tree-sitter-rust", version = "3" }
76
+ tree-sitter-rust = { package = "willbooster-tree-sitter-rust", version = "5" }
77
77
  ```
78
78
 
79
79
  ```rust
@@ -90,7 +90,16 @@ these wrappers, including attributed block statements. The `value` field identif
90
90
 
91
91
  Outer declaration attributes belong to an `attributed_declaration` node whose `declaration` field identifies
92
92
  the item. In declaration and statement positions, outer attributes without a following declaration or
93
- expression produce a syntax error.
93
+ expression produce a syntax error. In attribute positions, outer documentation comments (`///` and `/** ... */`) are
94
+ `attribute_item` nodes containing a `line_comment` or `block_comment`, with their existing `outer` and
95
+ `doc` fields. Inside macro token trees, outer documentation is a direct `line_comment` or
96
+ `block_comment` token child without an `attribute_item` wrapper. Inner documentation and ordinary
97
+ comments remain comment extras. Misplaced outer documentation where attributes are unavailable also
98
+ remains a comment extra with its `outer` and `doc` fields. At completed block, parameter,
99
+ parenthesized, and array boundaries, orphan outer documentation retains those fields and produces a syntax
100
+ error. Its missing owner cannot take a later item's body as an operand. Recovery after surrounding syntax
101
+ errors may return larger `ERROR` subtrees. Ordinary line comments are leaf nodes; read their `text` rather
102
+ than an anonymous `//` child.
94
103
 
95
104
  Parameter and list attributes retain their sibling nodes. Attributes inside parentheses also precede
96
105
  the operand as siblings. List and parenthesized operands remain queryable through
package/grammar.js CHANGED
@@ -8,7 +8,7 @@
8
8
 
9
9
  // @ts-check
10
10
 
11
- const { alias, choice, field, grammar, optional, prec, repeat, repeat1, seq, sym, token } =
11
+ const { alias, choice, field, grammar, optional, prec, repeat, repeat1, reserved, seq, sym, token } =
12
12
  /** @type {typeof globalThis & import('@willbooster/web-tree-sitter/dsl').DSL} */ (globalThis);
13
13
 
14
14
  // https://doc.rust-lang.org/reference/expressions.html#expression-precedence
@@ -144,6 +144,16 @@ module.exports = grammar({
144
144
  sym('frontmatter_content'),
145
145
  sym('_frontmatter_end'),
146
146
  sym('_frontmatter_context'),
147
+ sym('_ordinary_line_comment'),
148
+ sym('_extra_line_doc_comment_start'),
149
+ sym('_outer_line_comment_start'),
150
+ sym('_ordinary_block_comment_start'),
151
+ sym('_outer_block_comment_start'),
152
+ sym('_outer_line_doc_comment_marker'),
153
+ sym('_inner_line_doc_comment_marker'),
154
+ sym('_orphan_line_doc_comment_start'),
155
+ sym('_orphan_block_doc_comment_start'),
156
+ sym('_orphan_doc_missing_owner'),
147
157
  ],
148
158
 
149
159
  supertypes: ($) => [
@@ -187,7 +197,6 @@ module.exports = grammar({
187
197
  [$.scoped_identifier, $.scoped_type_identifier],
188
198
  [$.parameters, $._pattern],
189
199
  [$.parameters, $.tuple_struct_pattern],
190
- [$.array_expression],
191
200
  [$.visibility_modifier],
192
201
  [$.visibility_modifier, $.scoped_identifier, $.scoped_type_identifier],
193
202
  [$.foreign_mod_item, $.function_modifiers],
@@ -195,6 +204,11 @@ module.exports = grammar({
195
204
  [$.function_modifiers, $.trait_item],
196
205
  ],
197
206
 
207
+ reserved: {
208
+ global: () => [],
209
+ struct_name: () => ['else'],
210
+ },
211
+
198
212
  word: ($) => $.identifier,
199
213
 
200
214
  rules: {
@@ -354,6 +368,8 @@ module.exports = grammar({
354
368
 
355
369
  _non_special_token: ($) =>
356
370
  choice(
371
+ alias($._outer_line_comment, $.line_comment),
372
+ alias($._outer_block_comment, $.block_comment),
357
373
  $._literal,
358
374
  $.identifier,
359
375
  $.mutable_specifier,
@@ -395,7 +411,21 @@ module.exports = grammar({
395
411
  'while'
396
412
  ),
397
413
 
398
- attribute_item: ($) => seq('#', '[', $.attribute, ']'),
414
+ attribute_item: ($) =>
415
+ choice(
416
+ seq('#', '[', $.attribute, ']'),
417
+ alias($._outer_line_comment, $.line_comment),
418
+ alias($._outer_block_comment, $.block_comment)
419
+ ),
420
+
421
+ _orphan_doc_expression: ($) =>
422
+ seq(
423
+ alias($._orphan_doc_attribute, $.attribute_item),
424
+ field('value', alias(sym('_orphan_doc_missing_owner'), $.identifier))
425
+ ),
426
+
427
+ _orphan_doc_attribute: ($) =>
428
+ choice(alias($._orphan_line_comment, $.line_comment), alias($._orphan_block_comment, $.block_comment)),
399
429
 
400
430
  inner_attribute_item: ($) => seq('#', '!', '[', $.attribute, ']'),
401
431
 
@@ -796,6 +826,7 @@ module.exports = grammar({
796
826
  seq(repeat($.attribute_item), choice($.parameter, $.self_parameter, $.variadic_parameter, '_', $._type))
797
827
  ),
798
828
  optional(','),
829
+ optional(alias($._orphan_doc_expression, $.attributed_expression)),
799
830
  ')'
800
831
  ),
801
832
 
@@ -1211,15 +1242,22 @@ module.exports = grammar({
1211
1242
  array_expression: ($) =>
1212
1243
  seq(
1213
1244
  '[',
1214
- repeat(prec(PREC.call, $.attribute_item)),
1215
1245
  choice(
1216
- seq($._expression, ';', field('length', $._expression)),
1246
+ seq(repeat(prec(PREC.call, $.attribute_item)), $._expression, ';', field('length', $._expression)),
1217
1247
  seq(sepBy(',', seq(repeat(prec(PREC.call, $.attribute_item)), $._expression)), optional(','))
1218
1248
  ),
1249
+ optional(alias($._orphan_doc_expression, $.attributed_expression)),
1219
1250
  ']'
1220
1251
  ),
1221
1252
 
1222
- parenthesized_expression: ($) => seq('(', repeat(prec(PREC.call, $.attribute_item)), $._expression, ')'),
1253
+ parenthesized_expression: ($) =>
1254
+ seq(
1255
+ '(',
1256
+ repeat(prec(PREC.call, $.attribute_item)),
1257
+ $._expression,
1258
+ optional(alias($._orphan_doc_expression, $.attributed_expression)),
1259
+ ')'
1260
+ ),
1223
1261
 
1224
1262
  tuple_expression: ($) =>
1225
1263
  seq(
@@ -1238,7 +1276,7 @@ module.exports = grammar({
1238
1276
  field(
1239
1277
  'name',
1240
1278
  choice(
1241
- $._type_identifier,
1279
+ reserved('struct_name', $._type_identifier),
1242
1280
  prec.dynamic(-1, alias(choice(...primitiveTypes, ...reservedIdentifiers), sym('type_identifier'))),
1243
1281
  alias($.scoped_type_identifier_in_expression_position, $.scoped_type_identifier),
1244
1282
  $.generic_type_with_turbofish
@@ -1286,7 +1324,13 @@ module.exports = grammar({
1286
1324
  ),
1287
1325
 
1288
1326
  let_condition: ($) =>
1289
- seq('let', field('pattern', $._pattern), '=', field('value', prec.left(PREC.and, $._expression))),
1327
+ seq(
1328
+ optional($._outer_attributes),
1329
+ 'let',
1330
+ field('pattern', $._pattern),
1331
+ '=',
1332
+ field('value', prec.left(PREC.and, $._expression))
1333
+ ),
1290
1334
 
1291
1335
  _let_chain: ($) =>
1292
1336
  prec.left(
@@ -1396,7 +1440,15 @@ module.exports = grammar({
1396
1440
 
1397
1441
  try_block: ($) => seq('try', $.block),
1398
1442
 
1399
- block: ($) => seq(optional(seq($.label, ':')), '{', repeat($._statement), optional($._expression), '}'),
1443
+ block: ($) =>
1444
+ seq(
1445
+ optional(seq($.label, ':')),
1446
+ '{',
1447
+ repeat($._statement),
1448
+ optional($._expression),
1449
+ optional(alias($._orphan_doc_expression, $.attributed_expression)),
1450
+ '}'
1451
+ ),
1400
1452
 
1401
1453
  _pattern: ($) =>
1402
1454
  choice(
@@ -1637,32 +1689,33 @@ module.exports = grammar({
1637
1689
 
1638
1690
  comment: ($) => choice($.line_comment, $.block_comment),
1639
1691
 
1640
- line_comment: ($) =>
1641
- seq(
1642
- '//',
1643
- choice(
1644
- seq(token.immediate(prec(2, /\/\//)), /.*/),
1645
- seq($._line_doc_comment_marker, field('doc', alias(sym('_line_doc_content'), sym('doc_comment')))),
1646
- token.immediate(prec(1, /.*/))
1692
+ line_comment: () =>
1693
+ choice(
1694
+ sym('_ordinary_line_comment'),
1695
+ seq(
1696
+ alias(sym('_extra_line_doc_comment_start'), '//'),
1697
+ choice(
1698
+ field('inner', alias(sym('_inner_line_doc_comment_marker'), sym('inner_doc_comment_marker'))),
1699
+ field('outer', alias(sym('_outer_line_doc_comment_marker'), sym('outer_doc_comment_marker')))
1700
+ ),
1701
+ field('doc', alias(sym('_line_doc_content'), sym('doc_comment')))
1647
1702
  )
1648
1703
  ),
1649
1704
 
1650
- _line_doc_comment_marker: ($) =>
1651
- choice(
1652
- field('outer', alias($._outer_line_doc_comment_marker, sym('outer_doc_comment_marker'))),
1653
- field('inner', alias($._inner_line_doc_comment_marker, sym('inner_doc_comment_marker')))
1654
- ),
1705
+ _outer_line_comment: () => outerLineComment('_outer_line_comment_start'),
1655
1706
 
1656
- _inner_line_doc_comment_marker: () => token.immediate(prec(2, '!')),
1657
- _outer_line_doc_comment_marker: () => token.immediate(prec(2, '/')),
1707
+ _orphan_line_comment: () => outerLineComment('_orphan_line_doc_comment_start'),
1658
1708
 
1659
- block_comment: ($) =>
1709
+ block_comment: () =>
1660
1710
  seq(
1661
- '/*',
1711
+ alias(sym('_ordinary_block_comment_start'), '/*'),
1662
1712
  optional(
1663
1713
  choice(
1664
1714
  seq(
1665
- $._block_doc_comment_marker,
1715
+ choice(
1716
+ field('inner', alias(sym('_inner_block_doc_comment_marker'), sym('inner_doc_comment_marker'))),
1717
+ field('outer', alias(sym('_outer_block_doc_comment_marker'), sym('outer_doc_comment_marker')))
1718
+ ),
1666
1719
  optional(field('doc', alias(sym('_block_comment_content'), sym('doc_comment'))))
1667
1720
  ),
1668
1721
  sym('_block_comment_content')
@@ -1671,11 +1724,9 @@ module.exports = grammar({
1671
1724
  '*/'
1672
1725
  ),
1673
1726
 
1674
- _block_doc_comment_marker: () =>
1675
- choice(
1676
- field('outer', alias(sym('_outer_block_doc_comment_marker'), sym('outer_doc_comment_marker'))),
1677
- field('inner', alias(sym('_inner_block_doc_comment_marker'), sym('inner_doc_comment_marker')))
1678
- ),
1727
+ _outer_block_comment: () => outerBlockComment('_outer_block_comment_start'),
1728
+
1729
+ _orphan_block_comment: () => outerBlockComment('_orphan_block_doc_comment_start'),
1679
1730
 
1680
1731
  _path: ($) =>
1681
1732
  choice(
@@ -1773,3 +1824,22 @@ function sepBy1(sep, rule) {
1773
1824
  function sepBy(sep, rule) {
1774
1825
  return optional(sepBy1(sep, rule));
1775
1826
  }
1827
+
1828
+ /** @param {string} start */
1829
+ function outerLineComment(start) {
1830
+ return seq(
1831
+ alias(sym(start), '//'),
1832
+ field('outer', alias(sym('_outer_line_doc_comment_marker'), sym('outer_doc_comment_marker'))),
1833
+ field('doc', alias(sym('_line_doc_content'), sym('doc_comment')))
1834
+ );
1835
+ }
1836
+
1837
+ /** @param {string} start */
1838
+ function outerBlockComment(start) {
1839
+ return seq(
1840
+ alias(sym(start), '/*'),
1841
+ field('outer', alias(sym('_outer_block_doc_comment_marker'), sym('outer_doc_comment_marker'))),
1842
+ optional(field('doc', alias(sym('_block_comment_content'), sym('doc_comment')))),
1843
+ '*/'
1844
+ );
1845
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@willbooster/tree-sitter-rust",
3
- "version": "4.0.0",
3
+ "version": "5.0.0",
4
4
  "description": "Rust grammar for tree-sitter",
5
5
  "keywords": [
6
6
  "incremental",
@@ -156,7 +156,7 @@
156
156
 
157
157
  (escape_sequence) @escape
158
158
 
159
- (attribute_item) @attribute
159
+ (attribute_item (attribute)) @attribute
160
160
  (inner_attribute_item) @attribute
161
161
 
162
162
  "*" @operator
@@ -778,6 +778,14 @@
778
778
  {
779
779
  "type": "attribute",
780
780
  "named": true
781
+ },
782
+ {
783
+ "type": "block_comment",
784
+ "named": true
785
+ },
786
+ {
787
+ "type": "line_comment",
788
+ "named": true
781
789
  }
782
790
  ]
783
791
  }
@@ -2816,11 +2824,6 @@
2816
2824
  ]
2817
2825
  }
2818
2826
  },
2819
- {
2820
- "type": "inner_doc_comment_marker",
2821
- "named": true,
2822
- "fields": {}
2823
- },
2824
2827
  {
2825
2828
  "type": "integer_literal",
2826
2829
  "named": true,
@@ -2884,6 +2887,16 @@
2884
2887
  }
2885
2888
  ]
2886
2889
  }
2890
+ },
2891
+ "children": {
2892
+ "multiple": true,
2893
+ "required": false,
2894
+ "types": [
2895
+ {
2896
+ "type": "attribute_item",
2897
+ "named": true
2898
+ }
2899
+ ]
2887
2900
  }
2888
2901
  },
2889
2902
  {
@@ -3388,11 +3401,6 @@
3388
3401
  ]
3389
3402
  }
3390
3403
  },
3391
- {
3392
- "type": "outer_doc_comment_marker",
3393
- "named": true,
3394
- "fields": {}
3395
- },
3396
3404
  {
3397
3405
  "type": "parameter",
3398
3406
  "named": true,
@@ -3449,6 +3457,10 @@
3449
3457
  "type": "attribute_item",
3450
3458
  "named": true
3451
3459
  },
3460
+ {
3461
+ "type": "attributed_expression",
3462
+ "named": true
3463
+ },
3452
3464
  {
3453
3465
  "type": "parameter",
3454
3466
  "named": true
@@ -4240,6 +4252,10 @@
4240
4252
  "type": "_literal",
4241
4253
  "named": true
4242
4254
  },
4255
+ {
4256
+ "type": "block_comment",
4257
+ "named": true
4258
+ },
4243
4259
  {
4244
4260
  "type": "crate",
4245
4261
  "named": true
@@ -4248,6 +4264,10 @@
4248
4264
  "type": "identifier",
4249
4265
  "named": true
4250
4266
  },
4267
+ {
4268
+ "type": "line_comment",
4269
+ "named": true
4270
+ },
4251
4271
  {
4252
4272
  "type": "metavariable",
4253
4273
  "named": true
@@ -4291,6 +4311,10 @@
4291
4311
  "type": "_literal",
4292
4312
  "named": true
4293
4313
  },
4314
+ {
4315
+ "type": "block_comment",
4316
+ "named": true
4317
+ },
4294
4318
  {
4295
4319
  "type": "crate",
4296
4320
  "named": true
@@ -4299,6 +4323,10 @@
4299
4323
  "type": "identifier",
4300
4324
  "named": true
4301
4325
  },
4326
+ {
4327
+ "type": "line_comment",
4328
+ "named": true
4329
+ },
4302
4330
  {
4303
4331
  "type": "metavariable",
4304
4332
  "named": true
@@ -4346,6 +4374,10 @@
4346
4374
  "type": "_literal",
4347
4375
  "named": true
4348
4376
  },
4377
+ {
4378
+ "type": "block_comment",
4379
+ "named": true
4380
+ },
4349
4381
  {
4350
4382
  "type": "crate",
4351
4383
  "named": true
@@ -4354,6 +4386,10 @@
4354
4386
  "type": "identifier",
4355
4387
  "named": true
4356
4388
  },
4389
+ {
4390
+ "type": "line_comment",
4391
+ "named": true
4392
+ },
4357
4393
  {
4358
4394
  "type": "metavariable",
4359
4395
  "named": true
@@ -4397,6 +4433,10 @@
4397
4433
  "type": "_literal",
4398
4434
  "named": true
4399
4435
  },
4436
+ {
4437
+ "type": "block_comment",
4438
+ "named": true
4439
+ },
4400
4440
  {
4401
4441
  "type": "crate",
4402
4442
  "named": true
@@ -4405,6 +4445,10 @@
4405
4445
  "type": "identifier",
4406
4446
  "named": true
4407
4447
  },
4448
+ {
4449
+ "type": "line_comment",
4450
+ "named": true
4451
+ },
4408
4452
  {
4409
4453
  "type": "metavariable",
4410
4454
  "named": true
@@ -5783,6 +5827,10 @@
5783
5827
  "type": "in",
5784
5828
  "named": false
5785
5829
  },
5830
+ {
5831
+ "type": "inner_doc_comment_marker",
5832
+ "named": true
5833
+ },
5786
5834
  {
5787
5835
  "type": "item",
5788
5836
  "named": false
@@ -5831,6 +5879,10 @@
5831
5879
  "type": "mutable_specifier",
5832
5880
  "named": true
5833
5881
  },
5882
+ {
5883
+ "type": "outer_doc_comment_marker",
5884
+ "named": true
5885
+ },
5834
5886
  {
5835
5887
  "type": "pat",
5836
5888
  "named": false
Binary file
package/tree-sitter.json CHANGED
@@ -25,7 +25,7 @@
25
25
  }
26
26
  ],
27
27
  "metadata": {
28
- "version": "4.0.0",
28
+ "version": "5.0.0",
29
29
  "license": "MIT",
30
30
  "description": "Rust grammar for tree-sitter",
31
31
  "authors": [