janino.net

org.codehaus.janino
Class Parser

java.lang.Object
  extended by 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
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.
 
Constructor Summary
Parser(Scanner scanner)
           
 
Method Summary
protected  CompileException compileException(String message)
          Convenience method for throwing a CompileException.
 Scanner getScanner()
           
 Location location()
           
 Java.Atom parseAdditiveExpression()
           AdditiveExpression := MultiplicativeExpression { ( '+' | '-' ) MultiplicativeExpression }
 Java.Atom parseAndExpression()
           AndExpression := EqualityExpression { '&' EqualityExpression }
 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' ... | Statement | 'final' Type VariableDeclarators ';' | Expression ';' | Expression VariableDeclarators ';' (1) ) (1) "Expression" must pose a type, and has optional trailing brackets.
 List<Java.BlockStatement> parseBlockStatements()
           BlockStatements := { BlockStatement }
 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(String optionalDocComment, Java.Modifiers modifiers, Parser.ClassDeclarationContext context)
           ClassDeclarationRest := Identifier [ typeParameters ] [ 'extends' ReferenceType ] [ 'implements' ReferenceTypeList ] ClassBody
 Java.CompilationUnit parseCompilationUnit()
           CompilationUnit := [ PackageDeclaration ] { ImportDeclaration } { TypeDeclaration }
 Java.Atom parseConditionalAndExpression()
           ConditionalAndExpression := InclusiveOrExpression { '&&' InclusiveOrExpression }
 Java.Atom parseConditionalExpression()
           ConditionalExpression := ConditionalOrExpression [ '?'
 Java.Atom parseConditionalOrExpression()
           ConditionalOrExpression := ConditionalAndExpression { '||' ConditionalAndExpression ]
 Java.ConstructorDeclarator parseConstructorDeclarator(String optionalDocComment, Java.Modifiers modifiers)
           ConstructorDeclarator := Identifier FormalParameters [ 'throws' ReferenceTypeList ] '{' [ 'this' Arguments ';' | 'super' Arguments ';' | Primary '.'
 Java.Statement parseContinueStatement()
           ContinueStatement := 'continue' [ Identifier ] ';'
 Java.Rvalue parseDimExpr()
           DimExpr := '[' Expression ']'
 Java.Rvalue[] parseDimExprs()
           DimExprs := DimExpr { DimExpr }
 Java.Statement parseDoStatement()
           DoStatement := 'do' Statement 'while' '(' Expression ')' ';'
 Java.Statement parseEmptyStatement()
           EmptyStatement := ';'
 Java.Atom parseEqualityExpression()
           EqualityExpression := RelationalExpression { ( '==' | '!
 Java.Atom parseExclusiveOrExpression()
           ExclusiveOrExpression := AndExpression { '^' AndExpression }
 Java.Atom parseExpression()
           Expression := AssignmentExpression
 Java.Rvalue[] parseExpressionList()
           ExpressionList := Expression { ',' Expression }
 Java.Statement parseExpressionStatement()
           ExpressionStatement := Expression ';'
 Java.VariableDeclarator[] parseFieldDeclarationRest(String name)
           FieldDeclarationRest := VariableDeclaratorRest { ',' VariableDeclarator }
 Java.FunctionDeclarator.FormalParameter parseFormalParameter(boolean[] hasEllipsis)
           FormalParameter := [ 'final' ] Type [ '.' '.' '.' ] Identifier BracketsOpt
 Java.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 } (1) "Expression" must pose a type.
 Java.Statement parseIfStatement()
           IfStatement := 'if' '(' Expression ')' Statement [ 'else' Statement ]
 Java.CompilationUnit.ImportDeclaration parseImportDeclaration()
           ImportDeclaration := 'import' ImportDeclarationBody ';'
 Java.CompilationUnit.ImportDeclaration parseImportDeclarationBody()
           ImportDeclarationBody := [ 'static' ] Identifier { '.'
 Java.Atom parseInclusiveOrExpression()
           InclusiveOrExpression := ExclusiveOrExpression { '|' ExclusiveOrExpression }
 void parseInterfaceBody(Java.InterfaceDeclaration interfaceDeclaration)
           InterfaceBody := '{' { ';' | ModifiersOpt ( 'void' Identifier MethodDeclarationRest | 'class' ClassDeclarationRest | 'interface' InterfaceDeclarationRest | Type Identifier ( MethodDeclarationRest | FieldDeclarationRest ) ) } '}'
 Java.InterfaceDeclaration parseInterfaceDeclarationRest(String optionalDocComment, Java.Modifiers modifiers, Parser.InterfaceDeclarationContext context)
           InterfaceDeclarationRest := Identifier [ typeParameters ] [ 'extends' ReferenceTypeList ] InterfaceBody
 Java.Statement parseLabeledStatement()
           LabeledStatement := Identifier ':' Statement
 Java.Rvalue parseLiteral()
           Literal := IntegerLiteral | FloatingPointLiteral | BooleanLiteral | CharacterLiteral | StringLiteral | NullLiteral
 Java.Block parseMethodBody()
           MethodBody := Block
 Java.MethodDeclarator parseMethodDeclarationRest(String optionalDocComment, Java.Modifiers modifiers, Java.Type type, 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' InterfaceDeclarationRest
 Java.Atom parsePrimary()
           Primary := CastExpression | // CastExpression 15.16 '(' Expression ')' | // ParenthesizedExpression 15.8.5 Literal | // Literal 15.8.1 Name | // AmbiguousName Name Arguments | // MethodInvocation Name '[]' { '[]' } | // ArrayType 10.1 Name '[]' { '[]' } '.'
 String[] parseQualifiedIdentifier()
           QualifiedIdentifier := Identifier { '.'
 Java.ReferenceType parseReferenceType()
           ReferenceType := QualifiedIdentifier [ TypeArguments ]
 Java.ReferenceType[] parseReferenceTypeList()
           ReferenceTypeList := ReferenceType { ',' ReferenceType }
 Java.Atom parseRelationalExpression()
           RelationalExpression := ShiftExpression { 'instanceof' ReferenceType | '<' ShiftExpression [ { ',' TypeArgument } '>' ] | '<' TypeArgument [ { ',' TypeArgument } '>' ] | ( '>' | '<=' | '>=' ) ShiftExpression }
 Java.Statement parseReturnStatement()
           ReturnStatement := 'return' [ Expression ] ';'
 Java.Atom parseSelector(Java.Atom atom)
           Selector := '.'
 Java.Atom parseShiftExpression()
           ShiftExpression := AdditiveExpression { ( '<<' | '>>' | '>>>' ) AdditiveExpression }
 Java.Statement parseStatement()
           Statement := LabeledStatement | Block | IfStatement | ForStatement | WhileStatement | DoStatement | TryStatement | 'switch' ... | 'synchronized' ... | ReturnStatement | ThrowStatement | BreakStatement | ContinueStatement | EmptyStatement | ExpressionStatement
 Java.Statement parseSwitchStatement()
           SwitchStatement := 'switch' '(' Expression ')' '{' { SwitchLabels BlockStatements } '}' SwitchLabels := SwitchLabels { SwitchLabels } SwitchLabel := 'case' Expression ':' | 'default' ':'
 Java.Statement parseSynchronizedStatement()
           SynchronizedStatement := 'synchronized' '(' expression ')' Block
 Java.Statement parseThrowStatement()
           ThrowStatement := 'throw' Expression ';'
 Java.Statement parseTryStatement()
           TryStatement := 'try' Block Catches [ Finally ] | 'try' Block Finally Catches := CatchClause { CatchClause } CatchClause := 'catch' '(' FormalParameter ')' Block Finally := 'finally' Block
 Java.Type parseType()
           Type := ( 'byte' | 'short' | 'char' | 'int' | 'long' | 'float' | 'double' | 'boolean' | ReferenceType ) { '[' ']' }
 Java.Atom parseUnaryExpression()
           UnaryExpression := { PrefixOperator } Primary { Selector } { PostfixOperator } PrefixOperator := '++' | '--' | '+' | '-' | '~' | '!'
 Java.VariableDeclarator parseVariableDeclarator()
           VariableDeclarator := Identifier VariableDeclaratorRest
 Java.VariableDeclarator parseVariableDeclaratorRest(String name)
           VariableDeclaratorRest := { '[' ']' } [ '=' VariableInitializer ] Used by field declarations and local variable declarations.
 Java.VariableDeclarator[] parseVariableDeclarators()
           VariableDeclarators := VariableDeclarator { ',' VariableDeclarator }
 Java.ArrayInitializerOrRvalue parseVariableInitializer()
           VariableInitializer := ArrayInitializer | Expression
 Java.Statement parseWhileStatement()
           WhileStatement := 'while' '(' Expression ')' Statement
 Scanner.Token peek()
           
 int peek(int[] suspected)
          Checks whether the type of the next token is any of the suspected; does not consume the next token.
 boolean peek(String suspected)
           
 int peek(String[] suspected)
          Checks whether the value of the next token equals any of the suspected; does not consume the next token.
 boolean peekEof()
           
 String peekIdentifier()
           
 boolean peekLiteral()
           
 Scanner.Token peekNextButOne()
           
 boolean peekNextButOne(String suspected)
           
 boolean peekRead(String suspected)
           
 int peekRead(String[] values)
           
 Scanner.Token read()
           
 void read(String expected)
          Verifies that the value of the next token equals expected, and consumes the token.
 int read(String[] expected)
          Verifies that the value of the next token equals one of the expected, and consumes the token.
 String readIdentifier()
           
 String readOperator()
           
 void setWarningHandler(WarningHandler optionalWarningHandler)
          By default, warnings are discarded, but an application my install a WarningHandler.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

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,
                                                 IOException
   CompilationUnit := [ PackageDeclaration ]
                      { ImportDeclaration }
                      { TypeDeclaration }
 

Throws:
CompileException
IOException

parsePackageDeclaration

public Java.PackageDeclaration parsePackageDeclaration()
                                                throws CompileException,
                                                       IOException
   PackageDeclaration := 'package' QualifiedIdentifier ';'
 

Throws:
CompileException
IOException

parseImportDeclaration

public Java.CompilationUnit.ImportDeclaration parseImportDeclaration()
                                                              throws CompileException,
                                                                     IOException
   ImportDeclaration := 'import' ImportDeclarationBody ';'
 

Throws:
CompileException
IOException

parseImportDeclarationBody

public Java.CompilationUnit.ImportDeclaration parseImportDeclarationBody()
                                                                  throws CompileException,
                                                                         IOException
   ImportDeclarationBody := [ 'static' ] Identifier { '.' Identifier } [ '.' '*' ]
 

Throws:
CompileException
IOException

parseQualifiedIdentifier

public String[] parseQualifiedIdentifier()
                                  throws CompileException,
                                         IOException
   QualifiedIdentifier := Identifier { '.' Identifier }
 

Throws:
CompileException
IOException

parsePackageMemberTypeDeclaration

public Java.PackageMemberTypeDeclaration parsePackageMemberTypeDeclaration()
                                                                    throws CompileException,
                                                                           IOException
   PackageMemberTypeDeclaration :=
             ModifiersOpt 'class' ClassDeclarationRest |
             ModifiersOpt 'interface' InterfaceDeclarationRest
 

Throws:
CompileException
IOException

parseModifiers

public Java.Modifiers parseModifiers()
                              throws CompileException,
                                     IOException
   ModifiersAndAnnotations := { 'public' | 'protected' | 'private' | 'static' | 'abstract' | 'final' | 'native'
           | 'synchronized' | 'transient' | 'volatile' | 'strictfp' | Annotation }
 

Throws:
CompileException
IOException

parseClassDeclarationRest

public Java.NamedClassDeclaration parseClassDeclarationRest(String optionalDocComment,
                                                            Java.Modifiers modifiers,
                                                            Parser.ClassDeclarationContext context)
                                                     throws CompileException,
                                                            IOException
   ClassDeclarationRest :=
        Identifier [ typeParameters ]
        [ 'extends' ReferenceType ]
        [ 'implements' ReferenceTypeList ]
        ClassBody
 

Throws:
CompileException
IOException

parseClassBody

public void parseClassBody(Java.ClassDeclaration classDeclaration)
                    throws CompileException,
                           IOException
   ClassBody := '{' { ClassBodyDeclaration } '}'
 

Throws:
CompileException
IOException

parseClassBodyDeclaration

public void parseClassBodyDeclaration(Java.ClassDeclaration 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 |
       Type Identifier (
         MethodDeclarationRest |
         FieldDeclarationRest ';'
       )
     )

 

Throws:
CompileException
IOException

parseInterfaceDeclarationRest

public Java.InterfaceDeclaration parseInterfaceDeclarationRest(String optionalDocComment,
                                                               Java.Modifiers modifiers,
                                                               Parser.InterfaceDeclarationContext context)
                                                        throws CompileException,
                                                               IOException
   InterfaceDeclarationRest :=
     Identifier [ typeParameters ]
     [ 'extends' ReferenceTypeList ]
     InterfaceBody
 

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(String optionalDocComment,
                                                             Java.Modifiers modifiers)
                                                      throws CompileException,
                                                             IOException
   ConstructorDeclarator :=
     Identifier
     FormalParameters
     [ 'throws' ReferenceTypeList ]
     '{'
       [ 'this' Arguments ';' | 'super' Arguments ';' | Primary '.' 'super' Arguments ';' ]
       BlockStatements
     '}'
 

Throws:
CompileException
IOException

parseMethodDeclarationRest

public Java.MethodDeclarator parseMethodDeclarationRest(String optionalDocComment,
                                                        Java.Modifiers modifiers,
                                                        Java.Type type,
                                                        String name)
                                                 throws CompileException,
                                                        IOException
   MethodDeclarationRest :=
     FormalParameters
     { '[' ']' }
     [ 'throws' ReferenceTypeList ]
     ( ';' | MethodBody )
 

Throws:
CompileException
IOException

parseVariableInitializer

public Java.ArrayInitializerOrRvalue parseVariableInitializer()
                                                       throws CompileException,
                                                              IOException
   VariableInitializer :=
     ArrayInitializer |
     Expression
 

Throws:
CompileException
IOException

parseArrayInitializer

public Java.ArrayInitializer parseArrayInitializer()
                                            throws CompileException,
                                                   IOException
   ArrayInitializer :=
     '{' [ VariableInitializer { ',' VariableInitializer } [ ',' ] '}'
 

Throws:
CompileException
IOException

parseFormalParameters

public Java.FunctionDeclarator.FormalParameters parseFormalParameters()
                                                               throws CompileException,
                                                                      IOException
   FormalParameters := '(' [ FormalParameter { ',' FormalParameter } ] ')'
 

Throws:
CompileException
IOException

parseFormalParameter

public Java.FunctionDeclarator.FormalParameter parseFormalParameter(boolean[] hasEllipsis)
                                                             throws CompileException,
                                                                    IOException
   FormalParameter := [ 'final' ] Type [ '.' '.' '.' ] Identifier BracketsOpt
 

Throws:
CompileException
IOException

parseMethodBody

public Java.Block parseMethodBody()
                           throws CompileException,
                                  IOException
   MethodBody := Block
 

Throws:
CompileException
IOException

parseBlock

public Java.Block parseBlock()
                      throws CompileException,
                             IOException
   '{' BlockStatements '}'
 

Throws:
CompileException
IOException

parseBlockStatements

public List<Java.BlockStatement> parseBlockStatements()
                                               throws CompileException,
                                                      IOException
   BlockStatements := { BlockStatement }
 

Throws:
CompileException
IOException

parseBlockStatement

public Java.BlockStatement parseBlockStatement()
                                        throws CompileException,
                                               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
IOException

parseVariableDeclarators

public Java.VariableDeclarator[] parseVariableDeclarators()
                                                   throws CompileException,
                                                          IOException
   VariableDeclarators := VariableDeclarator { ',' VariableDeclarator }
 

Throws:
CompileException
IOException

parseFieldDeclarationRest

public Java.VariableDeclarator[] parseFieldDeclarationRest(String name)
                                                    throws CompileException,
                                                           IOException
   FieldDeclarationRest :=
     VariableDeclaratorRest
     { ',' VariableDeclarator }
 

Throws:
CompileException
IOException

parseVariableDeclarator

public Java.VariableDeclarator parseVariableDeclarator()
                                                throws CompileException,
                                                       IOException
   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

public Java.Statement parseStatement()
                              throws CompileException,
                                     IOException
   Statement :=
     LabeledStatement |
     Block |
     IfStatement |
     ForStatement |
     WhileStatement |
     DoStatement |
     TryStatement |
     'switch' ... |
     'synchronized' ... |
     ReturnStatement |
     ThrowStatement |
     BreakStatement |
     ContinueStatement |
     EmptyStatement |
     ExpressionStatement
 

Throws:
CompileException
IOException

parseLabeledStatement

public Java.Statement parseLabeledStatement()
                                     throws CompileException,
                                            IOException
   LabeledStatement := Identifier ':' Statement
 

Throws:
CompileException
IOException

parseIfStatement

public Java.Statement parseIfStatement()
                                throws CompileException,
                                       IOException
   IfStatement := 'if' '(' Expression ')' Statement [ 'else' Statement ]
 

Throws:
CompileException
IOException

parseForStatement

public Java.Statement parseForStatement()
                                 throws CompileException,
                                        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
IOException

parseWhileStatement

public Java.Statement parseWhileStatement()
                                   throws CompileException,
                                          IOException
   WhileStatement := 'while' '(' Expression ')' Statement
 

Throws:
CompileException
IOException

parseDoStatement

public Java.Statement parseDoStatement()
                                throws CompileException,
                                       IOException
   DoStatement := 'do' Statement 'while' '(' Expression ')' ';'
 

Throws:
CompileException
IOException

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:
CompileException
IOException

parseSwitchStatement

public Java.Statement parseSwitchStatement()
                                    throws CompileException,
                                           IOException
   SwitchStatement :=
     'switch' '(' Expression ')' '{' { SwitchLabels BlockStatements } '}'

   SwitchLabels := SwitchLabels { SwitchLabels }

   SwitchLabel := 'case' Expression ':' | 'default' ':'
 

Throws:
CompileException
IOException

parseSynchronizedStatement

public Java.Statement parseSynchronizedStatement()
                                          throws CompileException,
                                                 IOException
   SynchronizedStatement :=
     'synchronized' '(' expression ')' Block
 

Throws:
CompileException
IOException

parseReturnStatement

public Java.Statement parseReturnStatement()
                                    throws CompileException,
                                           IOException
   ReturnStatement := 'return' [ Expression ] ';'
 

Throws:
CompileException
IOException

parseThrowStatement

public Java.Statement parseThrowStatement()
                                   throws CompileException,
                                          IOException
   ThrowStatement := 'throw' Expression ';'
 

Throws:
CompileException
IOException

parseBreakStatement

public Java.Statement parseBreakStatement()
                                   throws CompileException,
                                          IOException
   BreakStatement := 'break' [ Identifier ] ';'
 

Throws:
CompileException
IOException

parseContinueStatement

public Java.Statement parseContinueStatement()
                                      throws CompileException,
                                             IOException
   ContinueStatement := 'continue' [ Identifier ] ';'
 

Throws:
CompileException
IOException

parseAssertStatement

public Java.Statement parseAssertStatement()
                                    throws CompileException,
                                           IOException
   AssertStatement := 'assert' Expression [ ':' Expression ] ';'
 

Throws:
CompileException
IOException

parseEmptyStatement

public Java.Statement parseEmptyStatement()
                                   throws CompileException,
                                          IOException
   EmptyStatement := ';'
 

Throws:
CompileException
IOException

parseExpressionList

public Java.Rvalue[] parseExpressionList()
                                  throws CompileException,
                                         IOException
   ExpressionList := Expression { ',' Expression }
 

Throws:
CompileException
IOException

parseType

public Java.Type parseType()
                    throws CompileException,
                           IOException
   Type := (
     'byte' | 'short' | 'char' | 'int' | 'long' |
     'float' | 'double' | 'boolean' |
     ReferenceType
   ) { '[' ']' }
 

Throws:
CompileException
IOException

parseReferenceType

public Java.ReferenceType parseReferenceType()
                                      throws CompileException,
                                             IOException
   ReferenceType := QualifiedIdentifier [ TypeArguments ]
 

Throws:
CompileException
IOException

parseReferenceTypeList

public Java.ReferenceType[] parseReferenceTypeList()
                                            throws CompileException,
                                                   IOException
   ReferenceTypeList := ReferenceType { ',' ReferenceType }
 

Throws:
CompileException
IOException

parseExpression

public Java.Atom parseExpression()
                          throws CompileException,
                                 IOException
   Expression := AssignmentExpression
 

Throws:
CompileException
IOException

parseAssignmentExpression

public Java.Atom parseAssignmentExpression()
                                    throws CompileException,
                                           IOException
   AssignmentExpression :=
     ConditionalExpression [ AssignmentOperator AssignmentExpression ]

   AssignmentOperator :=
     '=' | '*=' | '/=' | '%=' | '+=' | '-=' | '<<=' |
     '>>=' | '>>>=' | '&=' | '^=' | '|='
 

Throws:
CompileException
IOException

parseConditionalExpression

public Java.Atom parseConditionalExpression()
                                     throws CompileException,
                                            IOException
   ConditionalExpression :=
     ConditionalOrExpression [ '?' Expression ':' ConditionalExpression ]
 

Throws:
CompileException
IOException

parseConditionalOrExpression

public Java.Atom parseConditionalOrExpression()
                                       throws CompileException,
                                              IOException
   ConditionalOrExpression :=
     ConditionalAndExpression { '||' ConditionalAndExpression ]
 

Throws:
CompileException
IOException

parseConditionalAndExpression

public Java.Atom parseConditionalAndExpression()
                                        throws CompileException,
                                               IOException
   ConditionalAndExpression :=
     InclusiveOrExpression { '&&' InclusiveOrExpression }
 

Throws:
CompileException
IOException

parseInclusiveOrExpression

public Java.Atom parseInclusiveOrExpression()
                                     throws CompileException,
                                            IOException
   InclusiveOrExpression :=
     ExclusiveOrExpression { '|' ExclusiveOrExpression }
 

Throws:
CompileException
IOException

parseExclusiveOrExpression

public Java.Atom parseExclusiveOrExpression()
                                     throws CompileException,
                                            IOException
   ExclusiveOrExpression :=
     AndExpression { '^' AndExpression }
 

Throws:
CompileException
IOException

parseAndExpression

public Java.Atom parseAndExpression()
                             throws CompileException,
                                    IOException
   AndExpression :=
     EqualityExpression { '&' EqualityExpression }
 

Throws:
CompileException
IOException

parseEqualityExpression

public Java.Atom parseEqualityExpression()
                                  throws CompileException,
                                         IOException
   EqualityExpression :=
     RelationalExpression { ( '==' | '!=' ) RelationalExpression }
 

Throws:
CompileException
IOException

parseRelationalExpression

public Java.Atom parseRelationalExpression()
                                    throws CompileException,
                                           IOException
   RelationalExpression :=
     ShiftExpression {
       'instanceof' ReferenceType
       | '<' ShiftExpression [ { ',' TypeArgument } '>' ]
       | '<' TypeArgument [ { ',' TypeArgument } '>' ]
       | ( '>' | '<=' | '>=' ) ShiftExpression
     }
 

Throws:
CompileException
IOException

parseShiftExpression

public Java.Atom parseShiftExpression()
                               throws CompileException,
                                      IOException
   ShiftExpression :=
     AdditiveExpression { ( '<<' | '>>' | '>>>' ) AdditiveExpression }
 

Throws:
CompileException
IOException

parseAdditiveExpression

public Java.Atom parseAdditiveExpression()
                                  throws CompileException,
                                         IOException
   AdditiveExpression :=
     MultiplicativeExpression { ( '+' | '-' ) MultiplicativeExpression }
 

Throws:
CompileException
IOException

parseMultiplicativeExpression

public Java.Atom parseMultiplicativeExpression()
                                        throws CompileException,
                                               IOException
   MultiplicativeExpression :=
     UnaryExpression { ( '*' | '/' | '%' ) UnaryExpression }
 

Throws:
CompileException
IOException

parseUnaryExpression

public Java.Atom parseUnaryExpression()
                               throws CompileException,
                                      IOException
   UnaryExpression :=
     { PrefixOperator } Primary { Selector } { PostfixOperator }

   PrefixOperator := '++' | '--' | '+' | '-' | '~' | '!'

   PostfixOperator := '++' | '--'
 

Throws:
CompileException
IOException

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
     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
IOException

parseSelector

public Java.Atom parseSelector(Java.Atom atom)
                        throws CompileException,
                               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
IOException

parseDimExprs

public Java.Rvalue[] parseDimExprs()
                            throws CompileException,
                                   IOException
   DimExprs := DimExpr { DimExpr }
 

Throws:
CompileException
IOException

parseDimExpr

public Java.Rvalue parseDimExpr()
                         throws CompileException,
                                IOException
   DimExpr := '[' Expression ']'
 

Throws:
CompileException
IOException

parseArguments

public Java.Rvalue[] parseArguments()
                             throws CompileException,
                                    IOException
   Arguments := '(' [ ArgumentList ] ')'
 

Throws:
CompileException
IOException

parseArgumentList

public Java.Rvalue[] parseArgumentList()
                                throws CompileException,
                                       IOException
   ArgumentList := Expression { ',' Expression }
 

Throws:
CompileException
IOException

parseLiteral

public Java.Rvalue parseLiteral()
                         throws CompileException,
                                IOException
   Literal :=
     IntegerLiteral
     | FloatingPointLiteral
     | BooleanLiteral
     | CharacterLiteral
     | StringLiteral
     | NullLiteral
 

Throws:
CompileException
IOException

parseExpressionStatement

public Java.Statement parseExpressionStatement()
                                        throws CompileException,
                                               IOException
   ExpressionStatement := Expression ';'
 

Throws:
CompileException
IOException

location

public Location location()
Returns:
The location of the first character of the previously peek()ed or read() token

peek

public Scanner.Token peek()
                   throws CompileException,
                          IOException
Returns:
The next token, but does not consume it
Throws:
CompileException
IOException

peekNextButOne

public Scanner.Token peekNextButOne()
                             throws CompileException,
                                    IOException
Returns:
The next-but-one token, but consumes neither the next nor the next-but-one token
Throws:
CompileException
IOException

read

public Scanner.Token read()
                   throws CompileException,
                          IOException
Returns:
The next and also consumes it, or null iff the scanner is at end-of-input
Throws:
CompileException
IOException

peek

public boolean peek(String suspected)
             throws CompileException,
                    IOException
Returns:
Whether the value of the next token equals suspected; does not consume the next token
Throws:
CompileException
IOException

peek

public int peek(String[] suspected)
         throws CompileException,
                IOException
Checks whether the value of the next token equals any of the suspected; 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 the suspected
Throws:
CompileException
IOException

peek

public int peek(int[] suspected)
         throws CompileException,
                IOException
Checks whether the type of the next token is any of the suspected; 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 the suspected types
Throws:
CompileException
IOException

peekNextButOne

public boolean peekNextButOne(String suspected)
                       throws CompileException,
                              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
IOException

read

public void read(String expected)
          throws CompileException,
                 IOException
Verifies that the value of the next token equals expected, and consumes the token.

Throws:
CompileException - The value of the next token does not equal expected (this includes the case that the scanner is at end-of-input)
IOException

read

public int read(String[] expected)
         throws CompileException,
                IOException
Verifies that the value of the next token equals one of the expected, 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 the expected (this includes the case where the scanner is at end-of-input)
IOException

peekRead

public boolean peekRead(String suspected)
                 throws CompileException,
                        IOException
Returns:
Whether the value of the next token equals the suspected; if so, it consumes the next token
Throws:
CompileException
IOException

peekRead

public int peekRead(String[] values)
             throws CompileException,
                    IOException
Returns:
-1 iff the next token is none of values
Throws:
CompileException
IOException

peekEof

public boolean peekEof()
                throws CompileException,
                       IOException
Returns:
Whether the scanner is at end-of-input
Throws:
CompileException
IOException

peekIdentifier

public String peekIdentifier()
                      throws CompileException,
                             IOException
Returns:
null iff the next token is not an identifier, otherwise the value of the identifier token
Throws:
CompileException
IOException

peekLiteral

public boolean peekLiteral()
                    throws CompileException,
                           IOException
Returns:
Whether the next token is a literal
Throws:
CompileException
IOException

readIdentifier

public String readIdentifier()
                      throws CompileException,
                             IOException
Returns:
The value of the next token, which is an indentifier
Throws:
CompileException - The next token is not an identifier
IOException

readOperator

public String readOperator()
                    throws CompileException,
                           IOException
Returns:
The value of the next token, which is an operator
Throws:
CompileException - The next token is not an operator
IOException

setWarningHandler

public void setWarningHandler(WarningHandler optionalWarningHandler)
By default, warnings are discarded, but an application my install a WarningHandler.

Notice that there is no Parser.setErrorHandler() method, but parse errors always throw a CompileException. The reason being is that there is no reasonable way to recover from parse errors and continue parsing, so there is no need to install a custom parse error handler.

Parameters:
optionalWarningHandler - null to indicate that no warnings be issued

compileException

protected final CompileException compileException(String message)
Convenience method for throwing a CompileException.


janino.net