/* [The "BSD licence"] Copyright (c) 2007-2008 Terence Parr All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. The name of the author may not be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ /** A Java 1.5 grammar for ANTLR v3 derived from the spec * * This is a very close representation of the spec; the changes * are comestic (remove left recursion) and also fixes (the spec * isn't exactly perfect). I have run this on the 1.4.2 source * and some nasty looking enums from 1.5, but have not really * tested for 1.5 compatibility. * * I built this with: java -Xmx100M org.antlr.Tool java.g * and got two errors that are ok (for now): * java.g:691:9: Decision can match input such as * "'0'..'9'{'E', 'e'}{'+', '-'}'0'..'9'{'D', 'F', 'd', 'f'}" * using multiple alternatives: 3, 4 * As a result, alternative(s) 4 were disabled for that input * java.g:734:35: Decision can match input such as "{'$', 'A'..'Z', * '_', 'a'..'z', '\u00C0'..'\u00D6', '\u00D8'..'\u00F6', * '\u00F8'..'\u1FFF', '\u3040'..'\u318F', '\u3300'..'\u337F', * '\u3400'..'\u3D2D', '\u4E00'..'\u9FFF', '\uF900'..'\uFAFF'}" * using multiple alternatives: 1, 2 * As a result, alternative(s) 2 were disabled for that input * * You can turn enum on/off as a keyword :) * * Version 1.0 -- initial release July 5, 2006 (requires 3.0b2 or higher) * * Primary author: Terence Parr, July 2006 * * Version 1.0.1 -- corrections by Koen Vanderkimpen & Marko van Dooren, * October 25, 2006; * fixed normalInterfaceDeclaration: now uses typeParameters instead * of typeParameter (according to JLS, 3rd edition) * fixed castExpression: no longer allows expression next to type * (according to semantics in JLS, in contrast with syntax in JLS) * * Version 1.0.2 -- Terence Parr, Nov 27, 2006 * java spec I built this from had some bizarre for-loop control. * Looked weird and so I looked elsewhere...Yep, it's messed up. * simplified. * * Version 1.0.3 -- Chris Hogue, Feb 26, 2007 * Factored out an annotationName rule and used it in the annotation rule. * Not sure why, but typeName wasn't recognizing references to inner * annotations (e.g. @InterfaceName.InnerAnnotation()) * Factored out the elementValue section of an annotation reference. Created * elementValuePair and elementValuePairs rules, then used them in the * annotation rule. Allows it to recognize annotation references with * multiple, comma separated attributes. * Updated elementValueArrayInitializer so that it allows multiple elements. * (It was only allowing 0 or 1 element). * Updated localVariableDeclaration to allow annotations. Interestingly the JLS * doesn't appear to indicate this is legal, but it does work as of at least * JDK 1.5.0_06. * Moved the Identifier portion of annotationTypeElementRest to annotationMethodRest. * Because annotationConstantRest already references variableDeclarator which * has the Identifier portion in it, the parser would fail on constants in * annotation definitions because it expected two identifiers. * Added optional trailing ';' to the alternatives in annotationTypeElementRest. * Wouldn't handle an inner interface that has a trailing ';'. * Swapped the expression and type rule reference order in castExpression to * make it check for genericized casts first. It was failing to recognize a * statement like "Class TYPE = (Class)...;" because it was seeing * 'Class'. * Changed createdName to use typeArguments instead of nonWildcardTypeArguments. * Again, JLS doesn't seem to allow this, but java.lang.Class has an example of * of this construct. * Changed the 'this' alternative in primary to allow 'identifierSuffix' rather than * just 'arguments'. The case it couldn't handle was a call to an explicit * generic method invocation (e.g. this.doSomething()). Using identifierSuffix * may be overly aggressive--perhaps should create a more constrained thisSuffix rule? * * Version 1.0.4 -- Hiroaki Nakamura, May 3, 2007 * * Fixed formalParameterDecls, localVariableDeclaration, forInit, * and forVarControl to use variableModifier* not 'final'? (annotation)? * * Version 1.0.5 -- Terence, June 21, 2007 * --a[i].foo didn't work. Fixed unaryExpression * * Version 1.0.6 -- John Ridgway, March 17, 2008 * Made "assert" a switchable keyword like "enum". * Fixed compilationUnit to disallow "annotation importDeclaration ...". * Changed "Identifier ('.' Identifier)*" to "qualifiedName" in more * places. * Changed modifier* and/or variableModifier* to classOrInterfaceModifiers, * modifiers or variableModifiers, as appropriate. * Renamed "bound" to "typeBound" to better match language in the JLS. * Added "memberDeclaration" which rewrites to methodDeclaration or * fieldDeclaration and pulled type into memberDeclaration. So we parse * type and then move on to decide whether we're dealing with a field * or a method. * Modified "constructorDeclaration" to use "constructorBody" instead of * "methodBody". constructorBody starts with explicitConstructorInvocation, * then goes on to blockStatement*. Pulling explicitConstructorInvocation * out of expressions allowed me to simplify "primary". * Changed variableDeclarator to simplify it. * Changed type to use classOrInterfaceType, thus simplifying it; of course * I then had to add classOrInterfaceType, but it is used in several * places. * Fixed annotations, old version allowed "@X(y,z)", which is illegal. * Added optional comma to end of "elementValueArrayInitializer"; as per JLS. * Changed annotationTypeElementRest to use normalClassDeclaration and * normalInterfaceDeclaration rather than classDeclaration and * interfaceDeclaration, thus getting rid of a couple of grammar ambiguities. * Split localVariableDeclaration into localVariableDeclarationStatement * (includes the terminating semi-colon) and localVariableDeclaration. * This allowed me to use localVariableDeclaration in "forInit" clauses, * simplifying them. * Changed switchBlockStatementGroup to use multiple labels. This adds an * ambiguity, but if one uses appropriately greedy parsing it yields the * parse that is closest to the meaning of the switch statement. * Renamed "forVarControl" to "enhancedForControl" -- JLS language. * Added semantic predicates to test for shift operations rather than other * things. Thus, for instance, the string "< <" will never be treated * as a left-shift operator. * In "creator" we rule out "nonWildcardTypeArguments" on arrayCreation, * which are illegal. * Moved "nonWildcardTypeArguments into innerCreator. * Removed 'super' superSuffix from explicitGenericInvocation, since that * is only used in explicitConstructorInvocation at the beginning of a * constructorBody. (This is part of the simplification of expressions * mentioned earlier.) * Simplified primary (got rid of those things that are only used in * explicitConstructorInvocation). * Lexer -- removed "Exponent?" from FloatingPointLiteral choice 4, since it * led to an ambiguity. * * This grammar successfully parses every .java file in the JDK 1.5 source * tree (excluding those whose file names include '-', which are not * valid Java compilation units). * * Known remaining problems: * "Letter" and "JavaIDDigit" are wrong. The actual specification of * "Letter" should be "a character for which the method * Character.isJavaIdentifierStart(int) returns true." A "Java * letter-or-digit is a character for which the method * Character.isJavaIdentifierPart(int) returns true." */ grammar org.xtext.example.mydsl.MyDsl with org.eclipse.xtext.common.Terminals generate myDsl "http://www.xtext.org/example/mydsl/MyDsl" CompilationUnit: {CompilationUnit} // annotations=Annotations // (packagedeclaration=PackageDeclaration importdeclaration+=ImportDeclaration* typedeclaration+=TypeDeclaration* // | classorinterfacedeclaration=ClassOrInterfaceDeclaration typedeclaration+=TypeDeclaration*) // | packagedeclaration=PackageDeclaration? importdeclaration+=ImportDeclaration* typedeclaration+=TypeDeclaration*; PackageDeclaration: 'package' qualifiedname=QualifiedName ';'; ImportDeclaration: 'import' 'static'? qualifiedname=QualifiedName (=> '.' '*')? ';'; TypeDeclaration: {TypeDeclaration} (classorinterfacedeclaration=ClassOrInterfaceDeclaration | ';'); ClassOrInterfaceDeclaration: ClassOrInterfaceModifiers (classdeclaration=ClassDeclaration | interfacedeclaration=InterfaceDeclaration); ClassOrInterfaceModifiers: {ClassOrInterfaceModifiers} classorinterfacemodifier+=ClassOrInterfaceModifier*; ClassOrInterfaceModifier: {ClassOrInterfaceModifier} (annotation=Annotation // class or interface | 'public' // class or interface | 'protected' // class or interface | 'private' // class or interface | 'abstract' // class or interface | 'static' // class or interface | 'final' // class only -- does not apply to interfaces | 'strictfp' // class or interface ); Modifiers: {Modifiers} modifier+=Modifier*; ClassDeclaration: NormalClassDeclaration | EnumDeclaration; NormalClassDeclaration: 'class' identifier=ID typeparameters=TypeParameters? ('extends' type=Type)? ('implements' typelist=TypeList)? classbody=ClassBody; TypeParameters: '<' typeparameter+=TypeParameter (',' typeparameter+=TypeParameter)* '>'; TypeParameter: {TypeParameter} identifier=ID ('extends' typebound=TypeBound)?; TypeBound: type+=Type ('&' type+=Type)*; EnumDeclaration: //ENUM Identifier ('implements' typeList)? enumBody 'Enum' identifier=ID ('implements' typelist=TypeList)? enumbody=EnumBody; EnumBody: {EnumBody} '{' enumconstants=EnumConstants? ','? enumbodydeclarations=EnumBodyDeclarations? '}'; EnumConstants: enumconstant+=EnumConstant (',' enumconstant+=EnumConstant)*; EnumConstant: {EnumConstant} (annotations=Annotations? identifier=ID arguments=Arguments? classbody=ClassBody?); EnumBodyDeclarations: {EnumBodyDeclarations} ';' (classbodydeclaration+=ClassBodyDeclaration)*; InterfaceDeclaration: NormalInterfaceDeclaration | AnnotationTypeDeclaration; NormalInterfaceDeclaration: 'interface' identifier=ID typeparameters=TypeParameters? ('extends' typelist=TypeList)? interfacebody=InterfaceBody; TypeList: type+=Type (',' type+=Type)*; ClassBody: {ClassBody} '{' classbodydeclaration+=ClassBodyDeclaration* '}'; InterfaceBody: {InterfaceBody} '{' interfacebodydeclaration+=InterfaceBodyDeclaration* '}'; ClassBodyDeclaration: {ClassBodyDeclaration} ';' | 'static'? Block | Modifiers memberdecl=MemberDecl; MemberDecl: GenericMethodOrConstructorDecl | MemberDeclaration | 'void' identifier=ID voidmethoddeclaratorrest=VoidMethodDeclaratorRest | identifier=ID constructordeclaratorrest=ConstructorDeclaratorRest | InterfaceDeclaration | ClassDeclaration; MemberDeclaration: Type (methoddeclaration=MethodDeclaration | fielddeclaration=FieldDeclaration); GenericMethodOrConstructorDecl: TypeParameters genericmethodorconstructorrest=GenericMethodOrConstructorRest; GenericMethodOrConstructorRest: (type=Type | 'void') identifier=ID methoddeclaratorrest=MethodDeclaratorRest | identifier=ID constructordeclaratorrest=ConstructorDeclaratorRest; MethodDeclaration: identifier=ID methoddeclaratorrest=MethodDeclaratorRest; FieldDeclaration: VariableDeclarators ';'; InterfaceBodyDeclaration: {InterfaceBodyDeclaration} (modifiers=Modifiers interfacememberdecl=InterfaceMemberDecl | ';'); InterfaceMemberDecl: interfacemethodorfielddecl=InterfaceMethodOrFieldDecl | interfacegenericmethoddecl=InterfaceGenericMethodDecl | 'void' identifier=ID voidinterfacemethoddeclaratorrest=VoidInterfaceMethodDeclaratorRest | interfacedeclaration=InterfaceDeclaration | classdeclaration=ClassDeclaration; InterfaceMethodOrFieldDecl: type=Type identifier=ID interfacemethodorfieldrest=InterfaceMethodOrFieldRest; InterfaceMethodOrFieldRest: ConstantDeclaratorsRest ';' | InterfaceMethodDeclaratorRest; MethodDeclaratorRest: FormalParameters ('[' ']')* ('throws' qualifiednamelist=QualifiedNameList)? (methodbody=MethodBody | ';'); VoidMethodDeclaratorRest: FormalParameters ('throws' qualifiednamelist=QualifiedNameList)? (methodbody=MethodBody | ';'); InterfaceMethodDeclaratorRest: FormalParameters ('[' ']')* ('throws' qualifiednamelist=QualifiedNameList)? ';'; InterfaceGenericMethodDecl: TypeParameters (type=Type | 'void') identifier=ID interfacemethoddeclaratorrest=InterfaceMethodDeclaratorRest; VoidInterfaceMethodDeclaratorRest: FormalParameters ('throws' qualifiednamelist=QualifiedNameList)? ';'; ConstructorDeclaratorRest: FormalParameters ('throws' qualifiednamelist=QualifiedNameList)? constructorbody=ConstructorBody; ConstantDeclarator: identifier=ID constantdeclaratorrest=ConstantDeclaratorRest; VariableDeclarators: variabledeclarator+=VariableDeclarator (',' variabledeclarator+=VariableDeclarator)*; VariableDeclarator: {VariableDeclarator} variabledeclaratorid=VariableDeclaratorId ('=' variableinitializer=VariableInitializer)?; ConstantDeclaratorsRest: ConstantDeclaratorRest (',' constantdeclarator+=ConstantDeclarator)*; ConstantDeclaratorRest: ('[' ']')* '=' VariableInitializer; VariableDeclaratorId: identifier=ID ('[' ']')*; VariableInitializer: ArrayInitializer | Expression; ArrayInitializer: {ArrayInitializer} '{' (variableinitializer+=VariableInitializer (',' variableinitializer+=VariableInitializer)* (',')?)? '}'; Modifier: {Modifier} (annotation=Annotation | 'public' | 'protected' | 'private' | 'static' | 'abstract' | 'final' | 'native' | 'synchronized' | 'transient' | 'volatile' | 'strictfp'); PackageOrTypeName: QualifiedName; EnumConstantName: ID; TypeName: QualifiedName; Type: {Type} (classorinterfacetype=ClassOrInterfaceType ('[' ']')* | primitivetype=PrimitiveType ('[' ']')*); ClassOrInterfaceType: {ClassOrInterfaceType} identifier+=ID typearguments+=TypeArguments? ('.' identifier+=ID typearguments+=TypeArguments?)*; PrimitiveType: 'boolean' | 'char' | 'byte' | 'short' | 'int' | 'long' | 'float' | 'double'; VariableModifier: {VariableModifier} 'final' | Annotation; TypeArguments: '<' typeargument+=TypeArgument (',' typeargument+=TypeArgument)* '>'; TypeArgument: {TypeArgument} (type=Type | '?' (('extends' | 'super') type=Type)?); QualifiedNameList: qualifiedname+=QualifiedName (',' qualifiedname+=QualifiedName)*; FormalParameters: {FormalParameters} '(' formalparameterdecls=FormalParameterDecls? ')'; FormalParameterDecls: VariableModifiers type=Type formalparameterdeclsrest=FormalParameterDeclsRest; FormalParameterDeclsRest: {FormalParameterDeclsRest} (variabledeclaratorid=VariableDeclaratorId (',' formalparameterdecls=FormalParameterDecls)? | '...' variabledeclaratorid=VariableDeclaratorId); MethodBody: Block; ConstructorBody: {ConstructorBody} '{' => explicitconstructorinvocation=ExplicitConstructorInvocation? => blockstatement+=BlockStatement* '}'; //ExplicitConstructorInvocation: // (NonWildcardTypeArguments? ('this' | 'super') arguments=Arguments ';' // | primary=Primary '.' NonWildcardTypeArguments=NonWildcardTypeArguments? 'super' arguments=Arguments ';') //; ExplicitConstructorInvocation: // (nonwildcardtypearguments=NonWildcardTypeArguments? ('this' | 'super') | primary=Primary '.' nonwildcardtypearguments=NonWildcardTypeArguments? 'super' // ) arguments=Arguments ';'; QualifiedName: ID (=> '.' ID)*; Literal: IntegerLiteral | FloatingPointLiteral | CharacterLiteral | StringLiteral | BooleanLiteral | 'null'; IntegerLiteral: HexLiteral | OctalLiteral | DecimalLiteral; BooleanLiteral: 'true' | 'false'; // ANNOTATIONS Annotations: annotation+=Annotation+; Annotation: {Annotation} '@' annotationname=AnnotationName (=> '(' (elementvaluepairs=ElementValuePairs | elementvalue=ElementValue)? ')')?; AnnotationName: ID (=> '.' ID)*; ElementValuePairs: elementvaluepair+=ElementValuePair (=> ',' elementvaluepair+=ElementValuePair)*; ElementValuePair: identifier=ID '=' elementvalue=ElementValue; ElementValue: ConditionalExpression | Annotation | ElementValueArrayInitializer; ElementValueArrayInitializer: {ElementValueArrayInitializer} '{' (elementvalue+=ElementValue (=> ',' elementvalue+=ElementValue)*)? (=> ',')? '}'; AnnotationTypeDeclaration: '@' 'interface' identifier=ID annotationtypebody=AnnotationTypeBody; AnnotationTypeBody: {AnnotationTypeBody} '{' (=> annotationtypeelementdeclaration+=AnnotationTypeElementDeclaration)* '}'; AnnotationTypeElementDeclaration: Modifiers annotationtypeelementrest=AnnotationTypeElementRest; AnnotationTypeElementRest: type=Type annotationmethodorconstantrest=AnnotationMethodOrConstantRest ';' | NormalClassDeclaration ';'? | NormalInterfaceDeclaration ';'? | EnumDeclaration ';'? | AnnotationTypeDeclaration ';'?; AnnotationMethodOrConstantRest: (AnnotationMethodRest | AnnotationConstantRest); AnnotationMethodRest: {AnnotationMethodRest} (identifier=ID '(' ')' (=> defaultvalue=DefaultValue)?); AnnotationConstantRest: VariableDeclarators; DefaultValue: 'default' elementvalue=ElementValue; // STATEMENTS / BLOCKS Block: {Block} '{' blockstatement+=BlockStatement* '}'; BlockStatement: LocalVariableDeclarationStatement | ClassOrInterfaceDeclaration | Statement; LocalVariableDeclarationStatement: LocalVariableDeclaration ';'; LocalVariableDeclaration: VariableModifiers type=Type variabledeclarators=VariableDeclarators; VariableModifiers: {VariableModifiers} variablemodifier+=VariableModifier*; Statement: {Statement} (block1=Block //| ASSERT expression (':' expression)? ';' | 'if' parexpression=ParExpression ifstatement=Statement (=> //options {k=1;}: 'else' elsestatement=Statement)? | 'for' '(' forcontrol=ForControl ')' forstatement=Statement | 'while' parexpression=ParExpression whilestatement=Statement | 'do' statement=Statement 'while' dowhileparexpression=ParExpression ';' | 'try' try=Block (catches=Catches ('finally' finallyblock=Block)? | 'finally' finallyblock=Block) // | 'switch' parexpression=ParExpression '{' // switchblockstatementgroups=SwitchBlockStatementGroups '}' | 'synchronized' parexpression=ParExpression block=Block | 'return' expression=Expression? ';' | 'throw' expression=Expression ';' | 'break' identifier=ID? ';' | 'continue' identifier=ID? ';' | ';' | statementexpression=StatementExpression ';' | identifier=ID ':' statement=Statement); Catches: catchclause+=CatchClause (catchclause+=CatchClause)*; CatchClause: 'catch' '(' FormalParameter ')' block=Block; FormalParameter: variablemodifiers=VariableModifiers type=Type variabledeclaratorid=VariableDeclaratorId; SwitchBlockStatementGroups: {SwitchBlockStatementGroups} (switchblockstatementgroup+=SwitchBlockStatementGroup)*; /* The change here (switchLabel -> switchLabel+) technically makes this grammar ambiguous; but with appropriately greedy parsing it yields the most appropriate AST, one in which each group, except possibly the last one, has labels and statements. */ SwitchBlockStatementGroup: (switchlabel+=SwitchLabel)+ (blockstatement+=BlockStatement)*; //SwitchLabel: // {SwitchLabel} // ('case' constantexpression=ConstantExpression ':' | 'case' enumconstantname=EnumConstantName ':' | // 'default' ':'); SwitchLabel: {SwitchLabel} ('case' // ( constantexpression=ConstantExpression // | enumconstantname=EnumConstantName // ) ':' | 'default' ':'); ForControl //options {k=3;} // be efficient for common case: for (ID ID : ID) ... : {ForControl} ( // enhancedforcontrol=EnhancedForControl | forinit=ForInit? ';' expression=Expression? ';' forupdate=ForUpdate?); ForInit: LocalVariableDeclaration // | ExpressionList ; EnhancedForControl: VariableModifiers type=Type identifier=ID ':' expression=Expression; ForUpdate: ExpressionList; // EXPRESSIONS ParExpression: '(' Expression ')'; ExpressionList: expression1=Expression (',' expression2+=Expression)*; StatementExpression: Expression; ConstantExpression: Expression; Expression: ConditionalExpression (=> assignmentoperator=AssignmentOperator expression=Expression)?; AssignmentOperator: '=' | '+=' | '-=' | '*=' | '/=' | '&=' | '|=' | '^=' | '%=' // | ('<' '<' '=')=> t1='<' t2='<' t3='=' // { $t1.getLine() == $t2.getLine() && // $t1.getCharPositionInLine() + 1 == $t2.getCharPositionInLine() && // $t2.getLine() == $t3.getLine() && // $t2.getCharPositionInLine() + 1 == $t3.getCharPositionInLine() }? // | ('>' '>' '>' '=')=> t1='>' t2='>' t3='>' t4='=' // { $t1.getLine() == $t2.getLine() && // $t1.getCharPositionInLine() + 1 == $t2.getCharPositionInLine() && // $t2.getLine() == $t3.getLine() && // $t2.getCharPositionInLine() + 1 == $t3.getCharPositionInLine() && // $t3.getLine() == $t4.getLine() && // $t3.getCharPositionInLine() + 1 == $t4.getCharPositionInLine() }? // | ('>' '>' '=')=> t1='>' t2='>' t3='=' // { $t1.getLine() == $t2.getLine() && // $t1.getCharPositionInLine() + 1 == $t2.getCharPositionInLine() && // $t2.getLine() == $t3.getLine() && // $t2.getCharPositionInLine() + 1 == $t3.getCharPositionInLine() }? ; ConditionalExpression: conditionalorexpression=ConditionalOrExpression ('?' expression1=Expression ':' expression2=Expression)?; ConditionalOrExpression: conditionalandexpression+=ConditionalAndExpression ('||' conditionalandexpression+=ConditionalAndExpression)*; ConditionalAndExpression: inclusiveorexpression+=InclusiveOrExpression ('&&' inclusiveorexpression+=InclusiveOrExpression)*; InclusiveOrExpression: exclusiveorexpression+=ExclusiveOrExpression ('|' exclusiveorexpression+=ExclusiveOrExpression)*; ExclusiveOrExpression: andexpression+=AndExpression ('^' andexpression+=AndExpression)*; AndExpression: equalityexpression+=EqualityExpression ('&' equalityexpression+=EqualityExpression)*; EqualityExpression: instanceofexpression+=InstanceOfExpression (('==' | '!=') instanceofexpression+=InstanceOfExpression)*; InstanceOfExpression: RelationalExpression ('instanceof' type=Type)?; RelationalExpression: shiftexpression+=ShiftExpression (relationalop+=RelationalOp shiftexpression+=ShiftExpression)*; RelationalOp: //('<' '=')=> t1='<' t2='=' // { $t1.getLine() == $t2.getLine() && // $t1.getCharPositionInLine() + 1 == $t2.getCharPositionInLine() }? // | ('>' '=')=> t1='>' t2='=' // { $t1.getLine() == $t2.getLine() && // $t1.getCharPositionInLine() + 1 == $t2.getCharPositionInLine() }? // | '<' | '>'; ShiftExpression: additiveexpression+=AdditiveExpression (shiftop+=ShiftOp additiveexpression+=AdditiveExpression)*; ShiftOp: '<' '<' | '>' '>' '>' | '>' '>' //('<' '<')=> t1='<' t2='<' // { $t1.getLine() == $t2.getLine() && // $t1.getCharPositionInLine() + 1 == $t2.getCharPositionInLine() }? // | ('>' '>' '>')=> t1='>' t2='>' t3='>' // { $t1.getLine() == $t2.getLine() && // $t1.getCharPositionInLine() + 1 == $t2.getCharPositionInLine() && // $t2.getLine() == $t3.getLine() && // $t2.getCharPositionInLine() + 1 == $t3.getCharPositionInLine() }? // | ('>' '>')=> t1='>' t2='>' // { $t1.getLine() == $t2.getLine() && // $t1.getCharPositionInLine() + 1 == $t2.getCharPositionInLine() }? ; AdditiveExpression: multiplicativeexpression+=MultiplicativeExpression (('+' | '-') multiplicativeexpression+=MultiplicativeExpression)*; MultiplicativeExpression: unaryexpression+=UnaryExpression (('*' | '/' | '%') unaryexpression+=UnaryExpression)*; UnaryExpression: ('+' UnaryExpression | '-' UnaryExpression | '++' UnaryExpression | '--' UnaryExpression | UnaryExpressionNotPlusMinus); UnaryExpressionNotPlusMinus: {UnaryExpressionNotPlusMinus} ('~' unaryexpression=UnaryExpression | '!' unaryexpression=UnaryExpression // | castexpression=CastExpression | primary=Primary selector+=Selector* ('++' | '--')?); CastExpression: {CastExpression} '(' primitivetype=PrimitiveType ')' unaryexpression=UnaryExpression | '(' (Type | Expression) ')' unaryexpressionnotplusminus=UnaryExpressionNotPlusMinus; Primary: {Primary} (parexpression=ParExpression | 'this' ('.' identifier+=ID)* (=>identifiersuffix=IdentifierSuffix)? | 'super' supersuffix=SuperSuffix | literal=Literal | 'new' creator=Creator | identifier+=ID ('.' identifier+=ID)* (=>identifiersuffix=IdentifierSuffix)? | primitivetype=PrimitiveType ('[' ']')* '.' 'class' | 'void' '.' 'class'); IdentifierSuffix: {IdentifierSuffix} (('[' ']')+ '.' 'class' // | ('[' expression+=Expression ']')+ // can also be matched by selector, but do here | arguments=Arguments | '.' 'class' | '.' explicitgenericinvocation=ExplicitGenericInvocation | '.' 'this' | '.' 'super' arguments=Arguments | '.' 'new' innercreator=InnerCreator); Creator: NonWildcardTypeArguments createdname=CreatedName classcreatorrest=ClassCreatorRest | CreatedName (arraycreatorrest=ArrayCreatorRest | classcreatorrest=ClassCreatorRest); CreatedName: {CreatedName} (classorinterfacetype=ClassOrInterfaceType | primitivetype=PrimitiveType); InnerCreator: nonwildcardtypearguments=NonWildcardTypeArguments? identifier=ID classcreatorrest=ClassCreatorRest; ArrayCreatorRest: '[' (']' ('[' ']')* ArrayInitializer | expression+=Expression ']' (=>'[' expression+=Expression ']')* ('[' ']')*); ClassCreatorRest: arguments=Arguments classbody=ClassBody?; ExplicitGenericInvocation: nonwildcardtypearguments=NonWildcardTypeArguments identifier=ID arguments=Arguments; NonWildcardTypeArguments: '<' TypeList '>'; Selector: {Selector} ( // '.' identifier=Identifier arguments=Arguments? | // '.' 'this' | '.' 'super' supersuffix=SuperSuffix | '.' 'new' innercreator=InnerCreator | '[' expression=Expression ']'); SuperSuffix: {SuperSuffix} (arguments=Arguments | '.' identifier=ID arguments=Arguments?); Arguments: {Arguments} '(' expressionlist=ExpressionList? ')'; // LEXER terminal HexLiteral: '0' ('x' | 'X') HexDigit+ IntegerTypeSuffix?; terminal DecimalLiteral: ('0' | '1'..'9' '0'..'9'*) IntegerTypeSuffix?; terminal OctalLiteral: '0' ('0'..'7')+ IntegerTypeSuffix?; terminal fragment HexDigit: ('0'..'9' | 'a'..'f' | 'A'..'F'); terminal fragment IntegerTypeSuffix: ('l' | 'L'); terminal FloatingPointLiteral: ('0'..'9')+ '.' ('0'..'9')* Exponent? FloatTypeSuffix? | '.' ('0'..'9')+ Exponent? FloatTypeSuffix? | ('0'..'9')+ Exponent FloatTypeSuffix? | ('0'..'9')+ FloatTypeSuffix; terminal fragment Exponent: ('e' | 'E') ('+' | '-')? ('0'..'9')+; terminal fragment FloatTypeSuffix: ('f' | 'F' | 'd' | 'D'); terminal CharacterLiteral: '\'' (EscapeSequence | !('\'' | '\\')) '\''; terminal StringLiteral: '"' (EscapeSequence | !('\\' | '"'))* '"'; terminal fragment EscapeSequence: '\\' ('b' | 't' | 'n' | 'f' | 'r' | '\"' | '\'' | '\\') | UnicodeEscape | OctalEscape; terminal fragment OctalEscape: '\\' ('0'..'3') ('0'..'7') ('0'..'7') | '\\' ('0'..'7') ('0'..'7') | '\\' ('0'..'7'); terminal fragment UnicodeEscape: '\\' 'u' HexDigit HexDigit HexDigit HexDigit; //terminal Identifier: // ID; // Letter (Letter | JavaIDDigit)*; /**I found this char range in JavaCC's grammar, but Letter and Digit overlap. Still works, but... */ terminal fragment Letter: '\u0024' | '\u0041'..'\u005a' | '\u005f' | '\u0061'..'\u007a' | '\u00c0'..'\u00d6' | '\u00d8'..'\u00f6' | '\u00f8'..'\u00ff' | '\u0100'..'\u1fff' | '\u3040'..'\u318f' | '\u3300'..'\u337f' | '\u3400'..'\u3d2d' | '\u4e00'..'\u9fff' | '\uf900'..'\ufaff'; terminal fragment JavaIDDigit: '\u0030'..'\u0039' | '\u0660'..'\u0669' | '\u06f0'..'\u06f9' | '\u0966'..'\u096f' | '\u09e6'..'\u09ef' | '\u0a66'..'\u0a6f' | '\u0ae6'..'\u0aef' | '\u0b66'..'\u0b6f' | '\u0be7'..'\u0bef' | '\u0c66'..'\u0c6f' | '\u0ce6'..'\u0cef' | '\u0d66'..'\u0d6f' | '\u0e50'..'\u0e59' | '\u0ed0'..'\u0ed9' | '\u1040'..'\u1049'; //ENUM: 'enum' {if (!enumIsKeyword) $type=Identifier;} // ; //ASSERT // : 'assert' {if (!assertIsKeyword) $type=Identifier;} // ; //WS : (' '|'\r'|'\t'|'\u000C'|'\n') {$channel=HIDDEN;} // ; //COMMENT // : '/*' ( options {greedy=false;} : . )* '*/' {$channel=HIDDEN;} // ; //LINE_COMMENT // : '//' ~('\n'|'\r')* '\r'? '\n' {$channel=HIDDEN;} // ;