Class Parser

    • Method Detail

      • doc

        @Nullable
        public java.lang.String doc()
        Gets the text of the doc comment (a.k.a. "JAVADOC comment") preceding the next token.
        Returns:
        null if the next token is not preceded by a doc comment
      • getScanner

        public Scanner getScanner()
        Returns:
        The scanner that produces the tokens for this parser.
      • parseQualifiedIdentifier

        public java.lang.String[] parseQualifiedIdentifier()
                                                    throws CompileException,
                                                           java.io.IOException
           QualifiedIdentifier := Identifier { '.' Identifier }
         
        Throws:
        CompileException
        java.io.IOException
      • parseOptionalModifier

        @Nullable
        public Java.Modifier parseOptionalModifier()
                                            throws CompileException,
                                                   java.io.IOException
           Modifier :=
               Annotation
               | 'public' | 'protected' | 'private'
               | 'static' | 'abstract' | 'final' | 'native' | 'synchronized' | 'transient' | 'volatile' | 'strictfp'
               | 'default'
         
        Throws:
        CompileException
        java.io.IOException
      • parseEnumBody

        public void parseEnumBody​(Java.EnumDeclaration enumDeclaration)
                           throws CompileException,
                                  java.io.IOException
           EnumBody := '{' [ EnumConstant { ',' EnumConstant } [ ',' ] [ ';' ] { ClassBodyDeclaration } '}'
         
        Throws:
        CompileException
        java.io.IOException
      • parseClassBodyDeclaration

        public void parseClassBodyDeclaration​(Java.AbstractClassDeclaration 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 |
               [ TypeArguments ] Type Identifier MethodDeclarationRest |
               Type Identifier FieldDeclarationRest ';'
             )
         
        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​(@Nullable
                                                                     java.lang.String docComment,
                                                                     Java.Modifier[] modifiers)
                                                              throws CompileException,
                                                                     java.io.IOException
           ConstructorDeclarator :=
             Identifier
             FormalParameters
             [ 'throws' ReferenceTypeList ]
             '{'
               [ 'this' Arguments ';' | 'super' Arguments ';' | Primary '.' 'super' Arguments ';' ]
               BlockStatements
             '}'
         
        Throws:
        CompileException
        java.io.IOException
      • parseMethodDeclaration

        public Java.MethodDeclarator parseMethodDeclaration​(boolean allowDefaultClause,
                                                            Parser.MethodDeclarationContext context)
                                                     throws CompileException,
                                                            java.io.IOException
           MethodDeclaration :=
             [ DocComment ] Modifiers [ TypeParameters ] VoidOrType Identifier MethodDeclarationRest
         
        Parameters:
        allowDefaultClause - Whether a "default clause" for an "annotation type element" (JLS8 9.6.2) should be parsed
        Throws:
        CompileException
        java.io.IOException
      • parseCatchParameter

        public Java.CatchParameter parseCatchParameter()
                                                throws CompileException,
                                                       java.io.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:
        CompileException
        java.io.IOException
      • parseBlockStatement

        public Java.BlockStatement parseBlockStatement()
                                                throws CompileException,
                                                       java.io.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:
        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
      • 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
      • 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 PrimitiveType VariableDeclarators
             | Expression VariableDeclarators              (1)
             | Expression { ',' Expression }
         

        (1) "Expression" must pose a type.

        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
      • 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
      • 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
             PrimitiveType { '[]' } |                // Type
             PrimitiveType { '[]' } '.' 'class' |    // ClassLiteral 15.8.2
             'void' '.' 'class' |                    // ClassLiteral 15.8.2
             MethodReference                         // MethodReference JLS9 15.13
        
           Name :=
             Identifier { '.' Identifier }
        
           CastExpression :=
             '(' PrimitiveType { '[]' } ')' UnaryExpression |
             '(' Expression ')' UnaryExpression
        
           NewClassInstance := 'new' ReferenceType Arguments
        
           NewAnonymousClassInstance := 'new' ReferenceType Arguments [ ClassBody ]
        
           NewArray := 'new' Type DimExprs { '[]' }
        
           NewInitializedArray := 'new' ArrayType ArrayInitializer
         
        Throws:
        CompileException
        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
             '.' '<' TypeList '>' 'super' Arguments                  // Superconstructor invocation (?)
             '.' '<' TypeList '>' 'super' '.' . Identifier           // ???
             '.' '<' TypeList '>' 'super' '.' . Identifier Arguments // Supermethod invocation
             '.' '<' TypeList '>' Identifier Arguments // ExplicitGenericInvocation
             '.' 'this'                                      // QualifiedThis 15.8.4
             '.' 'super' Arguments                           // Qualified superclass constructor invocation (JLS7 8.8.7.1)
             '.' 'super' '.' Identifier |                    // SuperclassFieldReference (JLS7 15.11.2)
             '.' 'super' '.' Identifier Arguments |          // SuperclassMethodInvocation (JLS7 15.12.3)
             '.' 'new' Identifier Arguments [ ClassBody ] |  // QualifiedClassInstanceCreationExpression  15.9
             '.' 'class'
             '[' Expression ']'                              // ArrayAccessExpression 15.13
        
           ExplicitGenericInvocationSuffix :=
             'super' SuperSuffix
             | Identifier Arguments
         
        Throws:
        CompileException
        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
      • location

        public Location location()
        Returns:
        The location of the first character of the previously read (not peeked!) token
      • peekNextButOne

        public boolean peekNextButOne​(java.lang.String suspected)
                               throws CompileException,
                                      java.io.IOException
        Throws:
        CompileException
        java.io.IOException
      • setSourceVersion

        public void setSourceVersion​(int version)
      • setWarningHandler

        public void setWarningHandler​(@Nullable
                                      WarningHandler warningHandler)
        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:
        warningHandler - null to indicate that no warnings be issued