Class Parser
'JLS7' refers to the Java Language Specification, Java SE 7 Edition.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enum
The kinds of context where a class declaration can occur.static enum
The kinds of context where an interface declaration can occur.static enum
The kinds of context where a method declaration can occur. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final String[]
private String
The optional JAVADOC comment preceding theinvalid reference
#nextToken
private boolean
private final Scanner
private int
private final TokenStream
private WarningHandler
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate Java.Modifier[]
annotationTypeElementModifiers
(Java.Modifier[] modifiers) private Java.Modifier[]
checkModifiers
(Java.Modifier[] modifiers, String... allowedKeywords) Verifies that the modifiers are consistent.private Java.Modifier[]
classModifiers
(Java.Modifier[] modifiers) protected final CompileException
compileException
(String message) Convenience method for throwing aCompileException
.protected static CompileException
compileException
(String message, Location location) Convenience method for throwing aCompileException
.private Java.Modifier[]
constantModifiers
(Java.Modifier[] modifiers) private Java.Modifier[]
constructorModifiers
(Java.Modifier[] modifiers) doc()
Gets the text of the doc comment (a.k.a.private Java.Modifier[]
enumConstantModifiers
(Java.Modifier[] modifiers) private Java.Modifier[]
fieldModifiers
(Java.Modifier[] modifiers) private int
private static boolean
hasAccessModifier
(Java.Modifier[] modifiers, String... keywords) private static boolean
hasAccessModifierOtherThan
(Java.Modifier[] modifiers, String... keywords) private Java.Modifier[]
interfaceMethodModifiers
(Java.Modifier[] modifiers) private Java.Modifier[]
interfaceModifiers
(Java.Modifier[] modifiers) private static String
location()
private Java.Modifier[]
methodModifiers
(Java.Modifier[] modifiers) private Java.Modifier[]
packageMemberClassModifiers
(Java.Modifier[] modifiers) private Java.Modifier[]
packageMemberInterfaceModifiers
(Java.Modifier[] modifiers) private Java.Modifier[]
packageModifiers
(Java.Modifier[] modifiers) CompilationUnit := [ PackageDeclaration ] { ImportDeclaration } { TypeDeclaration }AdditiveExpression := MultiplicativeExpression { ( '+' | '-' ) MultiplicativeExpression }AndExpression := EqualityExpression { '&' EqualityExpression }private Java.Annotation
Annotation := MarkerAnnotation // JLS7 9.7.2 | SingleElementAnnotation // JLS7 9.7.3 | NormalAnnotation // JLS7 9.7.1 MarkerAnnotation := '@' Identifier SingleElementAnnotation := '@' Identifier '(' ElementValue ')' NormalAnnotation := '@' TypeName '(' ElementValuePairsOpt ')' ElementValuePairsOpt := [ ElementValuePair { ',' ElementValuePair } ]parseAnnotationTypeDeclarationRest
(String docComment, Java.Modifier[] modifiers, Parser.InterfaceDeclarationContext context) AnnotationTypeDeclarationRest := Identifier AnnotationTypeBodyArgumentList := Expression { ',' Expression }Arguments := '(' [ ArgumentList ] ')'ArrayInitializer := '{' [ VariableInitializer { ',' VariableInitializer } [ ',' ] '}'AssertStatement := 'assert' Expression [ ':' Expression ] ';'AssignmentExpression := ConditionalExpression [ AssignmentOperator AssignmentExpression ] AssignmentOperator := '=' | '*=' | '/=' | '%=' | '+=' | '-=' | '<<=' | '>>=' | '>>>=' | '&=' | '^=' | '|='Block := '{' BlockStatements '}'BlockStatement := Statement | (1) 'class' ...BlockStatements := { BlockStatement }(package private) int
BracketsOpt := { '[' ']' }BreakStatement := 'break' [ Identifier ] ';'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 ';' )parseClassDeclarationRest
(String docComment, Java.Modifier[] modifiers, Parser.ClassDeclarationContext context) ClassDeclarationRest := Identifier [ typeParameters ] [ 'extends' ReferenceType ] [ 'implements' ReferenceTypeList ] ClassBodyConditionalAndExpression := InclusiveOrExpression { '&&' InclusiveOrExpression }ConditionalExpression := ConditionalOrExpression [ '?' Expression ':' ConditionalExpression ]ConditionalOrExpression := ConditionalAndExpression { '||' ConditionalAndExpression ]parseConstructorDeclarator
(String docComment, Java.Modifier[] modifiers) ConstructorDeclarator := Identifier FormalParameters [ 'throws' ReferenceTypeList ] '{' [ 'this' Arguments ';' | 'super' Arguments ';' | Primary '.' 'super' Arguments ';' ] BlockStatements '}'ContinueStatement := 'continue' [ Identifier ] ';'DimExpr := '[' Expression ']'DimExprs := DimExpr { DimExpr }DoStatement := 'do' Statement 'while' '(' Expression ')' ';'private Java.ElementValue
ElementValue := ConditionalExpression | Annotation | ElementValueArrayInitializerprivate Java.ElementValue
ElementValueArrayInitializer := '{' { ElementValue | ',' } '}'private Java.ElementValuePair
ElementValuePair := Identifier '=' ElementValueEmptyStatement := ';'void
parseEnumBody
(Java.EnumDeclaration enumDeclaration) EnumBody := '{' [ EnumConstant { ',' EnumConstant } [ ',' ] [ ';' ] { ClassBodyDeclaration } '}'EnumConstant := [ Annotations ] Identifier [ Arguments ] [ ClassBody ]parseEnumDeclarationRest
(String docComment, Java.Modifier[] modifiers, Parser.ClassDeclarationContext context) EnumDeclarationRest := Identifier [ 'implements' ReferenceTypeList ] EnumBodyEqualityExpression := RelationalExpression { ( '==' | '!=' ) RelationalExpression }ExclusiveOrExpression := AndExpression { '^' AndExpression }Expression := AssignmentExpression | LambdaExpressionExpressionList := Expression { ',' Expression }ExpressionStatement := Expression ';'FieldDeclarationRest := VariableDeclaratorRest { ',' VariableDeclarator }parseFormalParameter
(boolean[] hasEllipsis) FormalParameter := [ 'final' ] Type FormalParameterRestFormalParameterList := FormalParameter { ',' FormalParameter }parseFormalParameterListRest
(Java.Type firstParameterType) FormalParameterListRest := Identifier { ',' FormalParameter }parseFormalParameterRest
(Java.Modifier[] modifiers, Java.Type type, boolean[] hasEllipsis) FormalParameterRest := [ '.' '.' '.' ] Identifier BracketsOptFormalParameters := '(' [ FormalParameterList ] ')'ForStatement := 'for' '(' [ ForInit ] ';' [ Expression ] ';' [ ExpressionList ] ')' Statement | 'for' '(' FormalParameter ':' Expression ')' Statement ForInit := Modifiers Type VariableDeclarators | ModifiersOpt PrimitiveType VariableDeclarators | Expression VariableDeclarators (1) | Expression { ',' Expression }IfStatement := 'if' '(' Expression ')' Statement [ 'else' Statement ]ImportDeclaration := 'import' ImportDeclarationBody ';'ImportDeclarationBody := [ 'static' ] Identifier { '.' Identifier } [ '.' '*' ]InclusiveOrExpression := ExclusiveOrExpression { '|' ExclusiveOrExpression }void
parseInterfaceBody
(Java.InterfaceDeclaration interfaceDeclaration) InterfaceBody := '{' { ';' | ModifiersOpt ( 'void' Identifier MethodDeclarationRest | 'class' ClassDeclarationRest | 'interface' InterfaceDeclarationRest | Type Identifier ( MethodDeclarationRest | FieldDeclarationRest ) ) } '}'parseInterfaceDeclarationRest
(String docComment, Java.Modifier[] modifiers, Parser.InterfaceDeclarationContext context) InterfaceDeclarationRest := Identifier [ typeParameters ] [ 'extends' ReferenceTypeList ] InterfaceBodyLabeledStatement := Identifier ':' Statementprivate Java.LambdaBody
LambdaBody :=private Java.LambdaExpression
LambdaExpression := LambdaParameters '->' LambdaBodyprivate Java.LambdaParameters
LambdaParameters := Identifier | '(' [ FormalParameterList ] ')' | '(' InferredFormalParameterList ')'Literal := IntegerLiteral | FloatingPointLiteral | BooleanLiteral | CharacterLiteral | StringLiteral | NullLiteralMethodBody := BlockEquivalent withparseMethodDeclaration(false, MethodDeclarationContext.CLASS_DECLARATION)
.parseMethodDeclaration
(boolean allowDefaultClause, Parser.MethodDeclarationContext context) MethodDeclaration := [ DocComment ] Modifiers [ TypeParameters ] VoidOrType Identifier MethodDeclarationRestparseMethodDeclarationRest
(String docComment, Java.Modifier[] modifiers, Java.TypeParameter[] typeParameters, Java.Type type, String name, boolean allowDefaultClause, Parser.MethodDeclarationContext context) MethodDeclarationRest := FormalParameters { '[' ']' } [ 'throws' ReferenceTypeList ] [ 'default' expression ] ( ';' | MethodBody )Modifiers := { Modifier }parseModuleDeclarationRest
(Java.Modifier[] modifiers) ModuleDeclarationRest := [ 'open' ] 'module' identifier { '.' identifier} '{' { ModuleDirective } '}'MultiplicativeExpression := UnaryExpression { ( '*' | '/' | '%' ) UnaryExpression }Modifier := Annotation | 'public' | 'protected' | 'private' | 'static' | 'abstract' | 'final' | 'native' | 'synchronized' | 'transient' | 'volatile' | 'strictfp' | 'default'PackageDeclaration := 'package' QualifiedIdentifier ';'parsePackageDeclarationRest
(String docComment, Java.Modifier[] modifiers) PackageDeclaration := { PackageModifier } 'package' identifier { '.' identifier} ';'PackageMemberTypeDeclaration := ModifiersOpt PackageMemberTypeDeclarationRestparsePackageMemberTypeDeclarationRest
(String docComment, Java.Modifier[] modifiers) PackageMemberTypeDeclarationRest := 'class' ClassDeclarationRest | 'enum' EnumDeclarationRest | 'interface' InterfaceDeclarationRest '@' 'interface' AnnotationTypeDeclarationRestPrimary := 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 MethodReference // MethodReference JLS9 15.13 Name := Identifier { '.' Identifier } CastExpression := '(' PrimitiveType { '[]' } ')' UnaryExpression | '(' Expression ')' UnaryExpression NewClassInstance := 'new' ReferenceType Arguments NewAnonymousClassInstance := 'new' ReferenceType Arguments [ ClassBody ] NewArray := 'new' Type DimExprs { '[]' } NewInitializedArray := 'new' ArrayType ArrayInitializerString[]
QualifiedIdentifier := Identifier { '.' Identifier }ReferenceType := { Annotation } QualifiedIdentifier [ TypeArguments ]ReferenceTypeList := ReferenceType { ',' ReferenceType }RelationalExpression := ShiftExpression { 'instanceof' ReferenceType | '<' ShiftExpression [ { ',' TypeArgument } '>' ] | '<' TypeArgument [ { ',' TypeArgument } '>' ] | ( '>' | '<=' | '>=' ) ShiftExpression }private Java.TryStatement.Resource
Resource := Modifiers Type VariableDeclarator | VariableAccessReturnStatement := 'return' [ Expression ] ';'parseSelector
(Java.Atom atom) Selector := '.' Identifier | // FieldAccess 15.11.1 '.' Identifier Arguments | // MethodInvocation '.' '<' TypeList '>' 'super' Arguments // Superconstructor invocation (?) '.' '<' TypeList '>' 'super' '.' .ShiftExpression := AdditiveExpression { ( '<<' | '>>' | '>>>' ) AdditiveExpression }Statement := LabeledStatement | Block | IfStatement | ForStatement | WhileStatement | DoStatement | TryStatement | 'switch' ...SwitchStatement := 'switch' '(' Expression ')' '{' { SwitchLabels BlockStatements } '}' SwitchLabels := SwitchLabels { SwitchLabels } SwitchLabel := 'case' Expression ':' | 'default' ':'SynchronizedStatement := 'synchronized' '(' expression ')' BlockThrowStatement := 'throw' Expression ';'TryStatement := 'try' Block Catches [ Finally ] | 'try' Block Finally Catches := CatchClause { CatchClause } CatchClause := 'catch' '(' FormalParameter ')' Block Finally := 'finally' BlockType := ( 'byte' | 'short' | 'char' | 'int' | 'long' | 'float' | 'double' | 'boolean' | ReferenceType ) { '[' ']' }private Java.TypeArgument
TypeArgument := ReferenceType { '[' ']' } <= The optional brackets are missing in JLS7, section 18!? | PrimitiveType '[' ']' { '[' ']' } | '?' extends ReferenceType | '?' super ReferenceTypeprivate Java.TypeArgument[]
TypeArguments := 'invalid input: '<'' [ TypeArgument { ',' TypeArgument } ] '>'private Java.TypeParameter
TypeParameter := identifier [ 'extends' ( identifier | ReferenceType { 'invalid input: '&'' ReferenceType }private Java.TypeParameter[]
TypeParameters := 'invalid input: '<'' TypeParameter { ',' TypeParameter } '>'UnaryExpression := { PrefixOperator } Primary { Selector } { PostfixOperator } PrefixOperator := '++' | '--' | '+' | '-' | '~' | '!' PostfixOperator := '++' | '--'VariableDeclarator := Identifier VariableDeclaratorRestVariableDeclaratorRest := { '[' ']' } [ '=' VariableInitializer ]VariableDeclarators := VariableDeclarator { ',' VariableDeclarator }VariableInitializer := ArrayInitializer | ExpressionVoidOrType := 'void' | TypeWhileStatement := 'while' '(' Expression ')' Statementpeek()
boolean
int
boolean
int
private boolean
boolean
peekNextButOne
(String suspected) boolean
peekNextButOne
(TokenType suspected) boolean
int
read()
void
int
void
setSourceVersion
(int version) void
setWarningHandler
(WarningHandler warningHandler) By default, warnings are discarded, but an application my install aWarningHandler
.private Java.Modifier[]
variableModifiers
(Java.Modifier[] modifiers) private void
Issues a warning if the given identifier does not comply with the class and interface type naming conventions (JLS7 6.8.2).private void
Issues a warning if the given identifier does not comply with the field naming conventions (JLS7 6.8.4) and constant naming conventions (JLS7 6.8.5).private void
Issues a warning if the given identifier does not comply with the local variable and parameter naming conventions (JLS7 6.8.6).private void
Issues a warning if the given identifier does not comply with the method naming conventions (JLS7 6.8.3).private void
Issues a warning if the given string does not comply with the package naming conventions.private void
private void
Issues a warning with the given message and location and returns.
-
Field Details
-
scanner
-
tokenStream
-
docComment
The optional JAVADOC comment preceding theinvalid reference
#nextToken
-
ACCESS_MODIFIER_KEYWORDS
-
preferParametrizedTypes
private boolean preferParametrizedTypes -
sourceVersion
private int sourceVersion -
warningHandler
-
MUTUALLY_EXCLUSIVE_ACCESS_MODIFIERS
-
-
Constructor Details
-
Parser
-
Parser
-
-
Method Details
-
doc
Gets the text of the doc comment (a.k.a. "JAVADOC comment") preceding the next token.- Returns:
null
if the next token is not preceded by a doc comment
-
getScanner
- Returns:
- The scanner that produces the tokens for this parser.
-
parseAbstractCompilationUnit
public Java.AbstractCompilationUnit parseAbstractCompilationUnit() throws CompileException, IOExceptionCompilationUnit := [ PackageDeclaration ] { ImportDeclaration } { TypeDeclaration }
- Throws:
CompileException
IOException
-
parseModuleDeclarationRest
public Java.ModuleDeclaration parseModuleDeclarationRest(Java.Modifier[] modifiers) throws CompileException, IOException ModuleDeclarationRest := [ 'open' ] 'module' identifier { '.' identifier} '{' { ModuleDirective } '}'
- Throws:
CompileException
IOException
-
parsePackageDeclaration
PackageDeclaration := 'package' QualifiedIdentifier ';'
- Throws:
CompileException
IOException
-
parsePackageDeclarationRest
public Java.PackageDeclaration parsePackageDeclarationRest(@Nullable String docComment, Java.Modifier[] modifiers) throws CompileException, IOException PackageDeclaration := { PackageModifier } 'package' identifier { '.' identifier} ';'
- Throws:
CompileException
IOException
-
parseImportDeclaration
public Java.AbstractCompilationUnit.ImportDeclaration parseImportDeclaration() throws CompileException, IOExceptionImportDeclaration := 'import' ImportDeclarationBody ';'
- Throws:
CompileException
IOException
-
parseImportDeclarationBody
public Java.AbstractCompilationUnit.ImportDeclaration parseImportDeclarationBody() throws CompileException, IOExceptionImportDeclarationBody := [ 'static' ] Identifier { '.' Identifier } [ '.' '*' ]
- Throws:
CompileException
IOException
-
parseQualifiedIdentifier
QualifiedIdentifier := Identifier { '.' Identifier }
- Throws:
CompileException
IOException
-
parsePackageMemberTypeDeclaration
public Java.PackageMemberTypeDeclaration parsePackageMemberTypeDeclaration() throws CompileException, IOExceptionPackageMemberTypeDeclaration := ModifiersOpt PackageMemberTypeDeclarationRest
- Throws:
CompileException
IOException
-
parsePackageMemberTypeDeclarationRest
private Java.PackageMemberTypeDeclaration parsePackageMemberTypeDeclarationRest(@Nullable String docComment, Java.Modifier[] modifiers) throws CompileException, IOException PackageMemberTypeDeclarationRest := 'class' ClassDeclarationRest | 'enum' EnumDeclarationRest | 'interface' InterfaceDeclarationRest '@' 'interface' AnnotationTypeDeclarationRest
- Throws:
CompileException
IOException
-
parseModifiers
Modifiers := { Modifier }
Includes the case "no modifiers".
- Throws:
CompileException
IOException
-
parseOptionalModifier
Modifier := Annotation | 'public' | 'protected' | 'private' | 'static' | 'abstract' | 'final' | 'native' | 'synchronized' | 'transient' | 'volatile' | 'strictfp' | 'default'
- Throws:
CompileException
IOException
-
parseAnnotation
Annotation := MarkerAnnotation // JLS7 9.7.2 | SingleElementAnnotation // JLS7 9.7.3 | NormalAnnotation // JLS7 9.7.1 MarkerAnnotation := '@' Identifier SingleElementAnnotation := '@' Identifier '(' ElementValue ')' NormalAnnotation := '@' TypeName '(' ElementValuePairsOpt ')' ElementValuePairsOpt := [ ElementValuePair { ',' ElementValuePair } ]
- Throws:
CompileException
IOException
-
parseElementValuePair
ElementValuePair := Identifier '=' ElementValue
- Throws:
CompileException
IOException
-
parseElementValue
ElementValue := ConditionalExpression | Annotation | ElementValueArrayInitializer
- Throws:
CompileException
IOException
-
parseElementValueArrayInitializer
ElementValueArrayInitializer := '{' { ElementValue | ',' } '}'
- Throws:
CompileException
IOException
-
parseClassDeclarationRest
public Java.NamedClassDeclaration parseClassDeclarationRest(@Nullable String docComment, Java.Modifier[] modifiers, Parser.ClassDeclarationContext context) throws CompileException, IOException ClassDeclarationRest := Identifier [ typeParameters ] [ 'extends' ReferenceType ] [ 'implements' ReferenceTypeList ] ClassBody
- Throws:
CompileException
IOException
-
parseEnumDeclarationRest
public Java.EnumDeclaration parseEnumDeclarationRest(@Nullable String docComment, Java.Modifier[] modifiers, Parser.ClassDeclarationContext context) throws CompileException, IOException EnumDeclarationRest := Identifier [ 'implements' ReferenceTypeList ] EnumBody
- Throws:
CompileException
IOException
-
parseClassBody
public void parseClassBody(Java.AbstractClassDeclaration classDeclaration) throws CompileException, IOException ClassBody := '{' { ClassBodyDeclaration } '}'
- Throws:
CompileException
IOException
-
parseEnumBody
public void parseEnumBody(Java.EnumDeclaration enumDeclaration) throws CompileException, IOException EnumBody := '{' [ EnumConstant { ',' EnumConstant } [ ',' ] [ ';' ] { ClassBodyDeclaration } '}'
- Throws:
CompileException
IOException
-
parseEnumConstant
EnumConstant := [ Annotations ] Identifier [ Arguments ] [ ClassBody ]
- Throws:
CompileException
IOException
-
parseClassBodyDeclaration
public 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 ';' )
- Throws:
CompileException
IOException
-
parseInterfaceDeclarationRest
public Java.InterfaceDeclaration parseInterfaceDeclarationRest(@Nullable String docComment, Java.Modifier[] modifiers, Parser.InterfaceDeclarationContext context) throws CompileException, IOException InterfaceDeclarationRest := Identifier [ typeParameters ] [ 'extends' ReferenceTypeList ] InterfaceBody
- Throws:
CompileException
IOException
-
parseAnnotationTypeDeclarationRest
public Java.AnnotationTypeDeclaration parseAnnotationTypeDeclarationRest(@Nullable String docComment, Java.Modifier[] modifiers, Parser.InterfaceDeclarationContext context) throws CompileException, IOException AnnotationTypeDeclarationRest := Identifier AnnotationTypeBody
- Throws:
CompileException
IOException
-
parseInterfaceBody
public void parseInterfaceBody(Java.InterfaceDeclaration interfaceDeclaration) throws CompileException, IOException InterfaceBody := '{' { ';' | ModifiersOpt ( 'void' Identifier MethodDeclarationRest | 'class' ClassDeclarationRest | 'interface' InterfaceDeclarationRest | Type Identifier ( MethodDeclarationRest | FieldDeclarationRest ) ) } '}'
- Throws:
CompileException
IOException
-
parseConstructorDeclarator
public Java.ConstructorDeclarator parseConstructorDeclarator(@Nullable String docComment, Java.Modifier[] modifiers) throws CompileException, IOException ConstructorDeclarator := Identifier FormalParameters [ 'throws' ReferenceTypeList ] '{' [ 'this' Arguments ';' | 'super' Arguments ';' | Primary '.' 'super' Arguments ';' ] BlockStatements '}'
- Throws:
CompileException
IOException
-
parseMethodDeclaration
Equivalent withparseMethodDeclaration(false, MethodDeclarationContext.CLASS_DECLARATION)
.- Throws:
CompileException
IOException
- See Also:
-
parseMethodDeclaration
public Java.MethodDeclarator parseMethodDeclaration(boolean allowDefaultClause, Parser.MethodDeclarationContext context) throws CompileException, IOException MethodDeclaration := [ DocComment ] Modifiers [ TypeParameters ] VoidOrType Identifier MethodDeclarationRest
- Parameters:
allowDefaultClause
- Whether a "default clause" for an "annotation type element" (JLS8 9.6.2) should be parsed- Throws:
CompileException
IOException
-
parseVoidOrType
VoidOrType := 'void' | Type
- Throws:
CompileException
IOException
-
parseMethodDeclarationRest
public Java.MethodDeclarator parseMethodDeclarationRest(@Nullable String docComment, Java.Modifier[] modifiers, @Nullable Java.TypeParameter[] typeParameters, Java.Type type, String name, boolean allowDefaultClause, Parser.MethodDeclarationContext context) throws CompileException, IOException MethodDeclarationRest := FormalParameters { '[' ']' } [ 'throws' ReferenceTypeList ] [ 'default' expression ] ( ';' | MethodBody )
- Parameters:
allowDefaultClause
- Whether a "default clause" for an "annotation type element" (JLS8 9.6.2) should be parsed- Throws:
CompileException
IOException
-
getSourceVersion
private int getSourceVersion() -
parseVariableInitializer
public Java.ArrayInitializerOrRvalue parseVariableInitializer() throws CompileException, IOExceptionVariableInitializer := ArrayInitializer | Expression
- Throws:
CompileException
IOException
-
parseArrayInitializer
ArrayInitializer := '{' [ VariableInitializer { ',' VariableInitializer } [ ',' ] '}'
- Throws:
CompileException
IOException
-
parseFormalParameters
public Java.FunctionDeclarator.FormalParameters parseFormalParameters() throws CompileException, IOExceptionFormalParameters := '(' [ FormalParameterList ] ')'
- Throws:
CompileException
IOException
-
parseFormalParameterList
public Java.FunctionDeclarator.FormalParameters parseFormalParameterList() throws CompileException, IOExceptionFormalParameterList := FormalParameter { ',' FormalParameter }
- Throws:
CompileException
IOException
-
parseFormalParameterListRest
public Java.FunctionDeclarator.FormalParameters parseFormalParameterListRest(Java.Type firstParameterType) throws CompileException, IOException FormalParameterListRest := Identifier { ',' FormalParameter }
- Throws:
CompileException
IOException
-
parseFormalParameter
public Java.FunctionDeclarator.FormalParameter parseFormalParameter(boolean[] hasEllipsis) throws CompileException, IOException FormalParameter := [ 'final' ] Type FormalParameterRest
- Throws:
CompileException
IOException
-
parseFormalParameterRest
public Java.FunctionDeclarator.FormalParameter parseFormalParameterRest(Java.Modifier[] modifiers, Java.Type type, boolean[] hasEllipsis) throws CompileException, IOException FormalParameterRest := [ '.' '.' '.' ] Identifier BracketsOpt
- Parameters:
hasEllipsis
- After return, the zeroth element indicates whether the formal parameter was declared with an ellipsis- Throws:
CompileException
IOException
-
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 ]
- Throws:
CompileException
IOException
-
parseBracketsOpt
BracketsOpt := { '[' ']' }
- Throws:
CompileException
IOException
-
parseMethodBody
MethodBody := Block
- Throws:
CompileException
IOException
-
parseBlock
Block := '{' BlockStatements '}'
- Throws:
CompileException
IOException
-
parseBlockStatements
BlockStatements := { BlockStatement }
- Throws:
CompileException
IOException
-
parseBlockStatement
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.
- Throws:
CompileException
IOException
-
parseVariableDeclarators
VariableDeclarators := VariableDeclarator { ',' VariableDeclarator }
- Throws:
CompileException
IOException
-
parseFieldDeclarationRest
public Java.VariableDeclarator[] parseFieldDeclarationRest(String name) throws CompileException, IOException FieldDeclarationRest := VariableDeclaratorRest { ',' VariableDeclarator }
- Throws:
CompileException
IOException
-
parseVariableDeclarator
VariableDeclarator := Identifier VariableDeclaratorRest
- Throws:
CompileException
IOException
-
parseVariableDeclaratorRest
public Java.VariableDeclarator parseVariableDeclaratorRest(String name) throws CompileException, IOException VariableDeclaratorRest := { '[' ']' } [ '=' VariableInitializer ]
Used by field declarations and local variable declarations.
- Throws:
CompileException
IOException
-
parseStatement
Statement := LabeledStatement | Block | IfStatement | ForStatement | WhileStatement | DoStatement | TryStatement | 'switch' ... | 'synchronized' ... | ReturnStatement | ThrowStatement | BreakStatement | ContinueStatement | EmptyStatement | ExpressionStatement
- Throws:
CompileException
IOException
-
parseLabeledStatement
LabeledStatement := Identifier ':' Statement
- Throws:
CompileException
IOException
-
parseIfStatement
IfStatement := 'if' '(' Expression ')' Statement [ 'else' Statement ]
- Throws:
CompileException
IOException
-
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.
- Throws:
CompileException
IOException
-
parseWhileStatement
WhileStatement := 'while' '(' Expression ')' Statement
- Throws:
CompileException
IOException
-
parseDoStatement
DoStatement := 'do' Statement 'while' '(' Expression ')' ';'
- Throws:
CompileException
IOException
-
parseTryStatement
TryStatement := 'try' Block Catches [ Finally ] | 'try' Block Finally Catches := CatchClause { CatchClause } CatchClause := 'catch' '(' FormalParameter ')' Block Finally := 'finally' Block
- Throws:
CompileException
IOException
-
parseResource
Resource := Modifiers Type VariableDeclarator | VariableAccess
- Throws:
CompileException
IOException
-
parseSwitchStatement
SwitchStatement := 'switch' '(' Expression ')' '{' { SwitchLabels BlockStatements } '}' SwitchLabels := SwitchLabels { SwitchLabels } SwitchLabel := 'case' Expression ':' | 'default' ':'
- Throws:
CompileException
IOException
-
parseSynchronizedStatement
SynchronizedStatement := 'synchronized' '(' expression ')' Block
- Throws:
CompileException
IOException
-
parseReturnStatement
ReturnStatement := 'return' [ Expression ] ';'
- Throws:
CompileException
IOException
-
parseThrowStatement
ThrowStatement := 'throw' Expression ';'
- Throws:
CompileException
IOException
-
parseBreakStatement
BreakStatement := 'break' [ Identifier ] ';'
- Throws:
CompileException
IOException
-
parseContinueStatement
ContinueStatement := 'continue' [ Identifier ] ';'
- Throws:
CompileException
IOException
-
parseAssertStatement
AssertStatement := 'assert' Expression [ ':' Expression ] ';'
- Throws:
CompileException
IOException
-
parseEmptyStatement
EmptyStatement := ';'
- Throws:
CompileException
IOException
-
parseExpressionList
ExpressionList := Expression { ',' Expression }
- Throws:
CompileException
IOException
-
parseType
Type := ( 'byte' | 'short' | 'char' | 'int' | 'long' | 'float' | 'double' | 'boolean' | ReferenceType ) { '[' ']' }
- Throws:
CompileException
IOException
-
parseReferenceType
ReferenceType := { Annotation } QualifiedIdentifier [ TypeArguments ]
- Throws:
CompileException
IOException
-
parseTypeParametersOpt
@Nullable private Java.TypeParameter[] parseTypeParametersOpt() throws CompileException, IOExceptionTypeParameters := 'invalid input: '<'' TypeParameter { ',' TypeParameter } '>'
- Throws:
CompileException
IOException
-
parseTypeParameter
TypeParameter := identifier [ 'extends' ( identifier | ReferenceType { 'invalid input: '&'' ReferenceType }
- Throws:
CompileException
IOException
-
parseTypeArgumentsOpt
TypeArguments := 'invalid input: '<'' [ TypeArgument { ',' TypeArgument } ] '>'
- Returns:
null
iff there are no type arguments- Throws:
CompileException
IOException
-
parseTypeArgument
TypeArgument := ReferenceType { '[' ']' } <= The optional brackets are missing in JLS7, section 18!? | PrimitiveType '[' ']' { '[' ']' } | '?' extends ReferenceType | '?' super ReferenceType
- Throws:
CompileException
IOException
-
parseReferenceTypeList
ReferenceTypeList := ReferenceType { ',' ReferenceType }
- Throws:
CompileException
IOException
-
parseExpression
Expression := AssignmentExpression | LambdaExpression
Notice that all other kinds of lambda expressions are parsed in
parsePrimary()
.- Throws:
CompileException
IOException
-
parseExpressionOrType
- Throws:
CompileException
IOException
-
parseAssignmentExpression
AssignmentExpression := ConditionalExpression [ AssignmentOperator AssignmentExpression ] AssignmentOperator := '=' | '*=' | '/=' | '%=' | '+=' | '-=' | '<<=' | '>>=' | '>>>=' | '&=' | '^=' | '|='
- Throws:
CompileException
IOException
-
parseConditionalExpression
ConditionalExpression := ConditionalOrExpression [ '?' Expression ':' ConditionalExpression ]
- Throws:
CompileException
IOException
-
parseConditionalOrExpression
ConditionalOrExpression := ConditionalAndExpression { '||' ConditionalAndExpression ]
- Throws:
CompileException
IOException
-
parseConditionalAndExpression
ConditionalAndExpression := InclusiveOrExpression { '&&' InclusiveOrExpression }
- Throws:
CompileException
IOException
-
parseInclusiveOrExpression
InclusiveOrExpression := ExclusiveOrExpression { '|' ExclusiveOrExpression }
- Throws:
CompileException
IOException
-
parseExclusiveOrExpression
ExclusiveOrExpression := AndExpression { '^' AndExpression }
- Throws:
CompileException
IOException
-
parseAndExpression
AndExpression := EqualityExpression { '&' EqualityExpression }
- Throws:
CompileException
IOException
-
parseEqualityExpression
EqualityExpression := RelationalExpression { ( '==' | '!=' ) RelationalExpression }
- Throws:
CompileException
IOException
-
parseRelationalExpression
RelationalExpression := ShiftExpression { 'instanceof' ReferenceType | '<' ShiftExpression [ { ',' TypeArgument } '>' ] | '<' TypeArgument [ { ',' TypeArgument } '>' ] | ( '>' | '<=' | '>=' ) ShiftExpression }
- Throws:
CompileException
IOException
-
parseShiftExpression
ShiftExpression := AdditiveExpression { ( '<<' | '>>' | '>>>' ) AdditiveExpression }
- Throws:
CompileException
IOException
-
parseAdditiveExpression
AdditiveExpression := MultiplicativeExpression { ( '+' | '-' ) MultiplicativeExpression }
- Throws:
CompileException
IOException
-
parseMultiplicativeExpression
MultiplicativeExpression := UnaryExpression { ( '*' | '/' | '%' ) UnaryExpression }
- Throws:
CompileException
IOException
-
parseUnaryExpression
UnaryExpression := { PrefixOperator } Primary { Selector } { PostfixOperator } PrefixOperator := '++' | '--' | '+' | '-' | '~' | '!' PostfixOperator := '++' | '--'
- Throws:
CompileException
IOException
-
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 '[]' { '[]' } '.' '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 MethodReference // MethodReference JLS9 15.13 Name := Identifier { '.' Identifier } CastExpression := '(' PrimitiveType { '[]' } ')' UnaryExpression | '(' Expression ')' UnaryExpression NewClassInstance := 'new' ReferenceType Arguments NewAnonymousClassInstance := 'new' ReferenceType Arguments [ ClassBody ] NewArray := 'new' Type DimExprs { '[]' } NewInitializedArray := 'new' ArrayType ArrayInitializer
- Throws:
CompileException
IOException
-
parseSelector
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
- Throws:
CompileException
IOException
-
parseDimExprs
DimExprs := DimExpr { DimExpr }
- Throws:
CompileException
IOException
-
parseDimExpr
DimExpr := '[' Expression ']'
- Throws:
CompileException
IOException
-
parseArguments
Arguments := '(' [ ArgumentList ] ')'
- Throws:
CompileException
IOException
-
parseArgumentList
ArgumentList := Expression { ',' Expression }
- Throws:
CompileException
IOException
-
parseLiteral
Literal := IntegerLiteral | FloatingPointLiteral | BooleanLiteral | CharacterLiteral | StringLiteral | NullLiteral
- Throws:
CompileException
IOException
-
parseLambdaExpression
LambdaExpression := LambdaParameters '->' LambdaBody
- Throws:
CompileException
IOException
-
parseLambdaParameters
LambdaParameters := Identifier | '(' [ FormalParameterList ] ')' | '(' InferredFormalParameterList ')'
- Throws:
CompileException
IOException
-
parseLambdaBody
LambdaBody :=
- Throws:
CompileException
IOException
-
parseExpressionStatement
ExpressionStatement := Expression ';'
- Throws:
CompileException
IOException
-
location
- Returns:
- The location of the first character of the previously read (not peeked!) token
-
peek
- Throws:
CompileException
IOException
-
read
- Throws:
CompileException
IOException
-
peek
- Throws:
CompileException
IOException
-
peek
- Throws:
CompileException
IOException
-
peek
- Throws:
CompileException
IOException
-
peek
- Throws:
CompileException
IOException
-
peekNextButOne
- Throws:
CompileException
IOException
-
peekNextButOne
- Throws:
CompileException
IOException
-
peekNextButOne
- Throws:
CompileException
IOException
-
read
- Throws:
CompileException
IOException
-
read
- Throws:
CompileException
IOException
-
read
- Throws:
CompileException
IOException
-
peekRead
- Throws:
CompileException
IOException
-
peekRead
- Throws:
CompileException
IOException
-
peekRead
- Throws:
CompileException
IOException
-
peekLiteral
- Throws:
CompileException
IOException
-
verifyStringIsConventionalPackageName
Issues a warning if the given string does not comply with the package naming conventions.- Throws:
CompileException
-
verifyIdentifierIsConventionalClassOrInterfaceName
private void verifyIdentifierIsConventionalClassOrInterfaceName(String id, Location loc) throws CompileException Issues a warning if the given identifier does not comply with the class and interface type naming conventions (JLS7 6.8.2).- Throws:
CompileException
-
verifyIdentifierIsConventionalMethodName
private void verifyIdentifierIsConventionalMethodName(String id, Location loc) throws CompileException Issues a warning if the given identifier does not comply with the method naming conventions (JLS7 6.8.3).- Throws:
CompileException
-
verifyIdentifierIsConventionalFieldName
private void verifyIdentifierIsConventionalFieldName(String id, Location loc) throws CompileException Issues a warning if the given identifier does not comply with the field naming conventions (JLS7 6.8.4) and constant naming conventions (JLS7 6.8.5).- Throws:
CompileException
-
verifyIdentifierIsConventionalLocalVariableOrParameterName
private void verifyIdentifierIsConventionalLocalVariableOrParameterName(String id, Location loc) throws CompileException Issues a warning if the given identifier does not comply with the local variable and parameter naming conventions (JLS7 6.8.6).- Throws:
CompileException
-
setSourceVersion
public void setSourceVersion(int version) -
setWarningHandler
By default, warnings are discarded, but an application my install aWarningHandler
.Notice that there is no
Parser.setErrorHandler()
method, but parse errors always throw aCompileException
. 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.- Parameters:
warningHandler
-null
to indicate that no warnings be issued
-
warning
- Throws:
CompileException
-
warning
private void warning(String handle, String message, @Nullable Location location) throws CompileException Issues a warning with the given message and location and returns. This is done through aWarningHandler
that was installed throughsetWarningHandler(WarningHandler)
.The
handle
argument qualifies the warning and is typically used by theWarningHandler
to suppress individual warnings.- Throws:
CompileException
- The optionally installedWarningHandler
decided to throw aCompileException
-
compileException
Convenience method for throwing aCompileException
. -
compileException
Convenience method for throwing aCompileException
. -
join
-
hasAccessModifier
-
hasAccessModifierOtherThan
-
packageModifiers
- Throws:
CompileException
-
classModifiers
- Throws:
CompileException
-
packageMemberClassModifiers
private Java.Modifier[] packageMemberClassModifiers(Java.Modifier[] modifiers) throws CompileException - Throws:
CompileException
-
fieldModifiers
- Throws:
CompileException
-
methodModifiers
- Throws:
CompileException
-
variableModifiers
- Throws:
CompileException
-
constructorModifiers
- Throws:
CompileException
-
enumConstantModifiers
- Throws:
CompileException
-
interfaceModifiers
- Throws:
CompileException
-
packageMemberInterfaceModifiers
private Java.Modifier[] packageMemberInterfaceModifiers(Java.Modifier[] modifiers) throws CompileException - Throws:
CompileException
-
constantModifiers
- Throws:
CompileException
-
interfaceMethodModifiers
- Throws:
CompileException
-
annotationTypeElementModifiers
private Java.Modifier[] annotationTypeElementModifiers(Java.Modifier[] modifiers) throws CompileException - Throws:
CompileException
-
checkModifiers
private Java.Modifier[] checkModifiers(Java.Modifier[] modifiers, String... allowedKeywords) throws CompileException Verifies that the modifiers are consistent.- No two annotations must have the same type.
- No access modifier must appear more than once
- Certain modifier combinations must not occur (e.g.
abstract final
).
- Returns:
- modifiers
- Throws:
CompileException
-