module lang::java::\syntax::Java15
rascal-0.40.16
Java 5.
Usage
import lang::java::\syntax::Java15;
Dependencies
import ParseTree;
extend lang::sdf2::filters::DirectThenCountPreferAvoid;
Description
The grammar was based on the SDF2 definition in the Java-frontend project for Stratego/XT See https://github.com/metaborg/java-front
Pitfalls
- The grammar has been changed to use Rascal's disambiguation constructs rather than SDF2's constructs, but this has not been well tested.
syntax CompilationUnit
start syntax CompilationUnit =
compilationUnit: PackageDec? ImportDec* TypeDec*
;
syntax PackageDec
syntax PackageDec =
packageDec: Anno* "package" PackageName ";"
;
syntax PackageName
syntax PackageName =
packageName: {Id "."}+
;
syntax ImportDec
syntax ImportDec
= typeImportDec: "import" TypeName ";"
| typeImportOnDemandDec: "import" PackageName "." "*" ";"
| staticImportOnDemandDec: "import" "static" TypeName "." "*" ";"
| staticImportDec: "import" "static" TypeName "." Id ";"
;
syntax TypeDec
syntax TypeDec =
InterfaceDec
| ClassDec
| semicolon: ";"
;
syntax InterfaceDec
syntax InterfaceDec
= AnnoDecHead "{" AnnoElemDec* "}"
| InterfaceDecHead "{" InterfaceMemberDec* "}"
;
syntax AnnoDecHead
syntax AnnoDecHead
= annoDecHead: (InterfaceMod | Anno)* "@" "interface" Id
;
syntax AnnoElemDec
syntax AnnoElemDec
= semicolon: ";"
| ClassDec
| ConstantDec
| InterfaceDec
| annoMethodDec: (Anno | AbstractMethodMod)* Type Id "(" ")" DefaultVal? ";"
;
syntax InterfaceDecHead
syntax InterfaceDecHead =
interfaceDecHead: (InterfaceMod | Anno)* "interface" Id TypeParams? ExtendsInterfaces?
;
syntax LocalVarDec
syntax LocalVarDec =
@prefer localVarDec: (Anno | VarMod)* Type {VarDec ","}+
;
syntax TypeParams
syntax TypeParams =
typeParams: "\<" {TypeParam ","}+ "\>"
;
syntax Literal
syntax Literal =
FloatLiteral
| CharLiteral
| BoolLiteral
| ClassLiteral
| StringLiteral
| NullLiteral
| IntLiteral
;
syntax ClassDec
syntax ClassDec =
EnumDec
| classDec: ClassDecHead ClassBody
;
syntax ClassDecHead
syntax ClassDecHead =
classDecHead: (ClassMod | Anno)* "class" Id TypeParams? Super? Interfaces?
;
syntax SignedInteger
lexical SignedInteger =
[+ \-]? [0-9]+
;
syntax ClassMod
syntax ClassMod =
"static"
| "public"
| "abstract"
| "protected"
| "final"
| "strictfp"
| "private"
;
syntax LEX_StringLiteral
lexical LEX_StringLiteral =
string: "\"" StringPart* "\""
;
syntax SwitchGroup
syntax SwitchGroup =
switchGroup: SwitchLabel+ BlockStm+
;
syntax ClassBodyDec
syntax ClassBodyDec =
InstanceInit
| ClassMemberDec
| StaticInit
| ConstrDec
;
syntax FloatType
syntax FloatType =
float: "float"
| double: "double"
;
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 ClassMemberDec
syntax ClassMemberDec =
semicolon: ";"
| ClassDec
| MethodDec
| FieldDec
| InterfaceDec
;
syntax LEX_CharLiteral
lexical LEX_CharLiteral =
char: "\'" CharContent "\'"
;
syntax ConstantDec
syntax ConstantDec =
constantDec: (ConstantMod | Anno)* Type {VarDec ","}+ ";"
;
syntax ConstantMod
syntax ConstantMod =
"static"
| "public"
| "final"
;
syntax SwitchBlock
syntax SwitchBlock =
switchBlock: "{" SwitchGroup* SwitchLabel* "}"
;
syntax CondMid
syntax CondMid =
bracket "?" Expr ":"
;
syntax WildcardBound
syntax WildcardBound =
wildcardLowerBound: "super" RefType
| wildcardUpperBound: "extends" RefType
;
syntax EscChar
lexical EscChar =
"\\"
;
syntax EnumDecHead
syntax EnumDecHead =
enumDecHead: (Anno | ClassMod)* "enum" Id Interfaces?
;
syntax PackageOrTypeName
syntax PackageOrTypeName =
packageOrTypeName: PackageOrTypeName "." Id
| packageOrTypeName: Id
;
syntax OctaEscape
lexical OctaEscape
= "\\" [0-3] [0-7]+ !>> [0-7]
| "\\" [0-7] !>> [0-7]
| "\\" [4-7] [0-7]
;
syntax IntType
syntax IntType =
long: "long"
| short: "short"
| char: "char"
| \int: "int"
| byte: "byte"
;
syntax VarInit
syntax VarInit =
Expr
| ArrayInit
;
syntax EnumBodyDecs
syntax EnumBodyDecs =
enumBodyDecs: ";" ClassBodyDec*
;
syntax ClassType
syntax ClassType =
classType: TypeDecSpec TypeArgs?
;
syntax ExtendsInterfaces
syntax ExtendsInterfaces =
extendsInterfaces: "extends" {InterfaceType ","}+
;
syntax EscEscChar
lexical EscEscChar =
"\\\\"
;
syntax FormalParam
syntax FormalParam =
param: (Anno | VarMod)* Type VarDecId
| varArityParam: (Anno | VarMod)* Type "..." VarDecId
;
syntax StaticInit
syntax StaticInit =
staticInit: "static" Block
;
syntax EnumConstArgs
syntax EnumConstArgs =
bracket "(" {Expr ","}* ")"
;
syntax LocalVarDecStm
syntax LocalVarDecStm =
@prefer localVarDecStm: LocalVarDec ";"
;
syntax HexaSignificandKeywords
keyword HexaSignificandKeywords = "0x." | "0X.";
syntax StringChars
lexical StringChars =
FooStringChars
;
syntax EnumConst
syntax EnumConst =
enumConst: Anno* Id EnumConstArgs? ClassBody?
;
syntax LAYOUT
lexical LAYOUT =
[\t-\n \a0C-\a0D \ ]
| Comment
;
syntax NumType
syntax NumType =
FloatType
| IntType
;
syntax MethodDecHead
syntax MethodDecHead =
methodDecHead: (Anno | MethodMod)* TypeParams? ResultType Id "(" {FormalParam ","}* ")" Throws?
| deprMethodDecHead: (MethodMod | Anno)* TypeParams? ResultType Id "(" {FormalParam ","}* ")" Dim+ Throws?
;
syntax Anno
syntax Anno =
\anno: "@" TypeName "(" {ElemValPair ","}* ")"
| markerAnno: "@" TypeName
| singleElemAnno: "@" TypeName "(" ElemVal ")"
;
syntax CharContent
lexical CharContent =
EscapeSeq
| UnicodeEscape
| single: SingleChar
;
syntax FieldDec
syntax FieldDec =
fieldDec: (FieldMod | Anno)* Type {VarDec ","}+ ";"
;
syntax FieldMod
syntax FieldMod =
"public"
| "static"
| "transient"
| "protected"
| "volatile"
| "final"
| "private"
;
syntax Comment
lexical Comment =
"/**/"
| "//" EOLCommentChars !>> ![\n \a0D] LineTerminator
| "/*" !>> [*] CommentPart* "*/"
| "/**" !>> [/] CommentPart* "*/"
;
syntax ArraySubscript
syntax ArraySubscript =
bracket "[" Expr "]"
;
syntax ConstrBody
syntax ConstrBody =
constrBody: "{" ConstrInv? BlockStm* "}"
;
syntax FieldAccess
syntax FieldAccess =
superField: "super" "." Id
| qSuperField: TypeName "." "super" "." Id
;
syntax OctaLiteral
lexical OctaLiteral =
OctaNumeral !>> [0-7] [L l]?
;
syntax ConstrInv
syntax ConstrInv =
altConstrInv: TypeArgs? "this" "(" {Expr ","}* ")" ";"
| superConstrInv: TypeArgs? "super" "(" {Expr ","}* ")" ";"
| qSuperConstrInv: Expr "." TypeArgs? "super" "(" {Expr ","}* ")" ";"
;
syntax HexaFloatNumeral
lexical HexaFloatNumeral =
HexaSignificand \ HexaSignificandKeywords !>> [0-9 A-F a-f] BinaryExponent
;
syntax IntLiteral
syntax IntLiteral =
hexa: HexaLiteral !>> [L l.]
| octa: OctaLiteral !>> [L l]
| deci: DeciLiteral !>> [L l]
;
syntax HexaLiteral
lexical HexaLiteral =
HexaNumeral !>> [0-9 A-F a-f] [L l]?
;
syntax InterfaceMemberDec
syntax InterfaceMemberDec =
ClassDec
| semicolon: ";"
| InterfaceDec
| AbstractMethodDec
| ConstantDec
;
syntax ElemValPair
syntax ElemValPair =
elemValPair: Id "=" ElemVal
;
syntax CatchClause
syntax CatchClause =
\catch: "catch" "(" FormalParam ")" Block
;
syntax ArrayInit
syntax ArrayInit =
arrayInit: "{" {VarInit ","}* "," "}"
| arrayInit: "{" {VarInit ","}* "}"
;
syntax VarDecId
syntax VarDecId =
arrayVarDecId: Id Dim+
| Id
;
syntax Modifier
syntax Modifier =
"final"
| "strictfp"
| "private"
| "synchronized"
| "volatile"
| "protected"
| "transient"
| "abstract"
| "native"
| "static"
| "public"
;
syntax ElemVal
syntax ElemVal =
Anno
| elemValArrayInit: "{" {ElemVal ","}* "}"
| elemValArrayInit: "{" {ElemVal ","}* "," "}"
| Expr!assign
;
syntax InterfaceType
syntax InterfaceType =
interfaceType: TypeDecSpec TypeArgs?
;
syntax ConstrMod
syntax ConstrMod =
"public"
| "protected"
| "private"
;
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 ConstrDec
syntax ConstrDec =
constrDec: ConstrHead ConstrBody
;
syntax DeciFloatDigits
lexical DeciFloatDigits =
[0-9]+
| [0-9]* "." [0-9]*
;
syntax ArrayAccess
syntax ArrayAccess =
arrayAccess: Expr!postDecr!postIncr!preDecr!preIncr!not!complement!plus!plusDec!minus!remain!div!mul!rightShift!uRightShift!leftShift!instanceOf!gt!ltEq!lt!gtEq!eq!notEq!and!excOr!or!lazyAnd!lazyOr!cond!assign!assignLeftShift!assignOr!assignAnd!assignRightShift!assignMul!assignRemain!assignPlus!assignExcOr!assignDiv!assignURightShift!assignMinus!castRef!castPrim ArraySubscript
;
syntax ArrayBaseType
syntax ArrayBaseType =
PrimType
| TypeName
| unboundWld: TypeName "\<" "?" "\>"
;
syntax TypeName
syntax TypeName =
typeName: PackageOrTypeName "." Id
| typeName: Id
;
syntax FloatLiteral
syntax FloatLiteral =
float: HexaFloatLiteral !>> [D F d f]
| float: DeciFloatLiteral !>> [D F d f]
;
syntax DeciFloatLiteral
lexical DeciFloatLiteral
= [0-9] !<< [0-9]+ DeciFloatExponentPart [D F d f]?
| [0-9] !<< [0-9]+ [D F d f]
| [0-9] !<< [0-9]+ "." [0-9]* !>> [0-9] DeciFloatExponentPart? [D F d f]?
| [0-9] !<< "." [0-9]+ !>> [0-9] DeciFloatExponentPart? [D F d f]?
;
syntax DeciLiteral
lexical DeciLiteral =
DeciNumeral !>> [. 0-9 D F d f] [L l]?
;
syntax DeciNumeral
lexical DeciNumeral
= [1-9] [0-9]*
| "0"
;
syntax SwitchLabel
syntax SwitchLabel =
\default: "default" ":"
| \case: "case" Expr ":"
;
syntax ConstrHead
syntax ConstrHead =
constrDecHead: (ConstrMod | Anno)* TypeParams? Id "(" {FormalParam ","}* ")" Throws?
;
syntax Stm
syntax Stm =
\continue: "continue" Id? ";"
| forEach: "for" "(" FormalParam ":" Expr ")" Stm
| \try: "try" Block CatchClause* "finally" Block
| \throw: "throw" Expr ";"
| Block
| assertStm: "assert" Expr ":" Expr ";"
| \for: "for" "(" {Expr ","}* ";" Expr? ";" {Expr ","}* ")" Stm
| \try: "try" Block CatchClause+
| labeled: Id ":" Stm
| \for: "for" "(" LocalVarDec ";" Expr? ";" {Expr ","}* ")" Stm
| \switch: "switch" "(" Expr ")" SwitchBlock
| \if: "if" "(" Expr ")" Stm "else" Stm
| doWhile: "do" Stm "while" "(" Expr ")" ";"
| synchronized: "synchronized" "(" Expr ")" Block
| @prefer \if: "if" "(" Expr ")" Stm
| empty: ";"
| \while: "while" "(" Expr ")" Stm
| assertStm: "assert" Expr ";"
| \return: "return" Expr? ";"
| \break: "break" Id? ";"
| exprStm: Expr ";"
;
syntax NullLiteral
syntax NullLiteral =
null: "null"
;
syntax ExceptionType
syntax ExceptionType =
ClassType
;
syntax EscapeSeq
lexical EscapeSeq =
NamedEscape
| OctaEscape
;
syntax LAYOUTLIST
layout LAYOUTLIST =
LAYOUT* !>> [\t-\n \a0C-\a0D \ ] !>> "/*" !>> "//"
;
syntax ResultType
syntax ResultType =
\void: "void"
| Type
;
syntax Expr
syntax Expr =
FieldAccess
| newInstance: "new" TypeArgs? ClassOrInterfaceType "(" {Expr ","}* ")" ClassBody?
| invoke: MethodSpec "(" {Expr ","}* ")"
| bracket "(" Expr ")"
| lit: Literal
| qThis: TypeName "." "this"
| ArrayCreationExpr
| this: "this"
| ArrayAccess
;
syntax NamedEscape
lexical NamedEscape =
namedEscape: "\\" [\" \' \\ b f n r t]
;
syntax ArrayType
syntax ArrayType =
arrayType: Type "[" "]"
;
syntax ClassBody
syntax ClassBody =
classBody: "{" ClassBodyDec* "}"
;
syntax BinaryExponent
lexical BinaryExponent =
[P p] SignedInteger !>> [0-9]
;
syntax BlockCommentChars
lexical BlockCommentChars =
![* \\]+
;
syntax TypeDecSpec
syntax TypeDecSpec =
member: TypeDecSpec TypeArgs "." Id
| TypeName
;
syntax PrimType
syntax PrimType =
boolean: "boolean"
| NumType
;
syntax EnumDec
syntax EnumDec =
enumDec: EnumDecHead EnumBody
;
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 ActualTypeArg
syntax ActualTypeArg =
wildcard: "?" WildcardBound?
| Type
;
syntax StringPart
lexical StringPart =
UnicodeEscape
| EscapeSeq
| chars: StringChars !>> ![\n \a0D \" \\] !>> [\a00]
;
syntax MethodName
syntax MethodName =
methodName: AmbName "." Id
| methodName: Id
;
syntax EOLCommentChars
lexical EOLCommentChars =
![\n \a0D]*
;
syntax InterfaceMod
syntax InterfaceMod =
"protected"
| "public"
| "static"
| "abstract"
| "private"
| "strictfp"
;
syntax SingleChar
lexical SingleChar =
![\n \a0D \' \\]
;
syntax ClassLiteral
syntax ClassLiteral =
voidClass: "void" "." "class"
| class: Type "." "class"
;
syntax StringLiteral
syntax StringLiteral =
LEX_StringLiteral
;
syntax AbstractMethodDec
syntax AbstractMethodDec =
abstractMethodDec: (Anno | AbstractMethodMod)* TypeParams? ResultType Id "(" {FormalParam ","}* ")" Throws? ";"
| deprAbstractMethodDec: (Anno | AbstractMethodMod)* TypeParams? ResultType Id "(" {FormalParam ","}* ")" Dim+ Throws? ";"
;
syntax AbstractMethodMod
syntax AbstractMethodMod =
"abstract"
| "public"
;
syntax CommentPart
lexical CommentPart =
UnicodeEscape
| BlockCommentChars !>> ![* \\]
| EscChar !>> [\\ u]
| Asterisk !>> [/]
| EscEscChar
;
syntax Id
syntax Id =
id: [$ A-Z _ a-z] !<< ID \ IDKeywords !>> [$ 0-9 A-Z _ a-z]
;
syntax TypeBound
syntax TypeBound =
typeBound: "extends" {ClassOrInterfaceType "&"}+
;
syntax BoolLiteral
syntax BoolLiteral
= \false: "false"
| \true: "true"
;
syntax MethodBody
syntax MethodBody =
noMethodBody: ";"
| Block
;
syntax ExprName
syntax ExprName =
exprName: AmbName "." Id
| exprName: Id
;
syntax DefaultVal
syntax DefaultVal =
defaultVal: "default" ElemVal
;
syntax MethodDec
syntax MethodDec =
methodDec: MethodDecHead MethodBody
;
syntax AmbName
syntax AmbName =
ambName: Id
| ambName: AmbName "." Id
;
syntax MethodMod
syntax MethodMod =
"static"
| "protected"
| "synchronized"
| "strictfp"
| "private"
| "final"
| "public"
| "native"
| "abstract"
;
syntax RefType
syntax RefType =
ArrayType
| ClassOrInterfaceType
;
syntax ArrayCreationExpr
syntax ArrayCreationExpr =
newArray: "new" ArrayBaseType DimExpr+ Dim* !>> "["
| newArray: "new" ArrayBaseType Dim+ !>> "[" ArrayInit
;
syntax LHS
syntax LHS =
ExprName
| bracket "(" LHS ")"
| ArrayAccess
| FieldAccess
;
syntax TypeArgs
syntax TypeArgs =
typeArgs: "\<" {ActualTypeArg ","}+ "\>"
;
syntax TypeParam
syntax TypeParam =
typeParam: TypeVarId TypeBound?
;
syntax DeciFloatExponentPart
lexical DeciFloatExponentPart =
[E e] SignedInteger !>> [0-9]
;
syntax MethodSpec
syntax MethodSpec =
methodName: MethodName
| superMethod: "super" "." TypeArgs? Id
| genericMethod: AmbName "." TypeArgs Id
| qSuperMethod: TypeName "." "super" "." TypeArgs? Id
;
syntax Type
syntax Type =
PrimType
| RefType
;
syntax Super
syntax Super =
superDec: "extends" ClassType
;
syntax CharLiteral
syntax CharLiteral =
LEX_CharLiteral
;
syntax EndOfFile
lexical EndOfFile =
;
syntax InstanceInit
syntax InstanceInit =
instanceInit: Block
;
syntax IDKeywords
keyword IDKeywords =
"null"
| Keyword
| "true"
| "false"
;
syntax EnumBody
syntax EnumBody =
enumBody: "{" {EnumConst ","}* EnumBodyDecs? "}"
| enumBody: "{" {EnumConst ","}* "," EnumBodyDecs? "}"
;
syntax CarriageReturn
lexical CarriageReturn =
[\a0D]
;
syntax Throws
syntax Throws =
throwsDec: "throws" {ExceptionType ","}+
;
syntax Block
syntax Block =
block: "{" BlockStm* "}"
;
syntax TypeVar
syntax TypeVar =
typeVar: TypeVarId
;
syntax Dim
syntax Dim =
dim: "[" "]"
;
syntax TypeVarId
syntax TypeVarId =
Id
;
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]
| EndOfFile !>> ![]
| [\a0D] [\n]
| CarriageReturn !>> [\n]
;
syntax HexaFloatLiteral
lexical HexaFloatLiteral =
HexaFloatNumeral [D F d f]?
;
syntax BlockStm
syntax BlockStm =
Stm
| classDecStm: ClassDec
| LocalVarDecStm
;
syntax DimExpr
syntax DimExpr =
dim: "[" Expr "]"
;
syntax Interfaces
syntax Interfaces =
implementsDec: "implements" {InterfaceType ","}+
;
syntax Asterisk
lexical Asterisk =
"*"
;
syntax VarDec
syntax VarDec =
varDec: VarDecId "=" VarInit
| varDec: VarDecId
;
syntax VarMod
syntax VarMod =
"final"
;
syntax ClassOrInterfaceType
syntax ClassOrInterfaceType =
classOrInterfaceType: TypeDecSpec TypeArgs?
;
function expectedAmb
bool expectedAmb({(Expr)`(<RefType t>) <Expr e>`, appl(_,[(Expr)`(<ExprName n>)`,*_])})
bool expectedAmb({appl(_,[*_,(Expr)`(<RefType t>) <Expr e>`]), appl(_,[appl(_,[*_,(Expr)`(<ExprName n>)`]),*_])})
default bool expectedAmb(set[Tree] t)