-
Notifications
You must be signed in to change notification settings - Fork 2
/
tree-sitter-ruby.ebnf
743 lines (551 loc) · 15.6 KB
/
tree-sitter-ruby.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
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
//
// From tree-sitter-ruby/src/grammar.json
//
//
// EBNF to generate railroad diagram at
// (IPV6) https://www.bottlecaps.de/rr/ui
// (IPV4) https://rr.red-dove.com/ui
//
program ::=
_statements? ( '__END__'[#x0D#x0A] uninterpreted | '__END__' '' )?
uninterpreted ::=
('.'|[ #x09#x0A#x0B#x0C#x0D])*
block_body ::=
_statements
_statements ::=
( _statement _terminator | empty_statement )+ _statement?
| _statement
begin_block ::=
'BEGIN' '{' _statements? '}'
end_block ::=
'END' '{' _statements? '}'
_statement ::=
undef
| alias
| if_modifier
| unless_modifier
| while_modifier
| until_modifier
| rescue_modifier
| begin_block
| end_block
| _expression
method ::=
'def' _method_rest
singleton_method ::=
'def' ( _variable | '(' _arg ')' ) ( '.' | '::' ) _method_rest
_method_rest ::=
_method_name ( _body_expr | parameters ( _terminator? body_statement? 'end' | _body_expr ) | bare_parameters? _terminator body_statement? 'end' )
rescue_modifier_arg ::=
_arg 'rescue' _arg
rescue_modifier_expression ::=
_expression 'rescue' _arg
_body_expr ::=
'=' ( _arg | rescue_modifier_arg )
parameters ::=
'(' ( _formal_parameter ( ',' _formal_parameter )* )? ')'
bare_parameters ::=
_simple_formal_parameter ( ',' _formal_parameter )*
block_parameters ::=
'|' ( _formal_parameter ( ',' _formal_parameter )* )? ','? ( ';' identifier ( ',' identifier )* )? '|'
_formal_parameter ::=
_simple_formal_parameter
| parameters
_simple_formal_parameter ::=
identifier
| splat_parameter
| hash_splat_parameter
| hash_splat_nil
| forward_parameter
| block_parameter
| keyword_parameter
| optional_parameter
forward_parameter ::=
'...'
splat_parameter ::=
'*' identifier?
hash_splat_parameter ::=
'**' identifier?
hash_splat_nil ::=
'**' 'nil'
block_parameter ::=
'&' identifier?
keyword_parameter ::=
identifier ':' _arg?
optional_parameter ::=
identifier '=' _arg
class ::=
'class' ( constant | scope_resolution ) ( superclass _terminator | _terminator ) body_statement? 'end'
superclass ::=
'<' _expression
singleton_class ::=
'class' _singleton_class_left_angle_left_langle _arg _terminator body_statement? 'end'
module ::=
'module' ( constant | scope_resolution ) _terminator? body_statement? 'end'
return_command ::=
'return' command_argument_list
yield_command ::=
'yield' command_argument_list
break_command ::=
'break' command_argument_list
next_command ::=
'next' command_argument_list
return ::=
'return' argument_list?
yield ::=
'yield' argument_list?
break ::=
'break' argument_list?
next ::=
'next' argument_list?
redo ::=
'redo' argument_list?
retry ::=
'retry' argument_list?
if_modifier ::=
_statement 'if' _expression
unless_modifier ::=
_statement 'unless' _expression
while_modifier ::=
_statement 'while' _expression
until_modifier ::=
_statement 'until' _expression
rescue_modifier ::=
_statement 'rescue' _expression
while ::=
'while' _statement do
until ::=
'until' _statement do
for ::=
'for' ( _lhs | left_assignment_list ) in do
in ::=
'in' _arg
do ::=
( 'do' | _terminator ) _statements? 'end'
case ::=
'case' ( _line_break? _statement )? _terminator? when* else? 'end'
case_match ::=
'case' _line_break? _statement _terminator? in_clause+ else? 'end'
when ::=
'when' pattern ( ',' pattern )* ( _terminator | then )
in_clause ::=
'in' _pattern_top_expr_body _guard? ( _terminator | then )
pattern ::=
_arg
| splat_argument
_guard ::=
if_guard
| unless_guard
if_guard ::=
'if' _expression
unless_guard ::=
'unless' _expression
_pattern_top_expr_body ::=
_pattern_expr | _array_pattern_n | _find_pattern_body | _hash_pattern_body
_array_pattern_n ::=
_pattern_expr ',' | _pattern_expr ',' ( _pattern_expr | _array_pattern_n ) | splat_parameter ( ',' _pattern_expr )*
_pattern_expr ::=
as_pattern
| _pattern_expr_alt
as_pattern ::=
_pattern_expr '=>' identifier
_pattern_expr_alt ::=
alternative_pattern
| _pattern_expr_basic
alternative_pattern ::=
_pattern_expr_basic ( '|' _pattern_expr_basic )+
_array_pattern_body ::=
_pattern_expr
| _array_pattern_n
array_pattern ::=
'[' _array_pattern_body? ']' | _pattern_constant '[' _array_pattern_body? ']' | _pattern_constant '(' _array_pattern_body? ')'
_find_pattern_body ::=
splat_parameter ( ',' _pattern_expr )+ ',' splat_parameter
find_pattern ::=
'[' _find_pattern_body ']'
| _pattern_constant '[' _find_pattern_body ']'
| _pattern_constant '(' _find_pattern_body ')'
_hash_pattern_body ::=
keyword_pattern ( ',' keyword_pattern )* ','? | keyword_pattern ( ',' keyword_pattern )* ',' _hash_pattern_any_rest | _hash_pattern_any_rest
keyword_pattern ::=
( identifier | constant | identifier_suffix | constant_suffix | string ) ':' _pattern_expr?
_hash_pattern_any_rest ::=
hash_splat_parameter
| hash_splat_nil
hash_pattern ::=
'{' _hash_pattern_body? '}' | _pattern_constant '[' _hash_pattern_body ']' | _pattern_constant '(' _hash_pattern_body ')'
_pattern_expr_basic ::=
_pattern_value | identifier | array_pattern | find_pattern | hash_pattern | parenthesized_pattern
parenthesized_pattern ::=
'(' _pattern_expr ')'
_pattern_value ::=
_pattern_primitive | _pattern_range | variable_reference_pattern | expression_reference_pattern | _pattern_constant
_pattern_range ::=
_pattern_primitive ( '..' | '...' ) _pattern_primitive
| ( '..' | '...' ) _pattern_primitive
| _pattern_primitive ( '..' | '...' )
_pattern_primitive ::=
_pattern_literal
| _pattern_lambda
_pattern_lambda ::=
lambda
_pattern_literal ::=
_literal | string | subshell | heredoc_beginning | regex | string_array | symbol_array | _keyword_variable
_keyword_variable ::=
nil | self | true | false | line | file | encoding
line ::=
'__LINE__'
file ::=
'__FILE__'
encoding ::=
'__ENCODING__'
variable_reference_pattern ::=
'^' ( identifier | _nonlocal_variable )
expression_reference_pattern ::=
'^' '(' _expression ')'
_pattern_constant ::=
constant | _pattern_constant_resolution
_pattern_constant_resolution ::=
_pattern_constant? '::' constant
if ::=
'if' _statement ( _terminator | then ) ( else | elsif )? 'end'
unless ::=
'unless' _statement ( _terminator | then ) ( else | elsif )? 'end'
elsif ::=
'elsif' _statement ( _terminator | then ) ( else | elsif )?
else ::=
'else' _terminator? _statements?
then ::=
_terminator _statements
| _terminator? 'then' _statements?
begin ::=
'begin' _terminator? _body_statement? 'end'
ensure ::=
'ensure' _statements?
rescue ::=
'rescue' exceptions? exception_variable? ( _terminator | then )
exceptions ::=
( _arg | splat_argument ) ( ',' ( _arg | splat_argument ) )*
exception_variable ::=
'=>' _lhs
body_statement ::=
_body_statement
_body_statement ::=
_statements ( rescue | else | ensure )*
| _statements? ( rescue | else | ensure )+
_expression ::=
command_binary
| command_unary
| command_assignment
| command_operator_assignment
| command_call
| command_call_with_block
| _chained_command_call
| return_command
| yield_command
| break_command
| next_command
| match_pattern
| test_pattern
| _arg
match_pattern ::=
_arg '=>' _pattern_top_expr_body
test_pattern ::=
_arg 'in' _pattern_top_expr_body
_arg ::=
_unary_minus_pow
| _primary
| assignment
| operator_assignment
| conditional
| range
| binary
| unary
_unary_minus_pow ::=
_unary_minus_num _pow
_pow ::=
_simple_numeric _binary_star_star _arg
_primary ::=
parenthesized_statements
| _lhs
| _function_identifier_call
| call
| array
| string_array
| symbol_array
| hash
| subshell
| _literal
| string
| character
| chained_string
| regex
| lambda
| method
| singleton_method
| class
| singleton_class
| module
| begin
| while
| until
| if
| unless
| for
| case
| case_match
| return
| yield
| break
| next
| redo
| retry
| parenthesized_unary
| heredoc_beginning
parenthesized_statements ::=
'(' _statements? ')'
element_reference ::=
_primary _element_reference_bracket _argument_list_with_trailing_comma? ']'
scope_resolution ::=
( '::' | _primary '::' ) constant
_call_operator ::=
'.'
| '&.'
| '::'
_call ::=
_primary _call_operator ( identifier | operator | constant | _function_identifier )
command_call ::=
( _call | _chained_command_call | ( _variable | _function_identifier ) ) command_argument_list
command_call_with_block ::=
( _call | ( _variable | _function_identifier ) ) command_argument_list block
| ( _call | ( _variable | _function_identifier ) ) command_argument_list do_block
_chained_command_call ::=
command_call_with_block _call_operator ( identifier | _function_identifier | operator | constant )
call ::=
( _call | ( _variable | _function_identifier ) | _primary _call_operator ) argument_list
| ( _call | ( _variable | _function_identifier ) | _primary _call_operator ) argument_list block
| ( _call | ( _variable | _function_identifier ) | _primary _call_operator ) argument_list do_block
| ( _call | ( _variable | _function_identifier ) ) block
| ( _call | ( _variable | _function_identifier ) ) do_block
command_argument_list ::=
_argument ( ',' _argument )*
argument_list ::=
'(' _argument_list_with_trailing_comma? ')'
_argument_list_with_trailing_comma ::=
_argument ( ',' _argument )* ','?
_argument ::=
_expression | splat_argument | hash_splat_argument | forward_argument | block_argument | pair
forward_argument ::=
'...'
splat_argument ::=
_splat_star _arg?
hash_splat_argument ::=
_hash_splat_star_star _arg?
block_argument ::=
_block_ampersand _arg?
do_block ::=
'do' _terminator? ( block_parameters _terminator? )? body_statement? 'end'
block ::=
'{' block_parameters? block_body? '}'
_arg_rhs ::=
_arg
| rescue_modifier_arg
assignment ::=
( _lhs | left_assignment_list ) '=' ( _arg_rhs | splat_argument | right_assignment_list )
command_assignment ::=
( _lhs | left_assignment_list ) '=' ( _expression | rescue_modifier_expression )
operator_assignment ::=
_lhs ( '+=' | '-=' | '*=' | '**=' | '/=' | '||=' | '|=' | '&&=' | '&=' | '%=' | '>>=' | '<<=' | '^=' ) _arg_rhs
command_operator_assignment ::=
_lhs ( '+=' | '-=' | '*=' | '**=' | '/=' | '||=' | '|=' | '&&=' | '&=' | '%=' | '>>=' | '<<=' | '^=' ) ( _expression | rescue_modifier_expression )
conditional ::=
_arg '?' _arg ':' _arg
range ::=
_arg ( '..' | '...' ) _arg | ( '..' | '...' ) _arg | _arg ( '..' | '...' )
binary ::=
_arg 'and' _arg
| _arg 'or' _arg
| _arg '||' _arg
| _arg '&&' _arg
| _arg ( '<<' | '>>' ) _arg
| _arg ( '<' | '<=' | '>' | '>=' ) _arg
| _arg '&' _arg
| _arg ( '^' | '|' ) _arg
| _arg ( '+' | _binary_minus ) _arg
| _arg ( '/' | '%' | _binary_star ) _arg
| _arg ( '==' | '!=' | '===' | '<=>' | '=~' | '!~' ) _arg
| _arg _binary_star_star _arg
command_binary ::=
_expression ( 'or' | 'and' ) _expression
unary ::=
'defined?' _arg
| 'not' _arg
| ( _unary_minus | _binary_minus | '+' ) _arg
| ( '!' | '~' ) _arg
command_unary ::=
'defined?' _expression
| 'not' _expression
| ( _unary_minus | '+' ) _expression
| ( '!' | '~' ) _expression
parenthesized_unary ::=
( 'defined?' | 'not' ) parenthesized_statements
unary_literal ::=
( _unary_minus_num | '+' ) _simple_numeric
_literal ::=
simple_symbol
| delimited_symbol
| _numeric
_numeric ::=
_simple_numeric
| unary_literal
_simple_numeric ::=
integer
| float
| complex
| rational
right_assignment_list ::=
( _arg | splat_argument ) ( ',' ( _arg | splat_argument ) )*
left_assignment_list ::=
_mlhs
_mlhs ::=
( _lhs | rest_assignment | destructured_left_assignment ) ( ',' ( _lhs | rest_assignment | destructured_left_assignment ) )* ','?
destructured_left_assignment ::=
'(' _mlhs ')'
rest_assignment ::=
'*' _lhs?
_function_identifier ::=
identifier_suffix
| constant_suffix
_function_identifier_call ::=
_function_identifier
_lhs ::=
_variable | true | false | nil | scope_resolution | element_reference | _call
_variable ::=
self | super | _nonlocal_variable | identifier | constant
operator ::=
'..'
| '|'
| '^'
| '&'
| '<=>'
| '=='
| '==='
| '=~'
| '>'
| '>='
| '<'
| '<='
| '+'
| '-'
| '*'
| '/'
| '%'
| '!'
| '!~'
| '**'
| '<<'
| '>>'
| '~'
| '+@'
| '-@'
| '~@'
| '[]'
| '[]='
| '`'
_method_name ::=
identifier
| _function_identifier
| constant
| setter
| simple_symbol
| delimited_symbol
| operator
| _nonlocal_variable
_nonlocal_variable ::=
instance_variable
| class_variable
| global_variable
setter ::=
identifier '='
undef ::=
'undef' _method_name ( ',' _method_name )*
alias ::=
'alias' _method_name _method_name
comment ::=
'#' '.'* | '=begin.'*'#x0D'?'#x0A' ( [^=] | '='[^e] | '=e'[^n] | '=en'[^d] )* '=end.'*
integer ::=
'0'[bB][01]('_'?[01])*|'0'[oO]?[0-7]('_'?[0-7])*|('0'[dD])?[0-9]('_'?[0-9])*|'0'[xX][0-9a-fA-F]('_'?[0-9a-fA-F])*
_int_or_float ::=
integer
| float
float ::=
[0-9]('_'?[0-9])*('.'[0-9])?('_'?[0-9])*([eE][\+-]?[0-9]('_'?[0-9])*)?
complex ::=
_int_or_float 'i'
| _int_or_float 'ri'
rational ::=
_int_or_float 'r'
super ::=
'super'
self ::=
'self'
true ::=
'true'
false ::=
'false'
nil ::=
'nil'
constant ::=
( [A-Z] [^\x00-\x1F #x09#x0A#x0B#x0C#x0D:;`"'@$#x23.,|^&<=>+#x2D-*/\%?!~()#x5B#x5D{}]* )
constant_suffix ::=
( [A-Z] [^\x00-\x1F #x09#x0A#x0B#x0C#x0D:;`"'@$#x23.,|^&<=>+#x2D-*/\%?!~()#x5B#x5D{}]* [?] )
| _constant_suffix
identifier ::=
( [^\x00-\x1F #x09#x0A#x0B#x0C#x0DA-Z0-9:;`"'@$#x23.,|^&<=>+#x2D-*/\%?!~()#x5B#x5D{}] [^\x00-\x1F #x09#x0A#x0B#x0C#x0D:;`"'@$#x23.,|^&<=>+#x2D-*/\%?!~()#x5B#x5D{}]* )
identifier_suffix ::=
( [^\x00-\x1F #x09#x0A#x0B#x0C#x0DA-Z0-9:;`"'@$#x23.,|^&<=>+#x2D-*/\%?!~()#x5B#x5D{}] [^\x00-\x1F #x09#x0A#x0B#x0C#x0D:;`"'@$#x23.,|^&<=>+#x2D-*/\%?!~()#x5B#x5D{}]* [?] )
| _identifier_suffix
instance_variable ::=
( '@' [^\x00-\x1F #x09#x0A#x0B#x0C#x0D0-9:;`"'@$#x23.,|^&<=>+#x2D-*/\%?!~()#x5B#x5D{}] [^\x00-\x1F #x09#x0A#x0B#x0C#x0D:;`"'@$#x23.,|^&<=>+#x2D-*/\%?!~()#x5B#x5D{}]* )
class_variable ::=
( '@@' [^\x00-\x1F #x09#x0A#x0B#x0C#x0D0-9:;`"'@$#x23.,|^&<=>+#x2D-*/\%?!~()#x5B#x5D{}] [^\x00-\x1F #x09#x0A#x0B#x0C#x0D:;`"'@$#x23.,|^&<=>+#x2D-*/\%?!~()#x5B#x5D{}]* )
global_variable ::=
'\$'('-'[a-zA-Z0-9_]|[!@&`'+~=/\,;.<>*$?:"]|[0-9]+|[a-zA-Z_][a-zA-Z0-9_]*)
chained_string ::=
string string+
character ::=
'\'?('\\S'('{'[0-9A-Fa-f]*'}'|[0-9A-Fa-f]*|'-\S'([MC]'-\S')?)?|'\S')
interpolation ::=
'#{' _statements? '}'
| _short_interpolation _nonlocal_variable
string ::=
_string_start _literal_contents? _string_end
subshell ::=
_subshell_start _literal_contents? _string_end
string_array ::=
_string_array_start [ #x09#x0A#x0B#x0C#x0D]+? ( _literal_contents ( [ #x09#x0A#x0B#x0C#x0D]+ _literal_contents )* )? [ #x09#x0A#x0B#x0C#x0D]+? _string_end
symbol_array ::=
_symbol_array_start [ #x09#x0A#x0B#x0C#x0D]+? ( _literal_contents ( [ #x09#x0A#x0B#x0C#x0D]+ _literal_contents )* )? [ #x09#x0A#x0B#x0C#x0D]+? _string_end
delimited_symbol ::=
_symbol_start _literal_contents? _string_end
regex ::=
_regex_start _literal_contents? _string_end
heredoc_body ::=
_heredoc_body_start ( heredoc_content | interpolation | escape_sequence )* heredoc_end
_literal_contents ::=
( string_content | interpolation | escape_sequence )+
escape_sequence ::=
( '\\' ( [^ux0-7] | 'x'[0-9a-fA-F]'{1,2}' | [0-7]'{1,3}' | 'u'[0-9a-fA-F]'{4}' | 'u{'[0-9a-fA-F ]+'}' ) )
array ::=
'[' _argument_list_with_trailing_comma? ']'
hash ::=
'{' ( ( pair | hash_splat_argument ) ( ',' ( pair | hash_splat_argument ) )* ','? )? '}'
pair ::=
_arg '=>' _arg | ( string ) ':' _arg | ( hash_key_symbol | identifier | constant | identifier_suffix | constant_suffix ) ':' ( _arg? | _no_line_break )
lambda ::=
'->' ( parameters | bare_parameters )? ( block | do_block )
empty_statement ::=
';'
_terminator ::=
_line_break
| ';'