Skip to main content

module lang::java::\syntax::Java18

rascal-0.40.16

Java 8 grammar.

Usage

import lang::java::\syntax::Java18;

Description

This Java grammar is based on the following references:

Benefits

  • the grammar is well-annotated with the source information

Pitfalls

  • the grammar contains too many non-terminals for the expression sub-language (priorities and associativities are still encoded with non-terminals)
  • same for the Statements; too many non-terminals for handy use with concrete-syntax patterns
  • the grammar is not well tested

syntax CompilationUnit

start syntax CompilationUnit = PackageDeclaration? Imports TypeDeclaration*;

syntax Literal

syntax Literal = IntegerLiteral
| FloatingPointLiteral
| BooleanLiteral
| CharacterLiteral
| StringLiteral
| NullLiteral
;

syntax Imports

syntax Imports = ImportDeclaration*;

syntax Type

syntax Type = PrimitiveType
| ReferenceType
;

syntax PrimitiveType

syntax PrimitiveType = Annotation* NumericType
| Annotation* "boolean"
;

syntax NumericType

syntax NumericType = IntegralType
| FloatingPointType
;

syntax IntegralType

syntax IntegralType = "byte" 
| "short"
| "int"
| "long"
| "char"
;

syntax FloatingPointType

syntax FloatingPointType = "float" | "double" ;

syntax ReferenceType

syntax ReferenceType = ClassOrInterfaceType 
| arrayType: ArrayType
;

syntax ClassOrInterfaceType

syntax ClassOrInterfaceType = ClassType 
;

syntax ClassType

syntax ClassType = Annotation* Identifier TypeArguments? 
| ClassOrInterfaceType "." Annotation* Identifier TypeArguments?
;

syntax InterfaceType

syntax InterfaceType = ClassType;

syntax TypeVariable

syntax TypeVariable = Annotation* Identifier;

syntax ArrayType

syntax ArrayType = PrimitiveType Dims 
| ClassOrInterfaceType Dims
;

syntax Dims

syntax Dims = Annotation* "[" "]" (Annotation* "[" "]")*;

syntax TypeParameter

syntax TypeParameter = typeParameter: TypeParameterModifier* Identifier TypeBound? ;

syntax TypeParameterModifier

syntax TypeParameterModifier = Annotation;

syntax TypeBound

syntax TypeBound = "extends" { ClassOrInterfaceType "&" }+
;

syntax AdditionalBound

syntax AdditionalBound = "&" InterfaceType ;

syntax TypeArguments

syntax TypeArguments = "\<"  {TypeArgument ","}* "\>" ;

syntax TypeArgument

syntax TypeArgument = ReferenceType 
| Wildcard
;

syntax Wildcard

syntax Wildcard = Annotation* "?" WildcardBounds? ;

syntax WildcardBounds

syntax WildcardBounds = "extends" ReferenceType
| "super" ReferenceType
;

syntax TypeName

syntax TypeName = Identifier
| PackageOrTypeName "." Identifier
;

syntax PackageOrTypeName

syntax PackageOrTypeName = Identifier 
| PackageOrTypeName "." Identifier
;

syntax ExpressionName

syntax ExpressionName = Identifier 
| AmbiguousName "." Identifier
;

syntax MethodName

syntax MethodName = Identifier;

syntax PackageName

syntax PackageName = Identifier 
| PackageName "." Identifier
;

syntax AmbiguousName

syntax AmbiguousName = Identifier 
| AmbiguousName "." Identifier
;

syntax PackageDeclaration

syntax PackageDeclaration = PackageModifier* "package" {Identifier "."}+ ";" ;

syntax PackageModifier

syntax PackageModifier = Annotation ;

syntax ImportDeclaration

syntax ImportDeclaration = importDeclaration: SingleTypeImportDeclaration       // import Class; 
| TypeImportOnDemandDeclaration // import br.unb.rascal.*;
| SingleStaticImportDeclaration // import static br.unb.rascal.Foo.m;
| StaticImportOnDemandDeclaration // import static br.unb.rascal.Foo.*;
;

syntax SingleTypeImportDeclaration

syntax SingleTypeImportDeclaration = "import" TypeName ";"+ ;

syntax TypeImportOnDemandDeclaration

syntax TypeImportOnDemandDeclaration = "import" PackageOrTypeName "." "*" ";"+ ;

syntax SingleStaticImportDeclaration

syntax SingleStaticImportDeclaration = "import" "static" TypeName "." Identifier ";"+;

syntax StaticImportOnDemandDeclaration

syntax StaticImportOnDemandDeclaration = "import" "static" TypeName "." "*" ";"+ ;

syntax TypeDeclaration

