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