@ant-yasa/uast-parser-java-js 0.1.15-beta

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.
Files changed (100) hide show
  1. package/.babelrc +12 -0
  2. package/.eslintignore +1 -0
  3. package/.eslintrc.js +38 -0
  4. package/.mocharc-integration.js +1 -0
  5. package/.mocharc.js +4 -0
  6. package/.prettierignore +1 -0
  7. package/.prettierrc +4 -0
  8. package/LEGAL.md +7 -0
  9. package/README.md +35 -0
  10. package/antlr/Java.g4 +1008 -0
  11. package/antlr/JavaLexer.g4 +241 -0
  12. package/antlr/JavaLexer_origin.g4 +241 -0
  13. package/antlr/JavaParser.g4 +765 -0
  14. package/antlr/JavaParser_origin.g4 +766 -0
  15. package/dist/package.json +73 -0
  16. package/dist/src/frontend/java/ASTBuilder.d.ts +171 -0
  17. package/dist/src/frontend/java/ASTBuilder.js +2369 -0
  18. package/dist/src/frontend/java/ASTBuilder.js.map +1 -0
  19. package/dist/src/frontend/java/ErrorListener.d.ts +9 -0
  20. package/dist/src/frontend/java/ErrorListener.js +24 -0
  21. package/dist/src/frontend/java/ErrorListener.js.map +1 -0
  22. package/dist/src/frontend/java/antlr/JavaLexer.d.ts +154 -0
  23. package/dist/src/frontend/java/antlr/JavaLexer.js +812 -0
  24. package/dist/src/frontend/java/antlr/JavaLexer.js.map +1 -0
  25. package/dist/src/frontend/java/antlr/JavaListener.d.ts +1393 -0
  26. package/dist/src/frontend/java/antlr/JavaListener.js +4 -0
  27. package/dist/src/frontend/java/antlr/JavaListener.js.map +1 -0
  28. package/dist/src/frontend/java/antlr/JavaParser.d.ts +2065 -0
  29. package/dist/src/frontend/java/antlr/JavaParser.js +15279 -0
  30. package/dist/src/frontend/java/antlr/JavaParser.js.map +1 -0
  31. package/dist/src/frontend/java/antlr/JavaVisitor.d.ts +892 -0
  32. package/dist/src/frontend/java/antlr/JavaVisitor.js +4 -0
  33. package/dist/src/frontend/java/antlr/JavaVisitor.js.map +1 -0
  34. package/dist/src/frontend/java/parser.d.ts +20 -0
  35. package/dist/src/frontend/java/parser.js +93 -0
  36. package/dist/src/frontend/java/parser.js.map +1 -0
  37. package/dist/src/frontend/java/testBuilder.d.ts +146 -0
  38. package/dist/src/frontend/java/testBuilder.js +464 -0
  39. package/dist/src/frontend/java/testBuilder.js.map +1 -0
  40. package/dist/src/frontend/java/types.d.ts +29 -0
  41. package/dist/src/frontend/java/types.js +3 -0
  42. package/dist/src/frontend/java/types.js.map +1 -0
  43. package/dist/src/frontend/javascript/parser.d.ts +8 -0
  44. package/dist/src/frontend/javascript/parser.js +1121 -0
  45. package/dist/src/frontend/javascript/parser.js.map +1 -0
  46. package/dist/src/index.d.ts +14 -0
  47. package/dist/src/index.js +76 -0
  48. package/dist/src/index.js.map +1 -0
  49. package/output.txt +3820 -0
  50. package/package.json +73 -0
  51. package/tests/benchmark/base/class.js +32 -0
  52. package/tests/benchmark/base/class.js.json +1809 -0
  53. package/tests/benchmark/base/class_test.js +13 -0
  54. package/tests/benchmark/base/class_test.js.json +435 -0
  55. package/tests/benchmark/base/comment.js +21 -0
  56. package/tests/benchmark/base/comment.js.json +1443 -0
  57. package/tests/benchmark/base/demo.ts +1060 -0
  58. package/tests/benchmark/base/empty.js +4 -0
  59. package/tests/benchmark/base/empty.js.json +202 -0
  60. package/tests/benchmark/base/export.js +36 -0
  61. package/tests/benchmark/base/export.js.json +6345 -0
  62. package/tests/benchmark/base/export1.js +2 -0
  63. package/tests/benchmark/base/export1.js.json +251 -0
  64. package/tests/benchmark/base/export2.js +35 -0
  65. package/tests/benchmark/base/export2.js.json +3297 -0
  66. package/tests/benchmark/base/expressions.js +25 -0
  67. package/tests/benchmark/base/expressions.js.json +1654 -0
  68. package/tests/benchmark/base/for.js +7 -0
  69. package/tests/benchmark/base/for.js.json +738 -0
  70. package/tests/benchmark/base/function.js +15 -0
  71. package/tests/benchmark/base/function.js.json +1458 -0
  72. package/tests/benchmark/base/import.js +3 -0
  73. package/tests/benchmark/base/import.js.json +416 -0
  74. package/tests/benchmark/base/literal.js +27 -0
  75. package/tests/benchmark/base/literal.js.json +1234 -0
  76. package/tests/benchmark/base/param.js +4 -0
  77. package/tests/benchmark/base/param.js.json +410 -0
  78. package/tests/benchmark/base/parseerror.js +1 -0
  79. package/tests/benchmark/base/parseerror.js.json +191 -0
  80. package/tests/benchmark/base/patternlike.js +24 -0
  81. package/tests/benchmark/base/patternlike.js.json +3720 -0
  82. package/tests/benchmark/base/react.js +8 -0
  83. package/tests/benchmark/base/react.js.json +211 -0
  84. package/tests/benchmark/base/switch.js +11 -0
  85. package/tests/benchmark/base/switch.js.json +741 -0
  86. package/tests/benchmark/base/taint.ts +61 -0
  87. package/tests/benchmark/base/temp.js +311 -0
  88. package/tests/benchmark/base/temp.js.json +22170 -0
  89. package/tests/benchmark/base/tmp.js +134 -0
  90. package/tests/benchmark/base/tmp.js.json +15409 -0
  91. package/tests/benchmark/base/vardecl.js +9 -0
  92. package/tests/benchmark/base/vardecl.js.json +491 -0
  93. package/tests/index.js +43 -0
  94. package/tests/index.ts +66 -0
  95. package/tests/test.d.ts +0 -0
  96. package/tests/test.js +38 -0
  97. package/tests/test.ts +60 -0
  98. package/tsconfig.json +24 -0
  99. package/tsconfig.test.json +13 -0
  100. package/webpack.config.js +21 -0
