Class Parser

  • All Implemented Interfaces:
    Constants, ParserActions, RuntimeConstants
    Direct Known Subclasses:
    BatchParser

    public class Parser
    extends Scanner
    implements ParserActions, Constants
    This class is used to parse Java statements and expressions. The result is a parse tree.

    This class implements an operator precedence parser. Errors are reported to the Environment object, if the error can't be resolved immediately, a SyntaxError exception is thrown.

    Error recovery is implemented by catching SyntaxError exceptions and discarding input tokens until an input token is reached that is possibly a legal continuation.

    The parse tree that is constructed represents the input exactly (no rewrites to simpler forms). This is important if the resulting tree is to be used for code formatting in a programming environment. Currently only documentation comments are retained.

    The parsing algorithm does NOT use any type information. Changes in the type system do not affect the structure of the parse tree. This restriction does introduce an ambiguity an expression of the form: (e1) e2 is assumed to be a cast if e2 does not start with an operator. That means that (a) - b is interpreted as subtract b from a and not cast negative b to type a. However, if a is a simple type (byte, int, ...) then it is assumed to be a cast.

    WARNING: The contents of this source file are not part of any supported API. Code that depends on them does so at its own risk: they are subject to change or removal without notice.

    • Field Detail

      • actions

        ParserActions actions
        Usually this.actions == (ParserActions)this. However, a delegate scanner can produce tokens for this parser, in which case (Scanner)this is unused, except for this.token and this.pos instance variables which are filled from the real scanner by this.scan() and the constructor.
      • args

        private Node[] args
      • argIndex

        protected int argIndex
      • aCount

        private int aCount
      • aTypes

        private Type[] aTypes
      • FPstate

        private int FPstate
      • scanner

        protected Scanner scanner
        Usually this.scanner == (Scanner)this. However, a delegate scanner can produce tokens for this parser, in which case (Scanner)this is unused, except for this.token and this.pos instance variables which are filled from the real scanner by this.scan() and the constructor.
    • Constructor Detail

      • Parser

        protected Parser​(Environment env,
                         java.io.InputStream in)
                  throws java.io.IOException
        Create a parser
        Throws:
        java.io.IOException
      • Parser

        protected Parser​(Scanner scanner)
                  throws java.io.IOException
        Create a parser, given a scanner.
        Throws:
        java.io.IOException
      • Parser

        public Parser​(Scanner scanner,
                      ParserActions actions)
               throws java.io.IOException
        Create a parser, given a scanner and the semantic callback.
        Throws:
        java.io.IOException
    • Method Detail

      • packageDeclaration

        @Deprecated
        protected void packageDeclaration​(long off,
                                          Identifier nm)
        Deprecated.
      • importClass

        @Deprecated
        protected void importClass​(long off,
                                   Identifier nm)
        Deprecated.
        Use the version with the IdentifierToken arguments.
      • importPackage

        @Deprecated
        protected void importPackage​(long off,
                                     Identifier nm)
        Deprecated.
        Use the version with the IdentifierToken arguments.
      • beginClass

        @Deprecated
        protected void beginClass​(long off,
                                  java.lang.String doc,
                                  int mod,
                                  Identifier nm,
                                  Identifier sup,
                                  Identifier[] impl)
        Deprecated.
        Use the version with the IdentifierToken arguments.
      • getCurrentClass

        protected ClassDefinition getCurrentClass()
        Report the current class under construction. By default, it's a no-op which returns null. It may only be called before the corresponding endClass().
      • endClass

        @Deprecated
        public void endClass​(long off,
                             ClassDefinition c)
        Deprecated.
        End class
        Specified by:
        endClass in interface ParserActions
        c - a cookie returned by the corresponding beginClass call
      • endClass

        @Deprecated
        protected void endClass​(long off,
                                Identifier nm)
        Deprecated.
        Use the version with the IdentifierToken arguments.
      • defineField

        @Deprecated
        protected void defineField​(long where,
                                   java.lang.String doc,
                                   int mod,
                                   Type t,
                                   Identifier nm,
                                   Identifier[] args,
                                   Identifier[] exp,
                                   Node val)
        Deprecated.
        Use the version with the IdentifierToken arguments.
      • addArgument

        protected final void addArgument​(Node n)
      • exprArgs

        protected final Expression[] exprArgs​(int index)
      • statArgs

        protected final Statement[] statArgs​(int index)
      • expect

        protected void expect​(int t)
                       throws SyntaxError,
                              java.io.IOException
        Expect a token, return its value, scan the next token or throw an exception.
        Throws:
        SyntaxError
        java.io.IOException
      • parseTypeExpression

        protected Expression parseTypeExpression()
                                          throws SyntaxError,
                                                 java.io.IOException
        Parse a type expression. Does not parse the []'s.
        Throws:
        SyntaxError
        java.io.IOException
      • parseMethodExpression

        protected Expression parseMethodExpression​(Expression e,
                                                   Identifier id)
                                            throws SyntaxError,
                                                   java.io.IOException
        Parse a method invocation. Should be called when the current then is the '(' of the argument list.
        Throws:
        SyntaxError
        java.io.IOException
      • parseNewInstanceExpression

        protected Expression parseNewInstanceExpression​(long p,
                                                        Expression outerArg,
                                                        Expression type)
                                                 throws SyntaxError,
                                                        java.io.IOException
        Parse a new instance expression. Should be called when the current token is the '(' of the argument list.
        Throws:
        SyntaxError
        java.io.IOException
      • parseBinaryExpression

        protected Expression parseBinaryExpression​(Expression e)
                                            throws SyntaxError,
                                                   java.io.IOException
        Given a left-hand term, parse an operator and right-hand term.
        Throws:
        SyntaxError
        java.io.IOException
      • recoverStatement

        protected boolean recoverStatement()
                                    throws SyntaxError,
                                           java.io.IOException
        Recover after a syntax error in a statement. This involves discarding tokens until EOF or a possible continuation is encountered.
        Throws:
        SyntaxError
        java.io.IOException
      • parseDeclaration

        protected Statement parseDeclaration​(long p,
                                             int mod,
                                             Expression type)
                                      throws SyntaxError,
                                             java.io.IOException
        Parse declaration, called after the type expression has been parsed and the current token is IDENT.
        Throws:
        SyntaxError
        java.io.IOException
      • topLevelExpression

        protected void topLevelExpression​(Expression e)
        Check if an expression is a legal toplevel expression. Only method, inc, dec, and new expression are allowed.
      • parseName

        protected IdentifierToken parseName​(boolean star)
                                     throws SyntaxError,
                                            java.io.IOException
        Parse an identifier. ie: a.b.c returns "a.b.c" If star is true then "a.b.*" is allowed. The return value encodes both the identifier and its location.
        Throws:
        SyntaxError
        java.io.IOException
      • parseType

        protected Type parseType()
                          throws SyntaxError,
                                 java.io.IOException
        Parse a type expression, this results in a Type. The parse includes trailing array brackets.
        Throws:
        SyntaxError
        java.io.IOException
      • parseArrayBrackets

        protected Type parseArrayBrackets​(Type t)
                                   throws SyntaxError,
                                          java.io.IOException
        Parse the tail of a type expression, which might be array brackets. Return the given type, as possibly modified by the suffix.
        Throws:
        SyntaxError
        java.io.IOException
      • parseModifiers

        protected int parseModifiers​(int mask)
                              throws java.io.IOException
        Parse a possibly-empty sequence of modifier keywords. Return the resulting bitmask. Diagnose repeated modifiers, but make no other checks. Only modifiers mentioned in the given bitmask are scanned; an unmatched modifier must be handled by the caller.
        Throws:
        java.io.IOException
      • parseField

        protected void parseField()
                           throws SyntaxError,
                                  java.io.IOException
        Parse a field.
        Throws:
        SyntaxError
        java.io.IOException
      • recoverField

        protected void recoverField​(ClassDefinition newClass)
                             throws SyntaxError,
                                    java.io.IOException
        Recover after a syntax error in a field. This involves discarding tokens until an EOF or a possible legal continuation is encountered.
        Throws:
        SyntaxError
        java.io.IOException
      • parseClass

        protected void parseClass()
                           throws SyntaxError,
                                  java.io.IOException
        Parse a top-level class or interface declaration.
        Throws:
        SyntaxError
        java.io.IOException
      • parseLocalClass

        protected Statement parseLocalClass​(int mod)
                                     throws SyntaxError,
                                            java.io.IOException
        Parse a block-local class or interface declaration.
        Throws:
        SyntaxError
        java.io.IOException
      • parseNamedClass

        protected ClassDefinition parseNamedClass​(int mod,
                                                  int ctx,
                                                  java.lang.String doc)
                                           throws SyntaxError,
                                                  java.io.IOException
        Parse a named class or interface declaration, starting at "class" or "interface".
        Throws:
        SyntaxError
        java.io.IOException
      • recoverFile

        protected void recoverFile()
                            throws java.io.IOException
        Recover after a syntax error in the file. This involves discarding tokens until an EOF or a possible legal continuation is encountered.
        Throws:
        java.io.IOException
      • parseFile

        public void parseFile()
        Parse an Java file.
      • scan

        public long scan()
                  throws java.io.IOException
        Description copied from class: Scanner
        Scan the next token.
        Overrides:
        scan in class Scanner
        Returns:
        the position of the previous token.
        Throws:
        java.io.IOException
      • match

        public void match​(int open,
                          int close)
                   throws java.io.IOException
        Description copied from class: Scanner
        Scan to a matching '}', ']' or ')'. The current token must be a '{', '[' or '(';
        Overrides:
        match in class Scanner
        Throws:
        java.io.IOException