Class Parser
- java.lang.Object
-
- org.codehaus.janino.Parser
-
public class Parser extends Object
A parser for the Java programming language.'JLS7' refers to the Java Language Specification, Java SE 7 Edition.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classParser.ClassDeclarationContextThe kinds of context where a class declaration can occur.static classParser.InterfaceDeclarationContextThe kinds of context where an interface declaration can occur.static classParser.MethodDeclarationContextThe kinds of context where a method declaration can occur.
-
Constructor Summary
Constructors Constructor Description Parser(Scanner scanner)Parser(Scanner scanner, TokenStream tokenStream)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected CompileExceptioncompileException(String message)Convenience method for throwing aCompileException.protected static CompileExceptioncompileException(String message, Location location)Convenience method for throwing aCompileException.Stringdoc()Gets the text of the doc comment (a.k.a.ScannergetScanner()Locationlocation()Java.AbstractCompilationUnitparseAbstractCompilationUnit()CompilationUnit := [ PackageDeclaration ] { ImportDeclaration } { TypeDeclaration }Java.AtomparseAdditiveExpression()AdditiveExpression := MultiplicativeExpression { ( '+' | '-' ) MultiplicativeExpression }Java.AtomparseAndExpression()AndExpression := EqualityExpression { '&' EqualityExpression }Java.AnnotationTypeDeclarationparseAnnotationTypeDeclarationRest(String docComment, Java.Modifier[] modifiers, Parser.InterfaceDeclarationContext context)AnnotationTypeDeclarationRest := Identifier AnnotationTypeBodyJava.Rvalue[]parseArgumentList()ArgumentList := Expression { ',' Expression }Java.Rvalue[]parseArguments()Arguments := '(' [ ArgumentList ] ')'Java.ArrayInitializerparseArrayInitializer()ArrayInitializer := '{' [ VariableInitializer { ',' VariableInitializer } [ ',' ] '}'Java.StatementparseAssertStatement()AssertStatement := 'assert' Expression [ ':' Expression ] ';'Java.AtomparseAssignmentExpression()AssignmentExpression := ConditionalExpression [ AssignmentOperator AssignmentExpression ] AssignmentOperator := '=' | '*=' | '/=' | '%=' | '+=' | '-=' | '<<=' | '>>=' | '>>>=' | '&=' | '^=' | '|='Java.BlockparseBlock()Block := '{' BlockStatements '}'Java.BlockStatementparseBlockStatement()BlockStatement := Statement | (1) 'class' ...List<Java.BlockStatement>parseBlockStatements()BlockStatements := { BlockStatement }Java.StatementparseBreakStatement()BreakStatement := 'break' [ Identifier ] ';'Java.CatchParameterparseCatchParameter()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 ]voidparseClassBody(Java.AbstractClassDeclaration classDeclaration)ClassBody := '{' { ClassBodyDeclaration } '}'voidparseClassBodyDeclaration(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.NamedClassDeclarationparseClassDeclarationRest(String docComment, Java.Modifier[] modifiers, Parser.ClassDeclarationContext context)ClassDeclarationRest := Identifier [ typeParameters ] [ 'extends' ReferenceType ] [ 'implements' ReferenceTypeList ] ClassBodyJava.AtomparseConditionalAndExpression()ConditionalAndExpression := InclusiveOrExpression { '&&' InclusiveOrExpression }Java.AtomparseConditionalExpression()ConditionalExpression := ConditionalOrExpression [ '?' Expression ':' ConditionalExpression ]Java.AtomparseConditionalOrExpression()ConditionalOrExpression := ConditionalAndExpression { '||' ConditionalAndExpression ]Java.ConstructorDeclaratorparseConstructorDeclarator(String docComment, Java.Modifier[] modifiers)ConstructorDeclarator := Identifier FormalParameters [ 'throws' ReferenceTypeList ] '{' [ 'this' Arguments ';' | 'super' Arguments ';' | Primary '.' 'super' Arguments ';' ] BlockStatements '}'Java.StatementparseContinueStatement()ContinueStatement := 'continue' [ Identifier ] ';'Java.RvalueparseDimExpr()DimExpr := '[' Expression ']'Java.Rvalue[]parseDimExprs()DimExprs := DimExpr { DimExpr }Java.StatementparseDoStatement()DoStatement := 'do' Statement 'while' '(' Expression ')' ';'Java.StatementparseEmptyStatement()EmptyStatement := ';'voidparseEnumBody(Java.EnumDeclaration enumDeclaration)EnumBody := '{' [ EnumConstant { ',' EnumConstant } [ ',' ] [ ';' ] { ClassBodyDeclaration } '}'Java.EnumConstantparseEnumConstant()EnumConstant := [ Annotations ] Identifier [ Arguments ] [ ClassBody ]Java.EnumDeclarationparseEnumDeclarationRest(String docComment, Java.Modifier[] modifiers, Parser.ClassDeclarationContext context)EnumDeclarationRest := Identifier [ 'implements' ReferenceTypeList ] EnumBodyJava.AtomparseEqualityExpression()EqualityExpression := RelationalExpression { ( '==' | '!=' ) RelationalExpression }Java.AtomparseExclusiveOrExpression()ExclusiveOrExpression := AndExpression { '^' AndExpression }Java.RvalueparseExpression()Expression := AssignmentExpression | LambdaExpressionJava.Rvalue[]parseExpressionList()ExpressionList := Expression { ',' Expression }Java.AtomparseExpressionOrType()Java.StatementparseExpressionStatement()ExpressionStatement := Expression ';'Java.VariableDeclarator[]parseFieldDeclarationRest(String name)FieldDeclarationRest := VariableDeclaratorRest { ',' VariableDeclarator }Java.FunctionDeclarator.FormalParameterparseFormalParameter(boolean[] hasEllipsis)FormalParameter := [ 'final' ] Type FormalParameterRestJava.FunctionDeclarator.FormalParametersparseFormalParameterList()FormalParameterList := FormalParameter { ',' FormalParameter }Java.FunctionDeclarator.FormalParametersparseFormalParameterListRest(Java.Type firstParameterType)FormalParameterListRest := Identifier { ',' FormalParameter }Java.FunctionDeclarator.FormalParameterparseFormalParameterRest(Java.Modifier[] modifiers, Java.Type type, boolean[] hasEllipsis)FormalParameterRest := [ '.' '.' '.' ] Identifier BracketsOptJava.FunctionDeclarator.FormalParametersparseFormalParameters()FormalParameters := '(' [ FormalParameterList ] ')'Java.StatementparseForStatement()ForStatement := 'for' '(' [ ForInit ] ';' [ Expression ] ';' [ ExpressionList ] ')' Statement | 'for' '(' FormalParameter ':' Expression ')' Statement ForInit := Modifiers Type VariableDeclarators | ModifiersOpt PrimitiveType VariableDeclarators | Expression VariableDeclarators (1) | Expression { ',' Expression }Java.StatementparseIfStatement()IfStatement := 'if' '(' Expression ')' Statement [ 'else' Statement ]Java.AbstractCompilationUnit.ImportDeclarationparseImportDeclaration()ImportDeclaration := 'import' ImportDeclarationBody ';'Java.AbstractCompilationUnit.ImportDeclarationparseImportDeclarationBody()ImportDeclarationBody := [ 'static' ] Identifier { '.' Identifier } [ '.' '*' ]Java.AtomparseInclusiveOrExpression()InclusiveOrExpression := ExclusiveOrExpression { '|' ExclusiveOrExpression }voidparseInterfaceBody(Java.InterfaceDeclaration interfaceDeclaration)InterfaceBody := '{' { ';' | ModifiersOpt ( 'void' Identifier MethodDeclarationRest | 'class' ClassDeclarationRest | 'interface' InterfaceDeclarationRest | Type Identifier ( MethodDeclarationRest | FieldDeclarationRest ) ) } '}'Java.InterfaceDeclarationparseInterfaceDeclarationRest(String docComment, Java.Modifier[] modifiers, Parser.InterfaceDeclarationContext context)InterfaceDeclarationRest := Identifier [ typeParameters ] [ 'extends' ReferenceTypeList ] InterfaceBodyJava.StatementparseLabeledStatement()LabeledStatement := Identifier ':' StatementJava.RvalueparseLiteral()Literal := IntegerLiteral | FloatingPointLiteral | BooleanLiteral | CharacterLiteral | StringLiteral | NullLiteralJava.BlockparseMethodBody()MethodBody := BlockJava.MethodDeclaratorparseMethodDeclaration()Equivalent withparseMethodDeclaration(false, MethodDeclarationContext.CLASS_DECLARATION).Java.MethodDeclaratorparseMethodDeclaration(boolean allowDefaultClause, Parser.MethodDeclarationContext context)MethodDeclaration := [ DocComment ] Modifiers [ TypeParameters ] VoidOrType Identifier MethodDeclarationRestJava.MethodDeclaratorparseMethodDeclarationRest(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 )Java.Modifier[]parseModifiers()Modifiers := { Modifier }Java.ModuleDeclarationparseModuleDeclarationRest(Java.Modifier[] modifiers)ModuleDeclarationRest := [ 'open' ] 'module' identifier { '.' identifier} '{' { ModuleDirective } '}'Java.AtomparseMultiplicativeExpression()MultiplicativeExpression := UnaryExpression { ( '*' | '/' | '%' ) UnaryExpression }Java.ModifierparseOptionalModifier()Modifier := Annotation | 'public' | 'protected' | 'private' | 'static' | 'abstract' | 'final' | 'native' | 'synchronized' | 'transient' | 'volatile' | 'strictfp' | 'default'Java.PackageDeclarationparsePackageDeclaration()PackageDeclaration := 'package' QualifiedIdentifier ';'Java.PackageDeclarationparsePackageDeclarationRest(String docComment, Java.Modifier[] modifiers)PackageDeclaration := { PackageModifier } 'package' identifier { '.' identifier} ';'Java.PackageMemberTypeDeclarationparsePackageMemberTypeDeclaration()PackageMemberTypeDeclaration := ModifiersOpt PackageMemberTypeDeclarationRestJava.AtomparsePrimary()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 ArrayInitializerString[]parseQualifiedIdentifier()QualifiedIdentifier := Identifier { '.' Identifier }Java.ReferenceTypeparseReferenceType()ReferenceType := { Annotation } QualifiedIdentifier [ TypeArguments ]Java.ReferenceType[]parseReferenceTypeList()ReferenceTypeList := ReferenceType { ',' ReferenceType }Java.AtomparseRelationalExpression()RelationalExpression := ShiftExpression { 'instanceof' ReferenceType | '<' ShiftExpression [ { ',' TypeArgument } '>' ] | '<' TypeArgument [ { ',' TypeArgument } '>' ] | ( '>' | '<=' | '>=' ) ShiftExpression }Java.StatementparseReturnStatement()ReturnStatement := 'return' [ Expression ] ';'Java.AtomparseSelector(Java.Atom atom)Selector := '.' Identifier | // FieldAccess 15.11.1 '.' Identifier Arguments | // MethodInvocation '.' '<' TypeList '>' 'super' Arguments // Superconstructor invocation (?) '.' '<' TypeList '>' 'super' '.' .Java.AtomparseShiftExpression()ShiftExpression := AdditiveExpression { ( '<<' | '>>' | '>>>' ) AdditiveExpression }Java.StatementparseStatement()Statement := LabeledStatement | Block | IfStatement | ForStatement | WhileStatement | DoStatement | TryStatement | 'switch' ...Java.StatementparseSwitchStatement()SwitchStatement := 'switch' '(' Expression ')' '{' { SwitchLabels BlockStatements } '}' SwitchLabels := SwitchLabels { SwitchLabels } SwitchLabel := 'case' Expression ':' | 'default' ':'Java.StatementparseSynchronizedStatement()SynchronizedStatement := 'synchronized' '(' expression ')' BlockJava.StatementparseThrowStatement()ThrowStatement := 'throw' Expression ';'Java.StatementparseTryStatement()TryStatement := 'try' Block Catches [ Finally ] | 'try' Block Finally Catches := CatchClause { CatchClause } CatchClause := 'catch' '(' FormalParameter ')' Block Finally := 'finally' BlockJava.TypeparseType()Type := ( 'byte' | 'short' | 'char' | 'int' | 'long' | 'float' | 'double' | 'boolean' | ReferenceType ) { '[' ']' }Java.AtomparseUnaryExpression()UnaryExpression := { PrefixOperator } Primary { Selector } { PostfixOperator } PrefixOperator := '++' | '--' | '+' | '-' | '~' | '!' PostfixOperator := '++' | '--'Java.VariableDeclaratorparseVariableDeclarator()VariableDeclarator := Identifier VariableDeclaratorRestJava.VariableDeclaratorparseVariableDeclaratorRest(String name)VariableDeclaratorRest := { '[' ']' } [ '=' VariableInitializer ]Java.VariableDeclarator[]parseVariableDeclarators()VariableDeclarators := VariableDeclarator { ',' VariableDeclarator }Java.ArrayInitializerOrRvalueparseVariableInitializer()VariableInitializer := ArrayInitializer | ExpressionJava.TypeparseVoidOrType()VoidOrType := 'void' | TypeJava.StatementparseWhileStatement()WhileStatement := 'while' '(' Expression ')' StatementTokenpeek()booleanpeek(String suspected)intpeek(String... suspected)booleanpeek(TokenType suspected)intpeek(TokenType... suspected)TokenpeekNextButOne()booleanpeekNextButOne(String suspected)booleanpeekNextButOne(TokenType suspected)booleanpeekRead(String suspected)intpeekRead(String... suspected)StringpeekRead(TokenType suspected)Tokenread()voidread(String expected)intread(String... expected)Stringread(TokenType expected)voidsetSourceVersion(int version)voidsetWarningHandler(WarningHandler warningHandler)By default, warnings are discarded, but an application my install aWarningHandler.
-
-
-
Constructor Detail
-
Parser
public Parser(Scanner scanner)
-
Parser
public Parser(Scanner scanner, TokenStream tokenStream)
-
-
Method Detail
-
doc
@Nullable public String doc()
Gets the text of the doc comment (a.k.a. "JAVADOC comment") preceding the next token.- Returns:
nullif the next token is not preceded by a doc comment
-
getScanner
public Scanner getScanner()
- Returns:
- The scanner that produces the tokens for this parser.
-
parseAbstractCompilationUnit
public Java.AbstractCompilationUnit parseAbstractCompilationUnit() throws CompileException, IOException
CompilationUnit := [ PackageDeclaration ] { ImportDeclaration } { TypeDeclaration }- Throws:
CompileExceptionIOException
-
parseModuleDeclarationRest
public Java.ModuleDeclaration parseModuleDeclarationRest(Java.Modifier[] modifiers) throws CompileException, IOException
ModuleDeclarationRest := [ 'open' ] 'module' identifier { '.' identifier} '{' { ModuleDirective } '}'- Throws:
CompileExceptionIOException
-
parsePackageDeclaration
public Java.PackageDeclaration parsePackageDeclaration() throws CompileException, IOException
PackageDeclaration := 'package' QualifiedIdentifier ';'
- Throws:
CompileExceptionIOException
-
parsePackageDeclarationRest
public Java.PackageDeclaration parsePackageDeclarationRest(@Nullable String docComment, Java.Modifier[] modifiers) throws CompileException, IOException
PackageDeclaration := { PackageModifier } 'package' identifier { '.' identifier} ';'- Throws:
CompileExceptionIOException
-
parseImportDeclaration
public Java.AbstractCompilationUnit.ImportDeclaration parseImportDeclaration() throws CompileException, IOException
ImportDeclaration := 'import' ImportDeclarationBody ';'
- Throws:
CompileExceptionIOException
-
parseImportDeclarationBody
public Java.AbstractCompilationUnit.ImportDeclaration parseImportDeclarationBody() throws CompileException, IOException
ImportDeclarationBody := [ 'static' ] Identifier { '.' Identifier } [ '.' '*' ]- Throws:
CompileExceptionIOException
-
parseQualifiedIdentifier
public String[] parseQualifiedIdentifier() throws CompileException, IOException
QualifiedIdentifier := Identifier { '.' Identifier }- Throws:
CompileExceptionIOException
-
parsePackageMemberTypeDeclaration
public Java.PackageMemberTypeDeclaration parsePackageMemberTypeDeclaration() throws CompileException, IOException
PackageMemberTypeDeclaration := ModifiersOpt PackageMemberTypeDeclarationRest
- Throws:
CompileExceptionIOException
-
parseModifiers
public Java.Modifier[] parseModifiers() throws CompileException, IOException
Modifiers := { Modifier }Includes the case "no modifiers".
- Throws:
CompileExceptionIOException
-
parseOptionalModifier
@Nullable public Java.Modifier parseOptionalModifier() throws CompileException, IOException
Modifier := Annotation | 'public' | 'protected' | 'private' | 'static' | 'abstract' | 'final' | 'native' | 'synchronized' | 'transient' | 'volatile' | 'strictfp' | 'default'- Throws:
CompileExceptionIOException
-
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:
CompileExceptionIOException
-
parseEnumDeclarationRest
public Java.EnumDeclaration parseEnumDeclarationRest(@Nullable String docComment, Java.Modifier[] modifiers, Parser.ClassDeclarationContext context) throws CompileException, IOException
EnumDeclarationRest := Identifier [ 'implements' ReferenceTypeList ] EnumBody
- Throws:
CompileExceptionIOException
-
parseClassBody
public void parseClassBody(Java.AbstractClassDeclaration classDeclaration) throws CompileException, IOException
ClassBody := '{' { ClassBodyDeclaration } '}'- Throws:
CompileExceptionIOException
-
parseEnumBody
public void parseEnumBody(Java.EnumDeclaration enumDeclaration) throws CompileException, IOException
EnumBody := '{' [ EnumConstant { ',' EnumConstant } [ ',' ] [ ';' ] { ClassBodyDeclaration } '}'- Throws:
CompileExceptionIOException
-
parseEnumConstant
public Java.EnumConstant parseEnumConstant() throws CompileException, IOException
EnumConstant := [ Annotations ] Identifier [ Arguments ] [ ClassBody ]
- Throws:
CompileExceptionIOException
-
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:
CompileExceptionIOException
-
parseInterfaceDeclarationRest
public Java.InterfaceDeclaration parseInterfaceDeclarationRest(@Nullable String docComment, Java.Modifier[] modifiers, Parser.InterfaceDeclarationContext context) throws CompileException, IOException
InterfaceDeclarationRest := Identifier [ typeParameters ] [ 'extends' ReferenceTypeList ] InterfaceBody- Throws:
CompileExceptionIOException
-
parseAnnotationTypeDeclarationRest
public Java.AnnotationTypeDeclaration parseAnnotationTypeDeclarationRest(@Nullable String docComment, Java.Modifier[] modifiers, Parser.InterfaceDeclarationContext context) throws CompileException, IOException
AnnotationTypeDeclarationRest := Identifier AnnotationTypeBody
- Throws:
CompileExceptionIOException
-
parseInterfaceBody
public void parseInterfaceBody(Java.InterfaceDeclaration interfaceDeclaration) throws CompileException, IOException
InterfaceBody := '{' { ';' | ModifiersOpt ( 'void' Identifier MethodDeclarationRest | 'class' ClassDeclarationRest | 'interface' InterfaceDeclarationRest | Type Identifier ( MethodDeclarationRest | FieldDeclarationRest ) ) } '}'- Throws:
CompileExceptionIOException
-
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:
CompileExceptionIOException
-
parseMethodDeclaration
public Java.MethodDeclarator parseMethodDeclaration() throws CompileException, IOException
Equivalent withparseMethodDeclaration(false, MethodDeclarationContext.CLASS_DECLARATION).
-
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:
CompileExceptionIOException
-
parseVoidOrType
public Java.Type parseVoidOrType() throws CompileException, IOException
VoidOrType := 'void' | Type
- Throws:
CompileExceptionIOException
-
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:
CompileExceptionIOException
-
parseVariableInitializer
public Java.ArrayInitializerOrRvalue parseVariableInitializer() throws CompileException, IOException
VariableInitializer := ArrayInitializer | Expression- Throws:
CompileExceptionIOException
-
parseArrayInitializer
public Java.ArrayInitializer parseArrayInitializer() throws CompileException, IOException
ArrayInitializer := '{' [ VariableInitializer { ',' VariableInitializer } [ ',' ] '}'- Throws:
CompileExceptionIOException
-
parseFormalParameters
public Java.FunctionDeclarator.FormalParameters parseFormalParameters() throws CompileException, IOException
FormalParameters := '(' [ FormalParameterList ] ')'- Throws:
CompileExceptionIOException
-
parseFormalParameterList
public Java.FunctionDeclarator.FormalParameters parseFormalParameterList() throws CompileException, IOException
FormalParameterList := FormalParameter { ',' FormalParameter }- Throws:
CompileExceptionIOException
-
parseFormalParameterListRest
public Java.FunctionDeclarator.FormalParameters parseFormalParameterListRest(Java.Type firstParameterType) throws CompileException, IOException
FormalParameterListRest := Identifier { ',' FormalParameter }- Throws:
CompileExceptionIOException
-
parseFormalParameter
public Java.FunctionDeclarator.FormalParameter parseFormalParameter(boolean[] hasEllipsis) throws CompileException, IOException
FormalParameter := [ 'final' ] Type FormalParameterRest
- Throws:
CompileExceptionIOException
-
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:
CompileExceptionIOException
-
parseCatchParameter
public 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 ]- Throws:
CompileExceptionIOException
-
parseMethodBody
public Java.Block parseMethodBody() throws CompileException, IOException
MethodBody := Block
- Throws:
CompileExceptionIOException
-
parseBlock
public Java.Block parseBlock() throws CompileException, IOException
Block := '{' BlockStatements '}'- Throws:
CompileExceptionIOException
-
parseBlockStatements
public List<Java.BlockStatement> parseBlockStatements() throws CompileException, IOException
BlockStatements := { BlockStatement }- Throws:
CompileExceptionIOException
-
parseBlockStatement
public 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.
- Throws:
CompileExceptionIOException
-
parseVariableDeclarators
public Java.VariableDeclarator[] parseVariableDeclarators() throws CompileException, IOException
VariableDeclarators := VariableDeclarator { ',' VariableDeclarator }- Throws:
CompileExceptionIOException
-
parseFieldDeclarationRest
public Java.VariableDeclarator[] parseFieldDeclarationRest(String name) throws CompileException, IOException
FieldDeclarationRest := VariableDeclaratorRest { ',' VariableDeclarator }- Throws:
CompileExceptionIOException
-
parseVariableDeclarator
public Java.VariableDeclarator parseVariableDeclarator() throws CompileException, IOException
VariableDeclarator := Identifier VariableDeclaratorRest
- Throws:
CompileExceptionIOException
-
parseVariableDeclaratorRest
public Java.VariableDeclarator parseVariableDeclaratorRest(String name) throws CompileException, IOException
VariableDeclaratorRest := { '[' ']' } [ '=' VariableInitializer ]Used by field declarations and local variable declarations.
- Throws:
CompileExceptionIOException
-
parseStatement
public Java.Statement parseStatement() throws CompileException, IOException
Statement := LabeledStatement | Block | IfStatement | ForStatement | WhileStatement | DoStatement | TryStatement | 'switch' ... | 'synchronized' ... | ReturnStatement | ThrowStatement | BreakStatement | ContinueStatement | EmptyStatement | ExpressionStatement- Throws:
CompileExceptionIOException
-
parseLabeledStatement
public Java.Statement parseLabeledStatement() throws CompileException, IOException
LabeledStatement := Identifier ':' Statement
- Throws:
CompileExceptionIOException
-
parseIfStatement
public Java.Statement parseIfStatement() throws CompileException, IOException
IfStatement := 'if' '(' Expression ')' Statement [ 'else' Statement ]- Throws:
CompileExceptionIOException
-
parseForStatement
public 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.
- Throws:
CompileExceptionIOException
-
parseWhileStatement
public Java.Statement parseWhileStatement() throws CompileException, IOException
WhileStatement := 'while' '(' Expression ')' Statement- Throws:
CompileExceptionIOException
-
parseDoStatement
public Java.Statement parseDoStatement() throws CompileException, IOException
DoStatement := 'do' Statement 'while' '(' Expression ')' ';'- Throws:
CompileExceptionIOException
-
parseTryStatement
public Java.Statement parseTryStatement() throws CompileException, IOException
TryStatement := 'try' Block Catches [ Finally ] | 'try' Block Finally Catches := CatchClause { CatchClause } CatchClause := 'catch' '(' FormalParameter ')' Block Finally := 'finally' Block- Throws:
CompileExceptionIOException
-
parseSwitchStatement
public Java.Statement parseSwitchStatement() throws CompileException, IOException
SwitchStatement := 'switch' '(' Expression ')' '{' { SwitchLabels BlockStatements } '}' SwitchLabels := SwitchLabels { SwitchLabels } SwitchLabel := 'case' Expression ':' | 'default' ':'- Throws:
CompileExceptionIOException
-
parseSynchronizedStatement
public Java.Statement parseSynchronizedStatement() throws CompileException, IOException
SynchronizedStatement := 'synchronized' '(' expression ')' Block- Throws:
CompileExceptionIOException
-
parseReturnStatement
public Java.Statement parseReturnStatement() throws CompileException, IOException
ReturnStatement := 'return' [ Expression ] ';'
- Throws:
CompileExceptionIOException
-
parseThrowStatement
public Java.Statement parseThrowStatement() throws CompileException, IOException
ThrowStatement := 'throw' Expression ';'
- Throws:
CompileExceptionIOException
-
parseBreakStatement
public Java.Statement parseBreakStatement() throws CompileException, IOException
BreakStatement := 'break' [ Identifier ] ';'
- Throws:
CompileExceptionIOException
-
parseContinueStatement
public Java.Statement parseContinueStatement() throws CompileException, IOException
ContinueStatement := 'continue' [ Identifier ] ';'
- Throws:
CompileExceptionIOException
-
parseAssertStatement
public Java.Statement parseAssertStatement() throws CompileException, IOException
AssertStatement := 'assert' Expression [ ':' Expression ] ';'
- Throws:
CompileExceptionIOException
-
parseEmptyStatement
public Java.Statement parseEmptyStatement() throws CompileException, IOException
EmptyStatement := ';'
- Throws:
CompileExceptionIOException
-
parseExpressionList
public Java.Rvalue[] parseExpressionList() throws CompileException, IOException
ExpressionList := Expression { ',' Expression }- Throws:
CompileExceptionIOException
-
parseType
public Java.Type parseType() throws CompileException, IOException
Type := ( 'byte' | 'short' | 'char' | 'int' | 'long' | 'float' | 'double' | 'boolean' | ReferenceType ) { '[' ']' }- Throws:
CompileExceptionIOException
-
parseReferenceType
public Java.ReferenceType parseReferenceType() throws CompileException, IOException
ReferenceType := { Annotation } QualifiedIdentifier [ TypeArguments ]- Throws:
CompileExceptionIOException
-
parseReferenceTypeList
public Java.ReferenceType[] parseReferenceTypeList() throws CompileException, IOException
ReferenceTypeList := ReferenceType { ',' ReferenceType }- Throws:
CompileExceptionIOException
-
parseExpression
public Java.Rvalue parseExpression() throws CompileException, IOException
Expression := AssignmentExpression | LambdaExpression
Notice that all other kinds of lambda expressions are parsed in
parsePrimary().- Throws:
CompileExceptionIOException
-
parseExpressionOrType
public Java.Atom parseExpressionOrType() throws CompileException, IOException
- Throws:
CompileExceptionIOException
-
parseAssignmentExpression
public Java.Atom parseAssignmentExpression() throws CompileException, IOException
AssignmentExpression := ConditionalExpression [ AssignmentOperator AssignmentExpression ] AssignmentOperator := '=' | '*=' | '/=' | '%=' | '+=' | '-=' | '<<=' | '>>=' | '>>>=' | '&=' | '^=' | '|='- Throws:
CompileExceptionIOException
-
parseConditionalExpression
public Java.Atom parseConditionalExpression() throws CompileException, IOException
ConditionalExpression := ConditionalOrExpression [ '?' Expression ':' ConditionalExpression ]- Throws:
CompileExceptionIOException
-
parseConditionalOrExpression
public Java.Atom parseConditionalOrExpression() throws CompileException, IOException
ConditionalOrExpression := ConditionalAndExpression { '||' ConditionalAndExpression ]- Throws:
CompileExceptionIOException
-
parseConditionalAndExpression
public Java.Atom parseConditionalAndExpression() throws CompileException, IOException
ConditionalAndExpression := InclusiveOrExpression { '&&' InclusiveOrExpression }- Throws:
CompileExceptionIOException
-
parseInclusiveOrExpression
public Java.Atom parseInclusiveOrExpression() throws CompileException, IOException
InclusiveOrExpression := ExclusiveOrExpression { '|' ExclusiveOrExpression }- Throws:
CompileExceptionIOException
-
parseExclusiveOrExpression
public Java.Atom parseExclusiveOrExpression() throws CompileException, IOException
ExclusiveOrExpression := AndExpression { '^' AndExpression }- Throws:
CompileExceptionIOException
-
parseAndExpression
public Java.Atom parseAndExpression() throws CompileException, IOException
AndExpression := EqualityExpression { '&' EqualityExpression }- Throws:
CompileExceptionIOException
-
parseEqualityExpression
public Java.Atom parseEqualityExpression() throws CompileException, IOException
EqualityExpression := RelationalExpression { ( '==' | '!=' ) RelationalExpression }- Throws:
CompileExceptionIOException
-
parseRelationalExpression
public Java.Atom parseRelationalExpression() throws CompileException, IOException
RelationalExpression := ShiftExpression { 'instanceof' ReferenceType | '<' ShiftExpression [ { ',' TypeArgument } '>' ] | '<' TypeArgument [ { ',' TypeArgument } '>' ] | ( '>' | '<=' | '>=' ) ShiftExpression }- Throws:
CompileExceptionIOException
-
parseShiftExpression
public Java.Atom parseShiftExpression() throws CompileException, IOException
ShiftExpression := AdditiveExpression { ( '<<' | '>>' | '>>>' ) AdditiveExpression }- Throws:
CompileExceptionIOException
-
parseAdditiveExpression
public Java.Atom parseAdditiveExpression() throws CompileException, IOException
AdditiveExpression := MultiplicativeExpression { ( '+' | '-' ) MultiplicativeExpression }- Throws:
CompileExceptionIOException
-
parseMultiplicativeExpression
public Java.Atom parseMultiplicativeExpression() throws CompileException, IOException
MultiplicativeExpression := UnaryExpression { ( '*' | '/' | '%' ) UnaryExpression }- Throws:
CompileExceptionIOException
-
parseUnaryExpression
public Java.Atom parseUnaryExpression() throws CompileException, IOException
UnaryExpression := { PrefixOperator } Primary { Selector } { PostfixOperator } PrefixOperator := '++' | '--' | '+' | '-' | '~' | '!' PostfixOperator := '++' | '--'- Throws:
CompileExceptionIOException
-
parsePrimary
public 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 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:
CompileExceptionIOException
-
parseSelector
public 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- Throws:
CompileExceptionIOException
-
parseDimExprs
public Java.Rvalue[] parseDimExprs() throws CompileException, IOException
DimExprs := DimExpr { DimExpr }- Throws:
CompileExceptionIOException
-
parseDimExpr
public Java.Rvalue parseDimExpr() throws CompileException, IOException
DimExpr := '[' Expression ']'
- Throws:
CompileExceptionIOException
-
parseArguments
public Java.Rvalue[] parseArguments() throws CompileException, IOException
Arguments := '(' [ ArgumentList ] ')'- Throws:
CompileExceptionIOException
-
parseArgumentList
public Java.Rvalue[] parseArgumentList() throws CompileException, IOException
ArgumentList := Expression { ',' Expression }- Throws:
CompileExceptionIOException
-
parseLiteral
public Java.Rvalue parseLiteral() throws CompileException, IOException
Literal := IntegerLiteral | FloatingPointLiteral | BooleanLiteral | CharacterLiteral | StringLiteral | NullLiteral- Throws:
CompileExceptionIOException
-
parseExpressionStatement
public Java.Statement parseExpressionStatement() throws CompileException, IOException
ExpressionStatement := Expression ';'
- Throws:
CompileExceptionIOException
-
location
public Location location()
- Returns:
- The location of the first character of the previously read (not peeked!) token
-
peek
public Token peek() throws CompileException, IOException
- Throws:
CompileExceptionIOException
-
read
public Token read() throws CompileException, IOException
- Throws:
CompileExceptionIOException
-
peek
public boolean peek(String suspected) throws CompileException, IOException
- Throws:
CompileExceptionIOException
-
peek
public int peek(String... suspected) throws CompileException, IOException
- Throws:
CompileExceptionIOException
-
peek
public boolean peek(TokenType suspected) throws CompileException, IOException
- Throws:
CompileExceptionIOException
-
peek
public int peek(TokenType... suspected) throws CompileException, IOException
- Throws:
CompileExceptionIOException
-
peekNextButOne
public Token peekNextButOne() throws CompileException, IOException
- Throws:
CompileExceptionIOException
-
peekNextButOne
public boolean peekNextButOne(String suspected) throws CompileException, IOException
- Throws:
CompileExceptionIOException
-
peekNextButOne
public boolean peekNextButOne(TokenType suspected) throws CompileException, IOException
- Throws:
CompileExceptionIOException
-
read
public void read(String expected) throws CompileException, IOException
- Throws:
CompileExceptionIOException
-
read
public int read(String... expected) throws CompileException, IOException
- Throws:
CompileExceptionIOException
-
read
public String read(TokenType expected) throws CompileException, IOException
- Throws:
CompileExceptionIOException
-
peekRead
public boolean peekRead(String suspected) throws CompileException, IOException
- Throws:
CompileExceptionIOException
-
peekRead
public int peekRead(String... suspected) throws CompileException, IOException
- Throws:
CompileExceptionIOException
-
peekRead
@Nullable public String peekRead(TokenType suspected) throws CompileException, IOException
- Throws:
CompileExceptionIOException
-
setSourceVersion
public void setSourceVersion(int version)
-
setWarningHandler
public void setWarningHandler(@Nullable WarningHandler warningHandler)
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-nullto indicate that no warnings be issued
-
compileException
protected final CompileException compileException(String message)
Convenience method for throwing aCompileException.
-
compileException
protected static CompileException compileException(String message, Location location)
Convenience method for throwing aCompileException.
-
-