@willbooster/tree-sitter-java 0.0.0-placeholder.0 → 1.0.0

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