syntax TypeDeclaration = ClassDeclaration ";"*
| InterfaceDeclaration ";"*
;

syntax ClassDeclaration

syntax ClassDeclaration = NormalClassDeclaration
| EnumDeclaration
;

syntax NormalClassDeclaration

syntax NormalClassDeclaration = normalClassDeclaration: ClassModifier* "class" Identifier TypeParameters? Superclass? Superinterfaces? ClassBody ;

syntax ClassModifier

syntax ClassModifier = Annotation 
| "public"
| "protected"
| "private"
| "abstract"
| "static"
| "final"
| "strictfp"
;

syntax TypeParameters

syntax TypeParameters = typeParameters : "\<" {TypeParameter ","}+ "\>" ;

syntax Superclass

syntax Superclass = "extends" ClassType ;

syntax Superinterfaces

syntax Superinterfaces = "implements" {InterfaceType ","}+ ;

syntax ClassBody

syntax ClassBody = classBody : "{" ClassBodyDeclaration* decls "}" ";"? ;

syntax ClassBodyDeclaration

syntax ClassBodyDeclaration = ClassMemberDeclaration 
| InstanceInitializer
| StaticInitializer
| ConstructorDeclaration
;

syntax ClassMemberDeclaration

syntax ClassMemberDeclaration = FieldDeclaration 
| MethodDeclaration
| ClassDeclaration
| InterfaceDeclaration
//| ";"
;

syntax FieldDeclaration

syntax FieldDeclaration = fieldDeclaration: FieldModifier* UnannType VariableDeclaratorList ";"+ ;

syntax FieldModifier

syntax FieldModifier = Annotation 
| "public"
| "protected"
| "private"
| "static"
| "final"
| "transient"
| "volatile"
;

syntax VariableDeclaratorList

syntax VariableDeclaratorList = variableDeclaratorList: {VariableDeclarator ","}+ ;

syntax VariableDeclarator

syntax VariableDeclarator = variableDeclarator: VariableDeclaratorId ("=" VariableInitializer)? ;

syntax VariableDeclaratorId

syntax VariableDeclaratorId = Identifier Dims? ;

syntax VariableInitializer

syntax VariableInitializer = Expression 
| ArrayInitializer
;

syntax UnannType

syntax UnannType = UnannPrimitiveType 
| UnannReferenceType
;

syntax UnannPrimitiveType

syntax UnannPrimitiveType = NumericType 
| "boolean"
;

syntax UnannReferenceType

syntax UnannReferenceType = UnannClassOrInterfaceType 
| UnannArrayType
;

syntax UnannClassOrInterfaceType

syntax UnannClassOrInterfaceType = UnannClassType 
;

syntax UnannClassType

syntax UnannClassType = Identifier TypeArguments? 
| UnannClassOrInterfaceType "." Annotation* Identifier TypeArguments?;

syntax UnannInterfaceType

syntax UnannInterfaceType = UnannClassType ;

syntax UnannTypeVariable

syntax UnannTypeVariable = Identifier ;

syntax UnannArrayType

syntax UnannArrayType = UnannPrimitiveType Dims 
| UnannClassOrInterfaceType Dims
;

syntax MethodDeclaration

syntax MethodDeclaration = methodDeclaration: MethodModifier* MethodHeader MethodBody ;

syntax MethodModifier

syntax MethodModifier = Annotation 
| "public"
| "protected"
| "private"
| "abstract"
| "static"
| "final"
| "synchronized"
| "native"
| "strictfp"
;

syntax MethodHeader

syntax MethodHeader = methodHeader: Result MethodDeclarator Throws?
| TypeParameters Annotation* Result MethodDeclarator Throws?
;

syntax Result

syntax Result = UnannType 
| "void"
;

syntax MethodDeclarator

syntax MethodDeclarator = Identifier "(" ")" Dims?
| Identifier "(" LastFormalParameter ")" Dims?
| Identifier "(" {FormalParameter ","}+ ("," LastFormalParameter)?")" Dims?
;

syntax FormalParameterList

syntax FormalParameterList = FormalParameters ;

syntax FormalParameters

syntax FormalParameters = formalParameter : FormalParameter ("," FormalParameters)?
| lastFormalParameter: LastFormalParameter
;

syntax FormalParameter

syntax FormalParameter = VariableModifier* mds UnannType atype VariableDeclaratorId vdid;

syntax LastFormalParameter

syntax LastFormalParameter = VariableModifier* UnannType Annotation* "..." VariableDeclaratorId 
;

syntax ReceiverParameter

syntax ReceiverParameter = Annotation* UnannType (Identifier ".")? "this" ;

syntax VariableModifier

syntax VariableModifier = Annotation 
| "final"
;

syntax Throws

syntax Throws = "throws" { ExceptionType "," }+;

