-
Notifications
You must be signed in to change notification settings - Fork 0
/
Java9.g4
662 lines (584 loc) · 23.8 KB
/
Java9.g4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
grammar Java9;
literal : IntegerLiteral | FloatingPointLiteral | BooleanLiteral | CharacterLiteral | StringLiteral | NullLiteral ;
type : primitiveType | referenceType ;
primitiveType : annotation* numericType | annotation* 'boolean' ;
numericType : integralType | floatingPointType ;
integralType : 'byte' | 'short' | 'int' | 'long' | 'char' ;
floatingPointType : 'float' | 'double' ;
referenceType : classOrInterfaceType | typeVariable | arrayType ;
classOrInterfaceType : classType | interfaceType ;
classType : annotation* identifier typeArguments? | classOrInterfaceType '.' annotation* identifier typeArguments? ;
interfaceType : classType ;
typeVariable : annotation* identifier ;
arrayType : primitiveType dims | classOrInterfaceType dims | typeVariable dims ;
dims : annotation* '[' ']' ( annotation* '[' ']' )* ;
typeParameter : typeParameterModifier* identifier typeBound? ;
typeParameterModifier : annotation ;
typeBound : 'extends' typeVariable | 'extends' classOrInterfaceType additionalBound* ;
additionalBound : '&' interfaceType ;
typeArguments : '<' typeArgumentList '>' ;
typeArgumentList : typeArgument ( ',' typeArgument )* ;
typeArgument : referenceType | wildcard ;
wildcard : annotation* '?' wildcardBounds? ;
wildcardBounds : 'extends' referenceType | 'super' referenceType ;
moduleName : identifier | moduleName '.' identifier ;
packageName : identifier | packageName '.' identifier ;
typeName : identifier | packageOrTypeName '.' identifier ;
expressionName : identifier | ambiguousName '.' identifier ;
methodName : identifier ;
packageOrTypeName : identifier | packageOrTypeName '.' identifier ;
ambiguousName : identifier | ambiguousName '.' identifier ;
compilationUnit : ordinaryCompilationUnit | modularCompilationUnit ;
ordinaryCompilationUnit : packageDeclaration? importDeclaration* typeDeclaration* ;
modularCompilationUnit : importDeclaration* moduleDeclaration ;
packageDeclaration : packageModifier* 'package' identifier ( '.' identifier )* ';' ;
packageModifier : annotation ;
importDeclaration : singleTypeImportDeclaration | typeImportOnDemandDeclaration | singleStaticImportDeclaration | staticImportOnDemandDeclaration ;
singleTypeImportDeclaration : 'import' typeName ';' ;
typeImportOnDemandDeclaration : 'import' packageOrTypeName '.' '*' ';' ;
singleStaticImportDeclaration : 'import' 'static' typeName '.' identifier ';' ;
staticImportOnDemandDeclaration : 'import' 'static' typeName '.' '*' ';' ;
typeDeclaration : classDeclaration | interfaceDeclaration | ';' ;
moduleDeclaration : annotation* 'open'? 'module' identifier ( '.' identifier )* '{' moduleDirective* '}' ;
moduleDirective : 'requires' requiresModifier* moduleName ';' | 'exports' packageName ( 'to' moduleName ( ',' moduleName )* )? ';' | 'opens' packageName ( 'to' moduleName ( ',' moduleName )* )? ';' | 'uses' typeName ';' | 'provides' typeName 'with' typeName ( ',' typeName )* ';' ;
requiresModifier : 'transitive' | 'static' ;
classDeclaration : normalClassDeclaration | enumDeclaration ;
normalClassDeclaration : classModifier* 'class' identifier typeParameters? superclass? superinterfaces? classBody ;
classModifier : annotation | 'public' | 'protected' | 'private' | 'abstract' | 'static' | 'final' | 'strictfp' ;
typeParameters : '<' typeParameterList '>' ;
typeParameterList : typeParameter ( ',' typeParameter )* ;
superclass : 'extends' classType ;
superinterfaces : 'implements' interfaceTypeList ;
interfaceTypeList : interfaceType ( ',' interfaceType )* ;
classBody : '{' classBodyDeclaration* '}' ;
classBodyDeclaration : classMemberDeclaration | instanceInitializer | staticInitializer | constructorDeclaration ;
classMemberDeclaration : fieldDeclaration | methodDeclaration | classDeclaration | interfaceDeclaration | ';' ;
fieldDeclaration : fieldModifier* unannType variableDeclaratorList ';' ;
fieldModifier : annotation | 'public' | 'protected' | 'private' | 'static' | 'final' | 'transient' | 'volatile' ;
variableDeclaratorList : variableDeclarator ( ',' variableDeclarator )* ;
variableDeclarator : variableDeclaratorId ( '=' variableInitializer )? ;
variableDeclaratorId : identifier dims? ;
variableInitializer : expression | arrayInitializer ;
unannType : unannPrimitiveType | unannReferenceType ;
unannPrimitiveType : numericType | 'boolean' ;
unannReferenceType : unannClassOrInterfaceType | unannTypeVariable | unannArrayType ;
unannClassOrInterfaceType : unannClassType | unannInterfaceType ;
unannClassType : identifier typeArguments? | unannClassOrInterfaceType '.' annotation* identifier typeArguments? ;
unannInterfaceType : unannClassType ;
unannTypeVariable : identifier ;
unannArrayType : unannPrimitiveType dims | unannClassOrInterfaceType dims | unannTypeVariable dims ;
methodDeclaration : methodModifier* methodHeader methodBody ;
methodModifier : annotation | 'public' | 'protected' | 'private' | 'abstract' | 'static' | 'final' | 'synchronized' | 'native' | 'strictfp' ;
methodHeader : result methodDeclarator throws_? | typeParameters annotation* result methodDeclarator throws_? ;
result : unannType | 'void' ;
methodDeclarator : identifier '(' formalParameterList? ')' dims? ;
formalParameterList : receiverParameter | formalParameters ',' lastFormalParameter | lastFormalParameter ;
formalParameters : formalParameter ( ',' formalParameter )* | receiverParameter ( ',' formalParameter )* ;
formalParameter : variableModifier* unannType variableDeclaratorId ;
variableModifier : annotation | 'final' ;
lastFormalParameter : variableModifier* unannType annotation* '...' variableDeclaratorId | formalParameter ;
receiverParameter : annotation* unannType ( identifier '.' )? 'this' ;
throws_ : 'throws' exceptionTypeList ;
exceptionTypeList : exceptionType ( ',' exceptionType )* ;
exceptionType : classType | typeVariable ;
methodBody : block | ';' ;
instanceInitializer : block ;
staticInitializer : 'static' block ;
constructorDeclaration : constructorModifier* constructorDeclarator throws_? constructorBody ;
constructorModifier : annotation | 'public' | 'protected' | 'private' ;
constructorDeclarator : typeParameters? simpleTypeName '(' formalParameterList? ')' ;
simpleTypeName : identifier ;
constructorBody : '{' explicitConstructorInvocation? blockStatements? '}' ;
explicitConstructorInvocation : typeArguments? 'this' '(' argumentList? ')' ';' | typeArguments? 'super' '(' argumentList? ')' ';' | expressionName '.' typeArguments? 'super' '(' argumentList? ')' ';' | primary '.' typeArguments? 'super' '(' argumentList? ')' ';' ;
enumDeclaration : classModifier* 'enum' identifier superinterfaces? enumBody ;
enumBody : '{' enumConstantList? ','? enumBodyDeclarations? '}' ;
enumConstantList : enumConstant ( ',' enumConstant )* ;
enumConstant : enumConstantModifier* identifier ( '(' argumentList? ')' )? classBody? ;
enumConstantModifier : annotation ;
enumBodyDeclarations : ';' classBodyDeclaration* ;
interfaceDeclaration : normalInterfaceDeclaration | annotationTypeDeclaration ;
normalInterfaceDeclaration : interfaceModifier* 'interface' identifier typeParameters? extendsInterfaces? interfaceBody ;
interfaceModifier : annotation | 'public' | 'protected' | 'private' | 'abstract' | 'static' | 'strictfp' ;
extendsInterfaces : 'extends' interfaceTypeList ;
interfaceBody : '{' interfaceMemberDeclaration* '}' ;
interfaceMemberDeclaration : constantDeclaration | interfaceMethodDeclaration | classDeclaration | interfaceDeclaration | ';' ;
constantDeclaration : constantModifier* unannType variableDeclaratorList ';' ;
constantModifier : annotation | 'public' | 'static' | 'final' ;
interfaceMethodDeclaration : interfaceMethodModifier* methodHeader methodBody ;
interfaceMethodModifier : annotation | 'public' | 'private' | 'abstract' | 'default' | 'static' | 'strictfp' ;
annotationTypeDeclaration : interfaceModifier* '@' 'interface' identifier annotationTypeBody ;
annotationTypeBody : '{' annotationTypeMemberDeclaration* '}' ;
annotationTypeMemberDeclaration : annotationTypeElementDeclaration | constantDeclaration | classDeclaration | interfaceDeclaration | ';' ;
annotationTypeElementDeclaration : annotationTypeElementModifier* unannType identifier '(' ')' dims? defaultValue? ';' ;
annotationTypeElementModifier : annotation | 'public' | 'abstract' ;
defaultValue : 'default' elementValue ;
annotation : normalAnnotation | markerAnnotation | singleElementAnnotation ;
normalAnnotation : '@' typeName '(' elementValuePairList? ')' ;
elementValuePairList : elementValuePair ( ',' elementValuePair )* ;
elementValuePair : identifier '=' elementValue ;
elementValue : conditionalExpression | elementValueArrayInitializer | annotation ;
elementValueArrayInitializer : '{' elementValueList? ','? '}' ;
elementValueList : elementValue ( ',' elementValue )* ;
markerAnnotation : '@' typeName ;
singleElementAnnotation : '@' typeName '(' elementValue ')' ;
arrayInitializer : '{' variableInitializerList? ','? '}' ;
variableInitializerList : variableInitializer ( ',' variableInitializer )* ;
block : '{' blockStatements? '}' ;
blockStatements : blockStatement blockStatement* ;
blockStatement : localVariableDeclarationStatement | classDeclaration | statement ;
localVariableDeclarationStatement : localVariableDeclaration ';' ;
localVariableDeclaration : variableModifier* unannType variableDeclaratorList ;
statement : statementWithoutTrailingSubstatement | labeledStatement | ifThenStatement | ifThenElseStatement | whileStatement | forStatement ;
statementNoShortIf : statementWithoutTrailingSubstatement | labeledStatementNoShortIf | ifThenElseStatementNoShortIf | whileStatementNoShortIf | forStatementNoShortIf ;
statementWithoutTrailingSubstatement : block | emptyStatement | expressionStatement | assertStatement | switchStatement | doStatement | breakStatement | continueStatement | returnStatement | synchronizedStatement | throwStatement | tryStatement ;
emptyStatement : ';' ;
labeledStatement : identifier ':' statement ;
labeledStatementNoShortIf : identifier ':' statementNoShortIf ;
expressionStatement : statementExpression ';' ;
statementExpression : assignment | preIncrementExpression | preDecrementExpression | postIncrementExpression | postDecrementExpression | methodInvocation | classInstanceCreationExpression ;
ifThenStatement : 'if' '(' expression ')' statement ;
ifThenElseStatement : 'if' '(' expression ')' statementNoShortIf 'else' statement ;
ifThenElseStatementNoShortIf : 'if' '(' expression ')' statementNoShortIf 'else' statementNoShortIf ;
assertStatement : 'assert' expression ';' | 'assert' expression ':' expression ';' ;
switchStatement : 'switch' '(' expression ')' switchBlock ;
switchBlock : '{' switchBlockStatementGroup* switchLabel* '}' ;
switchBlockStatementGroup : switchLabels blockStatements ;
switchLabels : switchLabel switchLabel* ;
switchLabel : 'case' constantExpression ':' | 'case' enumConstantName ':' | 'default' ':' ;
enumConstantName : identifier ;
whileStatement : 'while' '(' expression ')' statement ;
whileStatementNoShortIf : 'while' '(' expression ')' statementNoShortIf ;
doStatement : 'do' statement 'while' '(' expression ')' ';' ;
forStatement : basicForStatement | enhancedForStatement ;
forStatementNoShortIf : basicForStatementNoShortIf | enhancedForStatementNoShortIf ;
basicForStatement : 'for' '(' forInit? ';' expression? ';' forUpdate? ')' statement ;
basicForStatementNoShortIf : 'for' '(' forInit? ';' expression? ';' forUpdate? ')' statementNoShortIf ;
forInit : statementExpressionList | localVariableDeclaration ;
forUpdate : statementExpressionList ;
statementExpressionList : statementExpression ( ',' statementExpression )* ;
enhancedForStatement : 'for' '(' variableModifier* unannType variableDeclaratorId ':' expression ')' statement ;
enhancedForStatementNoShortIf : 'for' '(' variableModifier* unannType variableDeclaratorId ':' expression ')' statementNoShortIf ;
breakStatement : 'break' identifier? ';' ;
continueStatement : 'continue' identifier? ';' ;
returnStatement : 'return' expression? ';' ;
throwStatement : 'throw' expression ';' ;
synchronizedStatement : 'synchronized' '(' expression ')' block ;
tryStatement : 'try' block catches | 'try' block catches? finally_ | tryWithResourcesStatement ;
catches : catchClause catchClause* ;
catchClause : 'catch' '(' catchFormalParameter ')' block ;
catchFormalParameter : variableModifier* catchType variableDeclaratorId ;
catchType : unannClassType ( '|' classType )* ;
finally_ : 'finally' block ;
tryWithResourcesStatement : 'try' resourceSpecification block catches? finally_? ;
resourceSpecification : '(' resourceList ';'? ')' ;
resourceList : resource ( ';' resource )* ;
resource : variableModifier* unannType variableDeclaratorId '=' expression | variableAccess ;
primary : primaryNoNewArray | arrayCreationExpression ;
primaryNoNewArray : literal | classLiteral | 'this' | typeName '.' 'this' | '(' expression ')' | classInstanceCreationExpression | fieldAccess | arrayAccess | methodInvocation | methodReference ;
classLiteral : typeName ( '[' ']' )* '.' 'class' | numericType ( '[' ']' )* '.' 'class' | 'boolean' ( '[' ']' )* '.' 'class' | 'void' '.' 'class' ;
classInstanceCreationExpression : unqualifiedClassInstanceCreationExpression | expressionName '.' unqualifiedClassInstanceCreationExpression | primary '.' unqualifiedClassInstanceCreationExpression ;
unqualifiedClassInstanceCreationExpression : 'new' typeArguments? classOrInterfaceTypeToInstantiate '(' argumentList? ')' classBody? ;
classOrInterfaceTypeToInstantiate : annotation* identifier ( '.' annotation* identifier )* typeArgumentsOrDiamond? ;
typeArgumentsOrDiamond : typeArguments | '<>' ;
fieldAccess : primary '.' identifier | 'super' '.' identifier | typeName '.' 'super' '.' identifier ;
arrayAccess : expressionName '[' expression ']' | primaryNoNewArray '[' expression ']' ;
methodInvocation : methodName '(' argumentList? ')' | typeName '.' typeArguments? identifier '(' argumentList? ')' | expressionName '.' typeArguments? identifier '(' argumentList? ')' | primary '.' typeArguments? identifier '(' argumentList? ')' | 'super' '.' typeArguments? identifier '(' argumentList? ')' | typeName '.' 'super' '.' typeArguments? identifier '(' argumentList? ')' ;
argumentList : expression ( ',' expression )* ;
methodReference : expressionName '::' typeArguments? identifier | primary '::' typeArguments? identifier | referenceType '::' typeArguments? identifier | 'super' '::' typeArguments? identifier | typeName '.' 'super' '::' typeArguments? identifier | classType '::' typeArguments? 'new' | arrayType '::' 'new' ;
arrayCreationExpression : 'new' primitiveType dimExprs dims? | 'new' classOrInterfaceType dimExprs dims? | 'new' primitiveType dims arrayInitializer | 'new' classOrInterfaceType dims arrayInitializer ;
dimExprs : dimExpr dimExpr* ;
dimExpr : annotation* '[' expression ']' ;
expression : lambdaExpression | assignmentExpression ;
lambdaExpression : lambdaParameters '->' lambdaBody ;
lambdaParameters : identifier | '(' formalParameterList? ')' | '(' inferredFormalParameterList ')' ;
inferredFormalParameterList : identifier ( ',' identifier )* ;
lambdaBody : expression | block ;
assignmentExpression : conditionalExpression | assignment ;
assignment : leftHandSide assignmentOperator expression ;
leftHandSide : expressionName | fieldAccess | arrayAccess ;
assignmentOperator : '=' | '*=' | '/=' | '%=' | '+=' | '-=' | '<<=' | '>>=' | '>>>=' | '&=' | '^=' | '|=' ;
conditionalExpression : conditionalOrExpression | conditionalOrExpression '?' expression ':' conditionalExpression | conditionalOrExpression '?' expression ':' lambdaExpression ;
conditionalOrExpression : conditionalAndExpression | conditionalOrExpression '||' conditionalAndExpression ;
conditionalAndExpression : inclusiveOrExpression | conditionalAndExpression '&&' inclusiveOrExpression ;
inclusiveOrExpression : exclusiveOrExpression | inclusiveOrExpression '|' exclusiveOrExpression ;
exclusiveOrExpression : andExpression | exclusiveOrExpression '^' andExpression ;
andExpression : equalityExpression | andExpression '&' equalityExpression ;
equalityExpression : relationalExpression | equalityExpression '==' relationalExpression | equalityExpression '!=' relationalExpression ;
relationalExpression : shiftExpression | relationalExpression '<' shiftExpression | relationalExpression '>' shiftExpression | relationalExpression '<=' shiftExpression | relationalExpression '>=' shiftExpression | relationalExpression 'instanceof' referenceType ;
shiftExpression : additiveExpression | shiftExpression '<<' additiveExpression | shiftExpression '>>' additiveExpression | shiftExpression '>>>' additiveExpression ;
additiveExpression : multiplicativeExpression | additiveExpression '+' multiplicativeExpression | additiveExpression '-' multiplicativeExpression ;
multiplicativeExpression : unaryExpression | multiplicativeExpression '*' unaryExpression | multiplicativeExpression '/' unaryExpression | multiplicativeExpression '%' unaryExpression ;
unaryExpression : preIncrementExpression | preDecrementExpression | '+' unaryExpression | '-' unaryExpression | unaryExpressionNotPlusMinus ;
preIncrementExpression : '++' unaryExpression ;
preDecrementExpression : '--' unaryExpression ;
unaryExpressionNotPlusMinus : postfixExpression | '~' unaryExpression | '!' unaryExpression | castExpression ;
postfixExpression : primary | expressionName | postIncrementExpression | postDecrementExpression ;
postIncrementExpression : postfixExpression '++' ;
postDecrementExpression : postfixExpression '--' ;
castExpression : '(' primitiveType ')' unaryExpression | '(' referenceType additionalBound* ')' unaryExpressionNotPlusMinus | '(' referenceType additionalBound* ')' lambdaExpression ;
constantExpression : expression ;
variableAccess : expressionName | fieldAccess ;
// LEXER
identifier : Identifier | 'to' | 'module' | 'open' | 'with' | 'provides' | 'uses' | 'opens' | 'requires' | 'exports';
// 3.9 Keywords
ABSTRACT : 'abstract';
ASSERT : 'assert';
BOOLEAN : 'boolean';
BREAK : 'break';
BYTE : 'byte';
CASE : 'case';
CATCH : 'catch';
CHAR : 'char';
CLASS : 'class';
CONST : 'const';
CONTINUE : 'continue';
DEFAULT : 'default';
DO : 'do';
DOUBLE : 'double';
ELSE : 'else';
ENUM : 'enum';
EXTENDS : 'extends';
FINAL : 'final';
FINALLY : 'finally';
FLOAT : 'float';
FOR : 'for';
IF : 'if';
GOTO : 'goto';
IMPLEMENTS : 'implements';
IMPORT : 'import';
INSTANCEOF : 'instanceof';
INT : 'int';
INTERFACE : 'interface';
LONG : 'long';
NATIVE : 'native';
NEW : 'new';
PACKAGE : 'package';
PRIVATE : 'private';
PROTECTED : 'protected';
PUBLIC : 'public';
RETURN : 'return';
SHORT : 'short';
STATIC : 'static';
STRICTFP : 'strictfp';
SUPER : 'super';
SWITCH : 'switch';
SYNCHRONIZED : 'synchronized';
THIS : 'this';
THROW : 'throw';
THROWS : 'throws';
TRANSIENT : 'transient';
TRY : 'try';
VOID : 'void';
VOLATILE : 'volatile';
WHILE : 'while';
UNDER_SCORE : '_';//Introduced in Java 9
// 3.10.1 Integer Literals
IntegerLiteral
: DecimalIntegerLiteral
| HexIntegerLiteral
| OctalIntegerLiteral
| BinaryIntegerLiteral
;
fragment
DecimalIntegerLiteral
: DecimalNumeral IntegerTypeSuffix?
;
fragment
HexIntegerLiteral
: HexNumeral IntegerTypeSuffix?
;
fragment
OctalIntegerLiteral
: OctalNumeral IntegerTypeSuffix?
;
fragment
BinaryIntegerLiteral
: BinaryNumeral IntegerTypeSuffix?
;
fragment
IntegerTypeSuffix
: [lL]
;
fragment
DecimalNumeral
: '0'
| NonZeroDigit (Digits? | Underscores Digits)
;
fragment
Digits
: Digit (DigitsAndUnderscores? Digit)?
;
fragment
Digit
: '0'
| NonZeroDigit
;
fragment
NonZeroDigit
: [1-9]
;
fragment
DigitsAndUnderscores
: DigitOrUnderscore+
;
fragment
DigitOrUnderscore
: Digit
| '_'
;
fragment
Underscores
: '_'+
;
fragment
HexNumeral
: '0' [xX] HexDigits
;
fragment
HexDigits
: HexDigit (HexDigitsAndUnderscores? HexDigit)?
;
fragment
HexDigit
: [0-9a-fA-F]
;
fragment
HexDigitsAndUnderscores
: HexDigitOrUnderscore+
;
fragment
HexDigitOrUnderscore
: HexDigit
| '_'
;
fragment
OctalNumeral
: '0' Underscores? OctalDigits
;
fragment
OctalDigits
: OctalDigit (OctalDigitsAndUnderscores? OctalDigit)?
;
fragment
OctalDigit
: [0-7]
;
fragment
OctalDigitsAndUnderscores
: OctalDigitOrUnderscore+
;
fragment
OctalDigitOrUnderscore
: OctalDigit
| '_'
;
fragment
BinaryNumeral
: '0' [bB] BinaryDigits
;
fragment
BinaryDigits
: BinaryDigit (BinaryDigitsAndUnderscores? BinaryDigit)?
;
fragment
BinaryDigit
: [01]
;
fragment
BinaryDigitsAndUnderscores
: BinaryDigitOrUnderscore+
;
fragment
BinaryDigitOrUnderscore
: BinaryDigit
| '_'
;
// 3.10.2 Floating-Point Literals
FloatingPointLiteral
: DecimalFloatingPointLiteral
| HexadecimalFloatingPointLiteral
;
fragment
DecimalFloatingPointLiteral
: Digits '.' Digits? ExponentPart? FloatTypeSuffix?
| '.' Digits ExponentPart? FloatTypeSuffix?
| Digits ExponentPart FloatTypeSuffix?
| Digits FloatTypeSuffix
;
fragment
ExponentPart
: ExponentIndicator SignedInteger
;
fragment
ExponentIndicator
: [eE]
;
fragment
SignedInteger
: Sign? Digits
;
fragment
Sign
: [+-]
;
fragment
FloatTypeSuffix
: [fFdD]
;
fragment
HexadecimalFloatingPointLiteral
: HexSignificand BinaryExponent FloatTypeSuffix?
;
fragment
HexSignificand
: HexNumeral '.'?
| '0' [xX] HexDigits? '.' HexDigits
;
fragment
BinaryExponent
: BinaryExponentIndicator SignedInteger
;
fragment
BinaryExponentIndicator
: [pP]
;
// 3.10.3 Boolean Literals
BooleanLiteral
: 'true'
| 'false'
;
// 3.10.4 Character Literals
CharacterLiteral
: '\'' SingleCharacter '\''
| '\'' EscapeSequence '\''
;
fragment
SingleCharacter
: ~['\\\r\n]
;
// 3.10.5 String Literals
StringLiteral
: '"' StringCharacters? '"'
;
fragment
StringCharacters
: StringCharacter+
;
fragment
StringCharacter
: ~["\\\r\n]
| EscapeSequence
;
// 3.10.6 Escape Sequences for Character and String Literals
fragment
EscapeSequence
: '\\' [btnfr"'\\]
| OctalEscape
| UnicodeEscape // This is not in the spec but prevents having to preprocess the input
;
fragment
OctalEscape
: '\\' OctalDigit
| '\\' OctalDigit OctalDigit
| '\\' ZeroToThree OctalDigit OctalDigit
;
fragment
ZeroToThree
: [0-3]
;
// This is not in the spec but prevents having to preprocess the input
fragment
UnicodeEscape
: '\\' 'u'+ HexDigit HexDigit HexDigit HexDigit
;
// 3.10.7 The Null Literal
NullLiteral
: 'null'
;
// 3.11 Separators
LPAREN : '(';
RPAREN : ')';
LBRACE : '{';
RBRACE : '}';
LBRACK : '[';
RBRACK : ']';
SEMI : ';';
COMMA : ',';
DOT : '.';
ELLIPSIS : '...';
AT : '@';
COLONCOLON : '::';
// 3.12 Operators
ASSIGN : '=';
GT : '>';
LT : '<';
BANG : '!';
TILDE : '~';
QUESTION : '?';
COLON : ':';
ARROW : '->';
EQUAL : '==';
LE : '<=';
GE : '>=';
NOTEQUAL : '!=';
AND : '&&';
OR : '||';
INC : '++';
DEC : '--';
ADD : '+';
SUB : '-';
MUL : '*';
DIV : '/';
BITAND : '&';
BITOR : '|';
CARET : '^';
MOD : '%';
//LSHIFT : '<<';
//RSHIFT : '>>';
//URSHIFT : '>>>';
ADD_ASSIGN : '+=';
SUB_ASSIGN : '-=';
MUL_ASSIGN : '*=';
DIV_ASSIGN : '/=';
AND_ASSIGN : '&=';
OR_ASSIGN : '|=';
XOR_ASSIGN : '^=';
MOD_ASSIGN : '%=';
LSHIFT_ASSIGN : '<<=';
RSHIFT_ASSIGN : '>>=';
URSHIFT_ASSIGN : '>>>=';
// 3.8 Identifiers (must appear after all keywords in the grammar)
Identifier
: JavaLetter JavaLetterOrDigit*
;
fragment
JavaLetter
: [a-zA-Z$_] // these are the "java letters" below 0x7F
| // covers all characters above 0x7F which are not a surrogate
~[\u0000-\u007F\uD800-\uDBFF]
{Character.isJavaIdentifierStart(_input.LA(-1))}?
| // covers UTF-16 surrogate pairs encodings for U+10000 to U+10FFFF
[\uD800-\uDBFF] [\uDC00-\uDFFF]
{Character.isJavaIdentifierStart(Character.toCodePoint((char)_input.LA(-2), (char)_input.LA(-1)))}?
;
fragment
JavaLetterOrDigit
: [a-zA-Z0-9$_] // these are the "java letters or digits" below 0x7F
| // covers all characters above 0x7F which are not a surrogate
~[\u0000-\u007F\uD800-\uDBFF]
{Character.isJavaIdentifierPart(_input.LA(-1))}?
| // covers UTF-16 surrogate pairs encodings for U+10000 to U+10FFFF
[\uD800-\uDBFF] [\uDC00-\uDFFF]
{Character.isJavaIdentifierPart(Character.toCodePoint((char)_input.LA(-2), (char)_input.LA(-1)))}?
;
//
// Whitespace and comments
//
WS : [ \t\r\n\u000C]+ -> skip
;
COMMENT
: '/*' .*? '*/' -> channel(HIDDEN)
;
LINE_COMMENT
: '//' ~[\r\n]* -> channel(HIDDEN)
;