-
Notifications
You must be signed in to change notification settings - Fork 2
/
tree-sitter-java.ebnf
637 lines (466 loc) · 15.4 KB
/
tree-sitter-java.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
//
// From tree-sitter-java/src/grammar.json
//
//
// EBNF to generate railroad diagram at
// (IPV6) https://www.bottlecaps.de/rr/ui
// (IPV4) https://rr.red-dove.com/ui
//
program ::=
_toplevel_statement*
_toplevel_statement ::=
statement
| method_declaration
_literal ::=
decimal_integer_literal
| hex_integer_literal
| octal_integer_literal
| binary_integer_literal
| decimal_floating_point_literal
| hex_floating_point_literal
| true
| false
| character_literal
| string_literal
| null_literal
decimal_integer_literal ::=
( ( '0' | [1-9] ( '_'? [0-9]+ ( '_'+ [0-9]+ )* )? ) ( 'l' | 'L' )? )
hex_integer_literal ::=
( ( '0x' | '0X' ) ( [A-Fa-f0-9]+ ( '_' [A-Fa-f0-9]+ )* ) ( 'l' | 'L' )? )
octal_integer_literal ::=
( ( '0o' | '0O' | '0' ) [0-7]+ ( '_' [0-7]+ )* ( 'l' | 'L' )? )
binary_integer_literal ::=
( ( '0b' | '0B' ) [01]+ ( '_' [01]+ )* ( 'l' | 'L' )? )
decimal_floating_point_literal ::=
( ( [0-9]+ ( '_' [0-9]+ )* ) '.' ( [0-9]+ ( '_' [0-9]+ )* )? ( [eE] ( '-' | '+' )? ( [0-9]+ ( '_' [0-9]+ )* ) )? [fFdD]? | '.' ( [0-9]+ ( '_' [0-9]+ )* ) ( [eE] ( '-' | '+' )? ( [0-9]+ ( '_' [0-9]+ )* ) )? [fFdD]? | ( '0' | [1-9] ( '_'? [0-9]+ ( '_'+ [0-9]+ )* )? ) [eE] ( '-' | '+' )? ( [0-9]+ ( '_' [0-9]+ )* ) [fFdD]? | ( '0' | [1-9] ( '_'? [0-9]+ ( '_'+ [0-9]+ )* )? ) ( [eE] ( '-' | '+' )? ( [0-9]+ ( '_' [0-9]+ )* ) )? [fFdD] )
hex_floating_point_literal ::=
( ( '0x' | '0X' ) ( ( [A-Fa-f0-9]+ ( '_' [A-Fa-f0-9]+ )* ) '.'? | ( [A-Fa-f0-9]+ ( '_' [A-Fa-f0-9]+ )* )? '.' ( [A-Fa-f0-9]+ ( '_' [A-Fa-f0-9]+ )* ) ) ( [pP] ( '-' | '+' )? ( '0' | [1-9] ( '_'? [0-9]+ ( '_'+ [0-9]+ )* )? ) [fFdD]? )? )
true ::=
'true'
false ::=
'false'
character_literal ::=
( "'" ( [^\'#x0A] | '\.' | '\#x0A' )+ "'" )
string_literal ::=
_string_literal
| _multiline_string_literal
_string_literal ::=
'"' ( string_fragment | escape_sequence | string_interpolation )* '"'
_multiline_string_literal ::=
'"""' ( _multiline_string_fragment | _escape_sequence | string_interpolation )* '"""'
string_fragment ::=
[^"\]+
_multiline_string_fragment ::=
[^"\]+
| '"'([^"\]|'\"')*
string_interpolation ::=
'\{' expression '}'
_escape_sequence ::=
( '\\' [^abfnrtvxu'\"\\?] )
| escape_sequence
escape_sequence ::=
( '\\' ( [^xu0-7] | [0-7]'{1,3}' | 'x'[0-9a-fA-F]'{2}' | 'u'[0-9a-fA-F]'{4}' | 'u{'[0-9a-fA-F]+'}' ) )
null_literal ::=
'null'
expression ::=
assignment_expression
| binary_expression
| instanceof_expression
| lambda_expression
| ternary_expression
| update_expression
| primary_expression
| unary_expression
| cast_expression
| switch_expression
cast_expression ::=
'(' _type ')' expression | '(' _type ( '&' _type )* ')' ( primary_expression | lambda_expression )
assignment_expression ::=
( identifier | _reserved_identifier | field_access | array_access ) ( '=' | '+=' | '-=' | '*=' | '/=' | '&=' | '|=' | '^=' | '%=' | '<<=' | '>>=' | '>>>=' ) expression
binary_expression ::=
expression '>' expression
| expression '<' expression
| expression '>=' expression
| expression '<=' expression
| expression '==' expression
| expression '!=' expression
| expression '&&' expression
| expression '||' expression
| expression '+' expression
| expression '-' expression
| expression '*' expression
| expression '/' expression
| expression '&' expression
| expression '|' expression
| expression '^' expression
| expression '%' expression
| expression '<<' expression
| expression '>>' expression
| expression '>>>' expression
instanceof_expression ::=
expression 'instanceof' 'final'? ( _type ( identifier | _reserved_identifier )? | record_pattern )
lambda_expression ::=
( identifier | formal_parameters | inferred_parameters | _reserved_identifier ) '->' ( expression | block )
inferred_parameters ::=
'(' ( identifier | _reserved_identifier ) ( ',' ( identifier | _reserved_identifier ) )* ')'
ternary_expression ::=
expression '?' expression ':' expression
unary_expression ::=
'+' expression
| '-' expression
| '!' expression
| '~' expression
update_expression ::=
expression '++' | expression '--' | '++' expression | '--' expression
primary_expression ::=
_literal
| class_literal
| this
| identifier
| _reserved_identifier
| parenthesized_expression
| object_creation_expression
| field_access
| array_access
| method_invocation
| method_reference
| array_creation_expression
| template_expression
array_creation_expression ::=
'new' _annotation* _simple_type ( dimensions_expr+ dimensions? | dimensions array_initializer )
dimensions_expr ::=
_annotation* '[' expression ']'
parenthesized_expression ::=
'(' expression ')'
condition ::=
'(' expression ')'
class_literal ::=
_unannotated_type '.' 'class'
object_creation_expression ::=
_unqualified_object_creation_expression
| primary_expression '.' _unqualified_object_creation_expression
_unqualified_object_creation_expression ::=
'new' ( _annotation* type_arguments _annotation* | _annotation* ) _simple_type argument_list class_body?
field_access ::=
( primary_expression | super ) ( '.' super )? '.' ( identifier | _reserved_identifier | this )
template_expression ::=
primary_expression '.' string_literal
array_access ::=
primary_expression '[' expression ']'
method_invocation ::=
( ( identifier | _reserved_identifier ) | ( primary_expression | super ) '.' ( super '.' )? type_arguments? ( identifier | _reserved_identifier ) ) argument_list
argument_list ::=
'(' ( expression ( ',' expression )* )? ')'
method_reference ::=
( _type | primary_expression | super ) '::' type_arguments? ( 'new' | identifier )
type_arguments ::=
'<' ( ( _type | wildcard ) ( ',' ( _type | wildcard ) )* )? '>'
wildcard ::=
_annotation* '?' _wildcard_bounds?
_wildcard_bounds ::=
'extends' _type
| super _type
dimensions ::=
( _annotation* '[' ']' )+
switch_expression ::=
'switch' parenthesized_expression switch_block
switch_block ::=
'{' ( switch_block_statement_group* | switch_rule* ) '}'
switch_block_statement_group ::=
( switch_label ':' )+ statement*
switch_rule ::=
switch_label '->' ( expression_statement | throw_statement | block )
switch_label ::=
'case' ( pattern | expression ( ',' expression )* ) guard?
| 'default'
pattern ::=
type_pattern
| record_pattern
type_pattern ::=
_unannotated_type ( identifier | _reserved_identifier )
record_pattern ::=
( identifier | _reserved_identifier | generic_type ) record_pattern_body
record_pattern_body ::=
'(' ( ( record_pattern_component | record_pattern ) ( ',' ( record_pattern_component | record_pattern ) )* )? ')'
record_pattern_component ::=
underscore_pattern
| _unannotated_type ( identifier | _reserved_identifier )
underscore_pattern ::=
'_'
guard ::=
'when' expression
statement ::=
declaration
| expression_statement
| labeled_statement
| if_statement
| while_statement
| for_statement
| enhanced_for_statement
| block
| ';'
| assert_statement
| do_statement
| break_statement
| continue_statement
| return_statement
| yield_statement
| switch_expression
| synchronized_statement
| local_variable_declaration
| throw_statement
| try_statement
| try_with_resources_statement
block ::=
'{' statement* '}'
expression_statement ::=
expression ';'
labeled_statement ::=
identifier ':' statement
assert_statement ::=
'assert' expression ';'
| 'assert' expression ':' expression ';'
do_statement ::=
'do' statement 'while' parenthesized_expression ';'
break_statement ::=
'break' identifier? ';'
continue_statement ::=
'continue' identifier? ';'
return_statement ::=
'return' expression? ';'
yield_statement ::=
'yield' expression ';'
synchronized_statement ::=
'synchronized' parenthesized_expression block
throw_statement ::=
'throw' expression ';'
try_statement ::=
'try' block ( catch_clause+ | catch_clause* finally_clause )
catch_clause ::=
'catch' '(' catch_formal_parameter ')' block
catch_formal_parameter ::=
modifiers? catch_type _variable_declarator_id
catch_type ::=
_unannotated_type ( '|' _unannotated_type )*
finally_clause ::=
'finally' block
try_with_resources_statement ::=
'try' resource_specification block catch_clause* finally_clause?
resource_specification ::=
'(' resource ( ';' resource )* ';'? ')'
resource ::=
modifiers? _unannotated_type _variable_declarator_id '=' expression
| identifier
| field_access
if_statement ::=
'if' condition statement ( 'else' statement )?
while_statement ::=
'while' condition statement
for_statement ::=
'for' '(' ( local_variable_declaration | ( expression ( ',' expression )* )? ';' ) expression? ';' ( expression ( ',' expression )* )? ')' statement
enhanced_for_statement ::=
'for' '(' modifiers? _unannotated_type _variable_declarator_id ':' expression ')' statement
_annotation ::=
marker_annotation
| annotation
marker_annotation ::=
'@' _name
annotation ::=
'@' _name annotation_argument_list
annotation_argument_list ::=
'(' ( _element_value | element_value_pair ( ',' element_value_pair )* ) ')'
element_value_pair ::=
identifier '=' _element_value
_element_value ::=
expression | element_value_array_initializer | _annotation
element_value_array_initializer ::=
'{' ( _element_value ( ',' _element_value )* )? ','? '}'
declaration ::=
module_declaration | package_declaration | import_declaration | class_declaration | record_declaration | interface_declaration | annotation_type_declaration | enum_declaration
module_declaration ::=
_annotation* 'open'? 'module' _name module_body
module_body ::=
'{' module_directive* '}'
module_directive ::=
requires_module_directive
| exports_module_directive
| opens_module_directive
| uses_module_directive
| provides_module_directive
requires_module_directive ::=
'requires' requires_modifier* _name ';'
requires_modifier ::=
'transitive'
| 'static'
exports_module_directive ::=
'exports' _name ( 'to' _name ( ',' _name )* )? ';'
opens_module_directive ::=
'opens' _name ( 'to' _name ( ',' _name )* )? ';'
uses_module_directive ::=
'uses' _name ';'
provides_module_directive ::=
'provides' _name 'with' _name ( ',' _name )* ';'
package_declaration ::=
_annotation* 'package' _name ';'
import_declaration ::=
'import' 'static'? _name ( '.' asterisk )? ';'
asterisk ::=
'*'
enum_declaration ::=
modifiers? 'enum' identifier super_interfaces? enum_body
enum_body ::=
'{' ( enum_constant ( ',' enum_constant )* )? ','? enum_body_declarations? '}'
enum_body_declarations ::=
';' _class_body_declaration*
enum_constant ::=
modifiers? identifier argument_list? class_body?
class_declaration ::=
modifiers? 'class' identifier type_parameters? superclass? super_interfaces? permits? class_body
modifiers ::=
( _annotation | 'public' | 'protected' | 'private' | 'abstract' | 'static' | 'final' | 'strictfp' | 'default' | 'synchronized' | 'native' | 'transient' | 'volatile' | 'sealed' | 'non-sealed' )+
type_parameters ::=
'<' type_parameter ( ',' type_parameter )* '>'
type_parameter ::=
_annotation* identifier type_bound?
type_bound ::=
'extends' _type ( '&' _type )*
superclass ::=
'extends' _type
super_interfaces ::=
'implements' type_list
type_list ::=
_type ( ',' _type )*
permits ::=
'permits' type_list
class_body ::=
'{' _class_body_declaration* '}'
_class_body_declaration ::=
field_declaration
| record_declaration
| method_declaration
| compact_constructor_declaration
| class_declaration
| interface_declaration
| annotation_type_declaration
| enum_declaration
| block
| static_initializer
| constructor_declaration
| ';'
static_initializer ::=
'static' block
constructor_declaration ::=
modifiers? _constructor_declarator throws? constructor_body
_constructor_declarator ::=
type_parameters? identifier formal_parameters
constructor_body ::=
'{' explicit_constructor_invocation? statement* '}'
explicit_constructor_invocation ::=
( type_arguments? ( this | super ) | ( primary_expression ) '.' type_arguments? super ) argument_list ';'
_name ::=
identifier
| _reserved_identifier
| scoped_identifier
scoped_identifier ::=
_name '.' identifier
field_declaration ::=
modifiers? _unannotated_type _variable_declarator_list ';'
record_declaration ::=
modifiers? 'record' identifier type_parameters? formal_parameters super_interfaces? class_body
annotation_type_declaration ::=
modifiers? '@interface' identifier annotation_type_body
annotation_type_body ::=
'{' ( annotation_type_element_declaration | constant_declaration | class_declaration | interface_declaration | enum_declaration | annotation_type_declaration | ';' )* '}'
annotation_type_element_declaration ::=
modifiers? _unannotated_type ( identifier | _reserved_identifier ) '(' ')' dimensions? _default_value? ';'
_default_value ::=
'default' _element_value
interface_declaration ::=
modifiers? 'interface' identifier type_parameters? extends_interfaces? permits? interface_body
extends_interfaces ::=
'extends' type_list
interface_body ::=
'{' ( constant_declaration | enum_declaration | method_declaration | class_declaration | interface_declaration | record_declaration | annotation_type_declaration | ';' )* '}'
constant_declaration ::=
modifiers? _unannotated_type _variable_declarator_list ';'
_variable_declarator_list ::=
variable_declarator ( ',' variable_declarator )*
variable_declarator ::=
_variable_declarator_id ( '=' _variable_initializer )?
_variable_declarator_id ::=
( identifier | _reserved_identifier | underscore_pattern ) dimensions?
_variable_initializer ::=
expression
| array_initializer
array_initializer ::=
'{' ( _variable_initializer ( ',' _variable_initializer )* )? ','? '}'
_type ::=
_unannotated_type
| annotated_type
_unannotated_type ::=
_simple_type
| array_type
_simple_type ::=
void_type
| integral_type
| floating_point_type
| boolean_type
| identifier
| scoped_type_identifier
| generic_type
annotated_type ::=
_annotation+ _unannotated_type
scoped_type_identifier ::=
( identifier | scoped_type_identifier | generic_type ) '.' _annotation* identifier
generic_type ::=
( identifier | scoped_type_identifier ) type_arguments
array_type ::=
_unannotated_type dimensions
integral_type ::=
'byte'
| 'short'
| 'int'
| 'long'
| 'char'
floating_point_type ::=
'float'
| 'double'
boolean_type ::=
'boolean'
void_type ::=
'void'
_method_header ::=
( type_parameters _annotation* )? _unannotated_type _method_declarator throws?
_method_declarator ::=
( identifier | _reserved_identifier ) formal_parameters dimensions?
formal_parameters ::=
'(' ( receiver_parameter | ( receiver_parameter ',' )? ( ( formal_parameter | spread_parameter ) ( ',' ( formal_parameter | spread_parameter ) )* )? ) ')'
formal_parameter ::=
modifiers? _unannotated_type _variable_declarator_id
receiver_parameter ::=
_annotation* _unannotated_type ( identifier '.' )* this
spread_parameter ::=
modifiers? _unannotated_type '...' variable_declarator
throws ::=
'throws' _type ( ',' _type )*
local_variable_declaration ::=
modifiers? _unannotated_type _variable_declarator_list ';'
method_declaration ::=
modifiers? _method_header ( block | ';' )
compact_constructor_declaration ::=
modifiers? identifier block
_reserved_identifier ::=
( 'open' | 'module' | 'record' | 'with' | 'yield' | 'sealed' )
this ::=
'this'
super ::=
'super'
identifier ::=
[\p{L}_$][\p{L}\p{Nd}\u00A2_$]*
comment ::=
line_comment
| block_comment
line_comment ::=
'//' [^#x0A]*
block_comment ::=
'/*' [^*]*'\'*+([^\/*][^*]*'\'*+)* '/'