@willbooster/tree-sitter-rust 0.0.0-placeholder.0 → 1.1.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,1369 @@
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: ($) =>
311
+ seq(
312
+ optional('unsafe'),
313
+ $.extern_modifier,
314
+ choice(';', field('body', alias($._foreign_declaration_list, $.declaration_list)))
315
+ ),
316
+
317
+ declaration_list: ($) => seq('{', repeat($._declaration_statement), '}'),
318
+
319
+ // Items in `extern` blocks may be `safe` functions and `safe` or `unsafe` statics. `safe` is a keyword only where
320
+ // such an item starts: accepting it on every item would make it one wherever an item can start, breaking its uses as
321
+ // an identifier (`let safe = …; safe = …;`).
322
+ _foreign_declaration_list: ($) =>
323
+ seq(
324
+ '{',
325
+ repeat(
326
+ choice(
327
+ $._declaration_statement,
328
+ alias($._safe_function_signature_item, $.function_signature_item),
329
+ alias($._qualified_static_item, $.static_item)
330
+ )
331
+ ),
332
+ '}'
333
+ ),
334
+
335
+ _safe_function_signature_item: ($) =>
336
+ seq(optional($.visibility_modifier), alias($._safe_modifier, $.function_modifiers), $._function_signature),
337
+
338
+ // Aliasing the bare token instead would make `function_modifiers` a leaf without the `safe` child that other
339
+ // modifiers have.
340
+ _safe_modifier: () => 'safe',
341
+
342
+ _qualified_static_item: ($) =>
343
+ seq(optional($.visibility_modifier), choice('safe', 'unsafe'), $._static_declaration),
344
+
345
+ struct_item: ($) =>
346
+ seq(
347
+ optional($.visibility_modifier),
348
+ 'struct',
349
+ field('name', $._type_identifier),
350
+ field('type_parameters', optional($.type_parameters)),
351
+ choice(
352
+ seq(optional($.where_clause), field('body', $.field_declaration_list)),
353
+ seq(field('body', $.ordered_field_declaration_list), optional($.where_clause), ';'),
354
+ ';'
355
+ )
356
+ ),
357
+
358
+ union_item: ($) =>
359
+ seq(
360
+ optional($.visibility_modifier),
361
+ 'union',
362
+ field('name', $._type_identifier),
363
+ field('type_parameters', optional($.type_parameters)),
364
+ optional($.where_clause),
365
+ field('body', $.field_declaration_list)
366
+ ),
367
+
368
+ enum_item: ($) =>
369
+ seq(
370
+ optional($.visibility_modifier),
371
+ 'enum',
372
+ field('name', $._type_identifier),
373
+ field('type_parameters', optional($.type_parameters)),
374
+ optional($.where_clause),
375
+ field('body', $.enum_variant_list)
376
+ ),
377
+
378
+ enum_variant_list: ($) => seq('{', sepBy(',', seq(repeat($.attribute_item), $.enum_variant)), optional(','), '}'),
379
+
380
+ enum_variant: ($) =>
381
+ seq(
382
+ optional($.visibility_modifier),
383
+ field('name', $.identifier),
384
+ field('body', optional(choice($.field_declaration_list, $.ordered_field_declaration_list))),
385
+ optional(seq('=', field('value', $._expression)))
386
+ ),
387
+
388
+ field_declaration_list: ($) =>
389
+ seq('{', sepBy(',', seq(repeat($.attribute_item), $.field_declaration)), optional(','), '}'),
390
+
391
+ field_declaration: ($) =>
392
+ seq(optional($.visibility_modifier), field('name', $._field_identifier), ':', field('type', $._type)),
393
+
394
+ ordered_field_declaration_list: ($) =>
395
+ seq(
396
+ '(',
397
+ sepBy(',', seq(repeat($.attribute_item), optional($.visibility_modifier), field('type', $._type))),
398
+ optional(','),
399
+ ')'
400
+ ),
401
+
402
+ extern_crate_declaration: ($) =>
403
+ seq(
404
+ optional($.visibility_modifier),
405
+ 'extern',
406
+ $.crate,
407
+ field('name', $.identifier),
408
+ optional(seq('as', field('alias', $.identifier))),
409
+ ';'
410
+ ),
411
+
412
+ const_item: ($) =>
413
+ seq(
414
+ optional($.visibility_modifier),
415
+ 'const',
416
+ field('name', $.identifier),
417
+ ':',
418
+ field('type', $._type),
419
+ optional(seq('=', field('value', $._expression))),
420
+ ';'
421
+ ),
422
+
423
+ static_item: ($) => seq(optional($.visibility_modifier), $._static_declaration),
424
+
425
+ _static_declaration: ($) =>
426
+ seq(
427
+ 'static',
428
+
429
+ // Not actual rust syntax, but made popular by the lazy_static crate.
430
+ optional('ref'),
431
+
432
+ optional($.mutable_specifier),
433
+ field('name', $.identifier),
434
+ ':',
435
+ field('type', $._type),
436
+ optional(seq('=', field('value', $._expression))),
437
+ ';'
438
+ ),
439
+
440
+ type_item: ($) =>
441
+ seq(
442
+ optional($.visibility_modifier),
443
+ 'type',
444
+ field('name', $._type_identifier),
445
+ field('type_parameters', optional($.type_parameters)),
446
+ optional($.where_clause),
447
+ '=',
448
+ field('type', $._type),
449
+ optional($.where_clause),
450
+ ';'
451
+ ),
452
+
453
+ function_item: ($) =>
454
+ seq(
455
+ optional($.visibility_modifier),
456
+ optional($.function_modifiers),
457
+ 'fn',
458
+ field('name', choice($.identifier, $.metavariable)),
459
+ field('type_parameters', optional($.type_parameters)),
460
+ field('parameters', $.parameters),
461
+ optional(seq('->', field('return_type', $._type))),
462
+ optional($.where_clause),
463
+ field('body', $.block)
464
+ ),
465
+
466
+ function_signature_item: ($) =>
467
+ seq(optional($.visibility_modifier), optional($.function_modifiers), $._function_signature),
468
+
469
+ _function_signature: ($) =>
470
+ seq(
471
+ 'fn',
472
+ field('name', choice($.identifier, $.metavariable)),
473
+ field('type_parameters', optional($.type_parameters)),
474
+ field('parameters', $.parameters),
475
+ optional(seq('->', field('return_type', $._type))),
476
+ optional($.where_clause),
477
+ ';'
478
+ ),
479
+
480
+ function_modifiers: ($) => repeat1(choice('async', 'default', 'const', 'unsafe', $.extern_modifier)),
481
+
482
+ where_clause: ($) => prec.right(seq('where', optional(seq(sepBy1(',', $.where_predicate), optional(','))))),
483
+
484
+ where_predicate: ($) =>
485
+ seq(
486
+ field(
487
+ 'left',
488
+ choice(
489
+ $.lifetime,
490
+ $._type_identifier,
491
+ $.scoped_type_identifier,
492
+ $.generic_type,
493
+ $.reference_type,
494
+ $.pointer_type,
495
+ $.tuple_type,
496
+ $.array_type,
497
+ $.higher_ranked_trait_bound,
498
+ alias(choice(...primitiveTypes), $.primitive_type)
499
+ )
500
+ ),
501
+ field('bounds', $.trait_bounds)
502
+ ),
503
+
504
+ impl_item: ($) =>
505
+ seq(
506
+ optional('unsafe'),
507
+ 'impl',
508
+ field('type_parameters', optional($.type_parameters)),
509
+ optional(
510
+ seq(
511
+ optional('!'),
512
+ field('trait', choice($._type_identifier, $.scoped_type_identifier, $.generic_type)),
513
+ 'for'
514
+ )
515
+ ),
516
+ field('type', $._type),
517
+ optional($.where_clause),
518
+ choice(field('body', $.declaration_list), ';')
519
+ ),
520
+
521
+ trait_item: ($) =>
522
+ seq(
523
+ optional($.visibility_modifier),
524
+ optional('unsafe'),
525
+ 'trait',
526
+ field('name', $._type_identifier),
527
+ field('type_parameters', optional($.type_parameters)),
528
+ field('bounds', optional($.trait_bounds)),
529
+ optional($.where_clause),
530
+ field('body', $.declaration_list)
531
+ ),
532
+
533
+ associated_type: ($) =>
534
+ seq(
535
+ 'type',
536
+ field('name', $._type_identifier),
537
+ field('type_parameters', optional($.type_parameters)),
538
+ field('bounds', optional($.trait_bounds)),
539
+ optional($.where_clause),
540
+ ';'
541
+ ),
542
+
543
+ trait_bounds: ($) => seq(':', sepBy1('+', choice($._type, $.lifetime, $.higher_ranked_trait_bound))),
544
+
545
+ higher_ranked_trait_bound: ($) => seq('for', field('type_parameters', $.type_parameters), field('type', $._type)),
546
+
547
+ removed_trait_bound: ($) => seq('?', $._type),
548
+
549
+ type_parameters: ($) =>
550
+ prec(
551
+ 1,
552
+ seq(
553
+ '<',
554
+ sepBy1(
555
+ ',',
556
+ seq(
557
+ repeat($.attribute_item),
558
+ choice($.metavariable, $.type_parameter, $.lifetime_parameter, $.const_parameter)
559
+ )
560
+ ),
561
+ optional(','),
562
+ '>'
563
+ )
564
+ ),
565
+
566
+ const_parameter: ($) =>
567
+ seq(
568
+ 'const',
569
+ field('name', $.identifier),
570
+ ':',
571
+ field('type', $._type),
572
+ optional(seq('=', field('value', choice($.block, $.identifier, $._literal, $.negative_literal))))
573
+ ),
574
+
575
+ type_parameter: ($) =>
576
+ prec(
577
+ 1,
578
+ seq(
579
+ field('name', $._type_identifier),
580
+ optional(field('bounds', $.trait_bounds)),
581
+ optional(seq('=', field('default_type', $._type)))
582
+ )
583
+ ),
584
+
585
+ lifetime_parameter: ($) => prec(1, seq(field('name', $.lifetime), optional(field('bounds', $.trait_bounds)))),
586
+
587
+ let_declaration: ($) =>
588
+ seq(
589
+ 'let',
590
+ optional($.mutable_specifier),
591
+ field('pattern', $._pattern),
592
+ optional(seq(':', field('type', $._type))),
593
+ optional(seq('=', field('value', $._expression))),
594
+ optional(seq('else', field('alternative', $.block))),
595
+ ';'
596
+ ),
597
+
598
+ use_declaration: ($) => seq(optional($.visibility_modifier), 'use', field('argument', $._use_clause), ';'),
599
+
600
+ _use_clause: ($) => choice($._path, $.use_as_clause, $.use_list, $.scoped_use_list, $.use_wildcard),
601
+
602
+ scoped_use_list: ($) => seq(field('path', optional($._path)), '::', field('list', $.use_list)),
603
+
604
+ use_list: ($) => seq('{', sepBy(',', choice($._use_clause)), optional(','), '}'),
605
+
606
+ use_as_clause: ($) => seq(field('path', $._path), 'as', field('alias', $.identifier)),
607
+
608
+ use_wildcard: ($) => seq(optional(seq(optional($._path), '::')), '*'),
609
+
610
+ parameters: ($) =>
611
+ seq(
612
+ '(',
613
+ sepBy(
614
+ ',',
615
+ seq(optional($.attribute_item), choice($.parameter, $.self_parameter, $.variadic_parameter, '_', $._type))
616
+ ),
617
+ optional(','),
618
+ ')'
619
+ ),
620
+
621
+ self_parameter: ($) => seq(optional('&'), optional($.lifetime), optional($.mutable_specifier), $.self),
622
+
623
+ variadic_parameter: ($) =>
624
+ seq(optional($.mutable_specifier), optional(seq(field('pattern', $._pattern), ':')), '...'),
625
+
626
+ parameter: ($) =>
627
+ seq(optional($.mutable_specifier), field('pattern', choice($._pattern, $.self)), ':', field('type', $._type)),
628
+
629
+ extern_modifier: ($) => seq('extern', optional($.string_literal)),
630
+
631
+ visibility_modifier: ($) =>
632
+ choice($.crate, seq('pub', optional(seq('(', choice($.self, $.super, $.crate, seq('in', $._path)), ')')))),
633
+
634
+ // Section - Types
635
+
636
+ _type: ($) =>
637
+ choice(
638
+ $.abstract_type,
639
+ $.reference_type,
640
+ $.metavariable,
641
+ $.pointer_type,
642
+ $.generic_type,
643
+ $.scoped_type_identifier,
644
+ $.tuple_type,
645
+ $.unit_type,
646
+ $.array_type,
647
+ $.function_type,
648
+ $._type_identifier,
649
+ $.macro_invocation,
650
+ $.never_type,
651
+ $.dynamic_type,
652
+ $.bounded_type,
653
+ $.removed_trait_bound,
654
+ alias(choice(...primitiveTypes), $.primitive_type)
655
+ ),
656
+
657
+ bracketed_type: ($) => seq('<', choice($._type, $.qualified_type), '>'),
658
+
659
+ qualified_type: ($) => seq(field('type', $._type), 'as', field('alias', $._type)),
660
+
661
+ lifetime: ($) => prec(1, seq("'", $.identifier)),
662
+
663
+ array_type: ($) => seq('[', field('element', $._type), optional(seq(';', field('length', $._expression))), ']'),
664
+
665
+ for_lifetimes: ($) => seq('for', '<', sepBy1(',', $.lifetime), optional(','), '>'),
666
+
667
+ function_type: ($) =>
668
+ seq(
669
+ optional($.for_lifetimes),
670
+ prec(
671
+ PREC.call,
672
+ seq(
673
+ choice(
674
+ field('trait', choice($._type_identifier, $.scoped_type_identifier)),
675
+ seq(optional($.function_modifiers), 'fn')
676
+ ),
677
+ field('parameters', $.parameters)
678
+ )
679
+ ),
680
+ optional(seq('->', field('return_type', $._type)))
681
+ ),
682
+
683
+ tuple_type: ($) => seq('(', sepBy1(',', $._type), optional(','), ')'),
684
+
685
+ unit_type: () => seq('(', ')'),
686
+
687
+ generic_function: ($) =>
688
+ prec(
689
+ 1,
690
+ seq(
691
+ field('function', choice($.identifier, $.scoped_identifier, $.field_expression)),
692
+ '::',
693
+ field('type_arguments', $.type_arguments)
694
+ )
695
+ ),
696
+
697
+ generic_type: ($) =>
698
+ prec(
699
+ 1,
700
+ seq(
701
+ field('type', choice($._type_identifier, $._reserved_identifier, $.scoped_type_identifier)),
702
+ field('type_arguments', $.type_arguments)
703
+ )
704
+ ),
705
+
706
+ generic_type_with_turbofish: ($) =>
707
+ seq(
708
+ field('type', choice($._type_identifier, $.scoped_identifier)),
709
+ '::',
710
+ field('type_arguments', $.type_arguments)
711
+ ),
712
+
713
+ bounded_type: ($) =>
714
+ prec.left(-1, seq(choice($.lifetime, $._type, $.use_bounds), '+', choice($.lifetime, $._type, $.use_bounds))),
715
+
716
+ use_bounds: ($) =>
717
+ seq('use', token(prec(1, '<')), sepBy(',', choice($.lifetime, $._type_identifier)), optional(','), '>'),
718
+
719
+ type_arguments: ($) =>
720
+ seq(
721
+ token(prec(1, '<')),
722
+ sepBy1(',', seq(choice($._type, $.type_binding, $.lifetime, $._literal, $.block), optional($.trait_bounds))),
723
+ optional(','),
724
+ '>'
725
+ ),
726
+
727
+ type_binding: ($) =>
728
+ seq(
729
+ field('name', $._type_identifier),
730
+ field('type_arguments', optional($.type_arguments)),
731
+ '=',
732
+ field('type', $._type)
733
+ ),
734
+
735
+ reference_type: ($) => seq('&', optional($.lifetime), optional($.mutable_specifier), field('type', $._type)),
736
+
737
+ pointer_type: ($) => seq('*', choice('const', $.mutable_specifier), field('type', $._type)),
738
+
739
+ never_type: () => '!',
740
+
741
+ abstract_type: ($) =>
742
+ seq(
743
+ 'impl',
744
+ optional(seq('for', $.type_parameters)),
745
+ field(
746
+ 'trait',
747
+ prec(
748
+ 1,
749
+ choice(
750
+ $._type_identifier,
751
+ $.scoped_type_identifier,
752
+ $.removed_trait_bound,
753
+ $.generic_type,
754
+ $.function_type,
755
+ $.tuple_type,
756
+ $.bounded_type
757
+ )
758
+ )
759
+ )
760
+ ),
761
+
762
+ dynamic_type: ($) =>
763
+ seq(
764
+ 'dyn',
765
+ field(
766
+ 'trait',
767
+ choice(
768
+ $.higher_ranked_trait_bound,
769
+ $._type_identifier,
770
+ $.scoped_type_identifier,
771
+ $.generic_type,
772
+ $.function_type,
773
+ $.tuple_type
774
+ )
775
+ )
776
+ ),
777
+
778
+ mutable_specifier: () => 'mut',
779
+
780
+ // Section - Expressions
781
+
782
+ _expression_except_range: ($) =>
783
+ choice(
784
+ $.unary_expression,
785
+ $.reference_expression,
786
+ $.try_expression,
787
+ $.binary_expression,
788
+ $.assignment_expression,
789
+ $.compound_assignment_expr,
790
+ $.type_cast_expression,
791
+ $.call_expression,
792
+ $.return_expression,
793
+ $.yield_expression,
794
+ $._literal,
795
+ prec.left($.identifier),
796
+ alias(choice(...primitiveTypes), $.identifier),
797
+ prec.left($._reserved_identifier),
798
+ $.self,
799
+ $.scoped_identifier,
800
+ $.generic_function,
801
+ $.await_expression,
802
+ $.field_expression,
803
+ $.array_expression,
804
+ $.tuple_expression,
805
+ prec(1, $.macro_invocation),
806
+ $.unit_expression,
807
+ $.break_expression,
808
+ $.continue_expression,
809
+ $.index_expression,
810
+ $.metavariable,
811
+ $.closure_expression,
812
+ $.parenthesized_expression,
813
+ $.struct_expression,
814
+ $._expression_ending_with_block
815
+ ),
816
+
817
+ _expression: ($) => choice($._expression_except_range, $.range_expression),
818
+
819
+ _expression_ending_with_block: ($) =>
820
+ choice(
821
+ $.unsafe_block,
822
+ $.async_block,
823
+ $.gen_block,
824
+ $.try_block,
825
+ $.block,
826
+ $.if_expression,
827
+ $.match_expression,
828
+ $.while_expression,
829
+ $.loop_expression,
830
+ $.for_expression,
831
+ $.const_block
832
+ ),
833
+
834
+ macro_invocation: ($) =>
835
+ seq(
836
+ field('macro', choice($.scoped_identifier, $.identifier, $._reserved_identifier)),
837
+ '!',
838
+ alias($.delim_token_tree, $.token_tree)
839
+ ),
840
+
841
+ delim_token_tree: ($) =>
842
+ choice(
843
+ seq('(', repeat($._delim_tokens), ')'),
844
+ seq('[', repeat($._delim_tokens), ']'),
845
+ seq('{', repeat($._delim_tokens), '}')
846
+ ),
847
+
848
+ _delim_tokens: ($) => choice($._non_delim_token, alias($.delim_token_tree, $.token_tree)),
849
+
850
+ // Should match any token other than a delimiter.
851
+ _non_delim_token: ($) => choice($._non_special_token, '$'),
852
+
853
+ scoped_identifier: ($) =>
854
+ seq(
855
+ field(
856
+ 'path',
857
+ optional(choice($._path, $.bracketed_type, alias($.generic_type_with_turbofish, $.generic_type)))
858
+ ),
859
+ '::',
860
+ field('name', choice($.identifier, $.super))
861
+ ),
862
+
863
+ scoped_type_identifier_in_expression_position: ($) =>
864
+ prec(
865
+ -2,
866
+ seq(
867
+ field('path', optional(choice($._path, alias($.generic_type_with_turbofish, $.generic_type)))),
868
+ '::',
869
+ field('name', $._type_identifier)
870
+ )
871
+ ),
872
+
873
+ scoped_type_identifier: ($) =>
874
+ seq(
875
+ field(
876
+ 'path',
877
+ optional(
878
+ choice($._path, alias($.generic_type_with_turbofish, $.generic_type), $.bracketed_type, $.generic_type)
879
+ )
880
+ ),
881
+ '::',
882
+ field('name', $._type_identifier)
883
+ ),
884
+
885
+ range_expression: ($) =>
886
+ prec.left(
887
+ PREC.range,
888
+ choice(
889
+ seq($._expression, choice('..', '...', '..='), $._expression),
890
+ seq($._expression, '..'),
891
+ seq('..', $._expression),
892
+ '..'
893
+ )
894
+ ),
895
+
896
+ unary_expression: ($) => prec(PREC.unary, seq(choice('-', '*', '!'), $._expression)),
897
+
898
+ try_expression: ($) => prec(PREC.try, seq($._expression, '?')),
899
+
900
+ reference_expression: ($) =>
901
+ prec(
902
+ PREC.unary,
903
+ seq(
904
+ '&',
905
+ choice(seq('raw', choice('const', $.mutable_specifier)), optional($.mutable_specifier)),
906
+ field('value', $._expression)
907
+ )
908
+ ),
909
+
910
+ binary_expression: ($) => {
911
+ /** @type {[number, RuleOrLiteral][]} */
912
+ const table = [
913
+ [PREC.and, '&&'],
914
+ [PREC.or, '||'],
915
+ [PREC.bitand, '&'],
916
+ [PREC.bitor, '|'],
917
+ [PREC.bitxor, '^'],
918
+ [PREC.comparative, choice('==', '!=', '<', '<=', '>', '>=')],
919
+ [PREC.shift, choice('<<', '>>')],
920
+ [PREC.additive, choice('+', '-')],
921
+ [PREC.multiplicative, choice('*', '/', '%')],
922
+ ];
923
+
924
+ return choice(
925
+ ...table.map(([precedence, operator]) =>
926
+ prec.left(
927
+ precedence,
928
+ seq(field('left', $._expression), field('operator', operator), field('right', $._expression))
929
+ )
930
+ )
931
+ );
932
+ },
933
+
934
+ assignment_expression: ($) =>
935
+ prec.left(PREC.assign, seq(field('left', $._expression), '=', field('right', $._expression))),
936
+
937
+ compound_assignment_expr: ($) =>
938
+ prec.left(
939
+ PREC.assign,
940
+ seq(
941
+ field('left', $._expression),
942
+ field('operator', choice('+=', '-=', '*=', '/=', '%=', '&=', '|=', '^=', '<<=', '>>=')),
943
+ field('right', $._expression)
944
+ )
945
+ ),
946
+
947
+ type_cast_expression: ($) => prec.left(PREC.cast, seq(field('value', $._expression), 'as', field('type', $._type))),
948
+
949
+ return_expression: ($) => choice(prec.left(seq('return', $._expression)), prec(-1, 'return')),
950
+
951
+ yield_expression: ($) => choice(prec.left(seq('yield', $._expression)), prec(-1, 'yield')),
952
+
953
+ call_expression: ($) =>
954
+ prec(PREC.call, seq(field('function', $._expression_except_range), field('arguments', $.arguments))),
955
+
956
+ arguments: ($) => seq('(', sepBy(',', seq(repeat($.attribute_item), $._expression)), optional(','), ')'),
957
+
958
+ array_expression: ($) =>
959
+ seq(
960
+ '[',
961
+ repeat($.attribute_item),
962
+ choice(
963
+ seq($._expression, ';', field('length', $._expression)),
964
+ seq(sepBy(',', seq(repeat($.attribute_item), $._expression)), optional(','))
965
+ ),
966
+ ']'
967
+ ),
968
+
969
+ parenthesized_expression: ($) => seq('(', $._expression, ')'),
970
+
971
+ tuple_expression: ($) =>
972
+ seq(
973
+ '(',
974
+ repeat($.attribute_item),
975
+ seq($._expression, ','),
976
+ repeat(seq($._expression, ',')),
977
+ optional($._expression),
978
+ ')'
979
+ ),
980
+
981
+ unit_expression: () => seq('(', ')'),
982
+
983
+ struct_expression: ($) =>
984
+ seq(
985
+ field(
986
+ 'name',
987
+ choice(
988
+ $._type_identifier,
989
+ alias($.scoped_type_identifier_in_expression_position, $.scoped_type_identifier),
990
+ $.generic_type_with_turbofish
991
+ )
992
+ ),
993
+ field('body', $.field_initializer_list)
994
+ ),
995
+
996
+ field_initializer_list: ($) =>
997
+ seq(
998
+ '{',
999
+ sepBy(',', choice($.shorthand_field_initializer, $.field_initializer, $.base_field_initializer)),
1000
+ optional(','),
1001
+ '}'
1002
+ ),
1003
+
1004
+ shorthand_field_initializer: ($) => seq(repeat($.attribute_item), $.identifier),
1005
+
1006
+ field_initializer: ($) =>
1007
+ seq(
1008
+ repeat($.attribute_item),
1009
+ field('field', choice($._field_identifier, $.integer_literal)),
1010
+ ':',
1011
+ field('value', $._expression)
1012
+ ),
1013
+
1014
+ base_field_initializer: ($) => seq('..', $._expression),
1015
+
1016
+ if_expression: ($) =>
1017
+ prec.right(
1018
+ seq(
1019
+ 'if',
1020
+ field('condition', $._condition),
1021
+ field('consequence', $.block),
1022
+ optional(field('alternative', $.else_clause))
1023
+ )
1024
+ ),
1025
+
1026
+ let_condition: ($) =>
1027
+ seq('let', field('pattern', $._pattern), '=', field('value', prec.left(PREC.and, $._expression))),
1028
+
1029
+ _let_chain: ($) =>
1030
+ prec.left(
1031
+ PREC.and,
1032
+ choice(
1033
+ seq($._let_chain, '&&', $.let_condition),
1034
+ seq($._let_chain, '&&', $._expression),
1035
+ seq($.let_condition, '&&', $._expression),
1036
+ seq($.let_condition, '&&', $.let_condition),
1037
+ seq($._expression, '&&', $.let_condition)
1038
+ )
1039
+ ),
1040
+
1041
+ _condition: ($) => choice($._expression, $.let_condition, alias($._let_chain, $.let_chain)),
1042
+
1043
+ else_clause: ($) => seq('else', choice($.block, $.if_expression)),
1044
+
1045
+ match_expression: ($) => seq('match', field('value', $._expression), field('body', $.match_block)),
1046
+
1047
+ match_block: ($) => seq('{', optional(seq(repeat($.match_arm), alias($.last_match_arm, $.match_arm))), '}'),
1048
+
1049
+ match_arm: ($) =>
1050
+ prec.right(
1051
+ seq(
1052
+ repeat(choice($.attribute_item, $.inner_attribute_item)),
1053
+ field('pattern', $.match_pattern),
1054
+ '=>',
1055
+ choice(seq(field('value', $._expression), ','), field('value', prec(1, $._expression_ending_with_block)))
1056
+ )
1057
+ ),
1058
+
1059
+ last_match_arm: ($) =>
1060
+ seq(
1061
+ repeat(choice($.attribute_item, $.inner_attribute_item)),
1062
+ field('pattern', $.match_pattern),
1063
+ '=>',
1064
+ field('value', $._expression),
1065
+ optional(',')
1066
+ ),
1067
+
1068
+ match_pattern: ($) => seq($._pattern, optional(seq('if', field('condition', $._condition)))),
1069
+
1070
+ while_expression: ($) =>
1071
+ seq(optional(seq($.label, ':')), 'while', field('condition', $._condition), field('body', $.block)),
1072
+
1073
+ loop_expression: ($) => seq(optional(seq($.label, ':')), 'loop', field('body', $.block)),
1074
+
1075
+ for_expression: ($) =>
1076
+ seq(
1077
+ optional(seq($.label, ':')),
1078
+ 'for',
1079
+ field('pattern', $._pattern),
1080
+ 'in',
1081
+ field('value', $._expression),
1082
+ field('body', $.block)
1083
+ ),
1084
+
1085
+ const_block: ($) => seq('const', field('body', $.block)),
1086
+
1087
+ closure_expression: ($) =>
1088
+ prec(
1089
+ PREC.closure,
1090
+ seq(
1091
+ optional('static'),
1092
+ optional('async'),
1093
+ optional('move'),
1094
+ field('parameters', $.closure_parameters),
1095
+ choice(
1096
+ seq(optional(seq('->', field('return_type', $._type))), field('body', $.block)),
1097
+ field('body', choice($._expression, '_'))
1098
+ )
1099
+ )
1100
+ ),
1101
+
1102
+ closure_parameters: ($) => seq('|', sepBy(',', choice($._pattern, $.parameter)), '|'),
1103
+
1104
+ label: ($) => seq("'", $.identifier),
1105
+
1106
+ break_expression: ($) => prec.left(seq('break', optional($.label), optional($._expression))),
1107
+
1108
+ continue_expression: ($) => prec.left(seq('continue', optional($.label))),
1109
+
1110
+ index_expression: ($) => prec(PREC.call, seq($._expression, '[', $._expression, ']')),
1111
+
1112
+ await_expression: ($) => prec(PREC.field, seq($._expression, '.', 'await')),
1113
+
1114
+ field_expression: ($) =>
1115
+ prec(
1116
+ PREC.field,
1117
+ seq(field('value', $._expression), '.', field('field', choice($._field_identifier, $.integer_literal)))
1118
+ ),
1119
+
1120
+ unsafe_block: ($) => seq('unsafe', $.block),
1121
+
1122
+ async_block: ($) => seq('async', optional('move'), $.block),
1123
+
1124
+ gen_block: ($) => seq('gen', optional('move'), $.block),
1125
+
1126
+ try_block: ($) => seq('try', $.block),
1127
+
1128
+ block: ($) => seq(optional(seq($.label, ':')), '{', repeat($._statement), optional($._expression), '}'),
1129
+
1130
+ // Section - Patterns
1131
+
1132
+ _pattern: ($) =>
1133
+ choice(
1134
+ $._literal_pattern,
1135
+ alias(choice(...primitiveTypes), $.identifier),
1136
+ $.identifier,
1137
+ $.scoped_identifier,
1138
+ $.generic_pattern,
1139
+ $.tuple_pattern,
1140
+ $.tuple_struct_pattern,
1141
+ $.struct_pattern,
1142
+ $._reserved_identifier,
1143
+ $.ref_pattern,
1144
+ $.slice_pattern,
1145
+ $.captured_pattern,
1146
+ $.reference_pattern,
1147
+ $.remaining_field_pattern,
1148
+ $.mut_pattern,
1149
+ $.range_pattern,
1150
+ $.or_pattern,
1151
+ $.const_block,
1152
+ $.macro_invocation,
1153
+ '_'
1154
+ ),
1155
+
1156
+ generic_pattern: ($) =>
1157
+ seq(choice($.identifier, $.scoped_identifier), '::', field('type_arguments', $.type_arguments)),
1158
+
1159
+ tuple_pattern: ($) => seq('(', sepBy(',', choice($._pattern, $.closure_expression)), optional(','), ')'),
1160
+
1161
+ slice_pattern: ($) => seq('[', sepBy(',', $._pattern), optional(','), ']'),
1162
+
1163
+ tuple_struct_pattern: ($) =>
1164
+ seq(
1165
+ field('type', choice($.identifier, $.scoped_identifier, alias($.generic_type_with_turbofish, $.generic_type))),
1166
+ '(',
1167
+ sepBy(',', $._pattern),
1168
+ optional(','),
1169
+ ')'
1170
+ ),
1171
+
1172
+ struct_pattern: ($) =>
1173
+ seq(
1174
+ field('type', choice($._type_identifier, $.scoped_type_identifier)),
1175
+ '{',
1176
+ sepBy(',', choice($.field_pattern, $.remaining_field_pattern)),
1177
+ optional(','),
1178
+ '}'
1179
+ ),
1180
+
1181
+ field_pattern: ($) =>
1182
+ seq(
1183
+ optional('ref'),
1184
+ optional($.mutable_specifier),
1185
+ choice(
1186
+ field('name', alias($.identifier, $.shorthand_field_identifier)),
1187
+ seq(field('name', $._field_identifier), ':', field('pattern', $._pattern))
1188
+ )
1189
+ ),
1190
+
1191
+ remaining_field_pattern: () => '..',
1192
+
1193
+ mut_pattern: ($) => prec(-1, seq($.mutable_specifier, $._pattern)),
1194
+
1195
+ range_pattern: ($) =>
1196
+ choice(
1197
+ seq(
1198
+ field('left', choice($._literal_pattern, $._path)),
1199
+ choice(seq(choice('...', '..=', '..'), field('right', choice($._literal_pattern, $._path))), '..')
1200
+ ),
1201
+ seq(choice('..=', '..'), field('right', choice($._literal_pattern, $._path)))
1202
+ ),
1203
+
1204
+ ref_pattern: ($) => seq('ref', $._pattern),
1205
+
1206
+ captured_pattern: ($) => seq($.identifier, '@', $._pattern),
1207
+
1208
+ reference_pattern: ($) => seq('&', optional($.mutable_specifier), $._pattern),
1209
+
1210
+ or_pattern: ($) => prec.left(-2, choice(seq($._pattern, '|', $._pattern), seq('|', $._pattern))),
1211
+
1212
+ // Section - Literals
1213
+
1214
+ _literal: ($) =>
1215
+ choice(
1216
+ $.string_literal,
1217
+ $.raw_string_literal,
1218
+ $.char_literal,
1219
+ $.boolean_literal,
1220
+ $.integer_literal,
1221
+ $.float_literal
1222
+ ),
1223
+
1224
+ _literal_pattern: ($) =>
1225
+ choice(
1226
+ $.string_literal,
1227
+ $.raw_string_literal,
1228
+ $.char_literal,
1229
+ $.boolean_literal,
1230
+ $.integer_literal,
1231
+ $.float_literal,
1232
+ $.negative_literal
1233
+ ),
1234
+
1235
+ negative_literal: ($) => seq('-', choice($.integer_literal, $.float_literal)),
1236
+
1237
+ integer_literal: () =>
1238
+ token(seq(choice(/[0-9][0-9_]*/, /0x[0-9a-fA-F_]+/, /0b[01_]+/, /0o[0-7_]+/), optional(choice(...numericTypes)))),
1239
+
1240
+ string_literal: ($) =>
1241
+ seq(alias(/[bc]?"/, '"'), repeat(choice($.escape_sequence, $.string_content)), alias($.string_close, '"')),
1242
+
1243
+ raw_string_literal: ($) =>
1244
+ seq(
1245
+ $._raw_string_literal_start,
1246
+ alias($.raw_string_literal_content, $.string_content),
1247
+ $._raw_string_literal_end
1248
+ ),
1249
+
1250
+ char_literal: () =>
1251
+ token(
1252
+ seq(
1253
+ optional('b'),
1254
+ "'",
1255
+ optional(
1256
+ choice(seq('\\', choice(/[^xu]/, /u[0-9a-fA-F]{4}/, /u\{[0-9a-fA-F]+\}/, /x[0-9a-fA-F]{2}/)), /[^\\']/)
1257
+ ),
1258
+ "'"
1259
+ )
1260
+ ),
1261
+
1262
+ escape_sequence: () =>
1263
+ token.immediate(seq('\\', choice(/[^xu]/, /u[0-9a-fA-F]{4}/, /u\{[0-9a-fA-F]+\}/, /x[0-9a-fA-F]{2}/))),
1264
+
1265
+ boolean_literal: () => choice('true', 'false'),
1266
+
1267
+ comment: ($) => choice($.line_comment, $.block_comment),
1268
+
1269
+ line_comment: ($) =>
1270
+ seq(
1271
+ // All line comments start with two //
1272
+ '//',
1273
+ // Then are followed by:
1274
+ // - 2 or more slashes making it a regular comment
1275
+ // - 1 slash or 1 or more bang operators making it a doc comment
1276
+ // - or just content for the comment
1277
+ choice(
1278
+ // A tricky edge case where what looks like a doc comment is not
1279
+ seq(token.immediate(prec(2, /\/\//)), /.*/),
1280
+ // A regular doc comment
1281
+ seq($._line_doc_comment_marker, field('doc', alias($._line_doc_content, $.doc_comment))),
1282
+ token.immediate(prec(1, /.*/))
1283
+ )
1284
+ ),
1285
+
1286
+ _line_doc_comment_marker: ($) =>
1287
+ choice(
1288
+ // An outer line doc comment applies to the element that it is outside of
1289
+ field('outer', alias($._outer_line_doc_comment_marker, $.outer_doc_comment_marker)),
1290
+ // An inner line doc comment applies to the element it is inside of
1291
+ field('inner', alias($._inner_line_doc_comment_marker, $.inner_doc_comment_marker))
1292
+ ),
1293
+
1294
+ _inner_line_doc_comment_marker: () => token.immediate(prec(2, '!')),
1295
+ _outer_line_doc_comment_marker: () => token.immediate(prec(2, '/')),
1296
+
1297
+ block_comment: ($) =>
1298
+ seq(
1299
+ '/*',
1300
+ optional(
1301
+ choice(
1302
+ // Documentation block comments: /** docs */ or /*! docs */
1303
+ seq($._block_doc_comment_marker, optional(field('doc', alias($._block_comment_content, $.doc_comment)))),
1304
+ // Non-doc block comments
1305
+ $._block_comment_content
1306
+ )
1307
+ ),
1308
+ '*/'
1309
+ ),
1310
+
1311
+ _block_doc_comment_marker: ($) =>
1312
+ choice(
1313
+ field('outer', alias($._outer_block_doc_comment_marker, $.outer_doc_comment_marker)),
1314
+ field('inner', alias($._inner_block_doc_comment_marker, $.inner_doc_comment_marker))
1315
+ ),
1316
+
1317
+ _path: ($) =>
1318
+ choice(
1319
+ $.self,
1320
+ alias(choice(...primitiveTypes), $.identifier),
1321
+ $.metavariable,
1322
+ $.super,
1323
+ $.crate,
1324
+ $.identifier,
1325
+ $.scoped_identifier,
1326
+ $._reserved_identifier
1327
+ ),
1328
+
1329
+ identifier: () => /(r#)?[_\p{XID_Start}][_\p{XID_Continue}]*/u,
1330
+
1331
+ // oxlint-disable-next-line no-useless-escape -- tree-sitter's regex parser rejects an unescaped `[` in a character class.
1332
+ shebang: () => /#![\r\f\t\v ]*([^\[\n].*)?\n/,
1333
+
1334
+ _reserved_identifier: ($) => alias(choice('default', 'union', 'gen', 'raw'), $.identifier),
1335
+
1336
+ _type_identifier: ($) => alias($.identifier, $.type_identifier),
1337
+ _field_identifier: ($) => alias($.identifier, $.field_identifier),
1338
+
1339
+ self: () => 'self',
1340
+ super: () => 'super',
1341
+ crate: () => 'crate',
1342
+
1343
+ metavariable: () => /\$[a-zA-Z_]\w*/,
1344
+ },
1345
+ });
1346
+
1347
+ /**
1348
+ * Creates a rule to match one or more of the rules separated by the separator.
1349
+ *
1350
+ * @param {RuleOrLiteral} sep - The separator to use.
1351
+ * @param {RuleOrLiteral} rule
1352
+ *
1353
+ * @returns {SeqRule}
1354
+ */
1355
+ function sepBy1(sep, rule) {
1356
+ return seq(rule, repeat(seq(sep, rule)));
1357
+ }
1358
+
1359
+ /**
1360
+ * Creates a rule to optionally match one or more of the rules separated by the separator.
1361
+ *
1362
+ * @param {RuleOrLiteral} sep - The separator to use.
1363
+ * @param {RuleOrLiteral} rule
1364
+ *
1365
+ * @returns {ChoiceRule}
1366
+ */
1367
+ function sepBy(sep, rule) {
1368
+ return optional(sepBy1(sep, rule));
1369
+ }