public class Parser extends Object
'JLS7' refers to the Java Language Specification, Java SE 7 Edition.
| Modifier and Type | Class and Description |
|---|---|
static class |
Parser.ClassDeclarationContext
The kinds of context where a class declaration can occur.
|
static class |
Parser.InterfaceDeclarationContext
The kinds of context where an interface declaration can occur.
|
| Constructor and Description |
|---|
Parser(Scanner scanner) |
Parser(Scanner scanner,
TokenStream tokenStream) |
| Modifier and Type | Method and Description |
|---|---|
protected CompileException |
compileException(String message)
Convenience method for throwing a
CompileException. |
String |
doc()
Gets the text of the doc comment (a.k.a.
|
Scanner |
getScanner() |
Location |
location() |
Java.Atom |
parseAdditiveExpression()
AdditiveExpression :=
MultiplicativeExpression { ( '+' | '-' ) MultiplicativeExpression }
|
Java.Atom |
parseAndExpression()
AndExpression :=
EqualityExpression { '&' EqualityExpression }
|
Java.AnnotationTypeDeclaration |
parseAnnotationTypeDeclarationRest(String optionalDocComment,
Java.Modifiers modifiers,
Parser.InterfaceDeclarationContext context)
AnnotationTypeDeclarationRest := Identifier AnnotationTypeBody
|
Java.Rvalue[] |
parseArgumentList()
ArgumentList := Expression { ',' Expression }
|
Java.Rvalue[] |
parseArguments()
Arguments := '(' [ ArgumentList ] ')'
|
Java.ArrayInitializer |
parseArrayInitializer()
ArrayInitializer :=
'{' [ VariableInitializer { ',' VariableInitializer } [ ',' ] '}'
|
Java.Statement |
parseAssertStatement()
AssertStatement := 'assert' Expression [ ':' Expression ] ';'
|
Java.Atom |
parseAssignmentExpression()
AssignmentExpression :=
ConditionalExpression [ AssignmentOperator AssignmentExpression ]
AssignmentOperator :=
'=' | '*=' | '/=' | '%=' | '+=' | '-=' | '<<=' |
'>>=' | '>>>=' | '&=' | '^=' | '|='
|
Java.Block |
parseBlock()
Block := '{' BlockStatements '}'
|
Java.BlockStatement |
parseBlockStatement()
BlockStatement :=
Statement | (1)
'class' ... | (2)
Modifiers Type VariableDeclarators ';' |
Expression ';' |
Expression BracketsOpt VariableDeclarators ';' (3)
(1) Includes the "labeled statement".
|
List<Java.BlockStatement> |
parseBlockStatements()
BlockStatements := { BlockStatement }
|
Java.Statement |
parseBreakStatement()
BreakStatement := 'break' [ Identifier ] ';'
|
Java.CatchParameter |
parseCatchParameter()
CatchFormalParameter := { VariableModifier } CatchType VariableDeclaratorId
CatchType := UnannClassType { '|' ClassType }
VariableModifier := Annotation | 'final'
VariableDeclaratorId := Identifier [ Dims ]
Dims := { Annotation } '[' ']' { { Annotation } '[' ']' }
UnannClassType :=
Identifier [ TypeArguments ]
| UnannClassOrInterfaceType '.' { Annotation } Identifier [ TypeArguments ]
UnannInterfaceType := UnannClassType
UnannClassOrInterfaceType := UnannClassType | UnannInterfaceType
ClassType :=
{ Annotation } Identifier [ TypeArguments ]
| ClassOrInterfaceType '.' { Annotation } Identifier [ TypeArguments ]
|
void |
parseClassBody(Java.AbstractClassDeclaration classDeclaration)
ClassBody := '{' { ClassBodyDeclaration } '}'
|
void |
parseClassBodyDeclaration(Java.AbstractClassDeclaration classDeclaration)
ClassBodyDeclaration :=
';' |
ModifiersOpt (
Block | // Instance (JLS7 8.6) or static initializer (JLS7 8.7)
'void' Identifier MethodDeclarationRest |
'class' ClassDeclarationRest |
'interface' InterfaceDeclarationRest |
ConstructorDeclarator |
[ TypeArguments ] Type Identifier MethodDeclarationRest |
Type Identifier FieldDeclarationRest ';'
)
|
Java.NamedClassDeclaration |
parseClassDeclarationRest(String optionalDocComment,
Java.Modifiers modifiers,
Parser.ClassDeclarationContext context)
ClassDeclarationRest :=
Identifier [ typeParameters ]
[ 'extends' ReferenceType ]
[ 'implements' ReferenceTypeList ]
ClassBody
|
Java.CompilationUnit |
parseCompilationUnit()
CompilationUnit := [ PackageDeclaration ]
{ ImportDeclaration }
{ TypeDeclaration }
|
Java.Atom |
parseConditionalAndExpression()
ConditionalAndExpression :=
InclusiveOrExpression { '&&' InclusiveOrExpression }
|
Java.Atom |
parseConditionalExpression()
ConditionalExpression :=
ConditionalOrExpression [ '?'
|
Java.Atom |
parseConditionalOrExpression()
ConditionalOrExpression :=
ConditionalAndExpression { '||' ConditionalAndExpression ]
|
Java.ConstructorDeclarator |
parseConstructorDeclarator(String optionalDocComment,
Java.Modifiers modifiers)
ConstructorDeclarator :=
Identifier
FormalParameters
[ 'throws' ReferenceTypeList ]
'{'
[ 'this' Arguments ';' | 'super' Arguments ';' | Primary '.'
|
Java.Statement |
parseContinueStatement()
ContinueStatement := 'continue' [ Identifier ] ';'
|
Java.Rvalue |
parseDimExpr()
DimExpr := '[' Expression ']'
|
Java.Rvalue[] |
parseDimExprs()
DimExprs := DimExpr { DimExpr }
|
Java.Statement |
parseDoStatement()
DoStatement := 'do' Statement 'while' '(' Expression ')' ';'
|
Java.Statement |
parseEmptyStatement()
EmptyStatement := ';'
|
void |
parseEnumBody(Java.EnumDeclaration enumDeclaration)
EnumBody := '{' [ EnumConstant { ',' EnumConstant } [ ',' ] [ ';' ] { ClassBodyDeclaration } '}'
|
Java.EnumConstant |
parseEnumConstant()
EnumConstant := [ Annotations ] Identifier [ Arguments ] [ ClassBody ]
|
Java.EnumDeclaration |
parseEnumDeclarationRest(String optionalDocComment,
Java.Modifiers modifiers,
Parser.ClassDeclarationContext context)
EnumDeclarationRest := Identifier [ 'implements' ReferenceTypeList ] EnumBody
|
Java.Atom |
parseEqualityExpression()
EqualityExpression :=
RelationalExpression { ( '==' | '!
|
Java.Atom |
parseExclusiveOrExpression()
ExclusiveOrExpression :=
AndExpression { '^' AndExpression }
|
Java.Atom |
parseExpression()
Expression := AssignmentExpression | LambdaExpression
Notice that all other kinds of lambda expressions are parsed in
parsePrimary(). |
Java.Rvalue[] |
parseExpressionList()
ExpressionList := Expression { ',' Expression }
|
Java.Statement |
parseExpressionStatement()
ExpressionStatement := Expression ';'
|
Java.VariableDeclarator[] |
parseFieldDeclarationRest(String name)
FieldDeclarationRest :=
VariableDeclaratorRest
{ ',' VariableDeclarator }
|
Java.FunctionDeclarator.FormalParameter |
parseFormalParameter(boolean[] hasEllipsis)
FormalParameter := [ 'final' ] Type FormalParameterRest
|
Java.FunctionDeclarator.FormalParameters |
parseFormalParameterList()
FormalParameterList := FormalParameter { ',' FormalParameter }
|
Java.FunctionDeclarator.FormalParameters |
parseFormalParameterListRest(Java.Type firstParameterType)
FormalParameterListRest := Identifier { ',' FormalParameter }
|
Java.FunctionDeclarator.FormalParameter |
parseFormalParameterRest(boolean finaL,
Java.Type type,
boolean[] hasEllipsis)
FormalParameterRest := [ '.' '.' '.' ] Identifier BracketsOpt
|
Java.FunctionDeclarator.FormalParameters |
parseFormalParameters()
FormalParameters := '(' [ FormalParameterList ] ')'
|
Java.Statement |
parseForStatement()
ForStatement :=
'for' '(' [ ForInit ] ';' [ Expression ] ';' [ ExpressionList ] ')' Statement
| 'for' '(' FormalParameter ':' Expression ')' Statement
ForInit :=
Modifiers Type VariableDeclarators
| ModifiersOpt PrimitiveType VariableDeclarators
| Expression VariableDeclarators (1)
| Expression { ',' Expression }
(1) "Expression" must pose a type.
|
Java.Statement |
parseIfStatement()
IfStatement := 'if' '(' Expression ')' Statement [ 'else' Statement ]
|
Java.CompilationUnit.ImportDeclaration |
parseImportDeclaration()
ImportDeclaration := 'import' ImportDeclarationBody ';'
|
Java.CompilationUnit.ImportDeclaration |
parseImportDeclarationBody()
ImportDeclarationBody := [ 'static' ] Identifier { '.'
|
Java.Atom |
parseInclusiveOrExpression()
InclusiveOrExpression :=
ExclusiveOrExpression { '|' ExclusiveOrExpression }
|
void |
parseInterfaceBody(Java.InterfaceDeclaration interfaceDeclaration)
InterfaceBody := '{' {
';' |
ModifiersOpt (
'void' Identifier MethodDeclarationRest |
'class' ClassDeclarationRest |
'interface' InterfaceDeclarationRest |
Type Identifier (
MethodDeclarationRest |
FieldDeclarationRest
)
)
} '}'
|
Java.InterfaceDeclaration |
parseInterfaceDeclarationRest(String optionalDocComment,
Java.Modifiers modifiers,
Parser.InterfaceDeclarationContext context)
InterfaceDeclarationRest :=
Identifier [ typeParameters ]
[ 'extends' ReferenceTypeList ]
InterfaceBody
|
Java.Statement |
parseLabeledStatement()
LabeledStatement := Identifier ':' Statement
|
Java.Rvalue |
parseLiteral()
Literal :=
IntegerLiteral
| FloatingPointLiteral
| BooleanLiteral
| CharacterLiteral
| StringLiteral
| NullLiteral
|
Java.Block |
parseMethodBody()
MethodBody := Block
|
Java.MethodDeclarator |
parseMethodDeclaration(boolean allowDefaultClause)
MethodDeclaration :=
[ DocComment ] Modifiers [ TypeParameters ] VoidOrType Identifier MethodDeclarationRest
|
Java.MethodDeclarator |
parseMethodDeclarationRest(String optionalDocComment,
Java.Modifiers modifiers,
Java.TypeParameter[] optionalTypeParameters,
Java.Type type,
String name,
boolean allowDefaultClause)
MethodDeclarationRest :=
FormalParameters
{ '[' ']' }
[ 'throws' ReferenceTypeList ]
[ 'default' expression ]
( ';' | MethodBody )
|
Java.Modifiers |
parseModifiers()
Modifiers := {
'public' | 'protected' | 'private' | 'static' | 'abstract' | 'final' | 'native'
| 'synchronized' | 'transient' | 'volatile' | 'strictfp'
| Annotation
| 'default'
}
Includes the case "no modifiers".
|
Java.Atom |
parseMultiplicativeExpression()
MultiplicativeExpression :=
UnaryExpression { ( '*' | '/' | '%' ) UnaryExpression }
|
Java.PackageDeclaration |
parsePackageDeclaration()
PackageDeclaration := 'package' QualifiedIdentifier ';'
|
Java.PackageMemberTypeDeclaration |
parsePackageMemberTypeDeclaration()
PackageMemberTypeDeclaration := ModifiersOpt PackageMemberTypeDeclarationRest
|
Java.Atom |
parsePrimary()
Primary :=
CastExpression | // CastExpression 15.16
'(' Expression ')' | // ParenthesizedExpression 15.8.5
Literal | // Literal 15.8.1
Name | // AmbiguousName
Name Arguments | // MethodInvocation
Name '[]' { '[]' } | // ArrayType 10.1
Name '[]' { '[]' } '.'
|
String[] |
parseQualifiedIdentifier()
QualifiedIdentifier := Identifier { '.'
|
Java.ReferenceType |
parseReferenceType()
ReferenceType := QualifiedIdentifier [ TypeArguments ]
|
Java.ReferenceType[] |
parseReferenceTypeList()
ReferenceTypeList := ReferenceType { ',' ReferenceType }
|
Java.Atom |
parseRelationalExpression()
RelationalExpression :=
ShiftExpression {
'instanceof' ReferenceType
| '<' ShiftExpression [ { ',' TypeArgument } '>' ]
| '<' TypeArgument [ { ',' TypeArgument } '>' ]
| ( '>' | '<=' | '>=' ) ShiftExpression
}
|
Java.Statement |
parseReturnStatement()
ReturnStatement := 'return' [ Expression ] ';'
|
Java.Atom |
parseSelector(Java.Atom atom)
Selector :=
'.'
|
Java.Atom |
parseShiftExpression()
ShiftExpression :=
AdditiveExpression { ( '<<' | '>>' | '>>>' ) AdditiveExpression }
|
Java.Statement |
parseStatement()
Statement :=
LabeledStatement |
Block |
IfStatement |
ForStatement |
WhileStatement |
DoStatement |
TryStatement |
'switch' ... |
'synchronized' ... |
ReturnStatement |
ThrowStatement |
BreakStatement |
ContinueStatement |
EmptyStatement |
ExpressionStatement
|
Java.Statement |
parseSwitchStatement()
SwitchStatement :=
'switch' '(' Expression ')' '{' { SwitchLabels BlockStatements } '}'
SwitchLabels := SwitchLabels { SwitchLabels }
SwitchLabel := 'case' Expression ':' | 'default' ':'
|
Java.Statement |
parseSynchronizedStatement()
SynchronizedStatement :=
'synchronized' '(' expression ')' Block
|
Java.Statement |
parseThrowStatement()
ThrowStatement := 'throw' Expression ';'
|
Java.Statement |
parseTryStatement()
TryStatement :=
'try' Block Catches [ Finally ] |
'try' Block Finally
Catches := CatchClause { CatchClause }
CatchClause := 'catch' '(' FormalParameter ')' Block
Finally := 'finally' Block
|
Java.Type |
parseType()
Type := (
'byte' | 'short' | 'char' | 'int' | 'long' |
'float' | 'double' | 'boolean' |
ReferenceType
) { '[' ']' }
|
Java.Atom |
parseUnaryExpression()
UnaryExpression :=
{ PrefixOperator } Primary { Selector } { PostfixOperator }
PrefixOperator := '++' | '--' | '+' | '-' | '~' | '!'
|
Java.VariableDeclarator |
parseVariableDeclarator()
VariableDeclarator := Identifier VariableDeclaratorRest
|
Java.VariableDeclarator |
parseVariableDeclaratorRest(String name)
VariableDeclaratorRest := { '[' ']' } [ '=' VariableInitializer ]
Used by field declarations and local variable declarations.
|
Java.VariableDeclarator[] |
parseVariableDeclarators()
VariableDeclarators := VariableDeclarator { ',' VariableDeclarator }
|
Java.ArrayInitializerOrRvalue |
parseVariableInitializer()
VariableInitializer :=
ArrayInitializer |
Expression
|
Java.Type |
parseVoidOrType()
VoidOrType := 'void' | Type
|
Java.Statement |
parseWhileStatement()
WhileStatement := 'while' '(' Expression ')' Statement
|
Token |
peek() |
int |
peek(String... suspected) |
boolean |
peek(String suspected) |
int |
peek(TokenType... suspected) |
boolean |
peek(TokenType suspected) |
boolean |
peekLiteral() |
Token |
peekNextButOne() |
boolean |
peekNextButOne(String suspected) |
boolean |
peekNextButOne(TokenType suspected) |
int |
peekRead(String... suspected) |
boolean |
peekRead(String suspected) |
String |
peekRead(TokenType suspected) |
Token |
read() |
int |
read(String... expected) |
void |
read(String expected) |
String |
read(TokenType expected) |
void |
setWarningHandler(WarningHandler optionalWarningHandler)
By default, warnings are discarded, but an application my install a
WarningHandler. |
public Parser(Scanner scanner)
public Parser(Scanner scanner, TokenStream tokenStream)
@Nullable public String doc()
null if the next token is not preceded by a doc commentpublic Scanner getScanner()
public Java.CompilationUnit parseCompilationUnit() throws CompileException, IOException
CompilationUnit := [ PackageDeclaration ]
{ ImportDeclaration }
{ TypeDeclaration }
CompileExceptionIOExceptionpublic Java.PackageDeclaration parsePackageDeclaration() throws CompileException, IOException
PackageDeclaration := 'package' QualifiedIdentifier ';'
CompileExceptionIOExceptionpublic Java.CompilationUnit.ImportDeclaration parseImportDeclaration() throws CompileException, IOException
ImportDeclaration := 'import' ImportDeclarationBody ';'
CompileExceptionIOExceptionpublic Java.CompilationUnit.ImportDeclaration parseImportDeclarationBody() throws CompileException, IOException
ImportDeclarationBody := [ 'static' ] Identifier { '.' Identifier } [ '.' '*' ]
CompileExceptionIOExceptionpublic String[] parseQualifiedIdentifier() throws CompileException, IOException
QualifiedIdentifier := Identifier { '.' Identifier }
CompileExceptionIOExceptionpublic Java.PackageMemberTypeDeclaration parsePackageMemberTypeDeclaration() throws CompileException, IOException
PackageMemberTypeDeclaration := ModifiersOpt PackageMemberTypeDeclarationRest
CompileExceptionIOExceptionpublic Java.Modifiers parseModifiers() throws CompileException, IOException
Modifiers := {
'public' | 'protected' | 'private' | 'static' | 'abstract' | 'final' | 'native'
| 'synchronized' | 'transient' | 'volatile' | 'strictfp'
| Annotation
| 'default'
}
Includes the case "no modifiers".
CompileExceptionIOExceptionpublic Java.NamedClassDeclaration parseClassDeclarationRest(@Nullable String optionalDocComment, Java.Modifiers modifiers, Parser.ClassDeclarationContext context) throws CompileException, IOException
ClassDeclarationRest :=
Identifier [ typeParameters ]
[ 'extends' ReferenceType ]
[ 'implements' ReferenceTypeList ]
ClassBody
CompileExceptionIOExceptionpublic Java.EnumDeclaration parseEnumDeclarationRest(@Nullable String optionalDocComment, Java.Modifiers modifiers, Parser.ClassDeclarationContext context) throws CompileException, IOException
EnumDeclarationRest := Identifier [ 'implements' ReferenceTypeList ] EnumBody
CompileExceptionIOExceptionpublic void parseClassBody(Java.AbstractClassDeclaration classDeclaration) throws CompileException, IOException
ClassBody := '{' { ClassBodyDeclaration } '}'
CompileExceptionIOExceptionpublic void parseEnumBody(Java.EnumDeclaration enumDeclaration) throws CompileException, IOException
EnumBody := '{' [ EnumConstant { ',' EnumConstant } [ ',' ] [ ';' ] { ClassBodyDeclaration } '}'
CompileExceptionIOExceptionpublic Java.EnumConstant parseEnumConstant() throws CompileException, IOException
EnumConstant := [ Annotations ] Identifier [ Arguments ] [ ClassBody ]
CompileExceptionIOExceptionpublic void parseClassBodyDeclaration(Java.AbstractClassDeclaration classDeclaration) throws CompileException, IOException
ClassBodyDeclaration :=
';' |
ModifiersOpt (
Block | // Instance (JLS7 8.6) or static initializer (JLS7 8.7)
'void' Identifier MethodDeclarationRest |
'class' ClassDeclarationRest |
'interface' InterfaceDeclarationRest |
ConstructorDeclarator |
[ TypeArguments ] Type Identifier MethodDeclarationRest |
Type Identifier FieldDeclarationRest ';'
)
CompileExceptionIOExceptionpublic Java.InterfaceDeclaration parseInterfaceDeclarationRest(@Nullable String optionalDocComment, Java.Modifiers modifiers, Parser.InterfaceDeclarationContext context) throws CompileException, IOException
InterfaceDeclarationRest :=
Identifier [ typeParameters ]
[ 'extends' ReferenceTypeList ]
InterfaceBody
CompileExceptionIOExceptionpublic Java.AnnotationTypeDeclaration parseAnnotationTypeDeclarationRest(@Nullable String optionalDocComment, Java.Modifiers modifiers, Parser.InterfaceDeclarationContext context) throws CompileException, IOException
AnnotationTypeDeclarationRest := Identifier AnnotationTypeBody
CompileExceptionIOExceptionpublic void parseInterfaceBody(Java.InterfaceDeclaration interfaceDeclaration) throws CompileException, IOException
InterfaceBody := '{' {
';' |
ModifiersOpt (
'void' Identifier MethodDeclarationRest |
'class' ClassDeclarationRest |
'interface' InterfaceDeclarationRest |
Type Identifier (
MethodDeclarationRest |
FieldDeclarationRest
)
)
} '}'
CompileExceptionIOExceptionpublic Java.ConstructorDeclarator parseConstructorDeclarator(@Nullable String optionalDocComment, Java.Modifiers modifiers) throws CompileException, IOException
ConstructorDeclarator :=
Identifier
FormalParameters
[ 'throws' ReferenceTypeList ]
'{'
[ 'this' Arguments ';' | 'super' Arguments ';' | Primary '.' 'super' Arguments ';' ]
BlockStatements
'}'
CompileExceptionIOExceptionpublic Java.MethodDeclarator parseMethodDeclaration(boolean allowDefaultClause) throws CompileException, IOException
MethodDeclaration :=
[ DocComment ] Modifiers [ TypeParameters ] VoidOrType Identifier MethodDeclarationRest
allowDefaultClause - Whether a "default clause" for an "annotation type element" (JLS8 9.6.2) should be
parsedCompileExceptionIOExceptionpublic Java.Type parseVoidOrType() throws CompileException, IOException
VoidOrType := 'void' | Type
CompileExceptionIOExceptionpublic Java.MethodDeclarator parseMethodDeclarationRest(@Nullable String optionalDocComment, Java.Modifiers modifiers, @Nullable Java.TypeParameter[] optionalTypeParameters, Java.Type type, String name, boolean allowDefaultClause) throws CompileException, IOException
MethodDeclarationRest :=
FormalParameters
{ '[' ']' }
[ 'throws' ReferenceTypeList ]
[ 'default' expression ]
( ';' | MethodBody )
allowDefaultClause - Whether a "default clause" for an "annotation type element" (JLS8 9.6.2) should be
parsedCompileExceptionIOExceptionpublic Java.ArrayInitializerOrRvalue parseVariableInitializer() throws CompileException, IOException
VariableInitializer :=
ArrayInitializer |
Expression
CompileExceptionIOExceptionpublic Java.ArrayInitializer parseArrayInitializer() throws CompileException, IOException
ArrayInitializer :=
'{' [ VariableInitializer { ',' VariableInitializer } [ ',' ] '}'
CompileExceptionIOExceptionpublic Java.FunctionDeclarator.FormalParameters parseFormalParameters() throws CompileException, IOException
FormalParameters := '(' [ FormalParameterList ] ')'
CompileExceptionIOExceptionpublic Java.FunctionDeclarator.FormalParameters parseFormalParameterList() throws CompileException, IOException
FormalParameterList := FormalParameter { ',' FormalParameter }
CompileExceptionIOExceptionpublic Java.FunctionDeclarator.FormalParameters parseFormalParameterListRest(Java.Type firstParameterType) throws CompileException, IOException
FormalParameterListRest := Identifier { ',' FormalParameter }
CompileExceptionIOExceptionpublic Java.FunctionDeclarator.FormalParameter parseFormalParameter(boolean[] hasEllipsis) throws CompileException, IOException
FormalParameter := [ 'final' ] Type FormalParameterRest
CompileExceptionIOExceptionpublic Java.FunctionDeclarator.FormalParameter parseFormalParameterRest(boolean finaL, Java.Type type, boolean[] hasEllipsis) throws CompileException, IOException
FormalParameterRest := [ '.' '.' '.' ] Identifier BracketsOpt
CompileExceptionIOExceptionpublic Java.CatchParameter parseCatchParameter() throws CompileException, IOException
CatchFormalParameter := { VariableModifier } CatchType VariableDeclaratorId
CatchType := UnannClassType { '|' ClassType }
VariableModifier := Annotation | 'final'
VariableDeclaratorId := Identifier [ Dims ]
Dims := { Annotation } '[' ']' { { Annotation } '[' ']' }
UnannClassType :=
Identifier [ TypeArguments ]
| UnannClassOrInterfaceType '.' { Annotation } Identifier [ TypeArguments ]
UnannInterfaceType := UnannClassType
UnannClassOrInterfaceType := UnannClassType | UnannInterfaceType
ClassType :=
{ Annotation } Identifier [ TypeArguments ]
| ClassOrInterfaceType '.' { Annotation } Identifier [ TypeArguments ]
CompileExceptionIOExceptionpublic Java.Block parseMethodBody() throws CompileException, IOException
MethodBody := Block
CompileExceptionIOExceptionpublic Java.Block parseBlock() throws CompileException, IOException
Block := '{' BlockStatements '}'
CompileExceptionIOExceptionpublic List<Java.BlockStatement> parseBlockStatements() throws CompileException, IOException
BlockStatements := { BlockStatement }
CompileExceptionIOExceptionpublic Java.BlockStatement parseBlockStatement() throws CompileException, IOException
BlockStatement :=
Statement | (1)
'class' ... | (2)
Modifiers Type VariableDeclarators ';' |
Expression ';' |
Expression BracketsOpt VariableDeclarators ';' (3)
(1) Includes the "labeled statement".
(2) Local class declaration.
(3) Local variable declaration statement; "Expression" must pose a type, and has optional trailing brackets.
CompileExceptionIOExceptionpublic Java.VariableDeclarator[] parseVariableDeclarators() throws CompileException, IOException
VariableDeclarators := VariableDeclarator { ',' VariableDeclarator }
CompileExceptionIOExceptionpublic Java.VariableDeclarator[] parseFieldDeclarationRest(String name) throws CompileException, IOException
FieldDeclarationRest :=
VariableDeclaratorRest
{ ',' VariableDeclarator }
CompileExceptionIOExceptionpublic Java.VariableDeclarator parseVariableDeclarator() throws CompileException, IOException
VariableDeclarator := Identifier VariableDeclaratorRest
CompileExceptionIOExceptionpublic Java.VariableDeclarator parseVariableDeclaratorRest(String name) throws CompileException, IOException
VariableDeclaratorRest := { '[' ']' } [ '=' VariableInitializer ]
Used by field declarations and local variable declarations.
CompileExceptionIOExceptionpublic Java.Statement parseStatement() throws CompileException, IOException
Statement :=
LabeledStatement |
Block |
IfStatement |
ForStatement |
WhileStatement |
DoStatement |
TryStatement |
'switch' ... |
'synchronized' ... |
ReturnStatement |
ThrowStatement |
BreakStatement |
ContinueStatement |
EmptyStatement |
ExpressionStatement
CompileExceptionIOExceptionpublic Java.Statement parseLabeledStatement() throws CompileException, IOException
LabeledStatement := Identifier ':' Statement
CompileExceptionIOExceptionpublic Java.Statement parseIfStatement() throws CompileException, IOException
IfStatement := 'if' '(' Expression ')' Statement [ 'else' Statement ]
CompileExceptionIOExceptionpublic Java.Statement parseForStatement() throws CompileException, IOException
ForStatement :=
'for' '(' [ ForInit ] ';' [ Expression ] ';' [ ExpressionList ] ')' Statement
| 'for' '(' FormalParameter ':' Expression ')' Statement
ForInit :=
Modifiers Type VariableDeclarators
| ModifiersOpt PrimitiveType VariableDeclarators
| Expression VariableDeclarators (1)
| Expression { ',' Expression }
(1) "Expression" must pose a type.
CompileExceptionIOExceptionpublic Java.Statement parseWhileStatement() throws CompileException, IOException
WhileStatement := 'while' '(' Expression ')' Statement
CompileExceptionIOExceptionpublic Java.Statement parseDoStatement() throws CompileException, IOException
DoStatement := 'do' Statement 'while' '(' Expression ')' ';'
CompileExceptionIOExceptionpublic Java.Statement parseTryStatement() throws CompileException, IOException
TryStatement :=
'try' Block Catches [ Finally ] |
'try' Block Finally
Catches := CatchClause { CatchClause }
CatchClause := 'catch' '(' FormalParameter ')' Block
Finally := 'finally' Block
CompileExceptionIOExceptionpublic Java.Statement parseSwitchStatement() throws CompileException, IOException
SwitchStatement :=
'switch' '(' Expression ')' '{' { SwitchLabels BlockStatements } '}'
SwitchLabels := SwitchLabels { SwitchLabels }
SwitchLabel := 'case' Expression ':' | 'default' ':'
CompileExceptionIOExceptionpublic Java.Statement parseSynchronizedStatement() throws CompileException, IOException
SynchronizedStatement :=
'synchronized' '(' expression ')' Block
CompileExceptionIOExceptionpublic Java.Statement parseReturnStatement() throws CompileException, IOException
ReturnStatement := 'return' [ Expression ] ';'
CompileExceptionIOExceptionpublic Java.Statement parseThrowStatement() throws CompileException, IOException
ThrowStatement := 'throw' Expression ';'
CompileExceptionIOExceptionpublic Java.Statement parseBreakStatement() throws CompileException, IOException
BreakStatement := 'break' [ Identifier ] ';'
CompileExceptionIOExceptionpublic Java.Statement parseContinueStatement() throws CompileException, IOException
ContinueStatement := 'continue' [ Identifier ] ';'
CompileExceptionIOExceptionpublic Java.Statement parseAssertStatement() throws CompileException, IOException
AssertStatement := 'assert' Expression [ ':' Expression ] ';'
CompileExceptionIOExceptionpublic Java.Statement parseEmptyStatement() throws CompileException, IOException
EmptyStatement := ';'
CompileExceptionIOExceptionpublic Java.Rvalue[] parseExpressionList() throws CompileException, IOException
ExpressionList := Expression { ',' Expression }
CompileExceptionIOExceptionpublic Java.Type parseType() throws CompileException, IOException
Type := (
'byte' | 'short' | 'char' | 'int' | 'long' |
'float' | 'double' | 'boolean' |
ReferenceType
) { '[' ']' }
CompileExceptionIOExceptionpublic Java.ReferenceType parseReferenceType() throws CompileException, IOException
ReferenceType := QualifiedIdentifier [ TypeArguments ]
CompileExceptionIOExceptionpublic Java.ReferenceType[] parseReferenceTypeList() throws CompileException, IOException
ReferenceTypeList := ReferenceType { ',' ReferenceType }
CompileExceptionIOExceptionpublic Java.Atom parseExpression() throws CompileException, IOException
Expression := AssignmentExpression | LambdaExpression
Notice that all other kinds of lambda expressions are parsed in parsePrimary().
CompileExceptionIOExceptionpublic Java.Atom parseAssignmentExpression() throws CompileException, IOException
AssignmentExpression :=
ConditionalExpression [ AssignmentOperator AssignmentExpression ]
AssignmentOperator :=
'=' | '*=' | '/=' | '%=' | '+=' | '-=' | '<<=' |
'>>=' | '>>>=' | '&=' | '^=' | '|='
CompileExceptionIOExceptionpublic Java.Atom parseConditionalExpression() throws CompileException, IOException
ConditionalExpression :=
ConditionalOrExpression [ '?' Expression ':' ConditionalExpression ]
CompileExceptionIOExceptionpublic Java.Atom parseConditionalOrExpression() throws CompileException, IOException
ConditionalOrExpression :=
ConditionalAndExpression { '||' ConditionalAndExpression ]
CompileExceptionIOExceptionpublic Java.Atom parseConditionalAndExpression() throws CompileException, IOException
ConditionalAndExpression :=
InclusiveOrExpression { '&&' InclusiveOrExpression }
CompileExceptionIOExceptionpublic Java.Atom parseInclusiveOrExpression() throws CompileException, IOException
InclusiveOrExpression :=
ExclusiveOrExpression { '|' ExclusiveOrExpression }
CompileExceptionIOExceptionpublic Java.Atom parseExclusiveOrExpression() throws CompileException, IOException
ExclusiveOrExpression :=
AndExpression { '^' AndExpression }
CompileExceptionIOExceptionpublic Java.Atom parseAndExpression() throws CompileException, IOException
AndExpression :=
EqualityExpression { '&' EqualityExpression }
CompileExceptionIOExceptionpublic Java.Atom parseEqualityExpression() throws CompileException, IOException
EqualityExpression :=
RelationalExpression { ( '==' | '!=' ) RelationalExpression }
CompileExceptionIOExceptionpublic Java.Atom parseRelationalExpression() throws CompileException, IOException
RelationalExpression :=
ShiftExpression {
'instanceof' ReferenceType
| '<' ShiftExpression [ { ',' TypeArgument } '>' ]
| '<' TypeArgument [ { ',' TypeArgument } '>' ]
| ( '>' | '<=' | '>=' ) ShiftExpression
}
CompileExceptionIOExceptionpublic Java.Atom parseShiftExpression() throws CompileException, IOException
ShiftExpression :=
AdditiveExpression { ( '<<' | '>>' | '>>>' ) AdditiveExpression }
CompileExceptionIOExceptionpublic Java.Atom parseAdditiveExpression() throws CompileException, IOException
AdditiveExpression :=
MultiplicativeExpression { ( '+' | '-' ) MultiplicativeExpression }
CompileExceptionIOExceptionpublic Java.Atom parseMultiplicativeExpression() throws CompileException, IOException
MultiplicativeExpression :=
UnaryExpression { ( '*' | '/' | '%' ) UnaryExpression }
CompileExceptionIOExceptionpublic Java.Atom parseUnaryExpression() throws CompileException, IOException
UnaryExpression :=
{ PrefixOperator } Primary { Selector } { PostfixOperator }
PrefixOperator := '++' | '--' | '+' | '-' | '~' | '!'
PostfixOperator := '++' | '--'
CompileExceptionIOExceptionpublic Java.Atom parsePrimary() throws CompileException, IOException
Primary :=
CastExpression | // CastExpression 15.16
'(' Expression ')' | // ParenthesizedExpression 15.8.5
Literal | // Literal 15.8.1
Name | // AmbiguousName
Name Arguments | // MethodInvocation
Name '[]' { '[]' } | // ArrayType 10.1
Name '[]' { '[]' } '.' 'class' | // ClassLiteral 15.8.2
'this' | // This 15.8.3
'this' Arguments | // Alternate constructor invocation 8.8.5.1
'super' Arguments | // Unqualified superclass constructor invocation 8.8.5.1
'super' '.' Identifier | // SuperclassFieldAccess 15.11.2
'super' '.' Identifier Arguments | // SuperclassMethodInvocation 15.12.4.9
NewClassInstance |
NewAnonymousClassInstance | // ClassInstanceCreationExpression 15.9
NewArray | // ArrayCreationExpression 15.10
NewInitializedArray | // ArrayInitializer 10.6
PrimitiveType { '[]' } | // Type
PrimitiveType { '[]' } '.' 'class' | // ClassLiteral 15.8.2
'void' '.' 'class' // ClassLiteral 15.8.2
CastExpression :=
'(' PrimitiveType { '[]' } ')' UnaryExpression |
'(' Expression ')' UnaryExpression
NewClassInstance := 'new' ReferenceType Arguments
NewAnonymousClassInstance := 'new' ReferenceType Arguments [ ClassBody ]
NewArray := 'new' Type DimExprs { '[]' }
NewInitializedArray := 'new' ArrayType ArrayInitializer
CompileExceptionIOExceptionpublic Java.Atom parseSelector(Java.Atom atom) throws CompileException, IOException
Selector :=
'.' Identifier | // FieldAccess 15.11.1
'.' Identifier Arguments | // MethodInvocation
'.' '<' TypeList '>' 'super' Arguments // Superconstructor invocation (?)
'.' '<' TypeList '>' 'super' '.' . Identifier // ???
'.' '<' TypeList '>' 'super' '.' . Identifier Arguments // Supermethod invocation
'.' '<' TypeList '>' Identifier Arguments // ExplicitGenericInvocation
'.' 'this' // QualifiedThis 15.8.4
'.' 'super' Arguments // Qualified superclass constructor invocation (JLS7 8.8.7.1)
'.' 'super' '.' Identifier | // SuperclassFieldReference (JLS7 15.11.2)
'.' 'super' '.' Identifier Arguments | // SuperclassMethodInvocation (JLS7 15.12.3)
'.' 'new' Identifier Arguments [ ClassBody ] | // QualifiedClassInstanceCreationExpression 15.9
'.' 'class'
'[' Expression ']' // ArrayAccessExpression 15.13
ExplicitGenericInvocationSuffix :=
'super' SuperSuffix
| Identifier Arguments
CompileExceptionIOExceptionpublic Java.Rvalue[] parseDimExprs() throws CompileException, IOException
DimExprs := DimExpr { DimExpr }
CompileExceptionIOExceptionpublic Java.Rvalue parseDimExpr() throws CompileException, IOException
DimExpr := '[' Expression ']'
CompileExceptionIOExceptionpublic Java.Rvalue[] parseArguments() throws CompileException, IOException
Arguments := '(' [ ArgumentList ] ')'
CompileExceptionIOExceptionpublic Java.Rvalue[] parseArgumentList() throws CompileException, IOException
ArgumentList := Expression { ',' Expression }
CompileExceptionIOExceptionpublic Java.Rvalue parseLiteral() throws CompileException, IOException
Literal :=
IntegerLiteral
| FloatingPointLiteral
| BooleanLiteral
| CharacterLiteral
| StringLiteral
| NullLiteral
CompileExceptionIOExceptionpublic Java.Statement parseExpressionStatement() throws CompileException, IOException
ExpressionStatement := Expression ';'
CompileExceptionIOExceptionpublic Location location()
public Token peek() throws CompileException, IOException
CompileExceptionIOExceptionpublic Token read() throws CompileException, IOException
CompileExceptionIOExceptionpublic boolean peek(String suspected) throws CompileException, IOException
CompileExceptionIOExceptionpublic int peek(String... suspected) throws CompileException, IOException
CompileExceptionIOExceptionpublic boolean peek(TokenType suspected) throws CompileException, IOException
CompileExceptionIOExceptionpublic int peek(TokenType... suspected) throws CompileException, IOException
CompileExceptionIOExceptionpublic Token peekNextButOne() throws CompileException, IOException
CompileExceptionIOExceptionpublic boolean peekNextButOne(String suspected) throws CompileException, IOException
CompileExceptionIOExceptionpublic boolean peekNextButOne(TokenType suspected) throws CompileException, IOException
CompileExceptionIOExceptionpublic void read(String expected) throws CompileException, IOException
CompileExceptionIOExceptionpublic int read(String... expected) throws CompileException, IOException
CompileExceptionIOExceptionpublic String read(TokenType expected) throws CompileException, IOException
CompileExceptionIOExceptionpublic boolean peekRead(String suspected) throws CompileException, IOException
CompileExceptionIOExceptionpublic int peekRead(String... suspected) throws CompileException, IOException
CompileExceptionIOException@Nullable public String peekRead(TokenType suspected) throws CompileException, IOException
CompileExceptionIOExceptionpublic boolean peekLiteral()
throws CompileException,
IOException
CompileExceptionIOExceptionpublic void setWarningHandler(@Nullable WarningHandler optionalWarningHandler)
WarningHandler.
Notice that there is no Parser.setErrorHandler() method, but parse errors always throw a CompileException. The reason being is that there is no reasonable way to recover from parse errors and
continue parsing, so there is no need to install a custom parse error handler.
optionalWarningHandler - null to indicate that no warnings be issuedprotected final CompileException compileException(String message)
CompileException.Copyright © 2019. All rights reserved.