syntax ExceptionType

syntax ExceptionType = ClassType 
;

syntax MethodBody

syntax MethodBody = Block ";"*
| ";"
;

syntax InstanceInitializer

syntax InstanceInitializer = Block ;

syntax StaticInitializer

syntax StaticInitializer = "static" Block ";"* ;

syntax ConstructorDeclaration

syntax ConstructorDeclaration = ConstructorModifier* ConstructorDeclarator Throws? ConstructorBody ;

syntax ConstructorModifier

syntax ConstructorModifier = Annotation 
| "public"
| "protected"
| "private"
| "strictfp"
;

syntax ConstructorDeclarator

syntax ConstructorDeclarator = TypeParameters? SimpleTypeName "(" FormalParameterList? ")" ;

syntax SimpleTypeName

syntax SimpleTypeName = Identifier ;

syntax ConstructorBody

syntax ConstructorBody = "{" ExplicitConstructorInvocation? BlockStatements? "}" ";"* ;

syntax ExplicitConstructorInvocation

syntax ExplicitConstructorInvocation = TypeArguments? "this" "(" ArgumentList? ")" ";"  
| TypeArguments? "super" "(" ArgumentList? ")" ";"
| ExpressionName "." TypeArguments "super" "(" ArgumentList? ")" ";"
| Primary "." TypeArguments? "super" "(" ArgumentList? ")" ";"
;

syntax EnumDeclaration

syntax EnumDeclaration = enumDeclaration : ClassModifier* "enum" Identifier Superinterfaces? EnumBody ;

syntax EnumBody

syntax EnumBody = "{" EnumConstantList? ","? EnumBodyDeclarations? "}" ";"?;

syntax EnumConstantList

syntax EnumConstantList = { EnumConstant "," }+ ;

syntax EnumConstant

syntax EnumConstant = EnumConstantModifier* Identifier ("(" ArgumentList? ")")? ClassBody ? ;

syntax EnumConstantModifier

syntax EnumConstantModifier = Annotation ;

syntax EnumBodyDeclarations

syntax EnumBodyDeclarations =  ";" ClassBodyDeclaration* ;

syntax InterfaceDeclaration

syntax InterfaceDeclaration = NormalInterfaceDeclaration 
| AnnotationTypeDeclaration
;

syntax NormalInterfaceDeclaration

syntax NormalInterfaceDeclaration = normalInterfaceDeclaration : InterfaceModifier* "interface" Identifier TypeParameters? ExtendsInterfaces? InterfaceBody ;

syntax InterfaceModifier

syntax InterfaceModifier = Annotation 
| "public"
| "protected"
| "private"
| "abstract"
| "static"
| "strictfp"
;

syntax ExtendsInterfaces

syntax ExtendsInterfaces = "extends" {InterfaceType ","}+ ;

syntax InterfaceBody

syntax InterfaceBody = "{" InterfaceMemberDeclaration* "}" ";"* ;

syntax InterfaceMemberDeclaration

syntax InterfaceMemberDeclaration = ConstantDeclaration 
| InterfaceMethodDeclaration
| ClassDeclaration
| InterfaceDeclaration
;

syntax ConstantDeclaration

syntax ConstantDeclaration = ConstantModifier* UnannType VariableDeclaratorList ";" ;

syntax ConstantModifier

syntax ConstantModifier = Annotation 
| "public"
| "static"
| "final"
;

syntax InterfaceMethodDeclaration

syntax InterfaceMethodDeclaration = InterfaceMethodModifier* MethodHeader MethodBody ";"?;

syntax InterfaceMethodModifier

syntax InterfaceMethodModifier = Annotation 
| "public"
| "abstract"
| "default"
| "static"
| "strictfp"
;

syntax AnnotationTypeDeclaration

syntax AnnotationTypeDeclaration = InterfaceModifier* "@" "interface" Identifier AnnotationTypeBody ;

syntax AnnotationTypeBody

syntax AnnotationTypeBody = "{" AnnotationTypeMemberDeclaration* "}" ;

syntax AnnotationTypeMemberDeclaration

syntax AnnotationTypeMemberDeclaration = AnnotationTypeElementDeclaration 
| ConstantDeclaration
| ClassDeclaration
| InterfaceDeclaration
| InterfaceMethodDeclaration
// | ";"
;

syntax AnnotationTypeElementDeclaration

syntax AnnotationTypeElementDeclaration = AnnotationTypeElementModifier* UnannType Identifier "(" ")" Dims? DefaultValue? ;

syntax AnnotationTypeElementModifier

syntax AnnotationTypeElementModifier = Annotation 
| "public"
| "abstract"
;

syntax DefaultValue

syntax DefaultValue = "default" ElementValue ";"*;

