@willbooster/tree-sitter-rust 0.0.0-placeholder.0 → 1.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/grammar.js ADDED
@@ -0,0 +1,1336 @@
1
+ /**
2
+ * @file Rust grammar for tree-sitter
3
+ * @author Maxim Sokolov <maxim0xff@gmail.com>
4
+ * @author Max Brunsfeld <maxbrunsfeld@gmail.com>
5
+ * @author Amaan Qureshi <amaanq12@gmail.com>
6
+ * @license MIT
7
+ */
8
+
9
+ /// <reference types="tree-sitter-cli/dsl" />
10
+ // @ts-check
11
+
12
+ // https://doc.rust-lang.org/reference/expressions.html#expression-precedence
13
+ const PREC = {
14
+ call: 15,
15
+ field: 14,
16
+ try: 13,
17
+ unary: 12,
18
+ cast: 11,
19
+ multiplicative: 10,
20
+ additive: 9,
21
+ shift: 8,
22
+ bitand: 7,
23
+ bitxor: 6,
24
+ bitor: 5,
25
+ comparative: 4,
26
+ and: 3,
27
+ or: 2,
28
+ range: 1,
29
+ assign: 0,
30
+ closure: -1,
31
+ };
32
+
33
+ const numericTypes = [
34
+ 'u8',
35
+ 'i8',
36
+ 'u16',
37
+ 'i16',
38
+ 'u32',
39
+ 'i32',
40
+ 'u64',
41
+ 'i64',
42
+ 'u128',
43
+ 'i128',
44
+ 'isize',
45
+ 'usize',
46
+ 'f32',
47
+ 'f64',
48
+ ];
49
+
50
+ // https://doc.rust-lang.org/reference/tokens.html#punctuation
51
+ const TOKEN_TREE_NON_SPECIAL_PUNCTUATION = [
52
+ '+',
53
+ '-',
54
+ '*',
55
+ '/',
56
+ '%',
57
+ '^',
58
+ '!',
59
+ '&',
60
+ '|',
61
+ '&&',
62
+ '||',
63
+ '<<',
64
+ '>>',
65
+ '+=',
66
+ '-=',
67
+ '*=',
68
+ '/=',
69
+ '%=',
70
+ '^=',
71
+ '&=',
72
+ '|=',
73
+ '<<=',
74
+ '>>=',
75
+ '=',
76
+ '==',
77
+ '!=',
78
+ '>',
79
+ '<',
80
+ '>=',
81
+ '<=',
82
+ '@',
83
+ '_',
84
+ '.',
85
+ '..',
86
+ '...',
87
+ '..=',
88
+ ',',
89
+ ';',
90
+ ':',
91
+ '::',
92
+ '->',
93
+ '=>',
94
+ '#',
95
+ '?',
96
+ ];
97
+
98
+ const primitiveTypes = [...numericTypes, 'bool', 'str', 'char'];
99
+
100
+ // oxlint-disable-next-line unicorn/prefer-module -- This package is CommonJS, so tree-sitter loads grammar.js as CommonJS.
101
+ module.exports = grammar({
102
+ name: 'rust',
103
+
104
+ extras: ($) => [/\s/, $.line_comment, $.block_comment],
105
+
106
+ externals: ($) => [
107
+ $.string_content,
108
+ $.string_close,
109
+ $._raw_string_literal_start,
110
+ $.raw_string_literal_content,
111
+ $._raw_string_literal_end,
112
+ $.float_literal,
113
+ $._outer_block_doc_comment_marker,
114
+ $._inner_block_doc_comment_marker,
115
+ $._block_comment_content,
116
+ $._line_doc_content,
117
+ $._error_sentinel,
118
+ ],
119
+
120
+ supertypes: ($) => [$._expression, $._type, $._literal, $._literal_pattern, $._declaration_statement, $._pattern],
121
+
122
+ inline: ($) => [
123
+ $._path,
124
+ $._type_identifier,
125
+ $._tokens,
126
+ $._field_identifier,
127
+ $._non_special_token,
128
+ $._declaration_statement,
129
+ $._reserved_identifier,
130
+ $._expression_ending_with_block,
131
+ ],
132
+
133
+ conflicts: ($) => [
134
+ // Local ambiguity due to anonymous types:
135
+ // See https://internals.rust-lang.org/t/pre-rfc-deprecating-anonymous-parameters/3710
136
+ [$._type, $._pattern],
137
+ [$.unit_type, $.tuple_pattern],
138
+ [$.scoped_identifier, $.scoped_type_identifier],
139
+ [$.parameters, $._pattern],
140
+ [$.parameters, $.tuple_struct_pattern],
141
+ [$.array_expression],
142
+ [$.visibility_modifier],
143
+ [$.visibility_modifier, $.scoped_identifier, $.scoped_type_identifier],
144
+ [$.foreign_mod_item, $.function_modifiers],
145
+ ],
146
+
147
+ word: ($) => $.identifier,
148
+
149
+ rules: {
150
+ source_file: ($) => seq(optional($.shebang), repeat($._statement)),
151
+
152
+ _statement: ($) => choice($.expression_statement, $._declaration_statement),
153
+
154
+ empty_statement: () => ';',
155
+
156
+ expression_statement: ($) => choice(seq($._expression, ';'), prec(1, $._expression_ending_with_block)),
157
+
158
+ _declaration_statement: ($) =>
159
+ choice(
160
+ $.const_item,
161
+ $.macro_invocation,
162
+ $.macro_definition,
163
+ $.empty_statement,
164
+ $.attribute_item,
165
+ $.inner_attribute_item,
166
+ $.mod_item,
167
+ $.foreign_mod_item,
168
+ $.struct_item,
169
+ $.union_item,
170
+ $.enum_item,
171
+ $.type_item,
172
+ $.function_item,
173
+ $.function_signature_item,
174
+ $.impl_item,
175
+ $.trait_item,
176
+ $.associated_type,
177
+ $.let_declaration,
178
+ $.use_declaration,
179
+ $.extern_crate_declaration,
180
+ $.static_item
181
+ ),
182
+
183
+ // Section - Macro definitions
184
+
185
+ macro_definition: ($) => {
186
+ const rules = seq(repeat(seq($.macro_rule, ';')), optional($.macro_rule));
187
+
188
+ return seq(
189
+ 'macro_rules!',
190
+ field('name', choice($.identifier, $._reserved_identifier)),
191
+ choice(seq('(', rules, ')', ';'), seq('[', rules, ']', ';'), seq('{', rules, '}'))
192
+ );
193
+ },
194
+
195
+ macro_rule: ($) => seq(field('left', $.token_tree_pattern), '=>', field('right', $.token_tree)),
196
+
197
+ _token_pattern: ($) =>
198
+ choice(
199
+ $.token_tree_pattern,
200
+ $.token_repetition_pattern,
201
+ $.token_binding_pattern,
202
+ $.metavariable,
203
+ $._non_special_token
204
+ ),
205
+
206
+ token_tree_pattern: ($) =>
207
+ choice(
208
+ seq('(', repeat($._token_pattern), ')'),
209
+ seq('[', repeat($._token_pattern), ']'),
210
+ seq('{', repeat($._token_pattern), '}')
211
+ ),
212
+
213
+ token_binding_pattern: ($) => prec(1, seq(field('name', $.metavariable), ':', field('type', $.fragment_specifier))),
214
+
215
+ token_repetition_pattern: ($) =>
216
+ seq('$', '(', repeat($._token_pattern), ')', optional(/[^+*?]+/), choice('+', '*', '?')),
217
+
218
+ fragment_specifier: () =>
219
+ choice(
220
+ 'block',
221
+ 'expr',
222
+ 'expr_2021',
223
+ 'ident',
224
+ 'item',
225
+ 'lifetime',
226
+ 'literal',
227
+ 'meta',
228
+ 'pat',
229
+ 'pat_param',
230
+ 'path',
231
+ 'stmt',
232
+ 'tt',
233
+ 'ty',
234
+ 'vis'
235
+ ),
236
+
237
+ _tokens: ($) => choice($.token_tree, $.token_repetition, $.metavariable, $._non_special_token),
238
+
239
+ token_tree: ($) =>
240
+ choice(seq('(', repeat($._tokens), ')'), seq('[', repeat($._tokens), ']'), seq('{', repeat($._tokens), '}')),
241
+
242
+ token_repetition: ($) => seq('$', '(', repeat($._tokens), ')', optional(/[^+*?]+/), choice('+', '*', '?')),
243
+
244
+ // Matches non-delimiter tokens common to both macro invocations and
245
+ // definitions. This is everything except $ and metavariables (which begin
246
+ // with $).
247
+ _non_special_token: ($) =>
248
+ choice(
249
+ $._literal,
250
+ $.identifier,
251
+ $.mutable_specifier,
252
+ $.self,
253
+ $.super,
254
+ $.crate,
255
+ alias(choice(...primitiveTypes), $.primitive_type),
256
+ prec.right(repeat1(choice(...TOKEN_TREE_NON_SPECIAL_PUNCTUATION))),
257
+ "'",
258
+ 'as',
259
+ 'async',
260
+ 'await',
261
+ 'break',
262
+ 'const',
263
+ 'continue',
264
+ 'default',
265
+ 'enum',
266
+ 'fn',
267
+ 'for',
268
+ 'gen',
269
+ 'if',
270
+ 'impl',
271
+ 'let',
272
+ 'loop',
273
+ 'match',
274
+ 'mod',
275
+ 'pub',
276
+ 'return',
277
+ 'static',
278
+ 'struct',
279
+ 'trait',
280
+ 'type',
281
+ 'union',
282
+ 'unsafe',
283
+ 'use',
284
+ 'where',
285
+ 'while'
286
+ ),
287
+
288
+ // Section - Declarations
289
+
290
+ attribute_item: ($) => seq('#', '[', $.attribute, ']'),
291
+
292
+ inner_attribute_item: ($) => seq('#', '!', '[', $.attribute, ']'),
293
+
294
+ attribute: ($) =>
295
+ seq(
296
+ $._path,
297
+ optional(
298
+ choice(seq('=', field('value', $._expression)), field('arguments', alias($.delim_token_tree, $.token_tree)))
299
+ )
300
+ ),
301
+
302
+ mod_item: ($) =>
303
+ seq(
304
+ optional($.visibility_modifier),
305
+ 'mod',
306
+ field('name', $.identifier),
307
+ choice(';', field('body', $.declaration_list))
308
+ ),
309
+
310
+ foreign_mod_item: ($) => seq(optional('unsafe'), $.extern_modifier, choice(';', field('body', $.declaration_list))),
311
+
312
+ declaration_list: ($) => seq('{', repeat($._declaration_statement), '}'),
313
+
314
+ struct_item: ($) =>
315
+ seq(
316
+ optional($.visibility_modifier),
317
+ 'struct',
318
+ field('name', $._type_identifier),
319
+ field('type_parameters', optional($.type_parameters)),
320
+ choice(
321
+ seq(optional($.where_clause), field('body', $.field_declaration_list)),
322
+ seq(field('body', $.ordered_field_declaration_list), optional($.where_clause), ';'),
323
+ ';'
324
+ )
325
+ ),
326
+
327
+ union_item: ($) =>
328
+ seq(
329
+ optional($.visibility_modifier),
330
+ 'union',
331
+ field('name', $._type_identifier),
332
+ field('type_parameters', optional($.type_parameters)),
333
+ optional($.where_clause),
334
+ field('body', $.field_declaration_list)
335
+ ),
336
+
337
+ enum_item: ($) =>
338
+ seq(
339
+ optional($.visibility_modifier),
340
+ 'enum',
341
+ field('name', $._type_identifier),
342
+ field('type_parameters', optional($.type_parameters)),
343
+ optional($.where_clause),
344
+ field('body', $.enum_variant_list)
345
+ ),
346
+
347
+ enum_variant_list: ($) => seq('{', sepBy(',', seq(repeat($.attribute_item), $.enum_variant)), optional(','), '}'),
348
+
349
+ enum_variant: ($) =>
350
+ seq(
351
+ optional($.visibility_modifier),
352
+ field('name', $.identifier),
353
+ field('body', optional(choice($.field_declaration_list, $.ordered_field_declaration_list))),
354
+ optional(seq('=', field('value', $._expression)))
355
+ ),
356
+
357
+ field_declaration_list: ($) =>
358
+ seq('{', sepBy(',', seq(repeat($.attribute_item), $.field_declaration)), optional(','), '}'),
359
+
360
+ field_declaration: ($) =>
361
+ seq(optional($.visibility_modifier), field('name', $._field_identifier), ':', field('type', $._type)),
362
+
363
+ ordered_field_declaration_list: ($) =>
364
+ seq(
365
+ '(',
366
+ sepBy(',', seq(repeat($.attribute_item), optional($.visibility_modifier), field('type', $._type))),
367
+ optional(','),
368
+ ')'
369
+ ),
370
+
371
+ extern_crate_declaration: ($) =>
372
+ seq(
373
+ optional($.visibility_modifier),
374
+ 'extern',
375
+ $.crate,
376
+ field('name', $.identifier),
377
+ optional(seq('as', field('alias', $.identifier))),
378
+ ';'
379
+ ),
380
+
381
+ const_item: ($) =>
382
+ seq(
383
+ optional($.visibility_modifier),
384
+ 'const',
385
+ field('name', $.identifier),
386
+ ':',
387
+ field('type', $._type),
388
+ optional(seq('=', field('value', $._expression))),
389
+ ';'
390
+ ),
391
+
392
+ static_item: ($) =>
393
+ seq(
394
+ optional($.visibility_modifier),
395
+ 'static',
396
+
397
+ // Not actual rust syntax, but made popular by the lazy_static crate.
398
+ optional('ref'),
399
+
400
+ optional($.mutable_specifier),
401
+ field('name', $.identifier),
402
+ ':',
403
+ field('type', $._type),
404
+ optional(seq('=', field('value', $._expression))),
405
+ ';'
406
+ ),
407
+
408
+ type_item: ($) =>
409
+ seq(
410
+ optional($.visibility_modifier),
411
+ 'type',
412
+ field('name', $._type_identifier),
413
+ field('type_parameters', optional($.type_parameters)),
414
+ optional($.where_clause),
415
+ '=',
416
+ field('type', $._type),
417
+ optional($.where_clause),
418
+ ';'
419
+ ),
420
+
421
+ function_item: ($) =>
422
+ seq(
423
+ optional($.visibility_modifier),
424
+ optional($.function_modifiers),
425
+ 'fn',
426
+ field('name', choice($.identifier, $.metavariable)),
427
+ field('type_parameters', optional($.type_parameters)),
428
+ field('parameters', $.parameters),
429
+ optional(seq('->', field('return_type', $._type))),
430
+ optional($.where_clause),
431
+ field('body', $.block)
432
+ ),
433
+
434
+ function_signature_item: ($) =>
435
+ seq(
436
+ optional($.visibility_modifier),
437
+ optional($.function_modifiers),
438
+ 'fn',
439
+ field('name', choice($.identifier, $.metavariable)),
440
+ field('type_parameters', optional($.type_parameters)),
441
+ field('parameters', $.parameters),
442
+ optional(seq('->', field('return_type', $._type))),
443
+ optional($.where_clause),
444
+ ';'
445
+ ),
446
+
447
+ function_modifiers: ($) => repeat1(choice('async', 'default', 'const', 'unsafe', $.extern_modifier)),
448
+
449
+ where_clause: ($) => prec.right(seq('where', optional(seq(sepBy1(',', $.where_predicate), optional(','))))),
450
+
451
+ where_predicate: ($) =>
452
+ seq(
453
+ field(
454
+ 'left',
455
+ choice(
456
+ $.lifetime,
457
+ $._type_identifier,
458
+ $.scoped_type_identifier,
459
+ $.generic_type,
460
+ $.reference_type,
461
+ $.pointer_type,
462
+ $.tuple_type,
463
+ $.array_type,
464
+ $.higher_ranked_trait_bound,
465
+ alias(choice(...primitiveTypes), $.primitive_type)
466
+ )
467
+ ),
468
+ field('bounds', $.trait_bounds)
469
+ ),
470
+
471
+ impl_item: ($) =>
472
+ seq(
473
+ optional('unsafe'),
474
+ 'impl',
475
+ field('type_parameters', optional($.type_parameters)),
476
+ optional(
477
+ seq(
478
+ optional('!'),
479
+ field('trait', choice($._type_identifier, $.scoped_type_identifier, $.generic_type)),
480
+ 'for'
481
+ )
482
+ ),
483
+ field('type', $._type),
484
+ optional($.where_clause),
485
+ choice(field('body', $.declaration_list), ';')
486
+ ),
487
+
488
+ trait_item: ($) =>
489
+ seq(
490
+ optional($.visibility_modifier),
491
+ optional('unsafe'),
492
+ 'trait',
493
+ field('name', $._type_identifier),
494
+ field('type_parameters', optional($.type_parameters)),
495
+ field('bounds', optional($.trait_bounds)),
496
+ optional($.where_clause),
497
+ field('body', $.declaration_list)
498
+ ),
499
+
500
+ associated_type: ($) =>
501
+ seq(
502
+ 'type',
503
+ field('name', $._type_identifier),
504
+ field('type_parameters', optional($.type_parameters)),
505
+ field('bounds', optional($.trait_bounds)),
506
+ optional($.where_clause),
507
+ ';'
508
+ ),
509
+
510
+ trait_bounds: ($) => seq(':', sepBy1('+', choice($._type, $.lifetime, $.higher_ranked_trait_bound))),
511
+
512
+ higher_ranked_trait_bound: ($) => seq('for', field('type_parameters', $.type_parameters), field('type', $._type)),
513
+
514
+ removed_trait_bound: ($) => seq('?', $._type),
515
+
516
+ type_parameters: ($) =>
517
+ prec(
518
+ 1,
519
+ seq(
520
+ '<',
521
+ sepBy1(
522
+ ',',
523
+ seq(
524
+ repeat($.attribute_item),
525
+ choice($.metavariable, $.type_parameter, $.lifetime_parameter, $.const_parameter)
526
+ )
527
+ ),
528
+ optional(','),
529
+ '>'
530
+ )
531
+ ),
532
+
533
+ const_parameter: ($) =>
534
+ seq(
535
+ 'const',
536
+ field('name', $.identifier),
537
+ ':',
538
+ field('type', $._type),
539
+ optional(seq('=', field('value', choice($.block, $.identifier, $._literal, $.negative_literal))))
540
+ ),
541
+
542
+ type_parameter: ($) =>
543
+ prec(
544
+ 1,
545
+ seq(
546
+ field('name', $._type_identifier),
547
+ optional(field('bounds', $.trait_bounds)),
548
+ optional(seq('=', field('default_type', $._type)))
549
+ )
550
+ ),
551
+
552
+ lifetime_parameter: ($) => prec(1, seq(field('name', $.lifetime), optional(field('bounds', $.trait_bounds)))),
553
+
554
+ let_declaration: ($) =>
555
+ seq(
556
+ 'let',
557
+ optional($.mutable_specifier),
558
+ field('pattern', $._pattern),
559
+ optional(seq(':', field('type', $._type))),
560
+ optional(seq('=', field('value', $._expression))),
561
+ optional(seq('else', field('alternative', $.block))),
562
+ ';'
563
+ ),
564
+
565
+ use_declaration: ($) => seq(optional($.visibility_modifier), 'use', field('argument', $._use_clause), ';'),
566
+
567
+ _use_clause: ($) => choice($._path, $.use_as_clause, $.use_list, $.scoped_use_list, $.use_wildcard),
568
+
569
+ scoped_use_list: ($) => seq(field('path', optional($._path)), '::', field('list', $.use_list)),
570
+
571
+ use_list: ($) => seq('{', sepBy(',', choice($._use_clause)), optional(','), '}'),
572
+
573
+ use_as_clause: ($) => seq(field('path', $._path), 'as', field('alias', $.identifier)),
574
+
575
+ use_wildcard: ($) => seq(optional(seq(optional($._path), '::')), '*'),
576
+
577
+ parameters: ($) =>
578
+ seq(
579
+ '(',
580
+ sepBy(
581
+ ',',
582
+ seq(optional($.attribute_item), choice($.parameter, $.self_parameter, $.variadic_parameter, '_', $._type))
583
+ ),
584
+ optional(','),
585
+ ')'
586
+ ),
587
+
588
+ self_parameter: ($) => seq(optional('&'), optional($.lifetime), optional($.mutable_specifier), $.self),
589
+
590
+ variadic_parameter: ($) =>
591
+ seq(optional($.mutable_specifier), optional(seq(field('pattern', $._pattern), ':')), '...'),
592
+
593
+ parameter: ($) =>
594
+ seq(optional($.mutable_specifier), field('pattern', choice($._pattern, $.self)), ':', field('type', $._type)),
595
+
596
+ extern_modifier: ($) => seq('extern', optional($.string_literal)),
597
+
598
+ visibility_modifier: ($) =>
599
+ choice($.crate, seq('pub', optional(seq('(', choice($.self, $.super, $.crate, seq('in', $._path)), ')')))),
600
+
601
+ // Section - Types
602
+
603
+ _type: ($) =>
604
+ choice(
605
+ $.abstract_type,
606
+ $.reference_type,
607
+ $.metavariable,
608
+ $.pointer_type,
609
+ $.generic_type,
610
+ $.scoped_type_identifier,
611
+ $.tuple_type,
612
+ $.unit_type,
613
+ $.array_type,
614
+ $.function_type,
615
+ $._type_identifier,
616
+ $.macro_invocation,
617
+ $.never_type,
618
+ $.dynamic_type,
619
+ $.bounded_type,
620
+ $.removed_trait_bound,
621
+ alias(choice(...primitiveTypes), $.primitive_type)
622
+ ),
623
+
624
+ bracketed_type: ($) => seq('<', choice($._type, $.qualified_type), '>'),
625
+
626
+ qualified_type: ($) => seq(field('type', $._type), 'as', field('alias', $._type)),
627
+
628
+ lifetime: ($) => prec(1, seq("'", $.identifier)),
629
+
630
+ array_type: ($) => seq('[', field('element', $._type), optional(seq(';', field('length', $._expression))), ']'),
631
+
632
+ for_lifetimes: ($) => seq('for', '<', sepBy1(',', $.lifetime), optional(','), '>'),
633
+
634
+ function_type: ($) =>
635
+ seq(
636
+ optional($.for_lifetimes),
637
+ prec(
638
+ PREC.call,
639
+ seq(
640
+ choice(
641
+ field('trait', choice($._type_identifier, $.scoped_type_identifier)),
642
+ seq(optional($.function_modifiers), 'fn')
643
+ ),
644
+ field('parameters', $.parameters)
645
+ )
646
+ ),
647
+ optional(seq('->', field('return_type', $._type)))
648
+ ),
649
+
650
+ tuple_type: ($) => seq('(', sepBy1(',', $._type), optional(','), ')'),
651
+
652
+ unit_type: () => seq('(', ')'),
653
+
654
+ generic_function: ($) =>
655
+ prec(
656
+ 1,
657
+ seq(
658
+ field('function', choice($.identifier, $.scoped_identifier, $.field_expression)),
659
+ '::',
660
+ field('type_arguments', $.type_arguments)
661
+ )
662
+ ),
663
+
664
+ generic_type: ($) =>
665
+ prec(
666
+ 1,
667
+ seq(
668
+ field('type', choice($._type_identifier, $._reserved_identifier, $.scoped_type_identifier)),
669
+ field('type_arguments', $.type_arguments)
670
+ )
671
+ ),
672
+
673
+ generic_type_with_turbofish: ($) =>
674
+ seq(
675
+ field('type', choice($._type_identifier, $.scoped_identifier)),
676
+ '::',
677
+ field('type_arguments', $.type_arguments)
678
+ ),
679
+
680
+ bounded_type: ($) =>
681
+ prec.left(-1, seq(choice($.lifetime, $._type, $.use_bounds), '+', choice($.lifetime, $._type, $.use_bounds))),
682
+
683
+ use_bounds: ($) =>
684
+ seq('use', token(prec(1, '<')), sepBy(',', choice($.lifetime, $._type_identifier)), optional(','), '>'),
685
+
686
+ type_arguments: ($) =>
687
+ seq(
688
+ token(prec(1, '<')),
689
+ sepBy1(',', seq(choice($._type, $.type_binding, $.lifetime, $._literal, $.block), optional($.trait_bounds))),
690
+ optional(','),
691
+ '>'
692
+ ),
693
+
694
+ type_binding: ($) =>
695
+ seq(
696
+ field('name', $._type_identifier),
697
+ field('type_arguments', optional($.type_arguments)),
698
+ '=',
699
+ field('type', $._type)
700
+ ),
701
+
702
+ reference_type: ($) => seq('&', optional($.lifetime), optional($.mutable_specifier), field('type', $._type)),
703
+
704
+ pointer_type: ($) => seq('*', choice('const', $.mutable_specifier), field('type', $._type)),
705
+
706
+ never_type: () => '!',
707
+
708
+ abstract_type: ($) =>
709
+ seq(
710
+ 'impl',
711
+ optional(seq('for', $.type_parameters)),
712
+ field(
713
+ 'trait',
714
+ prec(
715
+ 1,
716
+ choice(
717
+ $._type_identifier,
718
+ $.scoped_type_identifier,
719
+ $.removed_trait_bound,
720
+ $.generic_type,
721
+ $.function_type,
722
+ $.tuple_type,
723
+ $.bounded_type
724
+ )
725
+ )
726
+ )
727
+ ),
728
+
729
+ dynamic_type: ($) =>
730
+ seq(
731
+ 'dyn',
732
+ field(
733
+ 'trait',
734
+ choice(
735
+ $.higher_ranked_trait_bound,
736
+ $._type_identifier,
737
+ $.scoped_type_identifier,
738
+ $.generic_type,
739
+ $.function_type,
740
+ $.tuple_type
741
+ )
742
+ )
743
+ ),
744
+
745
+ mutable_specifier: () => 'mut',
746
+
747
+ // Section - Expressions
748
+
749
+ _expression_except_range: ($) =>
750
+ choice(
751
+ $.unary_expression,
752
+ $.reference_expression,
753
+ $.try_expression,
754
+ $.binary_expression,
755
+ $.assignment_expression,
756
+ $.compound_assignment_expr,
757
+ $.type_cast_expression,
758
+ $.call_expression,
759
+ $.return_expression,
760
+ $.yield_expression,
761
+ $._literal,
762
+ prec.left($.identifier),
763
+ alias(choice(...primitiveTypes), $.identifier),
764
+ prec.left($._reserved_identifier),
765
+ $.self,
766
+ $.scoped_identifier,
767
+ $.generic_function,
768
+ $.await_expression,
769
+ $.field_expression,
770
+ $.array_expression,
771
+ $.tuple_expression,
772
+ prec(1, $.macro_invocation),
773
+ $.unit_expression,
774
+ $.break_expression,
775
+ $.continue_expression,
776
+ $.index_expression,
777
+ $.metavariable,
778
+ $.closure_expression,
779
+ $.parenthesized_expression,
780
+ $.struct_expression,
781
+ $._expression_ending_with_block
782
+ ),
783
+
784
+ _expression: ($) => choice($._expression_except_range, $.range_expression),
785
+
786
+ _expression_ending_with_block: ($) =>
787
+ choice(
788
+ $.unsafe_block,
789
+ $.async_block,
790
+ $.gen_block,
791
+ $.try_block,
792
+ $.block,
793
+ $.if_expression,
794
+ $.match_expression,
795
+ $.while_expression,
796
+ $.loop_expression,
797
+ $.for_expression,
798
+ $.const_block
799
+ ),
800
+
801
+ macro_invocation: ($) =>
802
+ seq(
803
+ field('macro', choice($.scoped_identifier, $.identifier, $._reserved_identifier)),
804
+ '!',
805
+ alias($.delim_token_tree, $.token_tree)
806
+ ),
807
+
808
+ delim_token_tree: ($) =>
809
+ choice(
810
+ seq('(', repeat($._delim_tokens), ')'),
811
+ seq('[', repeat($._delim_tokens), ']'),
812
+ seq('{', repeat($._delim_tokens), '}')
813
+ ),
814
+
815
+ _delim_tokens: ($) => choice($._non_delim_token, alias($.delim_token_tree, $.token_tree)),
816
+
817
+ // Should match any token other than a delimiter.
818
+ _non_delim_token: ($) => choice($._non_special_token, '$'),
819
+
820
+ scoped_identifier: ($) =>
821
+ seq(
822
+ field(
823
+ 'path',
824
+ optional(choice($._path, $.bracketed_type, alias($.generic_type_with_turbofish, $.generic_type)))
825
+ ),
826
+ '::',
827
+ field('name', choice($.identifier, $.super))
828
+ ),
829
+
830
+ scoped_type_identifier_in_expression_position: ($) =>
831
+ prec(
832
+ -2,
833
+ seq(
834
+ field('path', optional(choice($._path, alias($.generic_type_with_turbofish, $.generic_type)))),
835
+ '::',
836
+ field('name', $._type_identifier)
837
+ )
838
+ ),
839
+
840
+ scoped_type_identifier: ($) =>
841
+ seq(
842
+ field(
843
+ 'path',
844
+ optional(
845
+ choice($._path, alias($.generic_type_with_turbofish, $.generic_type), $.bracketed_type, $.generic_type)
846
+ )
847
+ ),
848
+ '::',
849
+ field('name', $._type_identifier)
850
+ ),
851
+
852
+ range_expression: ($) =>
853
+ prec.left(
854
+ PREC.range,
855
+ choice(
856
+ seq($._expression, choice('..', '...', '..='), $._expression),
857
+ seq($._expression, '..'),
858
+ seq('..', $._expression),
859
+ '..'
860
+ )
861
+ ),
862
+
863
+ unary_expression: ($) => prec(PREC.unary, seq(choice('-', '*', '!'), $._expression)),
864
+
865
+ try_expression: ($) => prec(PREC.try, seq($._expression, '?')),
866
+
867
+ reference_expression: ($) =>
868
+ prec(
869
+ PREC.unary,
870
+ seq(
871
+ '&',
872
+ choice(seq('raw', choice('const', $.mutable_specifier)), optional($.mutable_specifier)),
873
+ field('value', $._expression)
874
+ )
875
+ ),
876
+
877
+ binary_expression: ($) => {
878
+ /** @type {[number, RuleOrLiteral][]} */
879
+ const table = [
880
+ [PREC.and, '&&'],
881
+ [PREC.or, '||'],
882
+ [PREC.bitand, '&'],
883
+ [PREC.bitor, '|'],
884
+ [PREC.bitxor, '^'],
885
+ [PREC.comparative, choice('==', '!=', '<', '<=', '>', '>=')],
886
+ [PREC.shift, choice('<<', '>>')],
887
+ [PREC.additive, choice('+', '-')],
888
+ [PREC.multiplicative, choice('*', '/', '%')],
889
+ ];
890
+
891
+ return choice(
892
+ ...table.map(([precedence, operator]) =>
893
+ prec.left(
894
+ precedence,
895
+ seq(field('left', $._expression), field('operator', operator), field('right', $._expression))
896
+ )
897
+ )
898
+ );
899
+ },
900
+
901
+ assignment_expression: ($) =>
902
+ prec.left(PREC.assign, seq(field('left', $._expression), '=', field('right', $._expression))),
903
+
904
+ compound_assignment_expr: ($) =>
905
+ prec.left(
906
+ PREC.assign,
907
+ seq(
908
+ field('left', $._expression),
909
+ field('operator', choice('+=', '-=', '*=', '/=', '%=', '&=', '|=', '^=', '<<=', '>>=')),
910
+ field('right', $._expression)
911
+ )
912
+ ),
913
+
914
+ type_cast_expression: ($) => prec.left(PREC.cast, seq(field('value', $._expression), 'as', field('type', $._type))),
915
+
916
+ return_expression: ($) => choice(prec.left(seq('return', $._expression)), prec(-1, 'return')),
917
+
918
+ yield_expression: ($) => choice(prec.left(seq('yield', $._expression)), prec(-1, 'yield')),
919
+
920
+ call_expression: ($) =>
921
+ prec(PREC.call, seq(field('function', $._expression_except_range), field('arguments', $.arguments))),
922
+
923
+ arguments: ($) => seq('(', sepBy(',', seq(repeat($.attribute_item), $._expression)), optional(','), ')'),
924
+
925
+ array_expression: ($) =>
926
+ seq(
927
+ '[',
928
+ repeat($.attribute_item),
929
+ choice(
930
+ seq($._expression, ';', field('length', $._expression)),
931
+ seq(sepBy(',', seq(repeat($.attribute_item), $._expression)), optional(','))
932
+ ),
933
+ ']'
934
+ ),
935
+
936
+ parenthesized_expression: ($) => seq('(', $._expression, ')'),
937
+
938
+ tuple_expression: ($) =>
939
+ seq(
940
+ '(',
941
+ repeat($.attribute_item),
942
+ seq($._expression, ','),
943
+ repeat(seq($._expression, ',')),
944
+ optional($._expression),
945
+ ')'
946
+ ),
947
+
948
+ unit_expression: () => seq('(', ')'),
949
+
950
+ struct_expression: ($) =>
951
+ seq(
952
+ field(
953
+ 'name',
954
+ choice(
955
+ $._type_identifier,
956
+ alias($.scoped_type_identifier_in_expression_position, $.scoped_type_identifier),
957
+ $.generic_type_with_turbofish
958
+ )
959
+ ),
960
+ field('body', $.field_initializer_list)
961
+ ),
962
+
963
+ field_initializer_list: ($) =>
964
+ seq(
965
+ '{',
966
+ sepBy(',', choice($.shorthand_field_initializer, $.field_initializer, $.base_field_initializer)),
967
+ optional(','),
968
+ '}'
969
+ ),
970
+
971
+ shorthand_field_initializer: ($) => seq(repeat($.attribute_item), $.identifier),
972
+
973
+ field_initializer: ($) =>
974
+ seq(
975
+ repeat($.attribute_item),
976
+ field('field', choice($._field_identifier, $.integer_literal)),
977
+ ':',
978
+ field('value', $._expression)
979
+ ),
980
+
981
+ base_field_initializer: ($) => seq('..', $._expression),
982
+
983
+ if_expression: ($) =>
984
+ prec.right(
985
+ seq(
986
+ 'if',
987
+ field('condition', $._condition),
988
+ field('consequence', $.block),
989
+ optional(field('alternative', $.else_clause))
990
+ )
991
+ ),
992
+
993
+ let_condition: ($) =>
994
+ seq('let', field('pattern', $._pattern), '=', field('value', prec.left(PREC.and, $._expression))),
995
+
996
+ _let_chain: ($) =>
997
+ prec.left(
998
+ PREC.and,
999
+ choice(
1000
+ seq($._let_chain, '&&', $.let_condition),
1001
+ seq($._let_chain, '&&', $._expression),
1002
+ seq($.let_condition, '&&', $._expression),
1003
+ seq($.let_condition, '&&', $.let_condition),
1004
+ seq($._expression, '&&', $.let_condition)
1005
+ )
1006
+ ),
1007
+
1008
+ _condition: ($) => choice($._expression, $.let_condition, alias($._let_chain, $.let_chain)),
1009
+
1010
+ else_clause: ($) => seq('else', choice($.block, $.if_expression)),
1011
+
1012
+ match_expression: ($) => seq('match', field('value', $._expression), field('body', $.match_block)),
1013
+
1014
+ match_block: ($) => seq('{', optional(seq(repeat($.match_arm), alias($.last_match_arm, $.match_arm))), '}'),
1015
+
1016
+ match_arm: ($) =>
1017
+ prec.right(
1018
+ seq(
1019
+ repeat(choice($.attribute_item, $.inner_attribute_item)),
1020
+ field('pattern', $.match_pattern),
1021
+ '=>',
1022
+ choice(seq(field('value', $._expression), ','), field('value', prec(1, $._expression_ending_with_block)))
1023
+ )
1024
+ ),
1025
+
1026
+ last_match_arm: ($) =>
1027
+ seq(
1028
+ repeat(choice($.attribute_item, $.inner_attribute_item)),
1029
+ field('pattern', $.match_pattern),
1030
+ '=>',
1031
+ field('value', $._expression),
1032
+ optional(',')
1033
+ ),
1034
+
1035
+ match_pattern: ($) => seq($._pattern, optional(seq('if', field('condition', $._condition)))),
1036
+
1037
+ while_expression: ($) =>
1038
+ seq(optional(seq($.label, ':')), 'while', field('condition', $._condition), field('body', $.block)),
1039
+
1040
+ loop_expression: ($) => seq(optional(seq($.label, ':')), 'loop', field('body', $.block)),
1041
+
1042
+ for_expression: ($) =>
1043
+ seq(
1044
+ optional(seq($.label, ':')),
1045
+ 'for',
1046
+ field('pattern', $._pattern),
1047
+ 'in',
1048
+ field('value', $._expression),
1049
+ field('body', $.block)
1050
+ ),
1051
+
1052
+ const_block: ($) => seq('const', field('body', $.block)),
1053
+
1054
+ closure_expression: ($) =>
1055
+ prec(
1056
+ PREC.closure,
1057
+ seq(
1058
+ optional('static'),
1059
+ optional('async'),
1060
+ optional('move'),
1061
+ field('parameters', $.closure_parameters),
1062
+ choice(
1063
+ seq(optional(seq('->', field('return_type', $._type))), field('body', $.block)),
1064
+ field('body', choice($._expression, '_'))
1065
+ )
1066
+ )
1067
+ ),
1068
+
1069
+ closure_parameters: ($) => seq('|', sepBy(',', choice($._pattern, $.parameter)), '|'),
1070
+
1071
+ label: ($) => seq("'", $.identifier),
1072
+
1073
+ break_expression: ($) => prec.left(seq('break', optional($.label), optional($._expression))),
1074
+
1075
+ continue_expression: ($) => prec.left(seq('continue', optional($.label))),
1076
+
1077
+ index_expression: ($) => prec(PREC.call, seq($._expression, '[', $._expression, ']')),
1078
+
1079
+ await_expression: ($) => prec(PREC.field, seq($._expression, '.', 'await')),
1080
+
1081
+ field_expression: ($) =>
1082
+ prec(
1083
+ PREC.field,
1084
+ seq(field('value', $._expression), '.', field('field', choice($._field_identifier, $.integer_literal)))
1085
+ ),
1086
+
1087
+ unsafe_block: ($) => seq('unsafe', $.block),
1088
+
1089
+ async_block: ($) => seq('async', optional('move'), $.block),
1090
+
1091
+ gen_block: ($) => seq('gen', optional('move'), $.block),
1092
+
1093
+ try_block: ($) => seq('try', $.block),
1094
+
1095
+ block: ($) => seq(optional(seq($.label, ':')), '{', repeat($._statement), optional($._expression), '}'),
1096
+
1097
+ // Section - Patterns
1098
+
1099
+ _pattern: ($) =>
1100
+ choice(
1101
+ $._literal_pattern,
1102
+ alias(choice(...primitiveTypes), $.identifier),
1103
+ $.identifier,
1104
+ $.scoped_identifier,
1105
+ $.generic_pattern,
1106
+ $.tuple_pattern,
1107
+ $.tuple_struct_pattern,
1108
+ $.struct_pattern,
1109
+ $._reserved_identifier,
1110
+ $.ref_pattern,
1111
+ $.slice_pattern,
1112
+ $.captured_pattern,
1113
+ $.reference_pattern,
1114
+ $.remaining_field_pattern,
1115
+ $.mut_pattern,
1116
+ $.range_pattern,
1117
+ $.or_pattern,
1118
+ $.const_block,
1119
+ $.macro_invocation,
1120
+ '_'
1121
+ ),
1122
+
1123
+ generic_pattern: ($) =>
1124
+ seq(choice($.identifier, $.scoped_identifier), '::', field('type_arguments', $.type_arguments)),
1125
+
1126
+ tuple_pattern: ($) => seq('(', sepBy(',', choice($._pattern, $.closure_expression)), optional(','), ')'),
1127
+
1128
+ slice_pattern: ($) => seq('[', sepBy(',', $._pattern), optional(','), ']'),
1129
+
1130
+ tuple_struct_pattern: ($) =>
1131
+ seq(
1132
+ field('type', choice($.identifier, $.scoped_identifier, alias($.generic_type_with_turbofish, $.generic_type))),
1133
+ '(',
1134
+ sepBy(',', $._pattern),
1135
+ optional(','),
1136
+ ')'
1137
+ ),
1138
+
1139
+ struct_pattern: ($) =>
1140
+ seq(
1141
+ field('type', choice($._type_identifier, $.scoped_type_identifier)),
1142
+ '{',
1143
+ sepBy(',', choice($.field_pattern, $.remaining_field_pattern)),
1144
+ optional(','),
1145
+ '}'
1146
+ ),
1147
+
1148
+ field_pattern: ($) =>
1149
+ seq(
1150
+ optional('ref'),
1151
+ optional($.mutable_specifier),
1152
+ choice(
1153
+ field('name', alias($.identifier, $.shorthand_field_identifier)),
1154
+ seq(field('name', $._field_identifier), ':', field('pattern', $._pattern))
1155
+ )
1156
+ ),
1157
+
1158
+ remaining_field_pattern: () => '..',
1159
+
1160
+ mut_pattern: ($) => prec(-1, seq($.mutable_specifier, $._pattern)),
1161
+
1162
+ range_pattern: ($) =>
1163
+ choice(
1164
+ seq(
1165
+ field('left', choice($._literal_pattern, $._path)),
1166
+ choice(seq(choice('...', '..=', '..'), field('right', choice($._literal_pattern, $._path))), '..')
1167
+ ),
1168
+ seq(choice('..=', '..'), field('right', choice($._literal_pattern, $._path)))
1169
+ ),
1170
+
1171
+ ref_pattern: ($) => seq('ref', $._pattern),
1172
+
1173
+ captured_pattern: ($) => seq($.identifier, '@', $._pattern),
1174
+
1175
+ reference_pattern: ($) => seq('&', optional($.mutable_specifier), $._pattern),
1176
+
1177
+ or_pattern: ($) => prec.left(-2, choice(seq($._pattern, '|', $._pattern), seq('|', $._pattern))),
1178
+
1179
+ // Section - Literals
1180
+
1181
+ _literal: ($) =>
1182
+ choice(
1183
+ $.string_literal,
1184
+ $.raw_string_literal,
1185
+ $.char_literal,
1186
+ $.boolean_literal,
1187
+ $.integer_literal,
1188
+ $.float_literal
1189
+ ),
1190
+
1191
+ _literal_pattern: ($) =>
1192
+ choice(
1193
+ $.string_literal,
1194
+ $.raw_string_literal,
1195
+ $.char_literal,
1196
+ $.boolean_literal,
1197
+ $.integer_literal,
1198
+ $.float_literal,
1199
+ $.negative_literal
1200
+ ),
1201
+
1202
+ negative_literal: ($) => seq('-', choice($.integer_literal, $.float_literal)),
1203
+
1204
+ integer_literal: () =>
1205
+ token(seq(choice(/[0-9][0-9_]*/, /0x[0-9a-fA-F_]+/, /0b[01_]+/, /0o[0-7_]+/), optional(choice(...numericTypes)))),
1206
+
1207
+ string_literal: ($) =>
1208
+ seq(alias(/[bc]?"/, '"'), repeat(choice($.escape_sequence, $.string_content)), alias($.string_close, '"')),
1209
+
1210
+ raw_string_literal: ($) =>
1211
+ seq(
1212
+ $._raw_string_literal_start,
1213
+ alias($.raw_string_literal_content, $.string_content),
1214
+ $._raw_string_literal_end
1215
+ ),
1216
+
1217
+ char_literal: () =>
1218
+ token(
1219
+ seq(
1220
+ optional('b'),
1221
+ "'",
1222
+ optional(
1223
+ choice(seq('\\', choice(/[^xu]/, /u[0-9a-fA-F]{4}/, /u\{[0-9a-fA-F]+\}/, /x[0-9a-fA-F]{2}/)), /[^\\']/)
1224
+ ),
1225
+ "'"
1226
+ )
1227
+ ),
1228
+
1229
+ escape_sequence: () =>
1230
+ token.immediate(seq('\\', choice(/[^xu]/, /u[0-9a-fA-F]{4}/, /u\{[0-9a-fA-F]+\}/, /x[0-9a-fA-F]{2}/))),
1231
+
1232
+ boolean_literal: () => choice('true', 'false'),
1233
+
1234
+ comment: ($) => choice($.line_comment, $.block_comment),
1235
+
1236
+ line_comment: ($) =>
1237
+ seq(
1238
+ // All line comments start with two //
1239
+ '//',
1240
+ // Then are followed by:
1241
+ // - 2 or more slashes making it a regular comment
1242
+ // - 1 slash or 1 or more bang operators making it a doc comment
1243
+ // - or just content for the comment
1244
+ choice(
1245
+ // A tricky edge case where what looks like a doc comment is not
1246
+ seq(token.immediate(prec(2, /\/\//)), /.*/),
1247
+ // A regular doc comment
1248
+ seq($._line_doc_comment_marker, field('doc', alias($._line_doc_content, $.doc_comment))),
1249
+ token.immediate(prec(1, /.*/))
1250
+ )
1251
+ ),
1252
+
1253
+ _line_doc_comment_marker: ($) =>
1254
+ choice(
1255
+ // An outer line doc comment applies to the element that it is outside of
1256
+ field('outer', alias($._outer_line_doc_comment_marker, $.outer_doc_comment_marker)),
1257
+ // An inner line doc comment applies to the element it is inside of
1258
+ field('inner', alias($._inner_line_doc_comment_marker, $.inner_doc_comment_marker))
1259
+ ),
1260
+
1261
+ _inner_line_doc_comment_marker: () => token.immediate(prec(2, '!')),
1262
+ _outer_line_doc_comment_marker: () => token.immediate(prec(2, '/')),
1263
+
1264
+ block_comment: ($) =>
1265
+ seq(
1266
+ '/*',
1267
+ optional(
1268
+ choice(
1269
+ // Documentation block comments: /** docs */ or /*! docs */
1270
+ seq($._block_doc_comment_marker, optional(field('doc', alias($._block_comment_content, $.doc_comment)))),
1271
+ // Non-doc block comments
1272
+ $._block_comment_content
1273
+ )
1274
+ ),
1275
+ '*/'
1276
+ ),
1277
+
1278
+ _block_doc_comment_marker: ($) =>
1279
+ choice(
1280
+ field('outer', alias($._outer_block_doc_comment_marker, $.outer_doc_comment_marker)),
1281
+ field('inner', alias($._inner_block_doc_comment_marker, $.inner_doc_comment_marker))
1282
+ ),
1283
+
1284
+ _path: ($) =>
1285
+ choice(
1286
+ $.self,
1287
+ alias(choice(...primitiveTypes), $.identifier),
1288
+ $.metavariable,
1289
+ $.super,
1290
+ $.crate,
1291
+ $.identifier,
1292
+ $.scoped_identifier,
1293
+ $._reserved_identifier
1294
+ ),
1295
+
1296
+ identifier: () => /(r#)?[_\p{XID_Start}][_\p{XID_Continue}]*/u,
1297
+
1298
+ // oxlint-disable-next-line no-useless-escape -- tree-sitter's regex parser rejects an unescaped `[` in a character class.
1299
+ shebang: () => /#![\r\f\t\v ]*([^\[\n].*)?\n/,
1300
+
1301
+ _reserved_identifier: ($) => alias(choice('default', 'union', 'gen', 'raw'), $.identifier),
1302
+
1303
+ _type_identifier: ($) => alias($.identifier, $.type_identifier),
1304
+ _field_identifier: ($) => alias($.identifier, $.field_identifier),
1305
+
1306
+ self: () => 'self',
1307
+ super: () => 'super',
1308
+ crate: () => 'crate',
1309
+
1310
+ metavariable: () => /\$[a-zA-Z_]\w*/,
1311
+ },
1312
+ });
1313
+
1314
+ /**
1315
+ * Creates a rule to match one or more of the rules separated by the separator.
1316
+ *
1317
+ * @param {RuleOrLiteral} sep - The separator to use.
1318
+ * @param {RuleOrLiteral} rule
1319
+ *
1320
+ * @returns {SeqRule}
1321
+ */
1322
+ function sepBy1(sep, rule) {
1323
+ return seq(rule, repeat(seq(sep, rule)));
1324
+ }
1325
+
1326
+ /**
1327
+ * Creates a rule to optionally match one or more of the rules separated by the separator.
1328
+ *
1329
+ * @param {RuleOrLiteral} sep - The separator to use.
1330
+ * @param {RuleOrLiteral} rule
1331
+ *
1332
+ * @returns {ChoiceRule}
1333
+ */
1334
+ function sepBy(sep, rule) {
1335
+ return optional(sepBy1(sep, rule));
1336
+ }