-
Notifications
You must be signed in to change notification settings - Fork 2
/
tree-sitter-rust.ebnf
721 lines (547 loc) · 17.2 KB
/
tree-sitter-rust.ebnf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
//
// From tree-sitter-rust/src/grammar.json
//
//
// EBNF to generate railroad diagram at
// (IPV6) https://www.bottlecaps.de/rr/ui
// (IPV4) https://rr.red-dove.com/ui
//
source_file ::=
shebang? _statement*
_statement ::=
expression_statement
| _declaration_statement
empty_statement ::=
';'
expression_statement ::=
_expression ';'
| _expression_ending_with_block
_declaration_statement ::=
const_item
| macro_invocation
| macro_definition
| empty_statement
| attribute_item
| inner_attribute_item
| mod_item
| foreign_mod_item
| struct_item
| union_item
| enum_item
| type_item
| function_item
| function_signature_item
| impl_item
| trait_item
| associated_type
| let_declaration
| use_declaration
| extern_crate_declaration
| static_item
macro_definition ::=
'macro_rules!' ( identifier | _reserved_identifier ) ( '(' ( macro_rule ';' )* macro_rule? ')' ';' | '{' ( macro_rule ';' )* macro_rule? '}' )
macro_rule ::=
token_tree_pattern '=>' token_tree
_token_pattern ::=
token_tree_pattern
| token_repetition_pattern
| token_binding_pattern
| metavariable
| _non_special_token
token_tree_pattern ::=
'(' _token_pattern* ')'
| '[' _token_pattern* ']'
| '{' _token_pattern* '}'
token_binding_pattern ::=
metavariable ':' fragment_specifier
token_repetition_pattern ::=
'$' '(' _token_pattern* ')' [^+*?]+? ( '+' | '*' | '?' )
fragment_specifier ::=
'block'
| 'expr'
| 'ident'
| 'item'
| 'lifetime'
| 'literal'
| 'meta'
| 'pat'
| 'path'
| 'stmt'
| 'tt'
| 'ty'
| 'vis'
_tokens ::=
token_tree
| token_repetition
| metavariable
| _non_special_token
token_tree ::=
'(' _tokens* ')'
| '[' _tokens* ']'
| '{' _tokens* '}'
token_repetition ::=
'$' '(' _tokens* ')' [^+*?]+? ( '+' | '*' | '?' )
_non_special_token ::=
_literal
| identifier
| mutable_specifier
| self
| super
| crate
| ( 'u8' | 'i8' | 'u16' | 'i16' | 'u32' | 'i32' | 'u64' | 'i64' | 'u128' | 'i128' | 'isize' | 'usize' | 'f32' | 'f64' | 'bool' | 'str' | 'char' )
| ( '/' | '_' | '\\' | '-' | '=' | '->' | ',' | ';' | ':' | '::' | '!' | '?' | '.' | '@' | '*' | '&' | '#' | '%' | '^' | '+' | '<' | '>' | '|' | '~' )+
| "'"
| 'as'
| 'async'
| 'await'
| 'break'
| 'const'
| 'continue'
| 'default'
| 'enum'
| 'fn'
| 'for'
| 'if'
| 'impl'
| 'let'
| 'loop'
| 'match'
| 'mod'
| 'pub'
| 'return'
| 'static'
| 'struct'
| 'trait'
| 'type'
| 'union'
| 'unsafe'
| 'use'
| 'where'
| 'while'
attribute_item ::=
'#' '[' attribute ']'
inner_attribute_item ::=
'#' '!' '[' attribute ']'
attribute ::=
_path ( '=' _expression | delim_token_tree )?
mod_item ::=
visibility_modifier? 'mod' identifier ( ';' | declaration_list )
foreign_mod_item ::=
visibility_modifier? extern_modifier ( ';' | declaration_list )
declaration_list ::=
'{' _declaration_statement* '}'
struct_item ::=
visibility_modifier? 'struct' _type_identifier type_parameters? ( where_clause? field_declaration_list | ordered_field_declaration_list where_clause? ';' | ';' )
union_item ::=
visibility_modifier? 'union' _type_identifier type_parameters? where_clause? field_declaration_list
enum_item ::=
visibility_modifier? 'enum' _type_identifier type_parameters? where_clause? enum_variant_list
enum_variant_list ::=
'{' ( attribute_item* enum_variant ( ',' attribute_item* enum_variant )* )? ','? '}'
enum_variant ::=
visibility_modifier? identifier ( field_declaration_list | ordered_field_declaration_list )? ( '=' _expression )?
field_declaration_list ::=
'{' ( attribute_item* field_declaration ( ',' attribute_item* field_declaration )* )? ','? '}'
field_declaration ::=
visibility_modifier? _field_identifier ':' _type
ordered_field_declaration_list ::=
'(' ( attribute_item* visibility_modifier? _type ( ',' attribute_item* visibility_modifier? _type )* )? ','? ')'
extern_crate_declaration ::=
visibility_modifier? 'extern' crate identifier ( 'as' identifier )? ';'
const_item ::=
visibility_modifier? 'const' identifier ':' _type ( '=' _expression )? ';'
static_item ::=
visibility_modifier? 'static' 'ref'? mutable_specifier? identifier ':' _type ( '=' _expression )? ';'
type_item ::=
visibility_modifier? 'type' _type_identifier type_parameters? '=' _type where_clause? ';'
function_item ::=
visibility_modifier? function_modifiers? 'fn' ( identifier | metavariable ) type_parameters? parameters ( '->' _type )? where_clause? block
function_signature_item ::=
visibility_modifier? function_modifiers? 'fn' ( identifier | metavariable ) type_parameters? parameters ( '->' _type )? where_clause? ';'
function_modifiers ::=
( 'async' | 'default' | 'const' | 'unsafe' | extern_modifier )+
where_clause ::=
'where' where_predicate ( ',' where_predicate )* ','?
where_predicate ::=
( lifetime | _type_identifier | scoped_type_identifier | generic_type | reference_type | pointer_type | tuple_type | array_type | higher_ranked_trait_bound | ( 'u8' | 'i8' | 'u16' | 'i16' | 'u32' | 'i32' | 'u64' | 'i64' | 'u128' | 'i128' | 'isize' | 'usize' | 'f32' | 'f64' | 'bool' | 'str' | 'char' ) ) trait_bounds
impl_item ::=
'unsafe'? 'impl' type_parameters? ( '!'? ( _type_identifier | scoped_type_identifier | generic_type ) 'for' )? _type where_clause? ( declaration_list | ';' )
trait_item ::=
visibility_modifier? 'unsafe'? 'trait' _type_identifier type_parameters? trait_bounds? where_clause? declaration_list
associated_type ::=
'type' _type_identifier type_parameters? trait_bounds? ';'
trait_bounds ::=
':' ( _type | lifetime | higher_ranked_trait_bound | removed_trait_bound ) ( '+' ( _type | lifetime | higher_ranked_trait_bound | removed_trait_bound ) )*
higher_ranked_trait_bound ::=
'for' type_parameters _type
removed_trait_bound ::=
'?' _type
type_parameters ::=
'<' ( lifetime | metavariable | _type_identifier | constrained_type_parameter | optional_type_parameter | const_parameter ) ( ',' ( lifetime | metavariable | _type_identifier | constrained_type_parameter | optional_type_parameter | const_parameter ) )* ','? '>'
const_parameter ::=
'const' identifier ':' _type
constrained_type_parameter ::=
( lifetime | _type_identifier ) trait_bounds
optional_type_parameter ::=
( _type_identifier | constrained_type_parameter ) '=' _type
let_declaration ::=
'let' mutable_specifier? _pattern ( ':' _type )? ( '=' _expression )? ( 'else' block )? ';'
use_declaration ::=
visibility_modifier? 'use' _use_clause ';'
_use_clause ::=
_path
| use_as_clause
| use_list
| scoped_use_list
| use_wildcard
scoped_use_list ::=
_path? '::' use_list
use_list ::=
'{' ( ( _use_clause ) ( ',' ( _use_clause ) )* )? ','? '}'
use_as_clause ::=
_path 'as' identifier
use_wildcard ::=
( _path '::' )? '*'
parameters ::=
'(' ( attribute_item? ( parameter | self_parameter | variadic_parameter | '_' | _type ) ( ',' attribute_item? ( parameter | self_parameter | variadic_parameter | '_' | _type ) )* )? ','? ')'
self_parameter ::=
'&'? lifetime? mutable_specifier? self
variadic_parameter ::=
'...'
parameter ::=
mutable_specifier? ( _pattern | self ) ':' _type
extern_modifier ::=
'extern' string_literal?
visibility_modifier ::=
crate | 'pub' ( '(' ( self | super | crate | 'in' _path ) ')' )?
_type ::=
abstract_type
| reference_type
| metavariable
| pointer_type
| generic_type
| scoped_type_identifier
| tuple_type
| unit_type
| array_type
| function_type
| _type_identifier
| macro_invocation
| empty_type
| dynamic_type
| bounded_type
| ( 'u8' | 'i8' | 'u16' | 'i16' | 'u32' | 'i32' | 'u64' | 'i64' | 'u128' | 'i128' | 'isize' | 'usize' | 'f32' | 'f64' | 'bool' | 'str' | 'char' )
bracketed_type ::=
'<' ( _type | qualified_type ) '>'
qualified_type ::=
_type 'as' _type
lifetime ::=
"'" identifier
array_type ::=
'[' _type ( ';' _expression )? ']'
for_lifetimes ::=
'for' '<' lifetime ( ',' lifetime )* ','? '>'
function_type ::=
for_lifetimes? ( ( _type_identifier | scoped_type_identifier ) | function_modifiers? 'fn' ) parameters ( '->' _type )?
tuple_type ::=
'(' _type ( ',' _type )* ','? ')'
unit_type ::=
'(' ')'
generic_function ::=
( identifier | scoped_identifier | field_expression ) '::' type_arguments
generic_type ::=
( _type_identifier | _reserved_identifier | scoped_type_identifier ) type_arguments
generic_type_with_turbofish ::=
( _type_identifier | scoped_identifier ) '::' type_arguments
bounded_type ::=
lifetime '+' _type | _type '+' _type | _type '+' lifetime
type_arguments ::=
'<' ( _type | type_binding | lifetime | _literal | block ) ( ',' ( _type | type_binding | lifetime | _literal | block ) )* ','? '>'
type_binding ::=
_type_identifier type_arguments? '=' _type
reference_type ::=
'&' lifetime? mutable_specifier? _type
pointer_type ::=
'*' ( 'const' | mutable_specifier ) _type
empty_type ::=
'!'
abstract_type ::=
'impl' ( 'for' type_parameters )? ( _type_identifier | scoped_type_identifier | generic_type | function_type )
dynamic_type ::=
'dyn' ( _type_identifier | scoped_type_identifier | generic_type | function_type )
mutable_specifier ::=
'mut'
_expression_except_range ::=
unary_expression
| reference_expression
| try_expression
| binary_expression
| assignment_expression
| compound_assignment_expr
| type_cast_expression
| call_expression
| return_expression
| yield_expression
| _literal
| identifier
| ( 'u8' | 'i8' | 'u16' | 'i16' | 'u32' | 'i32' | 'u64' | 'i64' | 'u128' | 'i128' | 'isize' | 'usize' | 'f32' | 'f64' | 'bool' | 'str' | 'char' )
| _reserved_identifier
| self
| scoped_identifier
| generic_function
| await_expression
| field_expression
| array_expression
| tuple_expression
| macro_invocation
| unit_expression
| break_expression
| continue_expression
| index_expression
| metavariable
| closure_expression
| parenthesized_expression
| struct_expression
| _expression_ending_with_block
_expression ::=
_expression_except_range
| range_expression
_expression_ending_with_block ::=
unsafe_block
| async_block
| try_block
| block
| if_expression
| match_expression
| while_expression
| loop_expression
| for_expression
| const_block
macro_invocation ::=
( scoped_identifier | identifier | _reserved_identifier ) '!' delim_token_tree
delim_token_tree ::=
'(' _delim_tokens* ')'
| '[' _delim_tokens* ']'
| '{' _delim_tokens* '}'
_delim_tokens ::=
_non_delim_token
| delim_token_tree
_non_delim_token ::=
_non_special_token
| '$'
scoped_identifier ::=
( _path | bracketed_type | generic_type_with_turbofish )? '::' ( identifier | super )
scoped_type_identifier_in_expression_position ::=
( _path | generic_type_with_turbofish )? '::' _type_identifier
scoped_type_identifier ::=
( _path | generic_type_with_turbofish | bracketed_type | generic_type )? '::' _type_identifier
range_expression ::=
_expression ( '..' | '...' | '..=' ) _expression | _expression '..' | '..' _expression | '..'
unary_expression ::=
( '-' | '*' | '!' ) _expression
try_expression ::=
_expression '?'
reference_expression ::=
'&' mutable_specifier? _expression
binary_expression ::=
_expression '&&' _expression
| _expression '||' _expression
| _expression '&' _expression
| _expression '|' _expression
| _expression '^' _expression
| _expression ( '==' | '!=' | '<' | '<=' | '>' | '>=' ) _expression
| _expression ( '<<' | '>>' ) _expression
| _expression ( '+' | '-' ) _expression
| _expression ( '*' | '/' | '%' ) _expression
assignment_expression ::=
_expression '=' _expression
compound_assignment_expr ::=
_expression ( '+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^=' | '<<=' | '>>=' ) _expression
type_cast_expression ::=
_expression 'as' _type
return_expression ::=
'return' _expression
| 'return'
yield_expression ::=
'yield' _expression
| 'yield'
call_expression ::=
_expression_except_range arguments
arguments ::=
'(' ( attribute_item* _expression ( ',' attribute_item* _expression )* )? ','? ')'
array_expression ::=
'[' attribute_item* ( _expression ';' _expression | ( attribute_item* _expression ( ',' attribute_item* _expression )* )? ','? ) ']'
parenthesized_expression ::=
'(' _expression ')'
tuple_expression ::=
'(' attribute_item* _expression ',' ( _expression ',' )* _expression? ')'
unit_expression ::=
'(' ')'
struct_expression ::=
( _type_identifier | scoped_type_identifier_in_expression_position | generic_type_with_turbofish ) field_initializer_list
field_initializer_list ::=
'{' ( ( shorthand_field_initializer | field_initializer | base_field_initializer ) ( ',' ( shorthand_field_initializer | field_initializer | base_field_initializer ) )* )? ','? '}'
shorthand_field_initializer ::=
attribute_item* identifier
field_initializer ::=
attribute_item* _field_identifier ':' _expression
base_field_initializer ::=
'..' _expression
if_expression ::=
'if' _condition block else_clause?
let_condition ::=
'let' _pattern '=' _expression
_let_chain ::=
_let_chain '&&' let_condition | _let_chain '&&' _expression | let_condition '&&' _expression | let_condition '&&' let_condition | _expression '&&' let_condition
_condition ::=
_expression
| let_condition
| _let_chain
else_clause ::=
'else' ( block | if_expression )
match_expression ::=
'match' _expression match_block
match_block ::=
'{' ( match_arm* last_match_arm )? '}'
match_arm ::=
attribute_item* match_pattern '=>' ( _expression ',' | _expression_ending_with_block )
last_match_arm ::=
attribute_item* match_pattern '=>' _expression ','?
match_pattern ::=
( _pattern | closure_expression ) ( 'if' _condition )?
while_expression ::=
( label ':' )? 'while' _condition block
loop_expression ::=
( label ':' )? 'loop' block
for_expression ::=
( label ':' )? 'for' _pattern 'in' _expression block
const_block ::=
'const' block
closure_expression ::=
'static'? 'move'? closure_parameters ( ( '->' _type )? block | ( _expression | '_' ) )
closure_parameters ::=
'|' ( ( _pattern | parameter ) ( ',' ( _pattern | parameter ) )* )? '|'
label ::=
"'" identifier
break_expression ::=
'break' label? _expression?
continue_expression ::=
'continue' label?
index_expression ::=
_expression '[' _expression ']'
await_expression ::=
_expression '.' 'await'
field_expression ::=
_expression '.' ( _field_identifier | integer_literal )
unsafe_block ::=
'unsafe' block
async_block ::=
'async' 'move'? block
try_block ::=
'try' block
block ::=
( label ':' )? '{' _statement* _expression? '}'
_pattern ::=
_literal_pattern
| ( 'u8' | 'i8' | 'u16' | 'i16' | 'u32' | 'i32' | 'u64' | 'i64' | 'u128' | 'i128' | 'isize' | 'usize' | 'f32' | 'f64' | 'bool' | 'str' | 'char' )
| identifier
| scoped_identifier
| tuple_pattern
| tuple_struct_pattern
| struct_pattern
| _reserved_identifier
| ref_pattern
| slice_pattern
| captured_pattern
| reference_pattern
| remaining_field_pattern
| mut_pattern
| range_pattern
| or_pattern
| const_block
| macro_invocation
| '_'
tuple_pattern ::=
'(' ( ( _pattern | closure_expression ) ( ',' ( _pattern | closure_expression ) )* )? ','? ')'
slice_pattern ::=
'[' ( _pattern ( ',' _pattern )* )? ','? ']'
tuple_struct_pattern ::=
( identifier | scoped_identifier ) '(' ( _pattern ( ',' _pattern )* )? ','? ')'
struct_pattern ::=
( _type_identifier | scoped_type_identifier ) '{' ( ( field_pattern | remaining_field_pattern ) ( ',' ( field_pattern | remaining_field_pattern ) )* )? ','? '}'
field_pattern ::=
'ref'? mutable_specifier? ( identifier | _field_identifier ':' _pattern )
remaining_field_pattern ::=
'..'
mut_pattern ::=
mutable_specifier _pattern
range_pattern ::=
( _literal_pattern | _path ) ( ( '...' | '..=' ) ( _literal_pattern | _path ) | '..' )
ref_pattern ::=
'ref' _pattern
captured_pattern ::=
identifier '@' _pattern
reference_pattern ::=
'&' mutable_specifier? _pattern
or_pattern ::=
_pattern '|' _pattern
_literal ::=
string_literal
| raw_string_literal
| char_literal
| boolean_literal
| integer_literal
| float_literal
_literal_pattern ::=
string_literal
| raw_string_literal
| char_literal
| boolean_literal
| integer_literal
| float_literal
| negative_literal
negative_literal ::=
'-' ( integer_literal | float_literal )
integer_literal ::=
( ( [0-9][0-9_]* | '0x'[0-9a-fA-F_]+ | '0b'[01_]+ | '0o'[0-7_]+ ) ( 'u8' | 'i8' | 'u16' | 'i16' | 'u32' | 'i32' | 'u64' | 'i64' | 'u128' | 'i128' | 'isize' | 'usize' | 'f32' | 'f64' )? )
string_literal ::=
[bc]?'"' ( escape_sequence | _string_content )* '"'
char_literal ::=
( 'b'? "'" ( '\\' ( [^xu] | 'u'[0-9a-fA-F]'{4}' | 'u{'[0-9a-fA-F]+'}' | 'x'[0-9a-fA-F]'{2}' ) | [^\'] )? "'" )
escape_sequence ::=
( '\\' ( [^xu] | 'u'[0-9a-fA-F]'{4}' | 'u{'[0-9a-fA-F]+'}' | 'x'[0-9a-fA-F]'{2}' ) )
boolean_literal ::=
'true'
| 'false'
comment ::=
line_comment
| block_comment
line_comment ::=
( '//' '.'* )
_path ::=
self
| ( 'u8' | 'i8' | 'u16' | 'i16' | 'u32' | 'i32' | 'u64' | 'i64' | 'u128' | 'i128' | 'isize' | 'usize' | 'f32' | 'f64' | 'bool' | 'str' | 'char' )
| metavariable
| super
| crate
| identifier
| scoped_identifier
| _reserved_identifier
identifier ::=
('r#')?[_\p{XID_Start}][_\p{XID_Continue}]*
shebang ::=
'#!'[ #x09#x0A#x0B#x0C#x0D]*[^#x5B #x09#x0A#x0B#x0C#x0D]+
_reserved_identifier ::=
( 'default' | 'union' )
_type_identifier ::=
identifier
_field_identifier ::=
identifier
self ::=
'self'
super ::=
'super'
crate ::=
'crate'
metavariable ::=
'\$'[a-zA-Z_][a-zA-Z_]*