syntax Annotation

syntax Annotation = NormalAnnotation 
| MarkerAnnotation
| SingleElementAnnotation
;

syntax NormalAnnotation

syntax NormalAnnotation = "@" TypeName "(" ElementValuePairList? ")" ;

syntax ElementValuePairList

syntax ElementValuePairList = {ElementValuePair ","}+ ;

syntax ElementValuePair

syntax ElementValuePair = Identifier "=" ElementValue ;

syntax ElementValue

syntax ElementValue = ConditionalExpression 
| ElementValueArrayInitializer
| Annotation
;

syntax ElementValueArrayInitializer

syntax ElementValueArrayInitializer = "{" ElementValueList? ","? "}" ;

syntax ElementValueList

syntax ElementValueList = { ElementValue "," }*;

syntax MarkerAnnotation

syntax MarkerAnnotation = "@" TypeName ;

syntax SingleElementAnnotation

syntax SingleElementAnnotation = "@" TypeName "(" ElementValue ")" ;

syntax ArrayInitializer

syntax ArrayInitializer = "{" VariableInitializerList? ","? "}" ;

syntax VariableInitializerList

syntax VariableInitializerList = { VariableInitializer "," }+ ;

syntax Block

syntax Block = "{" BlockStatements? "}" ;

syntax BlockStatements

syntax BlockStatements = BlockStatement BlockStatement* ;

syntax BlockStatement

syntax BlockStatement = LocalVariableDeclarationStatement 
| ClassDeclaration
| Statement
;

syntax LocalVariableDeclarationStatement

syntax LocalVariableDeclarationStatement = LocalVariableDeclaration ";"+ ;

syntax LocalVariableDeclaration

syntax LocalVariableDeclaration = VariableModifier* UnannType VariableDeclaratorList ;

syntax Statement

syntax Statement = StatementWithoutTrailingSubstatement 
| LabeledStatement
| IfThenStatement
| IfThenElseStatement
| WhileStatement
| ForStatement
;

syntax StatementNoShortIf

syntax StatementNoShortIf = StatementWithoutTrailingSubstatement 
| LabeledStatementNoShortIf
| IfThenElseStatementNoShortIf
| WhileStatementNoShortIf
| ForStatementNoShortIf
;

syntax StatementWithoutTrailingSubstatement

syntax StatementWithoutTrailingSubstatement = Block 
| EmptyStatement
| ExpressionStatement
| AssertStatement
| SwitchStatement
| DoStatement
| BreakStatement
| ContinueStatement
| ReturnStatement
| SynchronizedStatement
| ThrowStatement
| TryStatement
;

syntax EmptyStatement

syntax EmptyStatement = ";" ;

syntax LabeledStatement

syntax LabeledStatement = Identifier ":" Statement ;

syntax LabeledStatementNoShortIf

syntax LabeledStatementNoShortIf = Identifier ":"  StatementNoShortIf ;

syntax ExpressionStatement

syntax ExpressionStatement = StatementExpression ";" ;

syntax StatementExpression

syntax StatementExpression = Assignment 
| PreIncrementExpression
| PreDecrementExpression
| PostIncrementExpression
| PostDecrementExpression
| MethodInvocation
| ClassInstanceCreationExpression
;

syntax IfThenStatement

syntax IfThenStatement = "if" "(" Expression ")" Statement ;

syntax IfThenElseStatement

syntax IfThenElseStatement = "if" "(" Expression ")" StatementNoShortIf "else" Statement ;

syntax IfThenElseStatementNoShortIf

syntax IfThenElseStatementNoShortIf = "if" "(" Expression ")" StatementNoShortIf "else" StatementNoShortIf ;

syntax AssertStatement

syntax AssertStatement = "assert" Expression ";"   
| "assert" Expression ":" Expression ";"
;

syntax SwitchStatement

syntax SwitchStatement = "switch" "(" Expression ")" SwitchBlock ;

syntax SwitchBlock

syntax SwitchBlock = "{" SwitchBlockStatementGroups SwitchLabel* "}" ;

syntax SwitchBlockStatementGroups

syntax SwitchBlockStatementGroups = SwitchBlockStatementGroup* ;

syntax SwitchBlockStatementGroup

syntax SwitchBlockStatementGroup = SwitchLabels BlockStatements ;

syntax SwitchLabels

syntax SwitchLabels = SwitchLabel+ ;

syntax SwitchLabel

syntax SwitchLabel = "case" ConstantExpression ":" 
| "default" ":"
;

syntax EnumConstantName

syntax EnumConstantName = Identifier ;

syntax WhileStatement

syntax WhileStatement = whileStatement: "while" "(" Expression ")" Statement ;

