tree-sitter-java-orchard 0.5.2

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,1361 @@
1
+ /**
2
+ * @file Java grammar for tree-sitter
3
+ * @author Ayman Nadeem <aymannadeem@github.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
+ const DIGITS = token(choice('0', seq(/[1-9]/, optional(seq(optional('_'), sep1(/[0-9]+/, /_+/))))));
13
+ const DECIMAL_DIGITS = token(sep1(/[0-9]+/, '_'));
14
+ const HEX_DIGITS = token(sep1(/[A-Fa-f0-9]+/, '_'));
15
+
16
+ /* eslint-disable no-multi-spaces */
17
+
18
+ const PREC = {
19
+ // https://introcs.cs.princeton.edu/java/11precedence/
20
+ COMMENT: 0, // // /* */
21
+ ASSIGN: 1, // = += -= *= /= %= &= ^= |= <<= >>= >>>=
22
+ DECL: 2,
23
+ ELEMENT_VAL: 2,
24
+ TERNARY: 3, // ?:
25
+ OR: 4, // ||
26
+ AND: 5, // &&
27
+ BIT_OR: 6, // |
28
+ BIT_XOR: 7, // ^
29
+ BIT_AND: 8, // &
30
+ EQUALITY: 9, // == !=
31
+ GENERIC: 10,
32
+ REL: 10, // < <= > >= instanceof
33
+ SHIFT: 11, // << >> >>>
34
+ ADD: 12, // + -
35
+ MULT: 13, // * / %
36
+ CAST: 14, // (Type)
37
+ OBJ_INST: 14, // new
38
+ UNARY: 15, // ++a --a a++ a-- + - ! ~
39
+ ARRAY: 16, // [Index]
40
+ OBJ_ACCESS: 16, // .
41
+ PARENS: 16, // (Expression)
42
+ CLASS_LITERAL: 17, // .
43
+ };
44
+
45
+ /* eslint-enable no-multi-spaces */
46
+
47
+ module.exports = grammar({
48
+ name: 'java_orchard',
49
+
50
+ extras: $ => [
51
+ $.line_comment,
52
+ $.block_comment,
53
+ /\s/,
54
+ ],
55
+
56
+ supertypes: $ => [
57
+ $.expression,
58
+ $.declaration,
59
+ $.statement,
60
+ $.primary_expression,
61
+ $._literal,
62
+ $._type,
63
+ $._simple_type,
64
+ $._unannotated_type,
65
+ $.module_directive,
66
+ ],
67
+
68
+ inline: $ => [
69
+ $._name,
70
+ $._simple_type,
71
+ $._class_body_declaration,
72
+ $._variable_initializer,
73
+ ],
74
+
75
+ conflicts: $ => [
76
+ [$.modifiers, $.annotated_type, $.receiver_parameter],
77
+ [$.modifiers, $.annotated_type, $.module_declaration, $.package_declaration],
78
+ [$._unannotated_type, $.primary_expression, $.inferred_parameters],
79
+ [$._unannotated_type, $.primary_expression],
80
+ [$._unannotated_type, $.primary_expression, $.scoped_type_identifier],
81
+ [$._unannotated_type, $.scoped_type_identifier],
82
+ [$._unannotated_type, $.generic_type],
83
+ [$.generic_type, $.primary_expression],
84
+ [$.expression, $.statement],
85
+ // Only conflicts in switch expressions
86
+ [$.lambda_expression, $.primary_expression],
87
+ [$.inferred_parameters, $.primary_expression],
88
+ [$.argument_list, $.record_pattern_body],
89
+ [$.yield_statement, $._reserved_identifier],
90
+ ],
91
+
92
+ word: $ => $.identifier,
93
+
94
+ rules: {
95
+ program: $ => repeat($._toplevel_statement),
96
+
97
+ _toplevel_statement: $ => choice(
98
+ $.statement,
99
+ $.method_declaration,
100
+ ),
101
+
102
+ // Literals
103
+
104
+ _literal: $ => choice(
105
+ $.decimal_integer_literal,
106
+ $.hex_integer_literal,
107
+ $.octal_integer_literal,
108
+ $.binary_integer_literal,
109
+ $.decimal_floating_point_literal,
110
+ $.hex_floating_point_literal,
111
+ $.true,
112
+ $.false,
113
+ $.character_literal,
114
+ $.string_literal,
115
+ $.null_literal,
116
+ ),
117
+
118
+ decimal_integer_literal: _ => token(seq(
119
+ DIGITS,
120
+ optional(choice('l', 'L')),
121
+ )),
122
+
123
+ hex_integer_literal: _ => token(seq(
124
+ choice('0x', '0X'),
125
+ HEX_DIGITS,
126
+ optional(choice('l', 'L')),
127
+ )),
128
+
129
+ octal_integer_literal: _ => token(seq(
130
+ choice('0o', '0O', '0'),
131
+ sep1(/[0-7]+/, '_'),
132
+ optional(choice('l', 'L')),
133
+ )),
134
+
135
+ binary_integer_literal: _ => token(seq(
136
+ choice('0b', '0B'),
137
+ sep1(/[01]+/, '_'),
138
+ optional(choice('l', 'L')),
139
+ )),
140
+
141
+ decimal_floating_point_literal: _ => token(choice(
142
+ seq(DECIMAL_DIGITS, '.', optional(DECIMAL_DIGITS), optional(seq((/[eE]/), optional(choice('-', '+')), DECIMAL_DIGITS)), optional(/[fFdD]/)),
143
+ seq('.', DECIMAL_DIGITS, optional(seq((/[eE]/), optional(choice('-', '+')), DECIMAL_DIGITS)), optional(/[fFdD]/)),
144
+ seq(DIGITS, /[eE]/, optional(choice('-', '+')), DECIMAL_DIGITS, optional(/[fFdD]/)),
145
+ seq(DIGITS, optional(seq((/[eE]/), optional(choice('-', '+')), DECIMAL_DIGITS)), (/[fFdD]/)),
146
+ )),
147
+
148
+ hex_floating_point_literal: _ => token(seq(
149
+ choice('0x', '0X'),
150
+ choice(
151
+ seq(HEX_DIGITS, optional('.')),
152
+ seq(optional(HEX_DIGITS), '.', HEX_DIGITS),
153
+ ),
154
+ optional(seq(
155
+ /[pP]/,
156
+ optional(choice('-', '+')),
157
+ DIGITS,
158
+ optional(/[fFdD]/),
159
+ )),
160
+ )),
161
+
162
+ true: _ => 'true',
163
+
164
+ false: _ => 'false',
165
+
166
+ character_literal: _ => token(seq(
167
+ '\'',
168
+ // this accepts multiple characters while java doesn't
169
+ // hence multiple characters (even unicode ones) are allowed
170
+ repeat1(choice(
171
+ /[^\\'\n]/,
172
+ /\\./,
173
+ /\\\n/,
174
+ /\\u+005[cC]./,
175
+ /\\u+005[cC]\n/,
176
+ )),
177
+ '\'',
178
+ )),
179
+
180
+ string_literal: $ => choice($._string_literal, $._multiline_string_literal),
181
+ _string_literal: $ => seq(
182
+ '"',
183
+ repeat(choice(
184
+ $.string_fragment,
185
+ $.escape_sequence,
186
+ $.string_interpolation,
187
+ )),
188
+ '"',
189
+ ),
190
+ _multiline_string_literal: $ => seq(
191
+ '"""',
192
+ repeat(choice(
193
+ alias($._multiline_string_fragment, $.multiline_string_fragment),
194
+ $._escape_sequence,
195
+ $.string_interpolation,
196
+ )),
197
+ '"""',
198
+ ),
199
+ // Workaround to https://github.com/tree-sitter/tree-sitter/issues/1156
200
+ // We give names to the token() constructs containing a regexp
201
+ // so as to obtain a node in the CST.
202
+
203
+ string_fragment: _ => token.immediate(prec(1, /[^"\\]+/)),
204
+ _multiline_string_fragment: _ => choice(
205
+ /[^"\\]+/,
206
+ /"([^"\\]|\\")*/,
207
+ ),
208
+
209
+ string_interpolation: $ => seq(
210
+ '\\{',
211
+ $.expression,
212
+ '}',
213
+ ),
214
+
215
+ _escape_sequence: $ => choice(
216
+ prec(2, token.immediate(seq('\\', /[^bfnrts'\"\\]/))),
217
+ prec(1, $.escape_sequence),
218
+ ),
219
+ escape_sequence: _ => token.immediate(seq(
220
+ choice('\\', /\\u+005[cC]/),
221
+ choice(
222
+ /[^xu0-7]/,
223
+ /[0-7]{1,3}/,
224
+ /x[0-9a-fA-F]{2}/,
225
+ /u+[0-9a-fA-F]{4}/,
226
+ /u\{[0-9a-fA-F]+\}/,
227
+ ))),
228
+
229
+ null_literal: _ => 'null',
230
+
231
+ // Expressions
232
+
233
+ expression: $ => choice(
234
+ $.assignment_expression,
235
+ $.binary_expression,
236
+ $.instanceof_expression,
237
+ $.lambda_expression,
238
+ $.ternary_expression,
239
+ $.update_expression,
240
+ $.primary_expression,
241
+ $.unary_expression,
242
+ $.cast_expression,
243
+ $.switch_expression,
244
+ ),
245
+
246
+ cast_expression: $ => prec(PREC.CAST, choice(
247
+ seq(
248
+ '(',
249
+ field('type', $._type),
250
+ ')',
251
+ field('value', $.expression),
252
+ ),
253
+ seq(
254
+ '(',
255
+ sep1(field('type', $._type), '&'),
256
+ ')',
257
+ field('value', choice($.primary_expression, $.lambda_expression)),
258
+ ),
259
+ )),
260
+
261
+ assignment_expression: $ => prec.right(PREC.ASSIGN, seq(
262
+ field('left', choice(
263
+ $.identifier,
264
+ $._reserved_identifier,
265
+ $.field_access,
266
+ $.array_access,
267
+ )),
268
+ field('operator', choice('=', '+=', '-=', '*=', '/=', '&=', '|=', '^=', '%=', '<<=', '>>=', '>>>=')),
269
+ field('right', $.expression),
270
+ )),
271
+
272
+ binary_expression: $ => choice(
273
+ ...[
274
+ ['>', PREC.REL],
275
+ ['<', PREC.REL],
276
+ ['>=', PREC.REL],
277
+ ['<=', PREC.REL],
278
+ ['==', PREC.EQUALITY],
279
+ ['!=', PREC.EQUALITY],
280
+ ['&&', PREC.AND],
281
+ ['||', PREC.OR],
282
+ ['+', PREC.ADD],
283
+ ['-', PREC.ADD],
284
+ ['*', PREC.MULT],
285
+ ['/', PREC.MULT],
286
+ ['&', PREC.BIT_AND],
287
+ ['|', PREC.BIT_OR],
288
+ ['^', PREC.BIT_XOR],
289
+ ['%', PREC.MULT],
290
+ ['<<', PREC.SHIFT],
291
+ ['>>', PREC.SHIFT],
292
+ ['>>>', PREC.SHIFT],
293
+ ].map(([operator, precedence]) =>
294
+ prec.left(precedence, seq(
295
+ field('left', $.expression),
296
+ // @ts-ignore
297
+ field('operator', operator),
298
+ field('right', $.expression),
299
+ )),
300
+ )),
301
+
302
+ instanceof_expression: $ => prec(PREC.REL, seq(
303
+ field('left', $.expression),
304
+ 'instanceof',
305
+ optional('final'),
306
+ choice(
307
+ seq(
308
+ field('right', $._type),
309
+ optional(field('name', choice($.identifier, $._reserved_identifier))),
310
+ ),
311
+ field('pattern', $.record_pattern),
312
+ ),
313
+ )),
314
+
315
+ lambda_expression: $ => seq(
316
+ field('parameters', choice(
317
+ $.identifier, $.formal_parameters, $.inferred_parameters, $._reserved_identifier,
318
+ )),
319
+ '->',
320
+ field('body', choice($.expression, $.block)),
321
+ ),
322
+
323
+ inferred_parameters: $ => seq(
324
+ '(',
325
+ commaSep1(choice($.identifier, $._reserved_identifier)),
326
+ ')',
327
+ ),
328
+
329
+ ternary_expression: $ => prec.right(PREC.TERNARY, seq(
330
+ field('condition', $.expression),
331
+ '?',
332
+ field('consequence', $.expression),
333
+ ':',
334
+ field('alternative', $.expression),
335
+ )),
336
+
337
+ unary_expression: $ => choice(...[
338
+ ['+', PREC.UNARY],
339
+ ['-', PREC.UNARY],
340
+ ['!', PREC.UNARY],
341
+ ['~', PREC.UNARY],
342
+ ].map(([operator, precedence]) =>
343
+ prec.left(precedence, seq(
344
+ // @ts-ignore
345
+ field('operator', operator),
346
+ field('operand', $.expression),
347
+ )),
348
+ )),
349
+
350
+ update_expression: $ => prec.left(PREC.UNARY, choice(
351
+ // Post (in|de)crement is evaluated before pre (in|de)crement
352
+ seq($.expression, '++'),
353
+ seq($.expression, '--'),
354
+ seq('++', $.expression),
355
+ seq('--', $.expression),
356
+ )),
357
+
358
+ primary_expression: $ => choice(
359
+ $._literal,
360
+ $.class_literal,
361
+ $.this,
362
+ $.identifier,
363
+ $._reserved_identifier,
364
+ $.parenthesized_expression,
365
+ $.object_creation_expression,
366
+ $.field_access,
367
+ $.array_access,
368
+ $.method_invocation,
369
+ $.method_reference,
370
+ $.array_creation_expression,
371
+ $.template_expression,
372
+ ),
373
+
374
+ array_creation_expression: $ => prec.right(seq(
375
+ 'new',
376
+ repeat($._annotation),
377
+ field('type', $._simple_type),
378
+ choice(
379
+ seq(
380
+ field('dimensions', repeat1($.dimensions_expr)),
381
+ field('dimensions', optional($.dimensions)),
382
+ ),
383
+ seq(
384
+ field('dimensions', $.dimensions),
385
+ field('value', $.array_initializer),
386
+ ),
387
+ ),
388
+ )),
389
+
390
+ dimensions_expr: $ => seq(repeat($._annotation), '[', $.expression, ']'),
391
+
392
+ parenthesized_expression: $ => seq('(', $.expression, ')'),
393
+
394
+ class_literal: $ => prec.dynamic(PREC.CLASS_LITERAL, seq($._unannotated_type, '.', 'class')),
395
+
396
+ object_creation_expression: $ => choice(
397
+ $._unqualified_object_creation_expression,
398
+ seq($.primary_expression, '.', $._unqualified_object_creation_expression),
399
+ ),
400
+
401
+ _unqualified_object_creation_expression: $ => prec.right(seq(
402
+ 'new',
403
+ choice(
404
+ seq(
405
+ repeat($._annotation),
406
+ field('type_arguments', $.type_arguments),
407
+ repeat($._annotation),
408
+ ),
409
+ repeat($._annotation),
410
+ ),
411
+ field('type', $._simple_type),
412
+ field('arguments', $.argument_list),
413
+ optional($.class_body),
414
+ )),
415
+
416
+ field_access: $ => seq(
417
+ field('object', choice($.primary_expression, $.super)),
418
+ optional(seq(
419
+ '.',
420
+ $.super,
421
+ )),
422
+ '.',
423
+ field('field', choice($.identifier, $._reserved_identifier, $.this)),
424
+ ),
425
+
426
+ template_expression: $ => seq(
427
+ field('template_processor', $.primary_expression),
428
+ '.',
429
+ field('template_argument', $.string_literal),
430
+ ),
431
+
432
+ array_access: $ => seq(
433
+ field('array', $.primary_expression),
434
+ '[',
435
+ field('index', $.expression),
436
+ ']',
437
+ ),
438
+
439
+ method_invocation: $ => seq(
440
+ choice(
441
+ field('name', choice($.identifier, $._reserved_identifier)),
442
+ seq(
443
+ field('object', choice($.primary_expression, $.super)),
444
+ '.',
445
+ optional(seq(
446
+ $.super,
447
+ '.',
448
+ )),
449
+ field('type_arguments', optional($.type_arguments)),
450
+ field('name', choice($.identifier, $._reserved_identifier)),
451
+ ),
452
+ ),
453
+ field('arguments', $.argument_list),
454
+ ),
455
+
456
+ argument_list: $ => seq('(', commaSep($.expression), ')'),
457
+
458
+ method_reference: $ => seq(
459
+ choice($._type, $.primary_expression, $.super),
460
+ '::',
461
+ optional($.type_arguments),
462
+ choice('new', $.identifier),
463
+ ),
464
+
465
+ type_arguments: $ => seq(
466
+ '<',
467
+ commaSep(choice($._type, $.wildcard)),
468
+ '>',
469
+ ),
470
+
471
+ wildcard: $ => seq(
472
+ repeat($._annotation),
473
+ '?',
474
+ optional($._wildcard_bounds),
475
+ ),
476
+
477
+ _wildcard_bounds: $ => choice(
478
+ seq('extends', $._type),
479
+ seq($.super, $._type),
480
+ ),
481
+
482
+ dimensions: $ => prec.right(repeat1(
483
+ seq(repeat($._annotation), '[', ']'),
484
+ )),
485
+
486
+ switch_expression: $ => seq(
487
+ 'switch',
488
+ field('condition', $.parenthesized_expression),
489
+ field('body', $.switch_block),
490
+ ),
491
+
492
+ switch_block: $ => seq(
493
+ '{',
494
+ choice(
495
+ repeat($.switch_block_statement_group),
496
+ repeat($.switch_rule),
497
+ ),
498
+ '}',
499
+ ),
500
+
501
+ switch_block_statement_group: $ => prec.left(seq(
502
+ repeat1(seq($.switch_label, ':')),
503
+ repeat($.statement),
504
+ )),
505
+
506
+ switch_rule: $ => seq(
507
+ $.switch_label,
508
+ '->',
509
+ choice($.expression_statement, $.throw_statement, $.block),
510
+ ),
511
+
512
+ switch_label: $ => choice(
513
+ seq('case',
514
+ choice(
515
+ $.pattern,
516
+ commaSep1($.expression),
517
+ ),
518
+ optional($.guard),
519
+ ),
520
+ 'default',
521
+ ),
522
+
523
+ pattern: $ => choice(
524
+ $.type_pattern,
525
+ $.record_pattern,
526
+ ),
527
+ type_pattern: $ => seq($._unannotated_type, choice($.identifier, $._reserved_identifier)),
528
+ record_pattern: $ => seq(choice($.identifier, $._reserved_identifier, $.generic_type), $.record_pattern_body),
529
+ record_pattern_body: $ => seq('(', commaSep(choice($.record_pattern_component, $.record_pattern)), ')'),
530
+ record_pattern_component: $ => choice(
531
+ $.underscore_pattern,
532
+ seq(
533
+ $._unannotated_type,
534
+ choice($.identifier, $._reserved_identifier),
535
+ ),
536
+ ),
537
+
538
+ underscore_pattern: _ => '_',
539
+
540
+ guard: $ => seq('when', $.expression),
541
+
542
+ // Statements
543
+
544
+ statement: $ => choice(
545
+ $.declaration,
546
+ $.expression_statement,
547
+ $.labeled_statement,
548
+ $.if_statement,
549
+ $.while_statement,
550
+ $.for_statement,
551
+ $.enhanced_for_statement,
552
+ $.block,
553
+ ';',
554
+ $.assert_statement,
555
+ $.do_statement,
556
+ $.break_statement,
557
+ $.continue_statement,
558
+ $.return_statement,
559
+ $.yield_statement,
560
+ $.switch_expression, // switch statements and expressions are identical
561
+ $.synchronized_statement,
562
+ $.local_variable_declaration,
563
+ $.throw_statement,
564
+ $.try_statement,
565
+ $.try_with_resources_statement,
566
+ ),
567
+
568
+ block: $ => seq(
569
+ '{', repeat($.statement), '}',
570
+ ),
571
+
572
+ expression_statement: $ => seq(
573
+ $.expression,
574
+ ';',
575
+ ),
576
+
577
+ labeled_statement: $ => seq(
578
+ $.identifier, ':', $.statement,
579
+ ),
580
+
581
+ assert_statement: $ => choice(
582
+ seq('assert', $.expression, ';'),
583
+ seq('assert', $.expression, ':', $.expression, ';'),
584
+ ),
585
+
586
+ do_statement: $ => seq(
587
+ 'do',
588
+ field('body', $.statement),
589
+ 'while',
590
+ field('condition', $.parenthesized_expression),
591
+ ';',
592
+ ),
593
+
594
+ break_statement: $ => seq('break', optional($.identifier), ';'),
595
+
596
+ continue_statement: $ => seq('continue', optional($.identifier), ';'),
597
+
598
+ return_statement: $ => seq(
599
+ 'return',
600
+ optional($.expression),
601
+ ';',
602
+ ),
603
+
604
+ yield_statement: $ => seq(
605
+ 'yield',
606
+ $.expression,
607
+ ';',
608
+ ),
609
+
610
+ synchronized_statement: $ => seq(
611
+ 'synchronized',
612
+ $.parenthesized_expression,
613
+ field('body', $.block),
614
+ ),
615
+
616
+ throw_statement: $ => seq('throw', $.expression, ';'),
617
+
618
+ try_statement: $ => seq(
619
+ 'try',
620
+ field('body', $.block),
621
+ choice(
622
+ repeat1($.catch_clause),
623
+ seq(repeat($.catch_clause), $.finally_clause),
624
+ ),
625
+ ),
626
+
627
+ catch_clause: $ => seq(
628
+ 'catch',
629
+ '(',
630
+ $.catch_formal_parameter,
631
+ ')',
632
+ field('body', $.block),
633
+ ),
634
+
635
+ catch_formal_parameter: $ => seq(
636
+ optional($.modifiers),
637
+ $.catch_type,
638
+ $._variable_declarator_id,
639
+ ),
640
+
641
+ catch_type: $ => seq(
642
+ $._unannotated_type, // first type's annotations will be parsed as modifiers
643
+ repeat(seq('|', $._type)),
644
+ ),
645
+
646
+ finally_clause: $ => seq('finally', $.block),
647
+
648
+ try_with_resources_statement: $ => seq(
649
+ 'try',
650
+ field('resources', $.resource_specification),
651
+ field('body', $.block),
652
+ repeat($.catch_clause),
653
+ optional($.finally_clause),
654
+ ),
655
+
656
+ resource_specification: $ => seq(
657
+ '(', sep1($.resource, ';'), optional(';'), ')',
658
+ ),
659
+
660
+ resource: $ => choice(
661
+ seq(
662
+ optional($.modifiers),
663
+ field('type', $._unannotated_type),
664
+ $._variable_declarator_id,
665
+ '=',
666
+ field('value', $.expression),
667
+ ),
668
+ $.identifier,
669
+ $.field_access,
670
+ ),
671
+
672
+ if_statement: $ => prec.right(seq(
673
+ 'if',
674
+ field('condition', $.parenthesized_expression),
675
+ field('consequence', $.statement),
676
+ optional(seq('else', field('alternative', $.statement))),
677
+ )),
678
+
679
+ while_statement: $ => seq(
680
+ 'while',
681
+ field('condition', $.parenthesized_expression),
682
+ field('body', $.statement),
683
+ ),
684
+
685
+ for_statement: $ => seq(
686
+ 'for', '(',
687
+ choice(
688
+ field('init', $.local_variable_declaration),
689
+ seq(
690
+ commaSep(field('init', $.expression)),
691
+ ';',
692
+ ),
693
+ ),
694
+ field('condition', optional($.expression)), ';',
695
+ commaSep(field('update', $.expression)), ')',
696
+ field('body', $.statement),
697
+ ),
698
+
699
+ enhanced_for_statement: $ => seq(
700
+ 'for',
701
+ '(',
702
+ optional($.modifiers),
703
+ field('type', $._unannotated_type),
704
+ $._variable_declarator_id,
705
+ ':',
706
+ field('value', $.expression),
707
+ ')',
708
+ field('body', $.statement),
709
+ ),
710
+
711
+ // Annotations
712
+
713
+ _annotation: $ => choice(
714
+ $.marker_annotation,
715
+ $.annotation,
716
+ ),
717
+
718
+ marker_annotation: $ => seq(
719
+ '@',
720
+ field('name', $._name),
721
+ ),
722
+
723
+ annotation: $ => seq(
724
+ '@',
725
+ field('name', $._name),
726
+ field('arguments', $.annotation_argument_list),
727
+ ),
728
+
729
+ annotation_argument_list: $ => seq(
730
+ '(',
731
+ choice(
732
+ $._element_value,
733
+ commaSep($.element_value_pair),
734
+ ),
735
+ ')',
736
+ ),
737
+
738
+ element_value_pair: $ => seq(
739
+ field('key', choice($.identifier, $._reserved_identifier)),
740
+ '=',
741
+ field('value', $._element_value),
742
+ ),
743
+
744
+ _element_value: $ => prec(PREC.ELEMENT_VAL, choice(
745
+ $.expression,
746
+ $.element_value_array_initializer,
747
+ $._annotation,
748
+ )),
749
+
750
+ element_value_array_initializer: $ => seq(
751
+ '{',
752
+ commaSep($._element_value),
753
+ optional(','),
754
+ '}',
755
+ ),
756
+
757
+ // Declarations
758
+
759
+ declaration: $ => prec(PREC.DECL, choice(
760
+ $.module_declaration,
761
+ $.package_declaration,
762
+ $.import_declaration,
763
+ $.class_declaration,
764
+ $.record_declaration,
765
+ $.interface_declaration,
766
+ $.annotation_type_declaration,
767
+ $.enum_declaration,
768
+ )),
769
+
770
+ module_declaration: $ => seq(
771
+ repeat($._annotation),
772
+ optional('open'),
773
+ 'module',
774
+ field('name', $._name),
775
+ field('body', $.module_body),
776
+ ),
777
+
778
+ module_body: $ => seq(
779
+ '{',
780
+ repeat($.module_directive),
781
+ '}',
782
+ ),
783
+
784
+ module_directive: $ => choice(
785
+ $.requires_module_directive,
786
+ $.exports_module_directive,
787
+ $.opens_module_directive,
788
+ $.uses_module_directive,
789
+ $.provides_module_directive,
790
+ ),
791
+
792
+ requires_module_directive: $ => seq(
793
+ 'requires',
794
+ repeat(field('modifiers', $.requires_modifier)),
795
+ field('module', $._name),
796
+ ';',
797
+ ),
798
+
799
+ requires_modifier: _ => choice(
800
+ 'transitive',
801
+ 'static',
802
+ ),
803
+
804
+ exports_module_directive: $ => seq(
805
+ 'exports',
806
+ field('package', $._name),
807
+ optional(seq(
808
+ 'to',
809
+ field('modules', $._name),
810
+ repeat(seq(',', field('modules', $._name))),
811
+ )),
812
+ ';',
813
+ ),
814
+
815
+ opens_module_directive: $ => seq(
816
+ 'opens',
817
+ field('package', $._name),
818
+ optional(seq(
819
+ 'to',
820
+ field('modules', $._name),
821
+ repeat(seq(',', field('modules', $._name))),
822
+ )),
823
+ ';',
824
+ ),
825
+
826
+ uses_module_directive: $ => seq(
827
+ 'uses',
828
+ field('type', $._name),
829
+ ';',
830
+ ),
831
+
832
+ provides_module_directive: $ => seq(
833
+ 'provides',
834
+ field('provided', $._name),
835
+ 'with',
836
+ $._name,
837
+ repeat(seq(',', (field('provider', $._name)))),
838
+ ';',
839
+ ),
840
+
841
+ package_declaration: $ => seq(
842
+ repeat($._annotation),
843
+ 'package',
844
+ $._name,
845
+ ';',
846
+ ),
847
+
848
+ import_declaration: $ => seq(
849
+ 'import',
850
+ choice(
851
+ seq(
852
+ 'module',
853
+ $._name
854
+ ),
855
+ seq(
856
+ optional('static'),
857
+ $._name,
858
+ optional(seq('.', $.asterisk))
859
+ )
860
+ ),
861
+ ';'
862
+ ),
863
+
864
+ asterisk: _ => '*',
865
+
866
+ enum_declaration: $ => seq(
867
+ optional($.modifiers),
868
+ 'enum',
869
+ field('name', $.identifier),
870
+ field('interfaces', optional($.super_interfaces)),
871
+ field('body', $.enum_body),
872
+ ),
873
+
874
+ enum_body: $ => seq(
875
+ '{',
876
+ commaSep($.enum_constant),
877
+ optional(','),
878
+ optional($.enum_body_declarations),
879
+ '}',
880
+ ),
881
+
882
+ enum_body_declarations: $ => seq(
883
+ ';',
884
+ repeat($._class_body_declaration),
885
+ ),
886
+
887
+ enum_constant: $ => (seq(
888
+ optional($.modifiers),
889
+ field('name', $.identifier),
890
+ field('arguments', optional($.argument_list)),
891
+ field('body', optional($.class_body)),
892
+ )),
893
+
894
+ class_declaration: $ => seq(
895
+ optional($.modifiers),
896
+ 'class',
897
+ field('name', $.identifier),
898
+ optional(field('type_parameters', $.type_parameters)),
899
+ optional(field('superclass', $.superclass)),
900
+ optional(field('interfaces', $.super_interfaces)),
901
+ optional(field('permits', $.permits)),
902
+ field('body', $.class_body),
903
+ ),
904
+
905
+ visibility: $ => choice(
906
+ 'public',
907
+ 'protected',
908
+ 'private',
909
+ ),
910
+
911
+ _modifier: $ => choice(
912
+ 'abstract',
913
+ 'static',
914
+ 'final',
915
+ 'strictfp',
916
+ 'default',
917
+ 'synchronized',
918
+ 'native',
919
+ 'transient',
920
+ 'volatile',
921
+ 'sealed',
922
+ 'non-sealed',
923
+ ),
924
+
925
+ modifiers: $ => repeat1(choice(
926
+ $._annotation,
927
+ $.visibility,
928
+ alias($._modifier, $.modifier),
929
+ )),
930
+
931
+ type_parameters: $ => seq(
932
+ '<', commaSep1($.type_parameter), '>',
933
+ ),
934
+
935
+ type_parameter: $ => seq(
936
+ repeat($._annotation),
937
+ alias($.identifier, $.type_identifier),
938
+ optional($.type_bound),
939
+ ),
940
+
941
+ type_bound: $ => seq('extends', $._type, repeat(seq('&', $._type))),
942
+
943
+ superclass: $ => seq(
944
+ 'extends',
945
+ $._type,
946
+ ),
947
+
948
+ super_interfaces: $ => seq(
949
+ 'implements',
950
+ $.type_list,
951
+ ),
952
+
953
+ type_list: $ => seq(
954
+ $._type,
955
+ repeat(seq(',', $._type)),
956
+ ),
957
+
958
+ permits: $ => seq(
959
+ 'permits',
960
+ $.type_list,
961
+ ),
962
+
963
+ class_body: $ => seq(
964
+ '{',
965
+ repeat($._class_body_declaration),
966
+ '}',
967
+ ),
968
+
969
+ _class_body_declaration: $ => choice(
970
+ $.field_declaration,
971
+ $.record_declaration,
972
+ $.method_declaration,
973
+ $.compact_constructor_declaration, // For records.
974
+ $.class_declaration,
975
+ $.interface_declaration,
976
+ $.annotation_type_declaration,
977
+ $.enum_declaration,
978
+ $.block,
979
+ $.static_initializer,
980
+ $.constructor_declaration,
981
+ ';',
982
+ ),
983
+
984
+ static_initializer: $ => seq(
985
+ 'static',
986
+ $.block,
987
+ ),
988
+
989
+ constructor_declaration: $ => seq(
990
+ optional($.modifiers),
991
+ $._constructor_declarator,
992
+ optional($.throws),
993
+ field('body', $.constructor_body),
994
+ ),
995
+
996
+ _constructor_declarator: $ => seq(
997
+ field('type_parameters', optional($.type_parameters)),
998
+ field('name', $.identifier),
999
+ field('parameters', $.formal_parameters),
1000
+ ),
1001
+
1002
+ constructor_body: $ => seq(
1003
+ '{',
1004
+ optional($.explicit_constructor_invocation),
1005
+ repeat($.statement),
1006
+ '}',
1007
+ ),
1008
+
1009
+ explicit_constructor_invocation: $ => seq(
1010
+ choice(
1011
+ seq(
1012
+ field('type_arguments', optional($.type_arguments)),
1013
+ field('constructor', choice($.this, $.super)),
1014
+ ),
1015
+ seq(
1016
+ field('object', choice($.primary_expression)),
1017
+ '.',
1018
+ field('type_arguments', optional($.type_arguments)),
1019
+ field('constructor', $.super),
1020
+ ),
1021
+ ),
1022
+ field('arguments', $.argument_list),
1023
+ ';',
1024
+ ),
1025
+
1026
+ _name: $ => choice(
1027
+ $.identifier,
1028
+ $._reserved_identifier,
1029
+ $.scoped_identifier,
1030
+ ),
1031
+
1032
+ scoped_identifier: $ => seq(
1033
+ field('scope', $._name),
1034
+ '.',
1035
+ field('name', $.identifier),
1036
+ ),
1037
+
1038
+ field_declaration: $ => seq(
1039
+ optional($.modifiers),
1040
+ field('type', $._unannotated_type),
1041
+ $._variable_declarator_list,
1042
+ ';',
1043
+ ),
1044
+
1045
+ record_declaration: $ => seq(
1046
+ optional($.modifiers),
1047
+ 'record',
1048
+ field('name', $.identifier),
1049
+ optional(field('type_parameters', $.type_parameters)),
1050
+ field('parameters', $.formal_parameters),
1051
+ optional(field('interfaces', $.super_interfaces)),
1052
+ field('body', $.class_body),
1053
+ ),
1054
+
1055
+ annotation_type_declaration: $ => seq(
1056
+ optional($.modifiers),
1057
+ '@interface',
1058
+ field('name', $.identifier),
1059
+ field('body', $.annotation_type_body),
1060
+ ),
1061
+
1062
+ annotation_type_body: $ => seq(
1063
+ '{',
1064
+ repeat(choice(
1065
+ $.annotation_type_element_declaration,
1066
+ $.constant_declaration,
1067
+ $.class_declaration,
1068
+ $.interface_declaration,
1069
+ $.enum_declaration,
1070
+ $.annotation_type_declaration,
1071
+ ';',
1072
+ )),
1073
+ '}',
1074
+ ),
1075
+
1076
+ annotation_type_element_declaration: $ => seq(
1077
+ optional($.modifiers),
1078
+ field('type', $._unannotated_type),
1079
+ field('name', choice($.identifier, $._reserved_identifier)),
1080
+ '(', ')',
1081
+ field('dimensions', optional($.dimensions)),
1082
+ optional($._default_value),
1083
+ ';',
1084
+ ),
1085
+
1086
+ _default_value: $ => seq(
1087
+ 'default',
1088
+ field('value', $._element_value),
1089
+ ),
1090
+
1091
+ interface_declaration: $ => seq(
1092
+ optional($.modifiers),
1093
+ 'interface',
1094
+ field('name', $.identifier),
1095
+ field('type_parameters', optional($.type_parameters)),
1096
+ optional($.extends_interfaces),
1097
+ optional(field('permits', $.permits)),
1098
+ field('body', $.interface_body),
1099
+ ),
1100
+
1101
+ extends_interfaces: $ => seq(
1102
+ 'extends',
1103
+ $.type_list,
1104
+ ),
1105
+
1106
+ interface_body: $ => seq(
1107
+ '{',
1108
+ repeat(choice(
1109
+ $.constant_declaration,
1110
+ $.enum_declaration,
1111
+ $.method_declaration,
1112
+ $.class_declaration,
1113
+ $.interface_declaration,
1114
+ $.record_declaration,
1115
+ $.annotation_type_declaration,
1116
+ ';',
1117
+ )),
1118
+ '}',
1119
+ ),
1120
+
1121
+ constant_declaration: $ => seq(
1122
+ optional($.modifiers),
1123
+ field('type', $._unannotated_type),
1124
+ $._variable_declarator_list,
1125
+ ';',
1126
+ ),
1127
+
1128
+ _variable_declarator_list: $ => commaSep1(
1129
+ field('declarator', $.variable_declarator),
1130
+ ),
1131
+
1132
+ variable_declarator: $ => seq(
1133
+ $._variable_declarator_id,
1134
+ optional(seq('=', field('value', $._variable_initializer))),
1135
+ ),
1136
+
1137
+ _variable_declarator_id: $ => seq(
1138
+ field('name', choice($.identifier, $._reserved_identifier, $.underscore_pattern)),
1139
+ field('dimensions', optional($.dimensions)),
1140
+ ),
1141
+
1142
+ _variable_initializer: $ => choice(
1143
+ $.expression,
1144
+ $.array_initializer,
1145
+ ),
1146
+
1147
+ array_initializer: $ => seq(
1148
+ '{',
1149
+ commaSep($._variable_initializer),
1150
+ optional(','),
1151
+ '}',
1152
+ ),
1153
+
1154
+ // Types
1155
+
1156
+ _type: $ => choice(
1157
+ $._unannotated_type,
1158
+ $.annotated_type,
1159
+ ),
1160
+
1161
+ _unannotated_type: $ => choice(
1162
+ $._simple_type,
1163
+ $.array_type,
1164
+ ),
1165
+
1166
+ _simple_type: $ => choice(
1167
+ $.void_type,
1168
+ $.integral_type,
1169
+ $.floating_point_type,
1170
+ $.boolean_type,
1171
+ alias($.identifier, $.type_identifier),
1172
+ $.scoped_type_identifier,
1173
+ $.generic_type,
1174
+ ),
1175
+
1176
+ annotated_type: $ => seq(
1177
+ repeat1($._annotation),
1178
+ $._unannotated_type,
1179
+ ),
1180
+
1181
+ scoped_type_identifier: $ => seq(
1182
+ choice(
1183
+ alias($.identifier, $.type_identifier),
1184
+ $.scoped_type_identifier,
1185
+ $.generic_type,
1186
+ ),
1187
+ '.',
1188
+ repeat($._annotation),
1189
+ alias($.identifier, $.type_identifier),
1190
+ ),
1191
+
1192
+ generic_type: $ => prec.dynamic(PREC.GENERIC, seq(
1193
+ choice(
1194
+ alias($.identifier, $.type_identifier),
1195
+ $.scoped_type_identifier,
1196
+ ),
1197
+ $.type_arguments,
1198
+ )),
1199
+
1200
+ array_type: $ => seq(
1201
+ field('element', $._unannotated_type),
1202
+ field('dimensions', $.dimensions),
1203
+ ),
1204
+
1205
+ integral_type: _ => choice(
1206
+ 'byte',
1207
+ 'short',
1208
+ 'int',
1209
+ 'long',
1210
+ 'char',
1211
+ ),
1212
+
1213
+ floating_point_type: _ => choice(
1214
+ 'float',
1215
+ 'double',
1216
+ ),
1217
+
1218
+ boolean_type: _ => 'boolean',
1219
+
1220
+ void_type: _ => 'void',
1221
+
1222
+ _method_header: $ => seq(
1223
+ optional(seq(
1224
+ field('type_parameters', $.type_parameters),
1225
+ repeat($._annotation),
1226
+ )),
1227
+ field('type', $._unannotated_type),
1228
+ $._method_declarator,
1229
+ optional($.throws),
1230
+ ),
1231
+
1232
+ _method_declarator: $ => seq(
1233
+ field('name', choice($.identifier, $._reserved_identifier)),
1234
+ field('parameters', $.formal_parameters),
1235
+ field('dimensions', optional($.dimensions)),
1236
+ ),
1237
+
1238
+ formal_parameters: $ => seq(
1239
+ '(',
1240
+ choice(
1241
+ $.receiver_parameter,
1242
+ seq(
1243
+ optional(seq($.receiver_parameter, ',')),
1244
+ commaSep(choice($.formal_parameter, $.spread_parameter)),
1245
+ ),
1246
+ ),
1247
+ ')',
1248
+ ),
1249
+
1250
+ formal_parameter: $ => seq(
1251
+ optional($.modifiers),
1252
+ field('type', $._unannotated_type),
1253
+ $._variable_declarator_id,
1254
+ ),
1255
+
1256
+ receiver_parameter: $ => seq(
1257
+ repeat($._annotation),
1258
+ $._unannotated_type,
1259
+ repeat(seq($.identifier, '.')),
1260
+ $.this,
1261
+ ),
1262
+
1263
+ spread_parameter: $ => seq(
1264
+ field('modifiers', optional($.modifiers)),
1265
+ field('type', $._unannotated_type),
1266
+ field('annotations', repeat($._annotation)),
1267
+ '...',
1268
+ $.variable_declarator,
1269
+ ),
1270
+
1271
+ throws: $ => seq(
1272
+ 'throws', commaSep1($._type),
1273
+ ),
1274
+
1275
+ local_variable_declaration: $ => seq(
1276
+ optional($.modifiers),
1277
+ field('type', $._unannotated_type),
1278
+ $._variable_declarator_list,
1279
+ ';',
1280
+ ),
1281
+
1282
+ method_declaration: $ => seq(
1283
+ optional($.modifiers),
1284
+ $._method_header,
1285
+ choice(field('body', $.block), ';'),
1286
+ ),
1287
+
1288
+ compact_constructor_declaration: $ => seq(
1289
+ optional($.modifiers),
1290
+ field('name', $.identifier),
1291
+ field('body', $.block),
1292
+ ),
1293
+
1294
+ _reserved_identifier: $ => choice(
1295
+ prec(-3, alias(
1296
+ choice(
1297
+ 'open',
1298
+ 'module',
1299
+ 'record',
1300
+ 'with',
1301
+ 'sealed',
1302
+ ),
1303
+ $.identifier,
1304
+ )),
1305
+ alias('yield', $.identifier),
1306
+ ),
1307
+
1308
+ this: _ => 'this',
1309
+
1310
+ super: _ => 'super',
1311
+
1312
+ // https://docs.oracle.com/javase/specs/jls/se8/html/jls-3.html#jls-IdentifierChars
1313
+ identifier: _ => /[\p{XID_Start}_$][\p{XID_Continue}\u00A2_$]*/,
1314
+
1315
+ line_comment: _ => token(prec(PREC.COMMENT, seq('//', /[^\n]*/))),
1316
+
1317
+ // http://stackoverflow.com/questions/13014947/regex-to-match-a-c-style-multiline-comment/36328890#36328890
1318
+ block_comment: _ => token(prec(PREC.COMMENT,
1319
+ seq(
1320
+ '/*',
1321
+ /[^*]*\*+([^/*][^*]*\*+)*/,
1322
+ '/',
1323
+ ),
1324
+ )),
1325
+ },
1326
+ });
1327
+
1328
+ /**
1329
+ * Creates a rule to match one or more of the rules separated by `separator`
1330
+ *
1331
+ * @param {RuleOrLiteral} rule
1332
+ *
1333
+ * @param {RuleOrLiteral} separator
1334
+ *
1335
+ * @returns {SeqRule}
1336
+ */
1337
+ function sep1(rule, separator) {
1338
+ return seq(rule, repeat(seq(separator, rule)));
1339
+ }
1340
+
1341
+ /**
1342
+ * Creates a rule to match one or more of the rules separated by a comma
1343
+ *
1344
+ * @param {RuleOrLiteral} rule
1345
+ *
1346
+ * @returns {SeqRule}
1347
+ */
1348
+ function commaSep1(rule) {
1349
+ return seq(rule, repeat(seq(',', rule)));
1350
+ }
1351
+
1352
+ /**
1353
+ * Creates a rule to optionally match one or more of the rules separated by a comma
1354
+ *
1355
+ * @param {RuleOrLiteral} rule
1356
+ *
1357
+ * @returns {ChoiceRule}
1358
+ */
1359
+ function commaSep(rule) {
1360
+ return optional(commaSep1(rule));
1361
+ }