Package org.codehaus.janino
Class Parser
- java.lang.Object
-
- org.codehaus.janino.Parser
-
public class Parser extends java.lang.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 class
Parser.ClassDeclarationContext
Enumerator for the kinds of context where a class declaration can occur.static class
Parser.InterfaceDeclarationContext
Enumerator for the kinds of context where an interface declaration can occur.
-
Field Summary
Fields Modifier and Type Field Description private static int[]
BASIC_TYPE_CODES
private static java.lang.String[]
BASIC_TYPE_NAMES
private static short[]
MODIFIER_CODES
private static java.lang.String[]
MODIFIER_NAMES
private static short[]
MUTUALLY_EXCLUSIVE_MODIFIER_CODES
private Scanner.Token
nextButOneToken
private Scanner.Token
nextToken
private WarningHandler
optionalWarningHandler
private Scanner
scanner
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected CompileException
compileException(java.lang.String message)
Convenience method for throwing aCompileException
.Scanner
getScanner()
private static int
indexOf(int[] values, int subject)
private static int
indexOf(java.lang.String[] strings, java.lang.String subject)
private static java.lang.String
join(java.lang.String[] sa, java.lang.String separator)
Location
location()
Java.Atom
parseAdditiveExpression()
AdditiveExpression := MultiplicativeExpression { ( '+' | '-' ) MultiplicativeExpression }Java.Atom
parseAndExpression()
AndExpression := EqualityExpression { '&' EqualityExpression }private Java.Annotation
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 } ]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()
'{' BlockStatements '}'Java.BlockStatement
parseBlockStatement()
BlockStatement := { Identifier ':' } ( ( Modifiers Type | ModifiersOpt BasicType ) VariableDeclarators ';' | 'class' ...java.util.List<Java.BlockStatement>
parseBlockStatements()
BlockStatements := { BlockStatement }(package private) int
parseBracketsOpt()
BracketsOpt := { '[' ']' }Java.Statement
parseBreakStatement()
BreakStatement := 'break' [ Identifier ] ';'void
parseClassBody(Java.ClassDeclaration classDeclaration)
ClassBody := '{' { ClassBodyDeclaration } '}'void
parseClassBodyDeclaration(Java.ClassDeclaration classDeclaration)
ClassBodyDeclaration := ';' | ModifiersOpt ( Block | // Instance (JLS7 8.6) or static initializer (JLS7 8.7) 'void' Identifier MethodDeclarationRest | 'class' ClassDeclarationRest | 'interface' InterfaceDeclarationRest | ConstructorDeclarator | Type Identifier ( MethodDeclarationRest | FieldDeclarationRest ';' ) )Java.NamedClassDeclaration
parseClassDeclarationRest(java.lang.String optionalDocComment, Java.Modifiers modifiers, Parser.ClassDeclarationContext context)
ClassDeclarationRest := Identifier [ typeParameters ] [ 'extends' ReferenceType ] [ 'implements' ReferenceTypeList ] ClassBodyJava.CompilationUnit
parseCompilationUnit()
CompilationUnit := [ PackageDeclaration ] { ImportDeclaration } { TypeDeclaration }Java.Atom
parseConditionalAndExpression()
ConditionalAndExpression := InclusiveOrExpression { '&&' InclusiveOrExpression }Java.Atom
parseConditionalExpression()
ConditionalExpression := ConditionalOrExpression [ '?' Expression ':' ConditionalExpression ]Java.Atom
parseConditionalOrExpression()
ConditionalOrExpression := ConditionalAndExpression { '||' ConditionalAndExpression ]Java.ConstructorDeclarator
parseConstructorDeclarator(java.lang.String optionalDocComment, Java.Modifiers modifiers)
ConstructorDeclarator := Identifier FormalParameters [ 'throws' ReferenceTypeList ] '{' [ 'this' Arguments ';' | 'super' Arguments ';' | Primary '.' 'super' Arguments ';' ] BlockStatements '}'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 ')' ';'private Java.ElementValue
parseElementValue()
ElementValue := ConditionalExpression | Annotation | ElementValueArrayInitializerprivate Java.ElementValue
parseElementValueArrayInitializer()
ElementValueArrayInitializer := '{' { ElementValue | ',' } '}'private Java.ElementValuePair
parseElementValuePair()
ElementValuePair := Identifier '=' ElementValueJava.Statement
parseEmptyStatement()
EmptyStatement := ';'Java.Atom
parseEqualityExpression()
EqualityExpression := RelationalExpression { ( '==' | '!=' ) RelationalExpression }Java.Atom
parseExclusiveOrExpression()
ExclusiveOrExpression := AndExpression { '^' AndExpression }Java.Atom
parseExpression()
Expression := AssignmentExpressionJava.Rvalue[]
parseExpressionList()
ExpressionList := Expression { ',' Expression }Java.Statement
parseExpressionStatement()
ExpressionStatement := Expression ';'Java.VariableDeclarator[]
parseFieldDeclarationRest(java.lang.String name)
FieldDeclarationRest := VariableDeclaratorRest { ',' VariableDeclarator }Java.FunctionDeclarator.FormalParameter
parseFormalParameter(boolean[] hasEllipsis)
FormalParameter := [ 'final' ] Type [ '.' '.' '.' ] Identifier BracketsOptJava.FunctionDeclarator.FormalParameters
parseFormalParameters()
FormalParameters := '(' [ FormalParameter { ',' FormalParameter } ] ')'Java.Statement
parseForStatement()
ForStatement := 'for' '(' [ ForInit ] ';' [ Expression ] ';' [ ExpressionList ] ')' Statement | 'for' '(' FormalParameter ':' Expression ')' Statement ForInit := Modifiers Type VariableDeclarators | ModifiersOpt BasicType VariableDeclarators | Expression VariableDeclarators (1) | Expression { ',' Expression }Java.Statement
parseIfStatement()
IfStatement := 'if' '(' Expression ')' Statement [ 'else' Statement ]Java.CompilationUnit.ImportDeclaration
parseImportDeclaration()
ImportDeclaration := 'import' ImportDeclarationBody ';'Java.CompilationUnit.ImportDeclaration
parseImportDeclarationBody()
ImportDeclarationBody := [ 'static' ] Identifier { '.' 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(java.lang.String optionalDocComment, Java.Modifiers modifiers, Parser.InterfaceDeclarationContext context)
InterfaceDeclarationRest := Identifier [ typeParameters ] [ 'extends' ReferenceTypeList ] InterfaceBodyJava.Statement
parseLabeledStatement()
LabeledStatement := Identifier ':' StatementJava.Rvalue
parseLiteral()
Literal := IntegerLiteral | FloatingPointLiteral | BooleanLiteral | CharacterLiteral | StringLiteral | NullLiteralJava.Block
parseMethodBody()
MethodBody := BlockJava.MethodDeclarator
parseMethodDeclarationRest(java.lang.String optionalDocComment, Java.Modifiers modifiers, Java.Type type, java.lang.String name)
MethodDeclarationRest := FormalParameters { '[' ']' } [ 'throws' ReferenceTypeList ] ( ';' | MethodBody )Java.Modifiers
parseModifiers()
ModifiersAndAnnotations := { 'public' | 'protected' | 'private' | 'static' | 'abstract' | 'final' | 'native' | 'synchronized' | 'transient' | 'volatile' | 'strictfp' | Annotation }Java.Atom
parseMultiplicativeExpression()
MultiplicativeExpression := UnaryExpression { ( '*' | '/' | '%' ) UnaryExpression }Java.PackageDeclaration
parsePackageDeclaration()
PackageDeclaration := 'package' QualifiedIdentifier ';'Java.PackageMemberTypeDeclaration
parsePackageMemberTypeDeclaration()
PackageMemberTypeDeclaration := ModifiersOpt 'class' ClassDeclarationRest | ModifiersOpt 'interface' InterfaceDeclarationRestJava.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 '[]' { '[]' } '.' '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 BasicType { '[]' } | // Type BasicType { '[]' } '.' '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 ArrayInitializerjava.lang.String[]
parseQualifiedIdentifier()
QualifiedIdentifier := Identifier { '.' 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 := '.' Identifier | // FieldAccess 15.11.1 '.' Identifier Arguments | // MethodInvocation '.' '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.13Java.Atom
parseShiftExpression()
ShiftExpression := AdditiveExpression { ( '<<' | '>>' | '>>>' ) AdditiveExpression }Java.Statement
parseStatement()
Statement := LabeledStatement | Block | IfStatement | ForStatement | WhileStatement | DoStatement | TryStatement | 'switch' ...Java.Statement
parseSwitchStatement()
SwitchStatement := 'switch' '(' Expression ')' '{' { SwitchLabels BlockStatements } '}' SwitchLabels := SwitchLabels { SwitchLabels } SwitchLabel := 'case' Expression ':' | 'default' ':'Java.Statement
parseSynchronizedStatement()
SynchronizedStatement := 'synchronized' '(' expression ')' BlockJava.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' BlockJava.Type
parseType()
Type := ( 'byte' | 'short' | 'char' | 'int' | 'long' | 'float' | 'double' | 'boolean' | ReferenceType ) { '[' ']' }private Java.TypeArgument
parseTypeArgument()
TypeArgument := ReferenceType { '[' ']' } <= The optional brackets are mising in JLS7, section 18!? | BasicType '[' ']' { '[' ']' } | '?' extends ReferenceType | '?' super ReferenceTypeprivate Java.TypeArgument[]
parseTypeArgumentsOpt()
TypeArguments := '<' TypeArgument { ',' TypeArgument } '>'private Java.TypeParameter
parseTypeParameter()
TypeParameter := identifier [ 'extends' ( identifier | ReferenceType { '&' ReferenceType }private Java.TypeParameter[]
parseTypeParametersOpt()
TypeParameters := '<' TypeParameter { ',' TypeParameter } '>'Java.Atom
parseUnaryExpression()
UnaryExpression := { PrefixOperator } Primary { Selector } { PostfixOperator } PrefixOperator := '++' | '--' | '+' | '-' | '~' | '!' PostfixOperator := '++' | '--'Java.VariableDeclarator
parseVariableDeclarator()
VariableDeclarator := Identifier VariableDeclaratorRestJava.VariableDeclarator
parseVariableDeclaratorRest(java.lang.String name)
VariableDeclaratorRest := { '[' ']' } [ '=' VariableInitializer ]Java.VariableDeclarator[]
parseVariableDeclarators()
VariableDeclarators := VariableDeclarator { ',' VariableDeclarator }Java.ArrayInitializerOrRvalue
parseVariableInitializer()
VariableInitializer := ArrayInitializer | ExpressionJava.Statement
parseWhileStatement()
WhileStatement := 'while' '(' Expression ')' StatementScanner.Token
peek()
int
peek(int[] suspected)
Checks whether the type of the next token is any of thesuspected
; does not consume the next token.boolean
peek(java.lang.String suspected)
int
peek(java.lang.String[] suspected)
Checks whether the value of the next token equals any of thesuspected
; does not consume the next token.boolean
peekEof()
java.lang.String
peekIdentifier()
boolean
peekLiteral()
Scanner.Token
peekNextButOne()
boolean
peekNextButOne(java.lang.String suspected)
boolean
peekRead(java.lang.String suspected)
int
peekRead(java.lang.String[] values)
Scanner.Token
read()
void
read(java.lang.String expected)
Verifies that the value of the next token equalsexpected
, and consumes the token.int
read(java.lang.String[] expected)
Verifies that the value of the next token equals one of theexpected
, and consumes the token.java.lang.String
readIdentifier()
java.lang.String
readOperator()
void
setWarningHandler(WarningHandler optionalWarningHandler)
By default, warnings are discarded, but an application my install aWarningHandler
.private void
verifyIdentifierIsConventionalClassOrInterfaceName(java.lang.String id, Location loc)
Issue a warning if the given identifier does not comply with the class and interface type naming conventions (JLS7 6.8.2).private void
verifyIdentifierIsConventionalFieldName(java.lang.String id, Location loc)
Issue 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
verifyIdentifierIsConventionalLocalVariableOrParameterName(java.lang.String id, Location loc)
Issue a warning if the given identifier does not comply with the local variable and parameter naming conventions (JLS7 6.8.6).private void
verifyIdentifierIsConventionalMethodName(java.lang.String id, Location loc)
Issue a warning if the given identifier does not comply with the method naming conventions (JLS7 6.8.3).private void
verifyStringIsConventionalPackageName(java.lang.String s, Location loc)
Issue a warning if the given string does not comply with the package naming conventions.private void
warning(java.lang.String handle, java.lang.String message, Location optionalLocation)
Issues a warning with the given message and location and returns.
-
-
-
Field Detail
-
scanner
private final Scanner scanner
-
MODIFIER_NAMES
private static final java.lang.String[] MODIFIER_NAMES
-
MODIFIER_CODES
private static final short[] MODIFIER_CODES
-
MUTUALLY_EXCLUSIVE_MODIFIER_CODES
private static final short[] MUTUALLY_EXCLUSIVE_MODIFIER_CODES
-
BASIC_TYPE_NAMES
private static final java.lang.String[] BASIC_TYPE_NAMES
-
BASIC_TYPE_CODES
private static final int[] BASIC_TYPE_CODES
-
nextToken
private Scanner.Token nextToken
-
nextButOneToken
private Scanner.Token nextButOneToken
-
optionalWarningHandler
private WarningHandler optionalWarningHandler
-
-
Constructor Detail
-
Parser
public Parser(Scanner scanner)
-
-
Method Detail
-
getScanner
public Scanner getScanner()
- Returns:
- The scanner that produces the tokens for this parser.
-
parseCompilationUnit
public Java.CompilationUnit parseCompilationUnit() throws CompileException, java.io.IOException
CompilationUnit := [ PackageDeclaration ] { ImportDeclaration } { TypeDeclaration }
- Throws:
CompileException
java.io.IOException
-
parsePackageDeclaration
public Java.PackageDeclaration parsePackageDeclaration() throws CompileException, java.io.IOException
PackageDeclaration := 'package' QualifiedIdentifier ';'
- Throws:
CompileException
java.io.IOException
-
parseImportDeclaration
public Java.CompilationUnit.ImportDeclaration parseImportDeclaration() throws CompileException, java.io.IOException
ImportDeclaration := 'import' ImportDeclarationBody ';'
- Throws:
CompileException
java.io.IOException
-
parseImportDeclarationBody
public Java.CompilationUnit.ImportDeclaration parseImportDeclarationBody() throws CompileException, java.io.IOException
ImportDeclarationBody := [ 'static' ] Identifier { '.' Identifier } [ '.' '*' ]
- Throws:
CompileException
java.io.IOException
-
parseQualifiedIdentifier
public java.lang.String[] parseQualifiedIdentifier() throws CompileException, java.io.IOException
QualifiedIdentifier := Identifier { '.' Identifier }
- Throws:
CompileException
java.io.IOException
-
parsePackageMemberTypeDeclaration
public Java.PackageMemberTypeDeclaration parsePackageMemberTypeDeclaration() throws CompileException, java.io.IOException
PackageMemberTypeDeclaration := ModifiersOpt 'class' ClassDeclarationRest | ModifiersOpt 'interface' InterfaceDeclarationRest
- Throws:
CompileException
java.io.IOException
-
parseModifiers
public Java.Modifiers parseModifiers() throws CompileException, java.io.IOException
ModifiersAndAnnotations := { 'public' | 'protected' | 'private' | 'static' | 'abstract' | 'final' | 'native' | 'synchronized' | 'transient' | 'volatile' | 'strictfp' | Annotation }
- Throws:
CompileException
java.io.IOException
-
parseAnnotation
private Java.Annotation parseAnnotation() throws CompileException, java.io.IOException
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
java.io.IOException
-
parseElementValuePair
private Java.ElementValuePair parseElementValuePair() throws CompileException, java.io.IOException
ElementValuePair := Identifier '=' ElementValue
- Throws:
CompileException
java.io.IOException
-
parseElementValue
private Java.ElementValue parseElementValue() throws CompileException, java.io.IOException
ElementValue := ConditionalExpression | Annotation | ElementValueArrayInitializer
- Throws:
CompileException
java.io.IOException
-
parseElementValueArrayInitializer
private Java.ElementValue parseElementValueArrayInitializer() throws CompileException, java.io.IOException
ElementValueArrayInitializer := '{' { ElementValue | ',' } '}'
- Throws:
CompileException
java.io.IOException
-
parseClassDeclarationRest
public Java.NamedClassDeclaration parseClassDeclarationRest(java.lang.String optionalDocComment, Java.Modifiers modifiers, Parser.ClassDeclarationContext context) throws CompileException, java.io.IOException
ClassDeclarationRest := Identifier [ typeParameters ] [ 'extends' ReferenceType ] [ 'implements' ReferenceTypeList ] ClassBody
- Throws:
CompileException
java.io.IOException
-
parseClassBody
public void parseClassBody(Java.ClassDeclaration classDeclaration) throws CompileException, java.io.IOException
ClassBody := '{' { ClassBodyDeclaration } '}'
- Throws:
CompileException
java.io.IOException
-
parseClassBodyDeclaration
public void parseClassBodyDeclaration(Java.ClassDeclaration classDeclaration) throws CompileException, java.io.IOException
ClassBodyDeclaration := ';' | ModifiersOpt ( Block | // Instance (JLS7 8.6) or static initializer (JLS7 8.7) 'void' Identifier MethodDeclarationRest | 'class' ClassDeclarationRest | 'interface' InterfaceDeclarationRest | ConstructorDeclarator | Type Identifier ( MethodDeclarationRest | FieldDeclarationRest ';' ) )
- Throws:
CompileException
java.io.IOException
-
parseInterfaceDeclarationRest
public Java.InterfaceDeclaration parseInterfaceDeclarationRest(java.lang.String optionalDocComment, Java.Modifiers modifiers, Parser.InterfaceDeclarationContext context) throws CompileException, java.io.IOException
InterfaceDeclarationRest := Identifier [ typeParameters ] [ 'extends' ReferenceTypeList ] InterfaceBody
- Throws:
CompileException
java.io.IOException
-
parseInterfaceBody
public void parseInterfaceBody(Java.InterfaceDeclaration interfaceDeclaration) throws CompileException, java.io.IOException
InterfaceBody := '{' { ';' | ModifiersOpt ( 'void' Identifier MethodDeclarationRest | 'class' ClassDeclarationRest | 'interface' InterfaceDeclarationRest | Type Identifier ( MethodDeclarationRest | FieldDeclarationRest ) ) } '}'
- Throws:
CompileException
java.io.IOException
-
parseConstructorDeclarator
public Java.ConstructorDeclarator parseConstructorDeclarator(java.lang.String optionalDocComment, Java.Modifiers modifiers) throws CompileException, java.io.IOException
ConstructorDeclarator := Identifier FormalParameters [ 'throws' ReferenceTypeList ] '{' [ 'this' Arguments ';' | 'super' Arguments ';' | Primary '.' 'super' Arguments ';' ] BlockStatements '}'
- Throws:
CompileException
java.io.IOException
-
parseMethodDeclarationRest
public Java.MethodDeclarator parseMethodDeclarationRest(java.lang.String optionalDocComment, Java.Modifiers modifiers, Java.Type type, java.lang.String name) throws CompileException, java.io.IOException
MethodDeclarationRest := FormalParameters { '[' ']' } [ 'throws' ReferenceTypeList ] ( ';' | MethodBody )
- Throws:
CompileException
java.io.IOException
-
parseVariableInitializer
public Java.ArrayInitializerOrRvalue parseVariableInitializer() throws CompileException, java.io.IOException
VariableInitializer := ArrayInitializer | Expression
- Throws:
CompileException
java.io.IOException
-
parseArrayInitializer
public Java.ArrayInitializer parseArrayInitializer() throws CompileException, java.io.IOException
ArrayInitializer := '{' [ VariableInitializer { ',' VariableInitializer } [ ',' ] '}'
- Throws:
CompileException
java.io.IOException
-
parseFormalParameters
public Java.FunctionDeclarator.FormalParameters parseFormalParameters() throws CompileException, java.io.IOException
FormalParameters := '(' [ FormalParameter { ',' FormalParameter } ] ')'
- Throws:
CompileException
java.io.IOException
-
parseFormalParameter
public Java.FunctionDeclarator.FormalParameter parseFormalParameter(boolean[] hasEllipsis) throws CompileException, java.io.IOException
FormalParameter := [ 'final' ] Type [ '.' '.' '.' ] Identifier BracketsOpt
- Throws:
CompileException
java.io.IOException
-
parseBracketsOpt
int parseBracketsOpt() throws CompileException, java.io.IOException
BracketsOpt := { '[' ']' }
- Throws:
CompileException
java.io.IOException
-
parseMethodBody
public Java.Block parseMethodBody() throws CompileException, java.io.IOException
MethodBody := Block
- Throws:
CompileException
java.io.IOException
-
parseBlock
public Java.Block parseBlock() throws CompileException, java.io.IOException
'{' BlockStatements '}'
- Throws:
CompileException
java.io.IOException
-
parseBlockStatements
public java.util.List<Java.BlockStatement> parseBlockStatements() throws CompileException, java.io.IOException
BlockStatements := { BlockStatement }
- Throws:
CompileException
java.io.IOException
-
parseBlockStatement
public Java.BlockStatement parseBlockStatement() throws CompileException, java.io.IOException
BlockStatement := { Identifier ':' } ( ( Modifiers Type | ModifiersOpt BasicType ) VariableDeclarators ';' | 'class' ... | Statement | 'final' Type VariableDeclarators ';' | Expression ';' | Expression VariableDeclarators ';' (1) )
(1) "Expression" must pose a type, and has optional trailing brackets.- Throws:
CompileException
java.io.IOException
-
parseVariableDeclarators
public Java.VariableDeclarator[] parseVariableDeclarators() throws CompileException, java.io.IOException
VariableDeclarators := VariableDeclarator { ',' VariableDeclarator }
- Throws:
CompileException
java.io.IOException
-
parseFieldDeclarationRest
public Java.VariableDeclarator[] parseFieldDeclarationRest(java.lang.String name) throws CompileException, java.io.IOException
FieldDeclarationRest := VariableDeclaratorRest { ',' VariableDeclarator }
- Throws:
CompileException
java.io.IOException
-
parseVariableDeclarator
public Java.VariableDeclarator parseVariableDeclarator() throws CompileException, java.io.IOException
VariableDeclarator := Identifier VariableDeclaratorRest
- Throws:
CompileException
java.io.IOException
-
parseVariableDeclaratorRest
public Java.VariableDeclarator parseVariableDeclaratorRest(java.lang.String name) throws CompileException, java.io.IOException
VariableDeclaratorRest := { '[' ']' } [ '=' VariableInitializer ]
Used by field declarations and local variable declarations.- Throws:
CompileException
java.io.IOException
-
parseStatement
public Java.Statement parseStatement() throws CompileException, java.io.IOException
Statement := LabeledStatement | Block | IfStatement | ForStatement | WhileStatement | DoStatement | TryStatement | 'switch' ... | 'synchronized' ... | ReturnStatement | ThrowStatement | BreakStatement | ContinueStatement | EmptyStatement | ExpressionStatement
- Throws:
CompileException
java.io.IOException
-
parseLabeledStatement
public Java.Statement parseLabeledStatement() throws CompileException, java.io.IOException
LabeledStatement := Identifier ':' Statement
- Throws:
CompileException
java.io.IOException
-
parseIfStatement
public Java.Statement parseIfStatement() throws CompileException, java.io.IOException
IfStatement := 'if' '(' Expression ')' Statement [ 'else' Statement ]
- Throws:
CompileException
java.io.IOException
-
parseForStatement
public Java.Statement parseForStatement() throws CompileException, java.io.IOException
ForStatement := 'for' '(' [ ForInit ] ';' [ Expression ] ';' [ ExpressionList ] ')' Statement | 'for' '(' FormalParameter ':' Expression ')' Statement ForInit := Modifiers Type VariableDeclarators | ModifiersOpt BasicType VariableDeclarators | Expression VariableDeclarators (1) | Expression { ',' Expression }
(1) "Expression" must pose a type.- Throws:
CompileException
java.io.IOException
-
parseWhileStatement
public Java.Statement parseWhileStatement() throws CompileException, java.io.IOException
WhileStatement := 'while' '(' Expression ')' Statement
- Throws:
CompileException
java.io.IOException
-
parseDoStatement
public Java.Statement parseDoStatement() throws CompileException, java.io.IOException
DoStatement := 'do' Statement 'while' '(' Expression ')' ';'
- Throws:
CompileException
java.io.IOException
-
parseTryStatement
public Java.Statement parseTryStatement() throws CompileException, java.io.IOException
TryStatement := 'try' Block Catches [ Finally ] | 'try' Block Finally Catches := CatchClause { CatchClause } CatchClause := 'catch' '(' FormalParameter ')' Block Finally := 'finally' Block
- Throws:
CompileException
java.io.IOException
-
parseSwitchStatement
public Java.Statement parseSwitchStatement() throws CompileException, java.io.IOException
SwitchStatement := 'switch' '(' Expression ')' '{' { SwitchLabels BlockStatements } '}' SwitchLabels := SwitchLabels { SwitchLabels } SwitchLabel := 'case' Expression ':' | 'default' ':'
- Throws:
CompileException
java.io.IOException
-
parseSynchronizedStatement
public Java.Statement parseSynchronizedStatement() throws CompileException, java.io.IOException
SynchronizedStatement := 'synchronized' '(' expression ')' Block
- Throws:
CompileException
java.io.IOException
-
parseReturnStatement
public Java.Statement parseReturnStatement() throws CompileException, java.io.IOException
ReturnStatement := 'return' [ Expression ] ';'
- Throws:
CompileException
java.io.IOException
-
parseThrowStatement
public Java.Statement parseThrowStatement() throws CompileException, java.io.IOException
ThrowStatement := 'throw' Expression ';'
- Throws:
CompileException
java.io.IOException
-
parseBreakStatement
public Java.Statement parseBreakStatement() throws CompileException, java.io.IOException
BreakStatement := 'break' [ Identifier ] ';'
- Throws:
CompileException
java.io.IOException
-
parseContinueStatement
public Java.Statement parseContinueStatement() throws CompileException, java.io.IOException
ContinueStatement := 'continue' [ Identifier ] ';'
- Throws:
CompileException
java.io.IOException
-
parseAssertStatement
public Java.Statement parseAssertStatement() throws CompileException, java.io.IOException
AssertStatement := 'assert' Expression [ ':' Expression ] ';'
- Throws:
CompileException
java.io.IOException
-
parseEmptyStatement
public Java.Statement parseEmptyStatement() throws CompileException, java.io.IOException
EmptyStatement := ';'
- Throws:
CompileException
java.io.IOException
-
parseExpressionList
public Java.Rvalue[] parseExpressionList() throws CompileException, java.io.IOException
ExpressionList := Expression { ',' Expression }
- Throws:
CompileException
java.io.IOException
-
parseType
public Java.Type parseType() throws CompileException, java.io.IOException
Type := ( 'byte' | 'short' | 'char' | 'int' | 'long' | 'float' | 'double' | 'boolean' | ReferenceType ) { '[' ']' }
- Throws:
CompileException
java.io.IOException
-
parseReferenceType
public Java.ReferenceType parseReferenceType() throws CompileException, java.io.IOException
ReferenceType := QualifiedIdentifier [ TypeArguments ]
- Throws:
CompileException
java.io.IOException
-
parseTypeParametersOpt
private Java.TypeParameter[] parseTypeParametersOpt() throws CompileException, java.io.IOException
TypeParameters := '<' TypeParameter { ',' TypeParameter } '>'
- Throws:
CompileException
java.io.IOException
-
parseTypeParameter
private Java.TypeParameter parseTypeParameter() throws CompileException, java.io.IOException
TypeParameter := identifier [ 'extends' ( identifier | ReferenceType { '&' ReferenceType }
- Throws:
CompileException
java.io.IOException
-
parseTypeArgumentsOpt
private Java.TypeArgument[] parseTypeArgumentsOpt() throws CompileException, java.io.IOException
TypeArguments := '<' TypeArgument { ',' TypeArgument } '>'
- Throws:
CompileException
java.io.IOException
-
parseTypeArgument
private Java.TypeArgument parseTypeArgument() throws CompileException, java.io.IOException
TypeArgument := ReferenceType { '[' ']' } <= The optional brackets are mising in JLS7, section 18!? | BasicType '[' ']' { '[' ']' } | '?' extends ReferenceType | '?' super ReferenceType
- Throws:
CompileException
java.io.IOException
-
parseReferenceTypeList
public Java.ReferenceType[] parseReferenceTypeList() throws CompileException, java.io.IOException
ReferenceTypeList := ReferenceType { ',' ReferenceType }
- Throws:
CompileException
java.io.IOException
-
parseExpression
public Java.Atom parseExpression() throws CompileException, java.io.IOException
Expression := AssignmentExpression
- Throws:
CompileException
java.io.IOException
-
parseAssignmentExpression
public Java.Atom parseAssignmentExpression() throws CompileException, java.io.IOException
AssignmentExpression := ConditionalExpression [ AssignmentOperator AssignmentExpression ] AssignmentOperator := '=' | '*=' | '/=' | '%=' | '+=' | '-=' | '<<=' | '>>=' | '>>>=' | '&=' | '^=' | '|='
- Throws:
CompileException
java.io.IOException
-
parseConditionalExpression
public Java.Atom parseConditionalExpression() throws CompileException, java.io.IOException
ConditionalExpression := ConditionalOrExpression [ '?' Expression ':' ConditionalExpression ]
- Throws:
CompileException
java.io.IOException
-
parseConditionalOrExpression
public Java.Atom parseConditionalOrExpression() throws CompileException, java.io.IOException
ConditionalOrExpression := ConditionalAndExpression { '||' ConditionalAndExpression ]
- Throws:
CompileException
java.io.IOException
-
parseConditionalAndExpression
public Java.Atom parseConditionalAndExpression() throws CompileException, java.io.IOException
ConditionalAndExpression := InclusiveOrExpression { '&&' InclusiveOrExpression }
- Throws:
CompileException
java.io.IOException
-
parseInclusiveOrExpression
public Java.Atom parseInclusiveOrExpression() throws CompileException, java.io.IOException
InclusiveOrExpression := ExclusiveOrExpression { '|' ExclusiveOrExpression }
- Throws:
CompileException
java.io.IOException
-
parseExclusiveOrExpression
public Java.Atom parseExclusiveOrExpression() throws CompileException, java.io.IOException
ExclusiveOrExpression := AndExpression { '^' AndExpression }
- Throws:
CompileException
java.io.IOException
-
parseAndExpression
public Java.Atom parseAndExpression() throws CompileException, java.io.IOException
AndExpression := EqualityExpression { '&' EqualityExpression }
- Throws:
CompileException
java.io.IOException
-
parseEqualityExpression
public Java.Atom parseEqualityExpression() throws CompileException, java.io.IOException
EqualityExpression := RelationalExpression { ( '==' | '!=' ) RelationalExpression }
- Throws:
CompileException
java.io.IOException
-
parseRelationalExpression
public Java.Atom parseRelationalExpression() throws CompileException, java.io.IOException
RelationalExpression := ShiftExpression { 'instanceof' ReferenceType | '<' ShiftExpression [ { ',' TypeArgument } '>' ] | '<' TypeArgument [ { ',' TypeArgument } '>' ] | ( '>' | '<=' | '>=' ) ShiftExpression }
- Throws:
CompileException
java.io.IOException
-
parseShiftExpression
public Java.Atom parseShiftExpression() throws CompileException, java.io.IOException
ShiftExpression := AdditiveExpression { ( '<<' | '>>' | '>>>' ) AdditiveExpression }
- Throws:
CompileException
java.io.IOException
-
parseAdditiveExpression
public Java.Atom parseAdditiveExpression() throws CompileException, java.io.IOException
AdditiveExpression := MultiplicativeExpression { ( '+' | '-' ) MultiplicativeExpression }
- Throws:
CompileException
java.io.IOException
-
parseMultiplicativeExpression
public Java.Atom parseMultiplicativeExpression() throws CompileException, java.io.IOException
MultiplicativeExpression := UnaryExpression { ( '*' | '/' | '%' ) UnaryExpression }
- Throws:
CompileException
java.io.IOException
-
parseUnaryExpression
public Java.Atom parseUnaryExpression() throws CompileException, java.io.IOException
UnaryExpression := { PrefixOperator } Primary { Selector } { PostfixOperator } PrefixOperator := '++' | '--' | '+' | '-' | '~' | '!' PostfixOperator := '++' | '--'
- Throws:
CompileException
java.io.IOException
-
parsePrimary
public Java.Atom parsePrimary() throws CompileException, java.io.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 BasicType { '[]' } | // Type BasicType { '[]' } '.' '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
- Throws:
CompileException
java.io.IOException
-
parseSelector
public Java.Atom parseSelector(Java.Atom atom) throws CompileException, java.io.IOException
Selector := '.' Identifier | // FieldAccess 15.11.1 '.' Identifier Arguments | // MethodInvocation '.' '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
- Throws:
CompileException
java.io.IOException
-
parseDimExprs
public Java.Rvalue[] parseDimExprs() throws CompileException, java.io.IOException
DimExprs := DimExpr { DimExpr }
- Throws:
CompileException
java.io.IOException
-
parseDimExpr
public Java.Rvalue parseDimExpr() throws CompileException, java.io.IOException
DimExpr := '[' Expression ']'
- Throws:
CompileException
java.io.IOException
-
parseArguments
public Java.Rvalue[] parseArguments() throws CompileException, java.io.IOException
Arguments := '(' [ ArgumentList ] ')'
- Throws:
CompileException
java.io.IOException
-
parseArgumentList
public Java.Rvalue[] parseArgumentList() throws CompileException, java.io.IOException
ArgumentList := Expression { ',' Expression }
- Throws:
CompileException
java.io.IOException
-
parseLiteral
public Java.Rvalue parseLiteral() throws CompileException, java.io.IOException
Literal := IntegerLiteral | FloatingPointLiteral | BooleanLiteral | CharacterLiteral | StringLiteral | NullLiteral
- Throws:
CompileException
java.io.IOException
-
parseExpressionStatement
public Java.Statement parseExpressionStatement() throws CompileException, java.io.IOException
ExpressionStatement := Expression ';'
- Throws:
CompileException
java.io.IOException
-
location
public Location location()
-
peek
public Scanner.Token peek() throws CompileException, java.io.IOException
- Returns:
- The next token, but does not consume it
- Throws:
CompileException
java.io.IOException
-
peekNextButOne
public Scanner.Token peekNextButOne() throws CompileException, java.io.IOException
- Returns:
- The next-but-one token, but consumes neither the next nor the next-but-one token
- Throws:
CompileException
java.io.IOException
-
read
public Scanner.Token read() throws CompileException, java.io.IOException
- Returns:
- The next and also consumes it, or
null
iff the scanner is at end-of-input - Throws:
CompileException
java.io.IOException
-
peek
public boolean peek(java.lang.String suspected) throws CompileException, java.io.IOException
- Returns:
- Whether the value of the next token equals
suspected
; does not consume the next token - Throws:
CompileException
java.io.IOException
-
peek
public int peek(java.lang.String[] suspected) throws CompileException, java.io.IOException
Checks whether the value of the next token equals any of thesuspected
; does not consume the next token.- Returns:
- The index of the first of the
suspected
that equals the value of the next token, or -1 if the value of the next token equals none of thesuspected
- Throws:
CompileException
java.io.IOException
-
peek
public int peek(int[] suspected) throws CompileException, java.io.IOException
Checks whether the type of the next token is any of thesuspected
; does not consume the next token.- Returns:
- The index of the first of the
suspected
types that is the next token's type, or -1 if the type of the next token is none of thesuspected
types - Throws:
CompileException
java.io.IOException
-
peekNextButOne
public boolean peekNextButOne(java.lang.String suspected) throws CompileException, java.io.IOException
- Returns:
- Whether the value of the next-but-one token equals the
suspected
; consumes neither the next nor the next-but-one token - Throws:
CompileException
java.io.IOException
-
read
public void read(java.lang.String expected) throws CompileException, java.io.IOException
Verifies that the value of the next token equalsexpected
, and consumes the token.- Throws:
CompileException
- The value of the next token does not equalexpected
(this includes the case that the scanner is at end-of-input)java.io.IOException
-
read
public int read(java.lang.String[] expected) throws CompileException, java.io.IOException
Verifies that the value of the next token equals one of theexpected
, and consumes the token.- Returns:
- The index of the consumed token within
expected
- Throws:
CompileException
- The value of the next token does not equal any of theexpected
(this includes the case where the scanner is at end-of-input)java.io.IOException
-
peekRead
public boolean peekRead(java.lang.String suspected) throws CompileException, java.io.IOException
- Returns:
- Whether the value of the next token equals the
suspected
; if so, it consumes the next token - Throws:
CompileException
java.io.IOException
-
peekRead
public int peekRead(java.lang.String[] values) throws CompileException, java.io.IOException
- Returns:
- -1 iff the next token is none of
values
- Throws:
CompileException
java.io.IOException
-
peekEof
public boolean peekEof() throws CompileException, java.io.IOException
- Returns:
- Whether the scanner is at end-of-input
- Throws:
CompileException
java.io.IOException
-
peekIdentifier
public java.lang.String peekIdentifier() throws CompileException, java.io.IOException
- Returns:
null
iff the next token is not an identifier, otherwise the value of the identifier token- Throws:
CompileException
java.io.IOException
-
peekLiteral
public boolean peekLiteral() throws CompileException, java.io.IOException
- Returns:
- Whether the next token is a literal
- Throws:
CompileException
java.io.IOException
-
readIdentifier
public java.lang.String readIdentifier() throws CompileException, java.io.IOException
- Returns:
- The value of the next token, which is an indentifier
- Throws:
CompileException
- The next token is not an identifierjava.io.IOException
-
readOperator
public java.lang.String readOperator() throws CompileException, java.io.IOException
- Returns:
- The value of the next token, which is an operator
- Throws:
CompileException
- The next token is not an operatorjava.io.IOException
-
indexOf
private static int indexOf(java.lang.String[] strings, java.lang.String subject)
-
indexOf
private static int indexOf(int[] values, int subject)
-
verifyStringIsConventionalPackageName
private void verifyStringIsConventionalPackageName(java.lang.String s, Location loc) throws CompileException
Issue a warning if the given string does not comply with the package naming conventions.- Throws:
CompileException
-
verifyIdentifierIsConventionalClassOrInterfaceName
private void verifyIdentifierIsConventionalClassOrInterfaceName(java.lang.String id, Location loc) throws CompileException
Issue 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(java.lang.String id, Location loc) throws CompileException
Issue a warning if the given identifier does not comply with the method naming conventions (JLS7 6.8.3).- Throws:
CompileException
-
verifyIdentifierIsConventionalFieldName
private void verifyIdentifierIsConventionalFieldName(java.lang.String id, Location loc) throws CompileException
Issue 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(java.lang.String id, Location loc) throws CompileException
Issue a warning if the given identifier does not comply with the local variable and parameter naming conventions (JLS7 6.8.6).- Throws:
CompileException
-
setWarningHandler
public void setWarningHandler(WarningHandler optionalWarningHandler)
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:
optionalWarningHandler
-null
to indicate that no warnings be issued
-
warning
private void warning(java.lang.String handle, java.lang.String message, Location optionalLocation) 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 qulifies the warning and is typically used by theWarningHandler
to suppress individual warnings.- Throws:
CompileException
- The optionally installedWarningHandler
decided to throw aCompileException
-
compileException
protected final CompileException compileException(java.lang.String message)
Convenience method for throwing aCompileException
.
-
join
private static java.lang.String join(java.lang.String[] sa, java.lang.String separator)
-
-