syntax WhileStatementNoShortIf

syntax WhileStatementNoShortIf = "while" "(" Expression ")" StatementNoShortIf ;

syntax DoStatement

syntax DoStatement = "do" Statement "while" "(" Expression ")" ;

syntax ForStatement

syntax ForStatement = BasicForStatement  
| EnhancedForStatement
;

syntax ForStatementNoShortIf

syntax ForStatementNoShortIf = BasicForStatementNoShortIf 
| EnhancedForStatementNoShortIf
;

syntax BasicForStatement

syntax BasicForStatement = "for" "(" ForInit? ";" Expression? ";" ForUpdate? ")" Statement ;

syntax BasicForStatementNoShortIf

syntax BasicForStatementNoShortIf = "for" "(" ForInit? ";" Expression? ";" ForUpdate? ")" StatementNoShortIf ;

syntax ForInit

syntax ForInit = StatementExpressionList 
| LocalVariableDeclaration
;

syntax ForUpdate

syntax  ForUpdate = StatementExpressionList ;

syntax StatementExpressionList

syntax StatementExpressionList = {StatementExpression ","} + ;

syntax EnhancedForStatement

syntax EnhancedForStatement = enhancedForStatement: "for" "(" VariableModifier* UnannType VariableDeclaratorId ":" Expression ")" Statement ;

syntax EnhancedForStatementNoShortIf

syntax EnhancedForStatementNoShortIf = "for" "(" VariableModifier* UnannType VariableDeclaratorId ":" Expression ")" StatementNoShortIf ;

syntax BreakStatement

syntax BreakStatement = "break" Identifier? ";" ;

syntax ContinueStatement

syntax ContinueStatement = "continue" Identifier? ";" ;

syntax ReturnStatement

syntax ReturnStatement = "return" Expression? ";" ;

syntax ThrowStatement

syntax ThrowStatement = "throw" Expression ";" ;

syntax SynchronizedStatement

syntax SynchronizedStatement = "synchronized" "(" Expression ")" Block ;

syntax TryStatement

syntax TryStatement = "try" Block Catches 
| "try" Block Catches? Finally
| TryWithResourcesStatement
;

syntax Catches

syntax Catches = CatchClause+ ;

syntax CatchClause

syntax CatchClause = "catch" "(" CatchFormalParameter ")" Block ;

syntax CatchFormalParameter

syntax CatchFormalParameter = VariableModifier* CatchType VariableDeclaratorId ;

syntax CatchType

syntax CatchType = UnannClassType ("|" ClassType)* ;

syntax Finally

syntax Finally = "finally" Block ;

syntax TryWithResourcesStatement

syntax TryWithResourcesStatement = "try" ResourceSpecification Block Catches? Finally? ;

syntax ResourceSpecification

syntax ResourceSpecification = "(" ResourceList ";"? ")" ;

syntax ResourceList

syntax ResourceList = { Resource ";" }*;

syntax Resource

syntax Resource = VariableModifier* UnannType VariableDeclaratorId "=" Expression ;

syntax Primary

syntax Primary = PrimaryNoNewArray 
| ArrayCreationExpression
;

syntax PrimaryNoNewArray

syntax PrimaryNoNewArray = Literal 
| ClassLiteral
| "this"
| TypeName "." "this"
| "(" Expression ")"
| ClassInstanceCreationExpression
| FieldAccess
| ArrayAccess
| MethodInvocation
| MethodReference
;

syntax ClassLiteral

syntax ClassLiteral = TypeName ("[" "]")* "." "class" 
| NumericType ("[" "]")* "." "class"
| "boolean" ("[" "]")* "." "class"
| "void" "." "class"
;

syntax ClassInstanceCreationExpression

syntax ClassInstanceCreationExpression = UnqualifiedClassInstanceCreationExpression 
| ExpressionName "." UnqualifiedClassInstanceCreationExpression
| Primary "." UnqualifiedClassInstanceCreationExpression
;

syntax UnqualifiedClassInstanceCreationExpression

syntax UnqualifiedClassInstanceCreationExpression = "new" TypeArguments? ClassOrInterfaceTypeToInstantiate "(" ArgumentList? ")" 
| AIC ;

syntax AIC

syntax AIC = "new" TypeArguments? ClassOrInterfaceTypeToInstantiate "(" ArgumentList? ")" ClassBody ;

syntax ClassOrInterfaceTypeToInstantiate

syntax ClassOrInterfaceTypeToInstantiate = {AnnotatedType "."}* TypeArgumentsOrDiamond? ;

syntax AnnotatedType

syntax AnnotatedType = Annotation* Identifier ;

syntax TypeArgumentsOrDiamond

syntax TypeArgumentsOrDiamond = TypeArguments  
;