package/antlr/Java.g4 ADDED
@@ -0,0 +1,1008 @@
1
+ /*
2
+ [The "BSD licence"]
3
+ Copyright (c) 2013 Terence Parr, Sam Harwell
4
+ Copyright (c) 2017 Ivan Kochurkin (upgrade to Java 8)
5
+ Copyright (c) 2021 Michał Lorek (upgrade to Java 11)
6
+ Copyright (c) 2022 Michał Lorek (upgrade to Java 17)
7
+ All rights reserved.
8
+
9
+ Redistribution and use in source and binary forms, with or without
10
+ modification, are permitted provided that the following conditions
11
+ are met:
12
+ 1. Redistributions of source code must retain the above copyright
13
+ notice, this list of conditions and the following disclaimer.
14
+ 2. Redistributions in binary form must reproduce the above copyright
15
+ notice, this list of conditions and the following disclaimer in the
16
+ documentation and/or other materials provided with the distribution.
17
+ 3. The name of the author may not be used to endorse or promote products
18
+ derived from this software without specific prior written permission.
19
+
20
+ THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21
+ IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22
+ OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23
+ IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24
+ INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25
+ NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26
+ DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27
+ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29
+ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
+ */
31
+ grammar Java;
32
+
33
+ compilationUnit
34
+ : packageDeclaration? (importDeclaration | ';')* (typeDeclaration | ';')*
35
+ | moduleDeclaration EOF
36
+ ;
37
+
38
+ packageDeclaration
39
+ : annotation* PACKAGE qualifiedName ';'
40
+ ;
41
+
42
+ importDeclaration
43
+ : IMPORT STATIC? qualifiedName ('.' '*')? ';'
44
+ | ';'
45
+ ;
46
+
47
+ typeDeclaration
48
+ : classOrInterfaceModifier*
49
+ (classDeclaration | enumDeclaration | interfaceDeclaration | annotationTypeDeclaration | recordDeclaration)
50
+ ;
51
+
52
+ modifier
53
+ : classOrInterfaceModifier
54
+ | NATIVE
55
+ | SYNCHRONIZED
56
+ | TRANSIENT
57
+ | VOLATILE
58
+ ;
59
+
60
+ classOrInterfaceModifier
61
+ : annotation
62
+ | PUBLIC
63
+ | PROTECTED
64
+ | PRIVATE
65
+ | STATIC
66
+ | ABSTRACT
67
+ | FINAL // FINAL for class only -- does not apply to interfaces
68
+ | STRICTFP
69
+ | SEALED // Java17
70
+ | NON_SEALED // Java17
71
+ ;
72
+
73
+ variableModifier
74
+ : FINAL
75
+ | annotation
76
+ ;
77
+
78
+ classDeclaration
79
+ : CLASS identifier typeParameters?
80
+ (EXTENDS typeType)?
81
+ (IMPLEMENTS typeList)?
82
+ (PERMITS typeList)? // Java17
83
+ classBody
84
+ ;
85
+
86
+ typeParameters
87
+ : '<' typeParameter (',' typeParameter)* '>'
88
+ ;
89
+
90
+ typeParameter
91
+ : annotation* identifier (EXTENDS annotation* typeBound)?
92
+ ;
93
+
94
+ typeBound
95
+ : typeType ('&' typeType)*
96
+ ;
97
+
98
+ enumDeclaration
99
+ : ENUM identifier (IMPLEMENTS typeList)? '{' enumConstants? ','? enumBodyDeclarations? '}'
100
+ ;
101
+
102
+ enumConstants
103
+ : enumConstant (',' enumConstant)*
104
+ ;
105
+
106
+ enumConstant
107
+ : annotation* identifier arguments? classBody?
108
+ ;
109
+
110
+ enumBodyDeclarations
111
+ : ';' classBodyDeclaration*
112
+ ;
113
+
114
+ interfaceDeclaration
115
+ : INTERFACE identifier typeParameters? (EXTENDS typeList)? (PERMITS typeList)? interfaceBody
116
+ ;
117
+
118
+ classBody
119
+ : '{' classBodyDeclaration* '}'
120
+ ;
121
+
122
+ interfaceBody
123
+ : '{' interfaceBodyDeclaration* '}'
124
+ ;
125
+
126
+ classBodyDeclaration
127
+ : ';'
128
+ | STATIC? block
129
+ | modifier* memberDeclaration
130
+ ;
131
+
132
+ memberDeclaration
133
+ : recordDeclaration //Java17
134
+ | methodDeclaration
135
+ | genericMethodDeclaration
136
+ | fieldDeclaration
137
+ | constructorDeclaration
138
+ | genericConstructorDeclaration
139
+ | interfaceDeclaration
140
+ | annotationTypeDeclaration
141
+ | classDeclaration
142
+ | enumDeclaration
143
+ ;
144
+
145
+ /* We use rule this even for void methods which cannot have [] after parameters.
146
+ This simplifies grammar and we can consider void to be a type, which
147
+ renders the [] matching as a context-sensitive issue or a semantic check
148
+ for invalid return type after parsing.
149
+ */
150
+ methodDeclaration
151
+ : typeTypeOrVoid identifier formalParameters ('[' ']')*
152
+ (THROWS qualifiedNameList)?
153
+ methodBody
154
+ ;
155
+
156
+ methodBody
157
+ : block
158
+ | ';'
159
+ ;
160
+
161
+ typeTypeOrVoid
162
+ : typeType
163
+ | VOID
164
+ ;
165
+
166
+ genericMethodDeclaration
167
+ : typeParameters methodDeclaration
168
+ ;
169
+
170
+ genericConstructorDeclaration
171
+ : typeParameters constructorDeclaration
172
+ ;
173
+
174
+ constructorDeclaration
175
+ : identifier formalParameters (THROWS qualifiedNameList)? constructorBody=block
176
+ ;
177
+
178
+ compactConstructorDeclaration
179
+ : modifier* identifier constructorBody=block
180
+ ;
181
+
182
+ fieldDeclaration
183
+ : typeType variableDeclarators ';'
184
+ ;
185
+
186
+ interfaceBodyDeclaration
187
+ : modifier* interfaceMemberDeclaration
188
+ | ';'
189
+ ;
190
+
191
+ interfaceMemberDeclaration
192
+ : constDeclaration
193
+ | interfaceMethodDeclaration
194
+ | genericInterfaceMethodDeclaration
195
+ | interfaceDeclaration
196
+ | annotationTypeDeclaration
197
+ | classDeclaration
198
+ | enumDeclaration
199
+ | recordDeclaration // Java17
200
+ ;
201
+
202
+ constDeclaration
203
+ : typeType constantDeclarator (',' constantDeclarator)* ';'
204
+ ;
205
+
206
+ constantDeclarator
207
+ : identifier ('[' ']')* '=' variableInitializer
208
+ ;
209
+
210
+ // Early versions of Java allows brackets after the method name, eg.
211
+ // public int[] return2DArray() [] { ... }
212
+ // is the same as
213
+ // public int[][] return2DArray() { ... }
214
+ interfaceMethodDeclaration
215
+ : interfaceMethodModifier* interfaceCommonBodyDeclaration
216
+ ;
217
+
218
+ // Java8
219
+ interfaceMethodModifier
220
+ : annotation
221
+ | PUBLIC
222
+ | ABSTRACT
223
+ | DEFAULT
224
+ | STATIC
225
+ | STRICTFP
226
+ ;
227
+
228
+ genericInterfaceMethodDeclaration
229
+ : interfaceMethodModifier* typeParameters interfaceCommonBodyDeclaration
230
+ ;
231
+
232
+ interfaceCommonBodyDeclaration
233
+ : annotation* typeTypeOrVoid identifier formalParameters ('[' ']')* (THROWS qualifiedNameList)? methodBody
234
+ ;
235
+
236
+ variableDeclarators
237
+ : variableDeclarator (',' variableDeclarator)*
238
+ ;
239
+
240
+ variableDeclarator
241
+ : variableDeclaratorId ('=' variableInitializer)?
242
+ ;
243
+
244
+ variableDeclaratorId
245
+ : identifier ('[' ']')*
246
+ ;
247
+
248
+ variableInitializer
249
+ : arrayInitializer
250
+ | expression
251
+ ;
252
+
253
+ arrayInitializer
254
+ : '{' (variableInitializer (',' variableInitializer)* (',')? )? '}'
255
+ ;
256
+
257
+ classOrInterfaceType
258
+ : (identifier typeArguments? '.')* typeIdentifier typeArguments?
259
+ ;
260
+
261
+ typeArgument
262
+ : typeType
263
+ | annotation* '?' ((EXTENDS | SUPER) typeType)?
264
+ ;
265
+
266
+ qualifiedNameList
267
+ : qualifiedName (',' qualifiedName)*
268
+ ;
269
+
270
+ formalParameters
271
+ : '(' ( receiverParameter?
272
+ | receiverParameter (',' formalParameterList)?
273
+ | formalParameterList?
274
+ ) ')'
275
+ ;
276
+
277
+ receiverParameter
278
+ : typeType (identifier '.')* THIS
279
+ ;
280
+
281
+ formalParameterList
282
+ : formalParameter (',' formalParameter)* (',' lastFormalParameter)?
283
+ | lastFormalParameter
284
+ ;
285
+
286
+ formalParameter
287
+ : variableModifier* typeType variableDeclaratorId
288
+ ;
289
+
290
+ lastFormalParameter
291
+ : variableModifier* typeType annotation* '...' variableDeclaratorId
292
+ ;
293
+
294
+ // local variable type inference
295
+ lambdaLVTIList
296
+ : lambdaLVTIParameter (',' lambdaLVTIParameter)*
297
+ ;
298
+
299
+ lambdaLVTIParameter
300
+ : variableModifier* VAR identifier
301
+ ;
302
+
303
+ qualifiedName
304
+ : identifier ('.' identifier)*
305
+ ;
306
+
307
+ literal
308
+ : integerLiteral
309
+ | floatLiteral
310
+ | CHAR_LITERAL
311
+ | STRING_LITERAL
312
+ | BOOL_LITERAL
313
+ | NULL_LITERAL
314
+ | TEXT_BLOCK // Java17
315
+ ;
316
+
317
+ integerLiteral
318
+ : DECIMAL_LITERAL
319
+ | HEX_LITERAL
320
+ | OCT_LITERAL
321
+ | BINARY_LITERAL
322
+ ;
323
+
324
+ floatLiteral
325
+ : FLOAT_LITERAL
326
+ | HEX_FLOAT_LITERAL
327
+ ;
328
+
329
+ // ANNOTATIONS
330
+ altAnnotationQualifiedName
331
+ : (identifier DOT)* '@' identifier
332
+ ;
333
+
334
+ annotation
335
+ : ('@' qualifiedName | altAnnotationQualifiedName) ('(' ( elementValuePairs | elementValue )? ')')?
336
+ ;
337
+
338
+ elementValuePairs
339
+ : elementValuePair (',' elementValuePair)*
340
+ ;
341
+
342
+ elementValuePair
343
+ : identifier '=' elementValue
344
+ ;
345
+
346
+ elementValue
347
+ : expression
348
+ | annotation
349
+ | elementValueArrayInitializer
350
+ ;
351
+
352
+ elementValueArrayInitializer
353
+ : '{' (elementValue (',' elementValue)*)? (',')? '}'
354
+ ;
355
+
356
+ annotationTypeDeclaration
357
+ : '@' INTERFACE identifier annotationTypeBody
358
+ ;
359
+
360
+ annotationTypeBody
361
+ : '{' (annotationTypeElementDeclaration)* '}'
362
+ ;
363
+
364
+ annotationTypeElementDeclaration
365
+ : modifier* annotationTypeElementRest
366
+ | ';' // this is not allowed by the grammar, but apparently allowed by the actual compiler
367
+ ;
368
+
369
+ annotationTypeElementRest
370
+ : typeType annotationMethodOrConstantRest ';'
371
+ | classDeclaration ';'?
372
+ | interfaceDeclaration ';'?
373
+ | enumDeclaration ';'?
374
+ | annotationTypeDeclaration ';'?
375
+ | recordDeclaration ';'? // Java17
376
+ ;
377
+
378
+ annotationMethodOrConstantRest
379
+ : annotationMethodRest
380
+ | annotationConstantRest
381
+ ;
382
+
383
+ annotationMethodRest
384
+ : identifier '(' ')' defaultValue?
385
+ ;
386
+
387
+ annotationConstantRest
388
+ : variableDeclarators
389
+ ;
390
+
391
+ defaultValue
392
+ : DEFAULT elementValue
393
+ ;
394
+
395
+ // MODULES - Java9
396
+
397
+ moduleDeclaration
398
+ : OPEN? MODULE qualifiedName moduleBody
399
+ ;
400
+
401
+ moduleBody
402
+ : '{' moduleDirective* '}'
403
+ ;
404
+
405
+ moduleDirective
406
+ : REQUIRES requiresModifier* qualifiedName ';'
407
+ | EXPORTS qualifiedName (TO qualifiedName)? ';'
408
+ | OPENS qualifiedName (TO qualifiedName)? ';'
409
+ | USES qualifiedName ';'
410
+ | PROVIDES qualifiedName WITH qualifiedName ';'
411
+ ;
412
+
413
+ requiresModifier
414
+ : TRANSITIVE
415
+ | STATIC
416
+ ;
417
+
418
+ // RECORDS - Java 17
419
+
420
+ recordDeclaration
421
+ : RECORD identifier typeParameters? recordHeader
422
+ (IMPLEMENTS typeList)?
423
+ recordBody
424
+ ;
425
+
426
+ recordHeader
427
+ : '(' recordComponentList? ')'
428
+ ;
429
+
430
+ recordComponentList
431
+ : recordComponent (',' recordComponent)*
432
+ ;
433
+
434
+ recordComponent
435
+ : typeType identifier
436
+ ;
437
+
438
+ recordBody
439
+ : '{' (classBodyDeclaration | compactConstructorDeclaration)* '}'
440
+ ;
441
+
442
+ // STATEMENTS / BLOCKS
443
+
444
+ block
445
+ : '{' blockStatement* '}'
446
+ ;
447
+
448
+ blockStatement
449
+ : localVariableDeclaration ';'
450
+ | localTypeDeclaration
451
+ | statement
452
+ ;
453
+
454
+ localVariableDeclaration
455
+ : variableModifier* (VAR identifier '=' expression | typeType variableDeclarators)
456
+ ;
457
+
458
+ identifier
459
+ : IDENTIFIER
460
+ | MODULE
461
+ | OPEN
462
+ | REQUIRES
463
+ | EXPORTS
464
+ | OPENS
465
+ | TO
466
+ | USES
467
+ | PROVIDES
468
+ | WITH
469
+ | TRANSITIVE
470
+ | YIELD
471
+ | SEALED
472
+ | PERMITS
473
+ | RECORD
474
+ | VAR
475
+ ;
476
+
477
+ typeIdentifier // Identifiers that are not restricted for type declarations
478
+ : IDENTIFIER
479
+ | MODULE
480
+ | OPEN
481
+ | REQUIRES
482
+ | EXPORTS
483
+ | OPENS
484
+ | TO
485
+ | USES
486
+ | PROVIDES
487
+ | WITH
488
+ | TRANSITIVE
489
+ | SEALED
490
+ | PERMITS
491
+ | RECORD
492
+ ;
493
+
494
+ localTypeDeclaration
495
+ : classOrInterfaceModifier*
496
+ (classDeclaration | interfaceDeclaration | recordDeclaration)
497
+ ;
498
+
499
+ statement
500
+ : blockLabel=block
501
+ | ASSERT expression (':' expression)? ';'
502
+ | IF parExpression statement (ELSE statement)?
503
+ | FOR '(' forControl ')' statement
504
+ | WHILE parExpression statement
505
+ | DO statement WHILE parExpression ';'
506
+ | TRY block (catchClause+ finallyBlock? | finallyBlock)
507
+ | TRY resourceSpecification block catchClause* finallyBlock?
508
+ | SWITCH parExpression '{' switchBlockStatementGroup* switchLabel* '}'
509
+ | SYNCHRONIZED parExpression block
510
+ | RETURN expression? ';'
511
+ | THROW expression ';'
512
+ | BREAK identifier? ';'
513
+ | CONTINUE identifier? ';'
514
+ | YIELD expression ';' // Java17
515
+ | SEMI
516
+ | statementExpression=expression ';'
517
+ | switchExpression ';'? // Java17
518
+ | identifierLabel=identifier ':' statement
519
+ ;
520
+
521
+ catchClause
522
+ : CATCH '(' variableModifier* catchType identifier ')' block
523
+ ;
524
+
525
+ catchType
526
+ : qualifiedName ('|' qualifiedName)*
527
+ ;
528
+
529
+ finallyBlock
530
+ : FINALLY block
531
+ ;
532
+
533
+ resourceSpecification
534
+ : '(' resources ';'? ')'
535
+ ;
536
+
537
+ resources
538
+ : resource (';' resource)*
539
+ ;
540
+
541
+ resource
542
+ : variableModifier* ( classOrInterfaceType variableDeclaratorId | VAR identifier ) '=' expression
543
+ | qualifiedName
544
+ ;
545
+
546
+ /** Matches cases then statements, both of which are mandatory.
547
+ * To handle empty cases at the end, we add switchLabel* to statement.
548
+ */
549
+ switchBlockStatementGroup
550
+ : switchLabel+ blockStatement+
551
+ ;
552
+
553
+ switchLabel
554
+ : CASE (constantExpression=expression | enumConstantName=IDENTIFIER | typeType varName=identifier) ':'
555
+ | DEFAULT ':'
556
+ ;
557
+
558
+ forControl
559
+ : enhancedForControl
560
+ | forInit? ';' expression? ';' forUpdate=expressionList?
561
+ ;
562
+
563
+ forInit
564
+ : localVariableDeclaration
565
+ | expressionList
566
+ ;
567
+
568
+ enhancedForControl
569
+ : variableModifier* (typeType | VAR) variableDeclaratorId ':' expression
570
+ ;
571
+
572
+ // EXPRESSIONS
573
+
574
+ parExpression
575
+ : '(' expression ')'
576
+ ;
577
+
578
+ expressionList
579
+ : expression (',' expression)*
580
+ ;
581
+
582
+ methodCall
583
+ : identifier '(' expressionList? ')'
584
+ | THIS '(' expressionList? ')'
585
+ | SUPER '(' expressionList? ')'
586
+ ;
587
+
588
+ expression
589
+ : primary
590
+ | expression bop='.'
591
+ (
592
+ identifier
593
+ | methodCall
594
+ | THIS
595
+ | NEW nonWildcardTypeArguments? innerCreator
596
+ | SUPER superSuffix
597
+ | explicitGenericInvocation
598
+ )
599
+ | expression '[' expression ']'
600
+ | methodCall
601
+ | NEW creator
602
+ | '(' annotation* typeType ('&' typeType)* ')' expression
603
+ | expression postfix=('++' | '--')
604
+ | prefix=('+'|'-'|'++'|'--') expression
605
+ | prefix=('~'|'!') expression
606
+ | expression bop=('*'|'/'|'%') expression
607
+ | expression bop=('+'|'-') expression
608
+ | expression ('<' '<' | '>' '>' '>' | '>' '>') expression
609
+ | expression bop=('<=' | '>=' | '>' | '<') expression
610
+ | expression bop=INSTANCEOF (typeType | pattern)
611
+ | expression bop=('==' | '!=') expression
612
+ | expression bop='&' expression
613
+ | expression bop='^' expression
614
+ | expression bop='|' expression
615
+ | expression bop='&&' expression
616
+ | expression bop='||' expression
617
+ | <assoc=right> expression bop='?' expression ':' expression
618
+ | <assoc=right> expression
619
+ bop=('=' | '+=' | '-=' | '*=' | '/=' | '&=' | '|=' | '^=' | '>>=' | '>>>=' | '<<=' | '%=')
620
+ expression
621
+ | lambdaExpression // Java8
622
+ | switchExpression // Java17
623
+
624
+ // Java 8 methodReference
625
+ | expression '::' typeArguments? identifier
626
+ | typeType '::' (typeArguments? identifier | NEW)
627
+ | classType '::' typeArguments? NEW
628
+ ;
629
+
630
+ // Java17
631
+ pattern
632
+ : variableModifier* typeType annotation* identifier
633
+ ;
634
+
635
+ // Java8
636
+ lambdaExpression
637
+ : lambdaParameters '->' lambdaBody
638
+ ;
639
+
640
+ // Java8
641
+ lambdaParameters
642
+ : identifier
643
+ | '(' formalParameterList? ')'
644
+ | '(' identifier (',' identifier)* ')'
645
+ | '(' lambdaLVTIList? ')'
646
+ ;
647
+
648
+ // Java8
649
+ lambdaBody
650
+ : expression
651
+ | block
652
+ ;
653
+
654
+ primary
655
+ : '(' expression ')'
656
+ | THIS
657
+ | SUPER
658
+ | literal
659
+ | identifier
660
+ | typeTypeOrVoid '.' CLASS
661
+ | nonWildcardTypeArguments (explicitGenericInvocationSuffix | THIS arguments)
662
+ ;
663
+
664
+ // Java17
665
+ switchExpression
666
+ : SWITCH parExpression '{' switchLabeledRule* '}'
667
+ ;
668
+
669
+ // Java17
670
+ switchLabeledRule
671
+ : CASE (expressionList | NULL_LITERAL | guardedPattern) (ARROW | COLON) switchRuleOutcome
672
+ | DEFAULT (ARROW | COLON) switchRuleOutcome
673
+ ;
674
+
675
+ // Java17
676
+ guardedPattern
677
+ : '(' guardedPattern ')'
678
+ | variableModifier* typeType annotation* identifier ('&&' expression)*
679
+ | guardedPattern '&&' expression
680
+ ;
681
+
682
+ // Java17
683
+ switchRuleOutcome
684
+ : block
685
+ | blockStatement*
686
+ ;
687
+
688
+ classType
689
+ : (classOrInterfaceType '.')? annotation* identifier typeArguments?
690
+ ;
691
+
692
+ creator
693
+ : nonWildcardTypeArguments createdName classCreatorRest
694
+ | createdName (arrayCreatorRest | classCreatorRest)
695
+ ;
696
+
697
+ createdName
698
+ : identifier typeArgumentsOrDiamond? ('.' identifier typeArgumentsOrDiamond?)*
699
+ | primitiveType
700
+ ;
701
+
702
+ innerCreator
703
+ : identifier nonWildcardTypeArgumentsOrDiamond? classCreatorRest
704
+ ;
705
+
706
+ arrayCreatorRest
707
+ : '[' (']' ('[' ']')* arrayInitializer | expression ']' ('[' expression ']')* ('[' ']')*)
708
+ ;
709
+
710
+ classCreatorRest
711
+ : arguments classBody?
712
+ ;
713
+
714
+ explicitGenericInvocation
715
+ : nonWildcardTypeArguments explicitGenericInvocationSuffix
716
+ ;
717
+
718
+ typeArgumentsOrDiamond
719
+ : '<' '>'
720
+ | typeArguments
721
+ ;
722
+
723
+ nonWildcardTypeArgumentsOrDiamond
724
+ : '<' '>'
725
+ | nonWildcardTypeArguments
726
+ ;
727
+
728
+ nonWildcardTypeArguments
729
+ : '<' typeList '>'
730
+ ;
731
+
732
+ typeList
733
+ : typeType (',' typeType)*
734
+ ;
735
+
736
+ typeType
737
+ : annotation* (classOrInterfaceType | primitiveType) (annotation* '[' ']')*
738
+ ;
739
+
740
+ primitiveType
741
+ : BOOLEAN
742
+ | CHAR
743
+ | BYTE
744
+ | SHORT
745
+ | INT
746
+ | LONG
747
+ | FLOAT
748
+ | DOUBLE
749
+ ;
750
+
751
+ typeArguments
752
+ : '<' typeArgument (',' typeArgument)* '>'
753
+ ;
754
+
755
+ superSuffix
756
+ : arguments
757
+ | '.' typeArguments? identifier arguments?
758
+ ;
759
+
760
+ explicitGenericInvocationSuffix
761
+ : SUPER superSuffix
762
+ | identifier arguments
763
+ ;
764
+
765
+ arguments
766
+ : '(' expressionList? ')'
767
+ ;
768
+ /*
769
+ [The "BSD licence"]
770
+ Copyright (c) 2013 Terence Parr, Sam Harwell
771
+ Copyright (c) 2017 Ivan Kochurkin (upgrade to Java 8)
772
+ Copyright (c) 2021 Michał Lorek (upgrade to Java 11)
773
+ Copyright (c) 2022 Michał Lorek (upgrade to Java 17)
774
+ All rights reserved.
775
+
776
+ Redistribution and use in source and binary forms, with or without
777
+ modification, are permitted provided that the following conditions
778
+ are met:
779
+ 1. Redistributions of source code must retain the above copyright
780
+ notice, this list of conditions and the following disclaimer.
781
+ 2. Redistributions in binary form must reproduce the above copyright
782
+ notice, this list of conditions and the following disclaimer in the
783
+ documentation and/or other materials provided with the distribution.
784
+ 3. The name of the author may not be used to endorse or promote products
785
+ derived from this software without specific prior written permission.
786
+
787
+ THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
788
+ IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
789
+ OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
790
+ IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
791
+ INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
792
+ NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
793
+ DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
794
+ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
795
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
796
+ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
797
+ */
798
+
799
+
800
+
801
+ // Keywords
802
+
803
+ ABSTRACT: 'abstract';
804
+ ASSERT: 'assert';
805
+ BOOLEAN: 'boolean';
806
+ BREAK: 'break';
807
+ BYTE: 'byte';
808
+ CASE: 'case';
809
+ CATCH: 'catch';
810
+ CHAR: 'char';
811
+ CLASS: 'class';
812
+ CONST: 'const';
813
+ CONTINUE: 'continue';
814
+ DEFAULT: 'default';
815
+ DO: 'do';
816
+ DOUBLE: 'double';
817
+ ELSE: 'else';
818
+ ENUM: 'enum';
819
+ EXTENDS: 'extends';
820
+ FINAL: 'final';
821
+ FINALLY: 'finally';
822
+ FLOAT: 'float';
823
+ FOR: 'for';
824
+ IF: 'if';
825
+ GOTO: 'goto';
826
+ IMPLEMENTS: 'implements';
827
+ IMPORT: 'import';
828
+ INSTANCEOF: 'instanceof';
829
+ INT: 'int';
830
+ INTERFACE: 'interface';
831
+ LONG: 'long';
832
+ NATIVE: 'native';
833
+ NEW: 'new';
834
+ PACKAGE: 'package';
835
+ PRIVATE: 'private';
836
+ PROTECTED: 'protected';
837
+ PUBLIC: 'public';
838
+ RETURN: 'return';
839
+ SHORT: 'short';
840
+ STATIC: 'static';
841
+ STRICTFP: 'strictfp';
842
+ SUPER: 'super';
843
+ SWITCH: 'switch';
844
+ SYNCHRONIZED: 'synchronized';
845
+ THIS: 'this';
846
+ THROW: 'throw';
847
+ THROWS: 'throws';
848
+ TRANSIENT: 'transient';
849
+ TRY: 'try';
850
+ VOID: 'void';
851
+ VOLATILE: 'volatile';
852
+ WHILE: 'while';
853
+
854
+ // Module related keywords
855
+ MODULE: 'module';
856
+ OPEN: 'open';
857
+ REQUIRES: 'requires';
858
+ EXPORTS: 'exports';
859
+ OPENS: 'opens';
860
+ TO: 'to';
861
+ USES: 'uses';
862
+ PROVIDES: 'provides';
863
+ WITH: 'with';
864
+ TRANSITIVE: 'transitive';
865
+
866
+ // Local Variable Type Inference
867
+ VAR: 'var'; // reserved type name
868
+
869
+ // Switch Expressions
870
+ YIELD: 'yield'; // reserved type name from Java 14
871
+
872
+ // Records
873
+ RECORD: 'record';
874
+
875
+ // Sealed Classes
876
+ SEALED: 'sealed';
877
+ PERMITS: 'permits';
878
+ NON_SEALED: 'non-sealed';
879
+
880
+ // Literals
881
+
882
+ DECIMAL_LITERAL: ('0' | [1-9] (Digits? | '_'+ Digits)) [lL]?;
883
+ HEX_LITERAL: '0' [xX] [0-9a-fA-F] ([0-9a-fA-F_]* [0-9a-fA-F])? [lL]?;
884
+ OCT_LITERAL: '0' '_'* [0-7] ([0-7_]* [0-7])? [lL]?;
885
+ BINARY_LITERAL: '0' [bB] [01] ([01_]* [01])? [lL]?;
886
+
887
+ FLOAT_LITERAL: (Digits '.' Digits? | '.' Digits) ExponentPart? [fFdD]?
888
+ | Digits (ExponentPart [fFdD]? | [fFdD])
889
+ ;
890
+
891
+ HEX_FLOAT_LITERAL: '0' [xX] (HexDigits '.'? | HexDigits? '.' HexDigits) [pP] [+-]? Digits [fFdD]?;
892
+
893
+ BOOL_LITERAL: 'true'
894
+ | 'false'
895
+ ;
896
+
897
+ CHAR_LITERAL: '\'' (~['\\\r\n] | EscapeSequence) '\'';
898
+
899
+ STRING_LITERAL: '"' (~["\\\r\n] | EscapeSequence)* '"';
900
+
901
+ TEXT_BLOCK: '"""' [ \t]* [\r\n] (. | EscapeSequence)*? '"""';
902
+
903
+ NULL_LITERAL: 'null';
904
+
905
+ // Separators
906
+
907
+ LPAREN: '(';
908
+ RPAREN: ')';
909
+ LBRACE: '{';
910
+ RBRACE: '}';
911
+ LBRACK: '[';
912
+ RBRACK: ']';
913
+ SEMI: ';';
914
+ COMMA: ',';
915
+ DOT: '.';
916
+
917
+ // Operators
918
+
919
+ ASSIGN: '=';
920
+ GT: '>';
921
+ LT: '<';
922
+ BANG: '!';
923
+ TILDE: '~';
924
+ QUESTION: '?';
925
+ COLON: ':';
926
+ EQUAL: '==';
927
+ LE: '<=';
928
+ GE: '>=';
929
+ NOTEQUAL: '!=';
930
+ AND: '&&';
931
+ OR: '||';
932
+ INC: '++';
933
+ DEC: '--';
934
+ ADD: '+';
935
+ SUB: '-';
936
+ MUL: '*';
937
+ DIV: '/';
938
+ BITAND: '&';
939
+ BITOR: '|';
940
+ CARET: '^';
941
+ MOD: '%';
942
+
943
+ ADD_ASSIGN: '+=';
944
+ SUB_ASSIGN: '-=';
945
+ MUL_ASSIGN: '*=';
946
+ DIV_ASSIGN: '/=';
947
+ AND_ASSIGN: '&=';
948
+ OR_ASSIGN: '|=';
949
+ XOR_ASSIGN: '^=';
950
+ MOD_ASSIGN: '%=';
951
+ LSHIFT_ASSIGN: '<<=';
952
+ RSHIFT_ASSIGN: '>>=';
953
+ URSHIFT_ASSIGN: '>>>=';
954
+
955
+ // Java 8 tokens
956
+
957
+ ARROW: '->';
958
+ COLONCOLON: '::';
959
+
960
+ // Additional symbols not defined in the lexical specification
961
+
962
+ AT: '@';
963
+ ELLIPSIS: '...';
964
+
965
+ // Whitespace and comments
966
+
967
+ WS: [ \t\r\n\u000C]+ -> channel(HIDDEN);
968
+ COMMENT: '/*' .*? '*/' -> channel(HIDDEN);
969
+ LINE_COMMENT: '//' ~[\r\n]* -> channel(HIDDEN);
970
+
971
+ // Identifiers
972
+
973
+ IDENTIFIER: Letter LetterOrDigit*;
974
+
975
+ // Fragment rules
976
+
977
+ fragment ExponentPart
978
+ : [eE] [+-]? Digits
979
+ ;
980
+
981
+ fragment EscapeSequence
982
+ : '\\' 'u005c'? [btnfr"'\\]
983
+ | '\\' 'u005c'? ([0-3]? [0-7])? [0-7]
984
+ | '\\' 'u'+ HexDigit HexDigit HexDigit HexDigit
985
+ ;
986
+
987
+ fragment HexDigits
988
+ : HexDigit ((HexDigit | '_')* HexDigit)?
989
+ ;
990
+
991
+ fragment HexDigit
992
+ : [0-9a-fA-F]
993
+ ;
994
+
995
+ fragment Digits
996
+ : [0-9] ([0-9_]* [0-9])?
997
+ ;
998
+
999
+ fragment LetterOrDigit
1000
+ : Letter
1001
+ | [0-9]
1002
+ ;
1003
+
1004
+ fragment Letter
1005
+ : [a-zA-Z$_] // these are the "java letters" below 0x7F
1006
+ | ~[\u0000-\u007F\uD800-\uDBFF] // covers all characters above 0x7F which are not a surrogate
1007
+ | [\uD800-\uDBFF] [\uDC00-\uDFFF] // covers UTF-16 surrogate pairs encodings for U+10000 to U+10FFFF
1008
+ ;