-
Notifications
You must be signed in to change notification settings - Fork 2
/
tree-sitter-scala.ebnf
705 lines (505 loc) · 17.4 KB
/
tree-sitter-scala.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
//
// From tree-sitter-scala/src/grammar.json
//
//
// EBNF to generate railroad diagram at
// (IPV6) https://www.bottlecaps.de/rr/ui
// (IPV4) https://rr.red-dove.com/ui
//
compilation_unit ::=
_shebang? ( _top_level_definition ( _semicolon _top_level_definition )* _semicolon? )?
_top_level_definition ::=
package_clause
| package_object
| _definition
| _end_marker
| expression
_definition ::=
given_definition
| extension_definition
| class_definition
| import_declaration
| export_declaration
| object_definition
| enum_definition
| trait_definition
| val_definition
| val_declaration
| var_definition
| var_declaration
| type_definition
| function_definition
| function_declaration
enum_definition ::=
annotation* 'enum' _class_constructor extends_clause? derives_clause? enum_body
_enum_block ::=
( enum_case_definitions | expression | _definition ) ( _semicolon ( enum_case_definitions | expression | _definition ) )* _semicolon?
enum_body ::=
':' _indent _enum_block _outdent
| '{' _enum_block? '}'
enum_case_definitions ::=
annotation* 'case' ( simple_enum_case ( ',' simple_enum_case )* | full_enum_case )
simple_enum_case ::=
_identifier extends_clause?
full_enum_case ::=
_identifier _full_enum_def
_full_enum_def ::=
type_parameters? class_parameters+ extends_clause?
package_clause ::=
'package' package_identifier template_body?
package_identifier ::=
_identifier ( '.' _identifier )*
package_object ::=
'package' 'object' _object_definition
import_declaration ::=
'import' _namespace_expression ( ',' _namespace_expression )*
export_declaration ::=
'export' _namespace_expression ( ',' _namespace_expression )*
_namespace_expression ::=
( _identifier ( '.' _identifier )* ) ( '.' ( namespace_wildcard | namespace_selectors | as_renamed_identifier ) )?
namespace_wildcard ::=
'*' | '_' | 'given'
_namespace_given_by_type ::=
'given' _type
namespace_selectors ::=
'{' ( _namespace_given_by_type | namespace_wildcard | _identifier | arrow_renamed_identifier | as_renamed_identifier ) ( ',' ( _namespace_given_by_type | namespace_wildcard | _identifier | arrow_renamed_identifier | as_renamed_identifier ) )* ','? '}'
_import_selectors ::=
namespace_selectors
arrow_renamed_identifier ::=
_identifier '=>' ( _identifier | wildcard )
as_renamed_identifier ::=
_identifier 'as' ( _identifier | wildcard )
object_definition ::=
annotation* modifiers? 'case'? 'object' _object_definition
_object_definition ::=
_identifier extends_clause? derives_clause? _definition_body?
class_definition ::=
annotation* modifiers? 'case'? 'class' _class_definition
_class_definition ::=
_class_constructor extends_clause? derives_clause? _definition_body?
_definition_body ::=
_automatic_semicolon? template_body
_class_constructor ::=
_identifier type_parameters? annotation? access_modifier? ( _automatic_semicolon? class_parameters )*
trait_definition ::=
annotation* modifiers? 'trait' _class_definition
type_parameters ::=
'[' _variant_type_parameter ( ',' _variant_type_parameter )* ','? ']'
_variant_type_parameter ::=
annotation* ( covariant_type_parameter | contravariant_type_parameter | _type_parameter | type_lambda )
covariant_type_parameter ::=
'+' _type_parameter
contravariant_type_parameter ::=
'-' _type_parameter
_type_parameter ::=
( wildcard | _identifier ) type_parameters? lower_bound? upper_bound? view_bound*? context_bound*?
upper_bound ::=
'<:' _type
lower_bound ::=
'>:' _type
view_bound ::=
'<%' _type
context_bound ::=
':' _type
template_body ::=
_indented_template_body
| _braced_template_body
_indented_template_body ::=
':' _indent self_type? _block _outdent
_braced_template_body ::=
'{' ( _braced_template_body1 | _braced_template_body2 )? '}'
_braced_template_body1 ::=
self_type? _block
_braced_template_body2 ::=
( _indent self_type? | self_type? _indent ) _block? _outdent
with_template_body ::=
_indent self_type? _block _outdent
_extension_template_body ::=
_indent _block _outdent
| '{' _block? '}'
_end_marker ::=
'end' ( 'if' | 'while' | 'for' | 'match' | 'try' | 'new' | 'this' | 'given' | 'extension' | 'val' | _identifier )
self_type ::=
_identifier _self_type_ascription? '=>'
_self_type_ascription ::=
':' _type
annotation ::=
'@' _simple_type arguments*
val_definition ::=
_start_val ( _pattern | identifiers ) ( ':' _type )? '=' _indentable_expression
val_declaration ::=
_start_val _identifier ( ',' _identifier )* ':' _type
_start_val ::=
annotation* modifiers? 'val'
var_declaration ::=
_start_var _identifier ( ',' _identifier )* ':' _type
var_definition ::=
_start_var ( _pattern | identifiers ) ( ':' _type )? '=' _indentable_expression
_start_var ::=
annotation* modifiers? 'var'
type_definition ::=
annotation* modifiers? opaque_modifier? 'type' _type_constructor ( '=' _type )?
_type_constructor ::=
_type_identifier type_parameters? lower_bound? upper_bound?
function_definition ::=
_function_declaration ( '=' _indentable_expression | block )
function_declaration ::=
_function_declaration
_function_declaration ::=
annotation* modifiers? 'def' _function_constructor ( ':' _type )?
_function_constructor ::=
_identifier type_parameters? ( _automatic_semicolon? parameters )* _automatic_semicolon?
opaque_modifier ::=
'opaque'
extension_definition ::=
'extension' type_parameters? parameters* ( _extension_template_body | function_definition | function_declaration )
given_definition ::=
annotation* modifiers? 'given' _given_constructor? ( _structural_instance | _annotated_type ( '=' _indentable_expression )? )
_given_constructor ::=
_identifier? type_parameters? ( _automatic_semicolon? parameters )* _automatic_semicolon? ':'
_structural_instance ::=
_constructor_application 'with' with_template_body
_constructor_application ::=
_annotated_type | compound_type | _structural_type | _simple_type arguments | _annotated_type arguments | compound_type arguments
_constructor_applications ::=
_constructor_application ( ',' _constructor_application )* | _constructor_application ( 'with' _constructor_application )*
modifiers ::=
'abstract' | 'final' | 'sealed' | 'implicit' | 'lazy' | 'override' | access_modifier | inline_modifier | infix_modifier | open_modifier | transparent_modifier+
access_modifier ::=
( 'private' | 'protected' ) access_qualifier?
access_qualifier ::=
'[' _identifier ']'
inline_modifier ::=
'inline'
infix_modifier ::=
'infix'
open_modifier ::=
'open'
transparent_modifier ::=
'transparent'
extends_clause ::=
'extends' _constructor_applications arguments?
derives_clause ::=
'derives' ( _type_identifier | stable_type_identifier ) ( ',' ( _type_identifier | stable_type_identifier ) )*
class_parameters ::=
_automatic_semicolon? '(' ( 'implicit' | 'using' )? ( class_parameter ( ',' class_parameter )* ','? )? ')'
parameters ::=
'(' 'implicit'? ( parameter ( ',' parameter )* ','? )? ')'
| _using_parameters_clause
_using_parameters_clause ::=
'(' 'using' ( parameter ( ',' parameter )* ','? | _param_type ( ',' _param_type )* ','? ) ')'
class_parameter ::=
annotation* modifiers? ( 'val' | 'var' )? _identifier ( ':' _param_type )? ( '=' expression )?
parameter ::=
annotation* inline_modifier? _identifier ':' _param_type ( '=' expression )?
_block ::=
( expression | _definition | _end_marker | ';' ) ( _semicolon ( expression | _definition | _end_marker | ';' ) )* _semicolon?
_indentable_expression ::=
indented_block | indented_cases | expression
block ::=
'{' _block? '}'
indented_block ::=
_indent _block _outdent _end_marker?
indented_cases ::=
_indent case_clause+ _outdent
_indented_type_cases ::=
_indent type_case_clause+ _outdent
_type ::=
function_type
| compound_type
| infix_type
| match_type
| _annotated_type
| literal_type
| _structural_type
| type_lambda
_annotated_type ::=
annotated_type | _simple_type
annotated_type ::=
_simple_type annotation+
_simple_type ::=
generic_type
| projected_type
| tuple_type
| singleton_type
| stable_type_identifier
| _type_identifier
| wildcard
compound_type ::=
_annotated_type ( 'with' _annotated_type )+
| _annotated_type _refinement
| _annotated_type ( 'with' _annotated_type )+ _refinement
_structural_type ::=
template_body
_refinement ::=
template_body
_infix_type_choice ::=
compound_type | infix_type | _annotated_type
infix_type ::=
_infix_type_choice _identifier _infix_type_choice
tuple_type ::=
'(' _type ( ',' _type )* ','? ')'
singleton_type ::=
( _identifier | stable_identifier ) '.' 'type'
stable_type_identifier ::=
( _identifier | stable_identifier ) '.' _type_identifier
stable_identifier ::=
( _identifier | stable_identifier ) '.' _identifier
generic_type ::=
_simple_type type_arguments
projected_type ::=
_simple_type '#' _type_identifier
match_type ::=
_infix_type_choice 'match' _indented_type_cases
type_case_clause ::=
'case' _infix_type_choice _arrow_then_type
function_type ::=
parameter_types _arrow_then_type
_arrow_then_type ::=
( '=>' | '?=>' ) _type
parameter_types ::=
_annotated_type | '(' ( _param_type ( ',' _param_type )* ','? )? ')' | compound_type | infix_type
_param_type ::=
_type
| lazy_parameter_type
| repeated_parameter_type
lazy_parameter_type ::=
'=>' _type
repeated_parameter_type ::=
_type '*'
_type_identifier ::=
_identifier
type_lambda ::=
'[' _type_parameter ( ',' _type_parameter )* ','? ']' '=>>' _type
_pattern ::=
_identifier
| stable_identifier
| interpolated_string_expression
| capture_pattern
| tuple_pattern
| case_class_pattern
| infix_pattern
| alternative_pattern
| typed_pattern
| given_pattern
| quote_expression
| literal
| wildcard
| repeat_pattern
case_class_pattern ::=
( _type_identifier | stable_type_identifier ) '(' ( _pattern ( ',' _pattern )* ','? )? ')'
infix_pattern ::=
_pattern _identifier _pattern
capture_pattern ::=
( _identifier | wildcard ) '@' _pattern
repeat_pattern ::=
_pattern '*'
typed_pattern ::=
_pattern ':' _type
given_pattern ::=
'given' _type
alternative_pattern ::=
_pattern '|' _pattern
tuple_pattern ::=
'(' _pattern ( ',' _pattern )* ')'
expression ::=
if_expression
| match_expression
| try_expression
| assignment_expression
| lambda_expression
| postfix_expression
| ascription_expression
| infix_expression
| prefix_expression
| return_expression
| throw_expression
| while_expression
| do_while_expression
| for_expression
| macro_body
| _simple_expression
_simple_expression ::=
identifier
| operator_identifier
| literal
| interpolated_string_expression
| unit
| tuple_expression
| wildcard
| block
| splice_expression
| case_block
| quote_expression
| instance_expression
| parenthesized_expression
| field_expression
| generic_function
| call_expression
lambda_expression ::=
( bindings | 'implicit'? _identifier | wildcard ) ( '=>' | '?=>' ) _indentable_expression
if_expression ::=
inline_modifier? 'if' _if_condition _indentable_expression ( ';'? 'else' _indentable_expression )?
_if_condition ::=
parenthesized_expression | _indentable_expression 'then'
match_expression ::=
inline_modifier? expression 'match' ( case_block | indented_cases )
try_expression ::=
'try' _indentable_expression catch_clause? finally_clause?
catch_clause ::=
'catch' ( _indentable_expression | _expr_case_clause )
_expr_case_clause ::=
'case' _case_pattern expression
finally_clause ::=
'finally' _indentable_expression
binding ::=
( _identifier | wildcard ) ( ':' _param_type )?
bindings ::=
'(' ( binding ( ',' binding )* ','? )? ')'
case_block ::=
'{' '}'
| '{' case_clause+ '}'
case_clause ::=
'case' _case_pattern _block?
_case_pattern ::=
_pattern guard? '=>'
guard ::=
'if' _postfix_expression_choice
assignment_expression ::=
( prefix_expression | _simple_expression ) '=' expression
generic_function ::=
expression type_arguments
call_expression ::=
_simple_expression ( arguments | case_block | block )
| _postfix_expression_choice ':' colon_argument
colon_argument ::=
( ( bindings | _identifier | wildcard ) '=>' )? ( indented_block | indented_cases )
field_expression ::=
_simple_expression '.' _identifier
instance_expression ::=
'new' _constructor_application template_body
| 'new' template_body
| 'new' _constructor_application
ascription_expression ::=
_postfix_expression_choice ':' ( _param_type | annotation )
infix_expression ::=
( infix_expression | prefix_expression | _simple_expression ) _identifier ( prefix_expression | _simple_expression | ':' colon_argument )
postfix_expression ::=
( infix_expression | prefix_expression | _simple_expression ) _identifier
_postfix_expression_choice ::=
postfix_expression | infix_expression | prefix_expression | _simple_expression
macro_body ::=
'macro' ( infix_expression | prefix_expression | _simple_expression )
prefix_expression ::=
( '+' | '-' | '!' | '~' ) _simple_expression
tuple_expression ::=
'(' expression ( ',' expression )+ ','? ')'
parenthesized_expression ::=
'(' expression ')'
type_arguments ::=
'[' _type ( ',' _type )* ','? ']'
arguments ::=
'(' ( _exprs_in_parens | 'using' _exprs_in_parens ) ')'
_exprs_in_parens ::=
expression ( ',' expression )* ','?
splice_expression ::=
'$' ( '{' _block '}' | '[' _type ']' | identifier )
quote_expression ::=
"'" ( '{' _block '}' | '[' _type ']' | identifier )
symbol_literal ::=
'__no_longer_used_symbol_literal_'
identifier ::=
_alpha_identifier | _backquoted_id | _soft_identifier
_soft_identifier ::=
'infix' | 'inline' | 'opaque' | 'open' | 'transparent' | 'end'
_alpha_identifier ::=
[\p{Lu}\p{Lt}\p{Nl}\p{Lo}\p{Lm}\$\p{Ll}_\u00AA\u00BB\u02B0-\u02B8\u02C0-\u02C1\u02E0-\u02E4\u037A\u1D78\u1D9B-\u1DBF\u2071\u207F\u2090-\u209C\u2C7C-\u2C7D\uA69C-\uA69D\uA770\uA7F8-\uA7F9\uAB5C-\uAB5F\$][\p{Lu}\p{Lt}\p{Nl}\p{Lo}\p{Lm}\$\p{Ll}_\u00AA\u00BB\u02B0-\u02B8\u02C0-\u02C1\u02E0-\u02E4\u037A\u1D78\u1D9B-\u1DBF\u2071\u207F\u2090-\u209C\u2C7C-\u2C7D\uA69C-\uA69D\uA770\uA7F8-\uA7F9\uAB5C-\uAB5F0-9\$_\p{Ll}]*('_'[#x2D-!#x23%&*+\/\:<=>?@\u005e\u007c~\p{Sm}\p{So}]+)?
_interpolation_identifier ::=
[\p{Lu}\p{Lt}\p{Nl}\p{Lo}\p{Lm}\p{Ll}_\u00AA\u00BB\u02B0-\u02B8\u02C0-\u02C1\u02E0-\u02E4\u037A\u1D78\u1D9B-\u1DBF\u2071\u207F\u2090-\u209C\u2C7C-\u2C7D\uA69C-\uA69D\uA770\uA7F8-\uA7F9\uAB5C-\uAB5F][\p{Lu}\p{Lt}\p{Nl}\p{Lo}\p{Lm}\p{Ll}_\u00AA\u00BB\u02B0-\u02B8\u02C0-\u02C1\u02E0-\u02E4\u037A\u1D78\u1D9B-\u1DBF\u2071\u207F\u2090-\u209C\u2C7C-\u2C7D\uA69C-\uA69D\uA770\uA7F8-\uA7F9\uAB5C-\uAB5F0-9_\p{Ll}]*
_backquoted_id ::=
'`'[^#x0A`]+'`'
_identifier ::=
identifier
| operator_identifier
identifiers ::=
identifier ',' identifier ( ',' identifier )*
wildcard ::=
'_'
operator_identifier ::=
( [#x2D-!#x23%&*+\/\:<=>?@\u005e\u007c~\p{Sm}\p{So}] | [#x2D-!#x23%&*+\:<=>?@\u005e\u007c~\p{Sm}\p{So}] [#x2D-!#x23%&*+\/\:<=>?@\u005e\u007c~\p{Sm}\p{So}]+ | [#x2D-!#x23%&*+\/\:<=>?@\u005e\u007c~\p{Sm}\p{So}] [#x2D-!#x23%&+\:<=>?@\u005e\u007c~\p{Sm}\p{So}] [#x2D-!#x23%&*+\/\:<=>?@\u005e\u007c~\p{Sm}\p{So}]* )
_non_null_literal ::=
integer_literal
| floating_point_literal
| boolean_literal
| character_literal
| string
literal_type ::=
_non_null_literal
literal ::=
_non_null_literal
| null_literal
integer_literal ::=
( [-]? ( [0-9]('_'?[0-9])* | '0'[xX][0-9a-fA-F]('_'?[0-9a-fA-F])* ) [lL]? )
floating_point_literal ::=
( [-]? ( [0-9]+'.'[0-9]+ [eE][+-]?[0-9]+? [dfDF]? | '.'[0-9]+ [eE][+-]?[0-9]+? [dfDF]? | [0-9]+ [eE][+-]?[0-9]+ [dfDF]? | [0-9]+ [eE][+-]?[0-9]+? [dfDF] ) )
boolean_literal ::=
'true'
| 'false'
character_literal ::=
( "'" ( '\\' ( [^xu] | 'u'[0-9a-fA-F]'{4}' | 'u{'[0-9a-fA-F]+'}' | 'x'[0-9a-fA-F]'{2}' ) | [^\'#x0A] )? "'" )
interpolated_string_expression ::=
identifier interpolated_string
_interpolated_string_start ::=
'"'
_interpolated_multiline_string_start ::=
'"""'
_dollar_escape ::=
'$' ( '$' | '"' )
_aliased_interpolation_identifier ::=
_interpolation_identifier
interpolation ::=
'$' ( _aliased_interpolation_identifier | block )
interpolated_string ::=
_interpolated_string_start ( _interpolated_string_middle ( _dollar_escape | interpolation ) )* _interpolated_string_end
| _interpolated_multiline_string_start ( _interpolated_multiline_string_middle ( _dollar_escape | interpolation ) )* _interpolated_multiline_string_end
string ::=
_simple_string
| _simple_multiline_string
_semicolon ::=
';'
| _automatic_semicolon
null_literal ::=
'null'
unit ::=
'(' ')'
return_expression ::=
'return' expression?
throw_expression ::=
'throw' expression
while_expression ::=
'while' parenthesized_expression expression | 'while' ( _indentable_expression 'do' ) _indentable_expression
do_while_expression ::=
'do' expression 'while' parenthesized_expression
for_expression ::=
'for' ( '(' enumerators ')' | '{' enumerators '}' ) ( expression | 'yield' _indentable_expression )
| 'for' enumerators ( 'do' _indentable_expression | 'yield' _indentable_expression )
enumerators ::=
enumerator ( _semicolon enumerator )* _automatic_semicolon?
| _indent enumerator ( _semicolon enumerator )* _automatic_semicolon? _outdent
enumerator ::=
'case'? _pattern ( '<-' | '=' ) expression guard?
| guard+
_shebang ::=
( '#!' '.'* )
comment ::=
'//' ( using_directive | _comment_text )
_comment_text ::=
'.'*
using_directive ::=
'>' 'using' using_directive_key using_directive_value
using_directive_key ::=
[^ #x09#x0A#x0B#x0C#x0D]+
using_directive_value ::=
'.'*
block_comment ::=
'/*' ( '.' | '//' )* '*/'