syntax FieldAccess

syntax FieldAccess = Primary "." Identifier 
| "super" "." Identifier
| TypeName "." "super" "." Identifier
;

syntax ArrayAccess

syntax ArrayAccess = ExpressionName "[" Expression "]" 
| PrimaryNoNewArray "[" Expression "]"
;

syntax MethodInvocation

syntax MethodInvocation = MethodName "(" ArgumentList? ")"  
| ExpressionName "." TypeArguments? Identifier "(" ArgumentList? ")"
| Primary "." TypeArguments? Identifier "(" ArgumentList? ")"
| "super" "." TypeArguments? Identifier "(" ArgumentList? ")"
| TypeName "." "super" "." TypeArguments? Identifier "(" ArgumentList? ")"
;

syntax ArgumentList

syntax ArgumentList = { Expression "," }+ ;

syntax MethodReference

syntax MethodReference = ExpressionName "::" TypeArguments? Identifier 
| Primary "::" TypeArguments? Identifier
| "super" "::" TypeArguments? Identifier
| TypeName "." "super" "::" TypeArguments? Identifier
| ClassType "::" TypeArguments? "new"
| ArrayType "::" "new"
;

syntax ArrayCreationExpression

syntax ArrayCreationExpression = "new" PrimitiveType DimExprs Dims? 
| "new" ClassOrInterfaceType DimExprs Dims?
| "new" PrimitiveType Dims ArrayInitializer
| "new" ClassOrInterfaceType Dims ArrayInitializer
;

syntax DimExprs

syntax DimExprs = DimExpr+ ;

syntax DimExpr

syntax DimExpr = Annotation* "[" Expression "]" ;

syntax Expression

syntax Expression = LambdaExpression 
| AssignmentExpression
;

syntax LambdaExpression

syntax LambdaExpression = LambdaParameters "-\>" LambdaBody ;

syntax LambdaParameters

syntax LambdaParameters = Identifier 
| "(" FormalParameterList? ")"
| "(" InferredFormalParameterList ")"
;

syntax InferredFormalParameterList

syntax InferredFormalParameterList = { Identifier "," }+;

syntax LambdaBody

syntax LambdaBody = Expression 
| Block
;

syntax AssignmentExpression

syntax AssignmentExpression = ConditionalExpression 
| Assignment
;

syntax Assignment

syntax Assignment = LeftHandSide AssignmentOperator Expression ;

syntax LeftHandSide

syntax LeftHandSide = ExpressionName 
| FieldAccess
| ArrayAccess
;

syntax AssignmentOperator

syntax AssignmentOperator = "=" 
| "*="
| "/="
| "%="
| "+="
| "-="
| "\<\<="
| "\>\>="
| "\>\>\>="
| "&="
| "^="
| "|="
;

syntax ConditionalExpression

syntax ConditionalExpression = ConditionalOrExpression 
| ConditionalOrExpression "?" Expression ":" ConditionalExpression
| ConditionalOrExpression "?" Expression ":" LambdaExpression
;

syntax ConditionalOrExpression

syntax ConditionalOrExpression = ConditionalAndExpression 
| ConditionalOrExpression "||" ConditionalAndExpression
;

syntax ConditionalAndExpression

syntax ConditionalAndExpression = InclusiveOrExpression 
| ConditionalAndExpression "&&" InclusiveOrExpression
;

syntax InclusiveOrExpression

syntax InclusiveOrExpression = ExclusiveOrExpression 
| InclusiveOrExpression "|" ExclusiveOrExpression
;

syntax ExclusiveOrExpression

syntax ExclusiveOrExpression = AndExpression 
| ExclusiveOrExpression "^" AndExpression
;

syntax AndExpression

syntax AndExpression = EqualityExpression 
| AndExpression "&" EqualityExpression
;

syntax EqualityExpression

syntax EqualityExpression = RelationalExpression 
| EqualityExpression "==" RelationalExpression
| EqualityExpression "!=" RelationalExpression
;

syntax RelationalExpression

syntax RelationalExpression = ShiftExpression 
| RelationalExpression "\<" ShiftExpression
| RelationalExpression "\>" ShiftExpression
| RelationalExpression "\<=" ShiftExpression
| RelationalExpression "\>=" ShiftExpression
| RelationalExpression "instanceof" ReferenceType
;

syntax ShiftExpression

syntax ShiftExpression = AdditiveExpression 
| ShiftExpression "\<\<" AdditiveExpression
| ShiftExpression "\>\>" AdditiveExpression
| ShiftExpression "\>\>\>" AdditiveExpression
;

syntax AdditiveExpression

syntax AdditiveExpression = MultiplicativeExpression 
| AdditiveExpression "+" !>> "+" MultiplicativeExpression
| AdditiveExpression "-" !>> "-" MultiplicativeExpression
;

