1/*
2 [The "BSD licence"]
3 Copyright (c) 2007-2008 Terence Parr
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/** A Java 1.5 grammar for ANTLR v3 derived from the spec
29 *
30 *  This is a very close representation of the spec; the changes
31 *  are comestic (remove left recursion) and also fixes (the spec
32 *  isn't exactly perfect).  I have run this on the 1.4.2 source
33 *  and some nasty looking enums from 1.5, but have not really
34 *  tested for 1.5 compatibility.
35 *
36 *  I built this with: java -Xmx100M org.antlr.Tool java.g
37 *  and got two errors that are ok (for now):
38 *  java.g:691:9: Decision can match input such as
39 *    "'0'..'9'{'E', 'e'}{'+', '-'}'0'..'9'{'D', 'F', 'd', 'f'}"
40 *    using multiple alternatives: 3, 4
41 *  As a result, alternative(s) 4 were disabled for that input
42 *  java.g:734:35: Decision can match input such as "{'$', 'A'..'Z',
43 *    '_', 'a'..'z', '\u00C0'..'\u00D6', '\u00D8'..'\u00F6',
44 *    '\u00F8'..'\u1FFF', '\u3040'..'\u318F', '\u3300'..'\u337F',
45 *    '\u3400'..'\u3D2D', '\u4E00'..'\u9FFF', '\uF900'..'\uFAFF'}"
46 *    using multiple alternatives: 1, 2
47 *  As a result, alternative(s) 2 were disabled for that input
48 *
49 *  You can turn enum on/off as a keyword :)
50 *
51 *  Version 1.0 -- initial release July 5, 2006 (requires 3.0b2 or higher)
52 *
53 *  Primary author: Terence Parr, July 2006
54 *
55 *  Version 1.0.1 -- corrections by Koen Vanderkimpen & Marko van Dooren,
56 *      October 25, 2006;
57 *      fixed normalInterfaceDeclaration: now uses typeParameters instead
58 *          of typeParameter (according to JLS, 3rd edition)
59 *      fixed castExpression: no longer allows expression next to type
60 *          (according to semantics in JLS, in contrast with syntax in JLS)
61 *
62 *  Version 1.0.2 -- Terence Parr, Nov 27, 2006
63 *      java spec I built this from had some bizarre for-loop control.
64 *          Looked weird and so I looked elsewhere...Yep, it's messed up.
65 *          simplified.
66 *
67 *  Version 1.0.3 -- Chris Hogue, Feb 26, 2007
68 *      Factored out an annotationName rule and used it in the annotation rule.
69 *          Not sure why, but typeName wasn't recognizing references to inner
70 *          annotations (e.g. @InterfaceName.InnerAnnotation())
71 *      Factored out the elementValue section of an annotation reference.  Created
72 *          elementValuePair and elementValuePairs rules, then used them in the
73 *          annotation rule.  Allows it to recognize annotation references with
74 *          multiple, comma separated attributes.
75 *      Updated elementValueArrayInitializer so that it allows multiple elements.
76 *          (It was only allowing 0 or 1 element).
77 *      Updated localVariableDeclaration to allow annotations.  Interestingly the JLS
78 *          doesn't appear to indicate this is legal, but it does work as of at least
79 *          JDK 1.5.0_06.
80 *      Moved the Identifier portion of annotationTypeElementRest to annotationMethodRest.
81 *          Because annotationConstantRest already references variableDeclarator which
82 *          has the Identifier portion in it, the parser would fail on constants in
83 *          annotation definitions because it expected two identifiers.
84 *      Added optional trailing ';' to the alternatives in annotationTypeElementRest.
85 *          Wouldn't handle an inner interface that has a trailing ';'.
86 *      Swapped the expression and type rule reference order in castExpression to
87 *          make it check for genericized casts first.  It was failing to recognize a
88 *          statement like  "Class<Byte> TYPE = (Class<Byte>)...;" because it was seeing
89 *          'Class<Byte' in the cast expression as a less than expression, then failing
90 *          on the '>'.
91 *      Changed createdName to use typeArguments instead of nonWildcardTypeArguments.
92 *      Changed the 'this' alternative in primary to allow 'identifierSuffix' rather than
93 *          just 'arguments'.  The case it couldn't handle was a call to an explicit
94 *          generic method invocation (e.g. this.<E>doSomething()).  Using identifierSuffix
95 *          may be overly aggressive--perhaps should create a more constrained thisSuffix rule?
96 *
97 *  Version 1.0.4 -- Hiroaki Nakamura, May 3, 2007
98 *
99 *  Fixed formalParameterDecls, localVariableDeclaration, forInit,
100 *  and forVarControl to use variableModifier* not 'final'? (annotation)?
101 *
102 *  Version 1.0.5 -- Terence, June 21, 2007
103 *  --a[i].foo didn't work. Fixed unaryExpression
104 *
105 *  Version 1.0.6 -- John Ridgway, March 17, 2008
106 *      Made "assert" a switchable keyword like "enum".
107 *      Fixed compilationUnit to disallow "annotation importDeclaration ...".
108 *      Changed "Identifier ('.' Identifier)*" to "qualifiedName" in more
109 *          places.
110 *      Changed modifier* and/or variableModifier* to classOrInterfaceModifiers,
111 *          modifiers or variableModifiers, as appropriate.
112 *      Renamed "bound" to "typeBound" to better match language in the JLS.
113 *      Added "memberDeclaration" which rewrites to methodDeclaration or
114 *      fieldDeclaration and pulled type into memberDeclaration.  So we parse
115 *          type and then move on to decide whether we're dealing with a field
116 *          or a method.
117 *      Modified "constructorDeclaration" to use "constructorBody" instead of
118 *          "methodBody".  constructorBody starts with explicitConstructorInvocation,
119 *          then goes on to blockStatement*.  Pulling explicitConstructorInvocation
120 *          out of expressions allowed me to simplify "primary".
121 *      Changed variableDeclarator to simplify it.
122 *      Changed type to use classOrInterfaceType, thus simplifying it; of course
123 *          I then had to add classOrInterfaceType, but it is used in several
124 *          places.
125 *      Fixed annotations, old version allowed "@X(y,z)", which is illegal.
126 *      Added optional comma to end of "elementValueArrayInitializer"; as per JLS.
127 *      Changed annotationTypeElementRest to use normalClassDeclaration and
128 *          normalInterfaceDeclaration rather than classDeclaration and
129 *          interfaceDeclaration, thus getting rid of a couple of grammar ambiguities.
130 *      Split localVariableDeclaration into localVariableDeclarationStatement
131 *          (includes the terminating semi-colon) and localVariableDeclaration.
132 *          This allowed me to use localVariableDeclaration in "forInit" clauses,
133 *           simplifying them.
134 *      Changed switchBlockStatementGroup to use multiple labels.  This adds an
135 *          ambiguity, but if one uses appropriately greedy parsing it yields the
136 *           parse that is closest to the meaning of the switch statement.
137 *      Renamed "forVarControl" to "enhancedForControl" -- JLS language.
138 *      Added semantic predicates to test for shift operations rather than other
139 *          things.  Thus, for instance, the string "< <" will never be treated
140 *          as a left-shift operator.
141 *      In "creator" we rule out "nonWildcardTypeArguments" on arrayCreation,
142 *          which are illegal.
143 *      Moved "nonWildcardTypeArguments into innerCreator.
144 *      Removed 'super' superSuffix from explicitGenericInvocation, since that
145 *          is only used in explicitConstructorInvocation at the beginning of a
146 *           constructorBody.  (This is part of the simplification of expressions
147 *           mentioned earlier.)
148 *      Simplified primary (got rid of those things that are only used in
149 *          explicitConstructorInvocation).
150 *      Lexer -- removed "Exponent?" from FloatingPointLiteral choice 4, since it
151 *          led to an ambiguity.
152 *
153 *      This grammar successfully parses every .java file in the JDK 1.5 source
154 *          tree (excluding those whose file names include '-', which are not
155 *          valid Java compilation units).
156 *
157 *  June 26, 2008
158 *
159 *	conditionalExpression had wrong precedence x?y:z.
160 *
161 *  Known remaining problems:
162 *      "Letter" and "JavaIDDigit" are wrong.  The actual specification of
163 *      "Letter" should be "a character for which the method
164 *      Character.isJavaIdentifierStart(int) returns true."  A "Java
165 *      letter-or-digit is a character for which the method
166 *      Character.isJavaIdentifierPart(int) returns true."
167 */
168grammar Java;
169
170// starting point for parsing a java file
171/* The annotations are separated out to make parsing faster, but must be associated with
172   a packageDeclaration or a typeDeclaration (and not an empty one). */
173compilationUnit
174    :   annotations
175        (   packageDeclaration importDeclaration* typeDeclaration*
176        |   classOrInterfaceDeclaration typeDeclaration*
177        )
178        EOF
179    |   packageDeclaration? importDeclaration* typeDeclaration*
180        EOF
181    ;
182
183packageDeclaration
184    :   'package' qualifiedName ';'
185    ;
186
187importDeclaration
188    :   'import' 'static'? qualifiedName ('.' '*')? ';'
189    ;
190
191typeDeclaration
192    :   classOrInterfaceDeclaration
193    |   ';'
194    ;
195
196classOrInterfaceDeclaration
197    :   classOrInterfaceModifiers (classDeclaration | interfaceDeclaration)
198    ;
199
200classOrInterfaceModifiers
201    :   classOrInterfaceModifier*
202    ;
203
204classOrInterfaceModifier
205    :   annotation       // class or interface
206    |   (   'public'     // class or interface
207        |   'protected'  // class or interface
208        |   'private'    // class or interface
209        |   'abstract'   // class or interface
210        |   'static'     // class or interface
211        |   'final'      // class only -- does not apply to interfaces
212        |   'strictfp'   // class or interface
213        )
214    ;
215
216modifiers
217    :   modifier*
218    ;
219
220classDeclaration
221    :   normalClassDeclaration
222    |   enumDeclaration
223    ;
224
225normalClassDeclaration
226    :   'class' Identifier typeParameters?
227        ('extends' type)?
228        ('implements' typeList)?
229        classBody
230    ;
231
232typeParameters
233    :   '<' typeParameter (',' typeParameter)* '>'
234    ;
235
236typeParameter
237    :   Identifier ('extends' typeBound)?
238    ;
239
240typeBound
241    :   type ('&' type)*
242    ;
243
244enumDeclaration
245    :   ENUM Identifier ('implements' typeList)? enumBody
246    ;
247
248enumBody
249    :   '{' enumConstants? ','? enumBodyDeclarations? '}'
250    ;
251
252enumConstants
253    :   enumConstant (',' enumConstant)*
254    ;
255
256enumConstant
257    :   annotations? Identifier arguments? classBody?
258    ;
259
260enumBodyDeclarations
261    :   ';' (classBodyDeclaration)*
262    ;
263
264interfaceDeclaration
265    :   normalInterfaceDeclaration
266    |   annotationTypeDeclaration
267    ;
268
269normalInterfaceDeclaration
270    :   'interface' Identifier typeParameters? ('extends' typeList)? interfaceBody
271    ;
272
273typeList
274    :   type (',' type)*
275    ;
276
277classBody
278    :   '{' classBodyDeclaration* '}'
279    ;
280
281interfaceBody
282    :   '{' interfaceBodyDeclaration* '}'
283    ;
284
285classBodyDeclaration
286    :   ';'
287    |   'static'? block
288    |   modifiers memberDecl
289    ;
290
291memberDecl
292    :   genericMethodOrConstructorDecl
293    |   memberDeclaration
294    |   'void' Identifier voidMethodDeclaratorRest
295    |   Identifier constructorDeclaratorRest
296    |   interfaceDeclaration
297    |   classDeclaration
298    ;
299
300memberDeclaration
301    :   type (methodDeclaration | fieldDeclaration)
302    ;
303
304genericMethodOrConstructorDecl
305    :   typeParameters genericMethodOrConstructorRest
306    ;
307
308genericMethodOrConstructorRest
309    :   (type | 'void') Identifier methodDeclaratorRest
310    |   Identifier constructorDeclaratorRest
311    ;
312
313methodDeclaration
314    :   Identifier methodDeclaratorRest
315    ;
316
317fieldDeclaration
318    :   variableDeclarators ';'
319    ;
320
321interfaceBodyDeclaration
322    :   modifiers interfaceMemberDecl
323    |   ';'
324    ;
325
326interfaceMemberDecl
327    :   interfaceMethodOrFieldDecl
328    |   interfaceGenericMethodDecl
329    |   'void' Identifier voidInterfaceMethodDeclaratorRest
330    |   interfaceDeclaration
331    |   classDeclaration
332    ;
333
334interfaceMethodOrFieldDecl
335    :   type Identifier interfaceMethodOrFieldRest
336    ;
337
338interfaceMethodOrFieldRest
339    :   constantDeclaratorsRest ';'
340    |   interfaceMethodDeclaratorRest
341    ;
342
343methodDeclaratorRest
344    :   formalParameters ('[' ']')*
345        ('throws' qualifiedNameList)?
346        (   methodBody
347        |   ';'
348        )
349    ;
350
351voidMethodDeclaratorRest
352    :   formalParameters ('throws' qualifiedNameList)?
353        (   methodBody
354        |   ';'
355        )
356    ;
357
358interfaceMethodDeclaratorRest
359    :   formalParameters ('[' ']')* ('throws' qualifiedNameList)? ';'
360    ;
361
362interfaceGenericMethodDecl
363    :   typeParameters (type | 'void') Identifier
364        interfaceMethodDeclaratorRest
365    ;
366
367voidInterfaceMethodDeclaratorRest
368    :   formalParameters ('throws' qualifiedNameList)? ';'
369    ;
370
371constructorDeclaratorRest
372    :   formalParameters ('throws' qualifiedNameList)? constructorBody
373    ;
374
375constantDeclarator
376    :   Identifier constantDeclaratorRest
377    ;
378
379variableDeclarators
380    :   variableDeclarator (',' variableDeclarator)*
381    ;
382
383variableDeclarator
384    :   variableDeclaratorId ('=' variableInitializer)?
385    ;
386
387constantDeclaratorsRest
388    :   constantDeclaratorRest (',' constantDeclarator)*
389    ;
390
391constantDeclaratorRest
392    :   ('[' ']')* '=' variableInitializer
393    ;
394
395variableDeclaratorId
396    :   Identifier ('[' ']')*
397    ;
398
399variableInitializer
400    :   arrayInitializer
401    |   expression
402    ;
403
404arrayInitializer
405    :   '{' (variableInitializer (',' variableInitializer)* (',')? )? '}'
406    ;
407
408modifier
409    :   annotation
410    |   (   'public'
411        |   'protected'
412        |   'private'
413        |   'static'
414        |   'abstract'
415        |   'final'
416        |   'native'
417        |   'synchronized'
418        |   'transient'
419        |   'volatile'
420        |   'strictfp'
421        )
422    ;
423
424packageOrTypeName
425    :   qualifiedName
426    ;
427
428enumConstantName
429    :   Identifier
430    ;
431
432typeName
433    :   qualifiedName
434    ;
435
436type
437	:	classOrInterfaceType ('[' ']')*
438	|	primitiveType ('[' ']')*
439	;
440
441classOrInterfaceType
442	:	Identifier typeArguments? ('.' Identifier typeArguments? )*
443	;
444
445primitiveType
446    :   'boolean'
447    |   'char'
448    |   'byte'
449    |   'short'
450    |   'int'
451    |   'long'
452    |   'float'
453    |   'double'
454    ;
455
456variableModifier
457    :   'final'
458    |   annotation
459    ;
460
461typeArguments
462    :   '<' typeArgument (',' typeArgument)* '>'
463    ;
464
465typeArgument
466    :   type
467    |   '?' (('extends' | 'super') type)?
468    ;
469
470qualifiedNameList
471    :   qualifiedName (',' qualifiedName)*
472    ;
473
474formalParameters
475    :   '(' formalParameterDecls? ')'
476    ;
477
478formalParameterDecls
479    :   variableModifiers type formalParameterDeclsRest
480    ;
481
482formalParameterDeclsRest
483    :   variableDeclaratorId (',' formalParameterDecls)?
484    |   '...' variableDeclaratorId
485    ;
486
487methodBody
488    :   block
489    ;
490
491constructorBody
492    :   block
493    ;
494
495qualifiedName
496    :   Identifier ('.' Identifier)*
497    ;
498
499literal
500    :   IntegerLiteral
501    |   FloatingPointLiteral
502    |   CharacterLiteral
503    |   StringLiteral
504    |   BooleanLiteral
505    |   'null'
506    ;
507
508// ANNOTATIONS
509
510annotations
511    :   annotation+
512    ;
513
514annotation
515    :   '@' annotationName ( '(' ( elementValuePairs | elementValue )? ')' )?
516    ;
517
518annotationName
519    : Identifier ('.' Identifier)*
520    ;
521
522elementValuePairs
523    :   elementValuePair (',' elementValuePair)*
524    ;
525
526elementValuePair
527    :   Identifier '=' elementValue
528    ;
529
530elementValue
531    :   conditionalExpression
532    |   annotation
533    |   elementValueArrayInitializer
534    ;
535
536elementValueArrayInitializer
537    :   '{' (elementValue (',' elementValue)*)? (',')? '}'
538    ;
539
540annotationTypeDeclaration
541    :   '@' 'interface' Identifier annotationTypeBody
542    ;
543
544annotationTypeBody
545    :   '{' (annotationTypeElementDeclaration)* '}'
546    ;
547
548annotationTypeElementDeclaration
549    :   modifiers annotationTypeElementRest
550	|	';' // this is not allowed by the grammar, but apparently allowed by the actual compiler
551    ;
552
553annotationTypeElementRest
554    :   type annotationMethodOrConstantRest ';'
555    |   normalClassDeclaration ';'?
556    |   normalInterfaceDeclaration ';'?
557    |   enumDeclaration ';'?
558    |   annotationTypeDeclaration ';'?
559    ;
560
561annotationMethodOrConstantRest
562    :   annotationMethodRest
563    |   annotationConstantRest
564    ;
565
566annotationMethodRest
567    :   Identifier '(' ')' defaultValue?
568    ;
569
570annotationConstantRest
571    :   variableDeclarators
572    ;
573
574defaultValue
575    :   'default' elementValue
576    ;
577
578// STATEMENTS / BLOCKS
579
580block
581    :   '{' blockStatement* '}'
582    ;
583
584blockStatement
585    :   localVariableDeclarationStatement
586    |   classOrInterfaceDeclaration
587    |   statement
588    ;
589
590localVariableDeclarationStatement
591    :    localVariableDeclaration ';'
592    ;
593
594localVariableDeclaration
595    :   variableModifiers type variableDeclarators
596    ;
597
598variableModifiers
599    :   variableModifier*
600    ;
601
602statement
603    :	block
604    |   ASSERT expression (':' expression)? ';'
605    |   'if' parExpression statement ('else' statement)?
606    |   'for' '(' forControl ')' statement
607    |   'while' parExpression statement
608    |   'do' statement 'while' parExpression ';'
609    |   'try' block (catches finallyBlock? | finallyBlock)
610	|	'try' resourceSpecification block catches? finallyBlock?
611    |   'switch' parExpression '{' switchBlockStatementGroups '}'
612    |   'synchronized' parExpression block
613    |   'return' expression? ';'
614    |   'throw' expression ';'
615    |   'break' Identifier? ';'
616    |   'continue' Identifier? ';'
617    |   ';'
618    |   statementExpression ';'
619    |   Identifier ':' statement
620    ;
621
622catches
623    :   catchClause+
624    ;
625
626catchClause
627    :   'catch' '(' variableModifiers catchType Identifier ')' block
628    ;
629
630catchType
631	:	qualifiedName ('|' qualifiedName)*
632	;
633
634finallyBlock
635	:	'finally' block
636	;
637
638resourceSpecification
639	:	'(' resources ';'? ')'
640	;
641
642resources
643	:	resource (';' resource)*
644	;
645
646resource
647	:	variableModifiers classOrInterfaceType variableDeclaratorId '=' expression
648	;
649
650formalParameter
651    :   variableModifiers type variableDeclaratorId
652    ;
653
654switchBlockStatementGroups
655    :   (switchBlockStatementGroup)*
656    ;
657
658/* The change here (switchLabel -> switchLabel+) technically makes this grammar
659   ambiguous; but with appropriately greedy parsing it yields the most
660   appropriate AST, one in which each group, except possibly the last one, has
661   labels and statements. */
662switchBlockStatementGroup
663    :   switchLabel+ blockStatement*
664    ;
665
666switchLabel
667    :   'case' constantExpression ':'
668    |   'case' enumConstantName ':'
669    |   'default' ':'
670    ;
671
672forControl
673    :   enhancedForControl
674    |   forInit? ';' expression? ';' forUpdate?
675    ;
676
677forInit
678    :   localVariableDeclaration
679    |   expressionList
680    ;
681
682enhancedForControl
683    :   variableModifiers type Identifier ':' expression
684    ;
685
686forUpdate
687    :   expressionList
688    ;
689
690// EXPRESSIONS
691
692parExpression
693    :   '(' expression ')'
694    ;
695
696expressionList
697    :   expression (',' expression)*
698    ;
699
700statementExpression
701    :   expression
702    ;
703
704constantExpression
705    :   expression
706    ;
707
708expression
709    :   conditionalExpression (assignmentOperator expression)?
710    ;
711
712assignmentOperator
713    :   '='
714    |   '+='
715    |   '-='
716    |   '*='
717    |   '/='
718    |   '&='
719    |   '|='
720    |   '^='
721    |   '%='
722    |   '<<='
723    |   '>>='
724    |   '>>>='
725    ;
726
727conditionalExpression
728    :   conditionalOrExpression ( '?' expression ':' conditionalExpression )?
729    ;
730
731conditionalOrExpression
732    :   conditionalAndExpression ( '||' conditionalAndExpression )*
733    ;
734
735conditionalAndExpression
736    :   inclusiveOrExpression ( '&&' inclusiveOrExpression )*
737    ;
738
739inclusiveOrExpression
740    :   exclusiveOrExpression ( '|' exclusiveOrExpression )*
741    ;
742
743exclusiveOrExpression
744    :   andExpression ( '^' andExpression )*
745    ;
746
747andExpression
748    :   equalityExpression ( '&' equalityExpression )*
749    ;
750
751equalityExpression
752    :   instanceOfExpression ( ('==' | '!=') instanceOfExpression )*
753    ;
754
755instanceOfExpression
756    :   relationalExpression ('instanceof' type)?
757    ;
758
759relationalExpression
760    :   shiftExpression ( relationalOp shiftExpression )*
761    ;
762
763relationalOp
764    :   '<='
765    |   '>='
766    |   '<'
767    |   '>'
768    ;
769
770shiftExpression
771    :   additiveExpression ( shiftOp additiveExpression )*
772    ;
773
774shiftOp
775    :   t1='<' t2='<'
776//        { $t1.getLine() == $t2.getLine() &&
777//          $t1.getCharPositionInLine() + 1 == $t2.getCharPositionInLine() }?
778    |   t1='>' t2='>' t3='>'
779//        { $t1.getLine() == $t2.getLine() &&
780//          $t1.getCharPositionInLine() + 1 == $t2.getCharPositionInLine() &&
781//          $t2.getLine() == $t3.getLine() &&
782//          $t2.getCharPositionInLine() + 1 == $t3.getCharPositionInLine() }?
783    |   t1='>' t2='>'
784//        { $t1.getLine() == $t2.getLine() &&
785//          $t1.getCharPositionInLine() + 1 == $t2.getCharPositionInLine() }?
786    ;
787
788
789additiveExpression
790    :   multiplicativeExpression ( ('+' | '-') multiplicativeExpression )*
791    ;
792
793multiplicativeExpression
794    :   unaryExpression ( ( '*' | '/' | '%' ) unaryExpression )*
795    ;
796
797unaryExpression
798    :   '+' unaryExpression
799    |   '-' unaryExpression
800    |   '++' unaryExpression
801    |   '--' unaryExpression
802    |   unaryExpressionNotPlusMinus
803    ;
804
805unaryExpressionNotPlusMinus
806    :   '~' unaryExpression
807    |   '!' unaryExpression
808    |   castExpression
809    |   primary selector* ('++'|'--')?
810    ;
811
812castExpression
813    :  '(' primitiveType ')' unaryExpression
814    |  '(' (type | expression) ')' unaryExpressionNotPlusMinus
815    ;
816
817primary
818    :   parExpression
819    |   'this' arguments?
820    |   'super' superSuffix
821    |   literal
822    |   'new' creator
823	|	nonWildcardTypeArguments (explicitGenericInvocationSuffix | 'this' arguments)
824    |   Identifier ('.' Identifier)* identifierSuffix?
825    |   primitiveType ('[' ']')* '.' 'class'
826    |   'void' '.' 'class'
827    ;
828
829identifierSuffix
830    :   ('[' ']')+ '.' 'class'
831    |   '[' expression ']'
832    |   arguments
833    |   '.' 'class'
834    |   '.' explicitGenericInvocation
835    |   '.' 'this'
836    |   '.' 'super' arguments
837    |   '.' 'new' nonWildcardTypeArguments? innerCreator
838    ;
839
840creator
841    :   nonWildcardTypeArguments createdName classCreatorRest
842    |   createdName (arrayCreatorRest | classCreatorRest)
843    ;
844
845createdName
846    :   Identifier typeArgumentsOrDiamond? ('.' Identifier typeArgumentsOrDiamond?)*
847	|	primitiveType
848    ;
849
850innerCreator
851    :   Identifier nonWildcardTypeArgumentsOrDiamond? classCreatorRest
852    ;
853
854arrayCreatorRest
855    :   '['
856        (   ']' ('[' ']')* arrayInitializer
857        |   expression ']' ('[' expression ']')* ('[' ']')*
858        )
859    ;
860
861classCreatorRest
862    :   arguments classBody?
863    ;
864
865explicitGenericInvocation
866    :   nonWildcardTypeArguments explicitGenericInvocationSuffix
867    ;
868
869nonWildcardTypeArguments
870    :   '<' typeList '>'
871    ;
872
873typeArgumentsOrDiamond
874	:	'<' '>'
875	|	typeArguments
876	;
877
878nonWildcardTypeArgumentsOrDiamond
879	:	'<' '>'
880	|	nonWildcardTypeArguments
881	;
882
883selector
884    :   '.' Identifier arguments?
885	|	'.' explicitGenericInvocation
886    |   '.' 'this'
887    |   '.' 'super' superSuffix
888    |   '.' 'new' nonWildcardTypeArguments? innerCreator
889    |   '[' expression ']'
890    ;
891
892superSuffix
893    :   arguments
894    |   '.' Identifier arguments?
895    ;
896
897explicitGenericInvocationSuffix
898	:	'super' superSuffix
899	|	Identifier arguments
900	;
901
902arguments
903    :   '(' expressionList? ')'
904    ;
905
906// LEXER
907
908// §3.9 Keywords
909
910ABSTRACT : 'abstract';
911ASSERT : 'assert';
912BOOLEAN : 'boolean';
913BREAK : 'break';
914BYTE : 'byte';
915CASE : 'case';
916CATCH : 'catch';
917CHAR : 'char';
918CLASS : 'class';
919CONST : 'const';
920CONTINUE : 'continue';
921DEFAULT : 'default';
922DO : 'do';
923DOUBLE : 'double';
924ELSE : 'else';
925ENUM : 'enum';
926EXTENDS : 'extends';
927FINAL : 'final';
928FINALLY : 'finally';
929FLOAT : 'float';
930FOR : 'for';
931IF : 'if';
932GOTO : 'goto';
933IMPLEMENTS : 'implements';
934IMPORT : 'import';
935INSTANCEOF : 'instanceof';
936INT : 'int';
937INTERFACE : 'interface';
938LONG : 'long';
939NATIVE : 'native';
940NEW : 'new';
941PACKAGE : 'package';
942PRIVATE : 'private';
943PROTECTED : 'protected';
944PUBLIC : 'public';
945RETURN : 'return';
946SHORT : 'short';
947STATIC : 'static';
948STRICTFP : 'strictfp';
949SUPER : 'super';
950SWITCH : 'switch';
951SYNCHRONIZED : 'synchronized';
952THIS : 'this';
953THROW : 'throw';
954THROWS : 'throws';
955TRANSIENT : 'transient';
956TRY : 'try';
957VOID : 'void';
958VOLATILE : 'volatile';
959WHILE : 'while';
960
961// §3.10.1 Integer Literals
962
963IntegerLiteral
964	:	DecimalIntegerLiteral
965	|	HexIntegerLiteral
966	|	OctalIntegerLiteral
967	|	BinaryIntegerLiteral
968	;
969
970fragment
971DecimalIntegerLiteral
972	:	DecimalNumeral IntegerTypeSuffix?
973	;
974
975fragment
976HexIntegerLiteral
977	:	HexNumeral IntegerTypeSuffix?
978	;
979
980fragment
981OctalIntegerLiteral
982	:	OctalNumeral IntegerTypeSuffix?
983	;
984
985fragment
986BinaryIntegerLiteral
987	:	BinaryNumeral IntegerTypeSuffix?
988	;
989
990fragment
991IntegerTypeSuffix
992	:	[lL]
993	;
994
995fragment
996DecimalNumeral
997	:	'0'
998	|	NonZeroDigit (Digits? | Underscores Digits)
999	;
1000
1001fragment
1002Digits
1003	:	Digit (DigitsAndUnderscores? Digit)?
1004	;
1005
1006fragment
1007Digit
1008	:	'0'
1009	|	NonZeroDigit
1010	;
1011
1012fragment
1013NonZeroDigit
1014	:	[1-9]
1015	;
1016
1017fragment
1018DigitsAndUnderscores
1019	:	DigitOrUnderscore+
1020	;
1021
1022fragment
1023DigitOrUnderscore
1024	:	Digit
1025	|	'_'
1026	;
1027
1028fragment
1029Underscores
1030	:	'_'+
1031	;
1032
1033fragment
1034HexNumeral
1035	:	'0' [xX] HexDigits
1036	;
1037
1038fragment
1039HexDigits
1040	:	HexDigit (HexDigitsAndUnderscores? HexDigit)?
1041	;
1042
1043fragment
1044HexDigit
1045	:	[0-9a-fA-F]
1046	;
1047
1048fragment
1049HexDigitsAndUnderscores
1050	:	HexDigitOrUnderscore+
1051	;
1052
1053fragment
1054HexDigitOrUnderscore
1055	:	HexDigit
1056	|	'_'
1057	;
1058
1059fragment
1060OctalNumeral
1061	:	'0' Underscores? OctalDigits
1062	;
1063
1064fragment
1065OctalDigits
1066	:	OctalDigit (OctalDigitsAndUnderscores? OctalDigit)?
1067	;
1068
1069fragment
1070OctalDigit
1071	:	[0-7]
1072	;
1073
1074fragment
1075OctalDigitsAndUnderscores
1076	:	OctalDigitOrUnderscore+
1077	;
1078
1079fragment
1080OctalDigitOrUnderscore
1081	:	OctalDigit
1082	|	'_'
1083	;
1084
1085fragment
1086BinaryNumeral
1087	:	'0' [bB] BinaryDigits
1088	;
1089
1090fragment
1091BinaryDigits
1092	:	BinaryDigit (BinaryDigitsAndUnderscores? BinaryDigit)?
1093	;
1094
1095fragment
1096BinaryDigit
1097	:	[01]
1098	;
1099
1100fragment
1101BinaryDigitsAndUnderscores
1102	:	BinaryDigitOrUnderscore+
1103	;
1104
1105fragment
1106BinaryDigitOrUnderscore
1107	:	BinaryDigit
1108	|	'_'
1109	;
1110
1111// §3.10.2 Floating-Point Literals
1112
1113FloatingPointLiteral
1114	:	DecimalFloatingPointLiteral
1115	|	HexadecimalFloatingPointLiteral
1116	;
1117
1118fragment
1119DecimalFloatingPointLiteral
1120	:	Digits '.' Digits? ExponentPart? FloatTypeSuffix?
1121	|	'.' Digits ExponentPart? FloatTypeSuffix?
1122	|	Digits ExponentPart FloatTypeSuffix?
1123	|	Digits FloatTypeSuffix
1124	;
1125
1126fragment
1127ExponentPart
1128	:	ExponentIndicator SignedInteger
1129	;
1130
1131fragment
1132ExponentIndicator
1133	:	[eE]
1134	;
1135
1136fragment
1137SignedInteger
1138	:	Sign? Digits
1139	;
1140
1141fragment
1142Sign
1143	:	[+\-]
1144	;
1145
1146fragment
1147FloatTypeSuffix
1148	:	[fFdD]
1149	;
1150
1151fragment
1152HexadecimalFloatingPointLiteral
1153	:	HexSignificand BinaryExponent FloatTypeSuffix?
1154	;
1155
1156fragment
1157HexSignificand
1158	:	HexNumeral '.'?
1159	|	'0' [xX] HexDigits? '.' HexDigits
1160	;
1161
1162fragment
1163BinaryExponent
1164	:	BinaryExponentIndicator SignedInteger
1165	;
1166
1167fragment
1168BinaryExponentIndicator
1169	:	[pP]
1170	;
1171
1172// §3.10.3 Boolean Literals
1173
1174BooleanLiteral
1175	:	'true'
1176	|	'false'
1177	;
1178
1179// §3.10.4 Character Literals
1180
1181CharacterLiteral
1182	:	'\'' SingleCharacter '\''
1183	|	'\'' EscapeSequence '\''
1184	;
1185
1186fragment
1187SingleCharacter
1188	:	~['\\]
1189	;
1190
1191// §3.10.5 String Literals
1192
1193StringLiteral
1194	:	'"' StringCharacters? '"'
1195	;
1196
1197fragment
1198StringCharacters
1199	:	StringCharacter+
1200	;
1201
1202fragment
1203StringCharacter
1204	:	~["\\]
1205	|	EscapeSequence
1206	;
1207
1208// §3.10.6 Escape Sequences for Character and String Literals
1209
1210fragment
1211EscapeSequence
1212	:	'\\' [btnfr"'\\]
1213	|	OctalEscape
1214	;
1215
1216fragment
1217OctalEscape
1218	:	'\\' OctalDigit
1219	|	'\\' OctalDigit OctalDigit
1220	|	'\\' ZeroToThree OctalDigit OctalDigit
1221	;
1222
1223fragment
1224ZeroToThree
1225	:	[0-3]
1226	;
1227
1228// §3.10.7 The Null Literal
1229
1230NullLiteral
1231	:	'null'
1232	;
1233
1234// §3.11 Separators
1235
1236LPAREN : '(';
1237RPAREN : ')';
1238LBRACE : '{';
1239RBRACE : '}';
1240LBRACK : '[';
1241RBRACK : ']';
1242SEMI : ';';
1243COMMA : ',';
1244DOT : '.';
1245
1246// §3.12 Operators
1247
1248ASSIGN : '=';
1249GT : '>';
1250LT : '<';
1251BANG : '!';
1252TILDE : '~';
1253QUESTION : '?';
1254COLON : ':';
1255EQUAL : '==';
1256LE : '<=';
1257GE : '>=';
1258NOTEQUAL : '!=';
1259AND : '&&';
1260OR : '||';
1261INC : '++';
1262DEC : '--';
1263ADD : '+';
1264SUB : '-';
1265MUL : '*';
1266DIV : '/';
1267BITAND : '&';
1268BITOR : '|';
1269CARET : '^';
1270MOD : '%';
1271
1272ADD_ASSIGN : '+=';
1273SUB_ASSIGN : '-=';
1274MUL_ASSIGN : '*=';
1275DIV_ASSIGN : '/=';
1276AND_ASSIGN : '&=';
1277OR_ASSIGN : '|=';
1278XOR_ASSIGN : '^=';
1279MOD_ASSIGN : '%=';
1280LSHIFT_ASSIGN : '<<=';
1281RSHIFT_ASSIGN : '>>=';
1282URSHIFT_ASSIGN : '>>>=';
1283
1284// §3.8 Identifiers (must appear after all keywords in the grammar)
1285
1286Identifier
1287	:	JavaLetter JavaLetterOrDigit*
1288	;
1289
1290fragment
1291JavaLetter
1292	:	[a-zA-Z$_] // these are the "java letters" below 0xFF
1293	|	// covers all characters above 0xFF which are not a surrogate
1294		~[\u0000-\u00FF\uD800-\uDBFF]
1295		{Character.isJavaIdentifierStart(_input.LA(-1))}?
1296	|	// covers UTF-16 surrogate pairs encodings for U+10000 to U+10FFFF
1297		[\uD800-\uDBFF] [\uDC00-\uDFFF]
1298		{Character.isJavaIdentifierStart(Character.toCodePoint((char)_input.LA(-2), (char)_input.LA(-1)))}?
1299	;
1300
1301fragment
1302JavaLetterOrDigit
1303	:	[a-zA-Z0-9$_] // these are the "java letters or digits" below 0xFF
1304	|	// covers all characters above 0xFF which are not a surrogate
1305		~[\u0000-\u00FF\uD800-\uDBFF]
1306		{Character.isJavaIdentifierPart(_input.LA(-1))}?
1307	|	// covers UTF-16 surrogate pairs encodings for U+10000 to U+10FFFF
1308		[\uD800-\uDBFF] [\uDC00-\uDFFF]
1309		{Character.isJavaIdentifierPart(Character.toCodePoint((char)_input.LA(-2), (char)_input.LA(-1)))}?
1310	;
1311
1312//
1313// Additional symbols not defined in the lexical specification
1314//
1315
1316AT : '@';
1317ELLIPSIS : '...';
1318
1319//
1320// Whitespace and comments
1321//
1322
1323WS  :  [ \t\r\n\u000C]+ -> skip
1324    ;
1325
1326COMMENT
1327    :   '/*' .*? '*/' -> skip
1328    ;
1329
1330LINE_COMMENT
1331    :   '//' ~[\r\n]* -> skip
1332    ;
1333