syntax MultiplicativeExpression

syntax MultiplicativeExpression = UnaryExpression 
| MultiplicativeExpression "*" UnaryExpression
| MultiplicativeExpression "/" UnaryExpression
| MultiplicativeExpression "%" UnaryExpression
;

syntax UnaryExpression

syntax UnaryExpression = PreIncrementExpression 
| PreDecrementExpression
> "+" !>> "+" UnaryExpression
| "-" !>> "-" UnaryExpression
| UnaryExpressionNotPlusMinus
;

syntax PreIncrementExpression

syntax PreIncrementExpression = "++" UnaryExpression ;

syntax PreDecrementExpression

syntax PreDecrementExpression = "--" UnaryExpression ;

syntax UnaryExpressionNotPlusMinus

syntax UnaryExpressionNotPlusMinus = PostfixExpression 
| "~" UnaryExpression
| "!" UnaryExpression
| CastExpression
;

syntax PostfixExpression

syntax PostfixExpression = Primary 
| ExpressionName
| PostIncrementExpression
| PostDecrementExpression
;

syntax PostIncrementExpression

syntax PostIncrementExpression = PostfixExpression "++" ;

syntax PostDecrementExpression

syntax PostDecrementExpression = PostfixExpression "--" ;

syntax CastExpression

syntax CastExpression = "(" PrimitiveType ")" UnaryExpression 
| "(" ReferenceType AdditionalBound* ")" UnaryExpressionNotPlusMinus
| "(" ReferenceType AdditionalBound* ")" LambdaExpression
;

syntax ConstantExpression

syntax ConstantExpression = Expression ;

syntax SignedInteger

lexical SignedInteger =
[+ \-]? [0-9]+
;

syntax LEX_StringLiteral

lexical LEX_StringLiteral =
string: "\"" StringPart* "\""
;

syntax HexaSignificand

lexical HexaSignificand =
[0] [X x] [0-9 A-F a-f]* "." [0-9 A-F a-f]*
| [0] [X x] [0-9 A-F a-f]+
;

syntax OctaNumeral

lexical OctaNumeral =
[0] [0-7]+
;

syntax HexaNumeral

lexical HexaNumeral =
[0] [X x] [0-9 A-F a-f]+
;

syntax LEX_CharLiteral

lexical LEX_CharLiteral =
char: "\'" CharContent "\'"
;

syntax EscChar

lexical EscChar =
"\\"
;

syntax OctaEscape

lexical OctaEscape 
= "\\" [0-3] [0-7]+ !>> [0-7]
| "\\" [0-7] !>> [0-7]
| "\\" [4-7] [0-7]
;

syntax EscEscChar

lexical EscEscChar =
"\\\\"
;

syntax DeciNumeral

lexical DeciNumeral =
"0"
| [1-9]
| [1-9] [0-9 _]* [0-9];

syntax HexaSignificandKeywords

keyword HexaSignificandKeywords 
= "0X."
| "0x."
;

syntax BinaryNumeral

lexical BinaryNumeral =
"0" [b B] [0-1] [0-1 _]* !>> [0-1]
;

syntax StringChars

lexical StringChars =
FooStringChars
;

syntax LAYOUT

lexical LAYOUT =
[\t-\n \a0C-\a0D \ ]
| Comment
;

syntax CharContent

lexical CharContent =
EscapeSeq
| UnicodeEscape
| single: SingleChar
;

syntax Comment

lexical Comment =
"/**/"
| "//" EOLCommentChars !>> ![\n \a0D] LineTerminator
| "/*" !>> [*] CommentPart* "*/"
| "/**" !>> [/] CommentPart* "*/"
;

syntax FloatingPointLiteral

syntax FloatingPointLiteral =
float: HexaFloatLiteral !>> [D F d f]
| float: DeciFloatLiteral !>> [D F d f]
;

syntax OctaLiteral

lexical OctaLiteral =
OctaNumeral !>> [0-7] [L l]?
;

syntax HexaFloatNumeral

lexical HexaFloatNumeral =
HexaSignificand \ HexaSignificandKeywords !>> [0-9 A-F a-f] BinaryExponent
;

syntax IntegerLiteral

syntax IntegerLiteral =
hexa: HexaLiteral !>> [L l.]
| octa: OctaLiteral !>> [L l]
| deci: DeciLiteral !>> [L l]
| binary: BinaryIntegerLiteral !>> [L l]
;

syntax HexaLiteral

lexical HexaLiteral =
HexaNumeral !>> [0-9 A-F a-f] [L l]?
;

syntax DeciFloatLiteral

lexical DeciFloatLiteral =
DeciFloatNumeral [D F d f]?
;

syntax BinaryIntegerLiteral

lexical BinaryIntegerLiteral =
BinaryNumeral [L l]?
;

syntax ID

lexical ID =
// Yes, this would be more correct, but REALLY slow at the moment
//JavaLetter JavaLetterDigits*
//
// therefore we go the ascii route:
[$ A-Z _ a-z] [$ 0-9 A-Z _ a-z]*
;

syntax DeciFloatDigits

lexical DeciFloatDigits =
[0-9]+
| [0-9]* "." [0-9]*
;

syntax DeciLiteral

lexical DeciLiteral =
DeciNumeral !>> [. 0-9 D F d f] [L l]?
;

syntax EscapeSeq

lexical EscapeSeq =
NamedEscape
| OctaEscape
;

syntax LAYOUTLIST

layout LAYOUTLIST  =
LAYOUT* !>> [\t-\n \a0C-\a0D \ ] !>> "/*" !>> "//"
;

syntax NamedEscape

lexical NamedEscape =
namedEscape: "\\" [\" \' \\ b f n r t]
;

syntax BinaryExponent

lexical BinaryExponent =
[P p] SignedInteger !>> [0-9]
;

syntax BlockCommentChars

lexical BlockCommentChars =
![* \\]+
;

syntax Keyword

keyword Keyword =
"continue"
| "package"
| "short"
| "boolean"
| "for"
| "extends"
| "do"
| "strictfp"
| "if"
| "enum"
| "synchronized"
| "else"
| "interface"
| "return"
| "private"
| "volatile"
| "default"
| "throws"
| "static"
| "long"
| "throw"
| "this"
| "catch"
| "super"
| "const"
| "switch"
| "int"
| "implements"
| "native"
| "abstract"
| "break"
| "goto"
| "final"
| "class"
| "byte"
| "instanceof"
| "void"
| "finally"
| "try"
| "new"
| "float"
| "public"
| "transient"
| "char"
| "assert"
| "case"
| "while"
| "double"
| "protected"
| "import"
;

syntax FooStringChars

lexical FooStringChars =
([\a00] | ![\n \a0D \" \\])+
;

syntax StringPart

lexical StringPart =
UnicodeEscape
| EscapeSeq
| chars: StringChars !>> ![\n \a0D \" \\] !>> [\a00]
;

syntax EOLCommentChars

lexical EOLCommentChars =
![\n \a0D]*
;

syntax SingleChar

lexical SingleChar =
![\n \a0D \' \\]
;

syntax CommentPart

lexical CommentPart 
= UnicodeEscape
| BlockCommentChars !>> ![* \\]
| EscChar !>> [\\ u]
| Asterisk !>> [/]
| EscEscChar
;

syntax Identifier

syntax Identifier = id: [$ A-Z _ a-z] !<< ID \ IDKeywords !>> [$ 0-9 A-Z _ a-z];

syntax BooleanLiteral

syntax BooleanLiteral
= \false: "false"
| \true: "true"
;

syntax DeciFloatExponentPart

lexical DeciFloatExponentPart =
[E e] SignedInteger !>> [0-9]
;

syntax DeciFloatDigitsKeywords

keyword DeciFloatDigitsKeywords =
"."
;

syntax IDKeywords

keyword IDKeywords =
"null"
| Keyword
| "true"
| "false"
;

syntax DeciFloatNumeral

lexical DeciFloatNumeral
= [0-9] !<< [0-9]+ DeciFloatExponentPart
| [0-9] !<< [0-9]+ >> [D F d f]
| [0-9] !<< [0-9]+ "." [0-9]* !>> [0-9] DeciFloatExponentPart?
| [0-9] !<< "." [0-9]+ !>> [0-9] DeciFloatExponentPart?
;

syntax CarriageReturn

lexical CarriageReturn =
[\a0D]
;

syntax UnicodeEscape

lexical UnicodeEscape =
unicodeEscape: "\\" [u]+ [0-9 A-F a-f] [0-9 A-F a-f] [0-9 A-F a-f] [0-9 A-F a-f]
;

syntax LineTerminator

lexical LineTerminator =
[\n]
| () !>> ![]
| [\a0D] [\n]
| CarriageReturn !>> [\n]
;

syntax HexaFloatLiteral

lexical HexaFloatLiteral =
HexaFloatNumeral [D F d f]?
;

syntax Asterisk

lexical Asterisk =
"*"
;

syntax CharacterLiteral

syntax CharacterLiteral =
LEX_CharLiteral
;

syntax StringLiteral

syntax StringLiteral =
LEX_StringLiteral
;

syntax Null

syntax Null = "null" ;

syntax NullLiteral

syntax NullLiteral = Null ;