Class Parser


  • class Parser
    extends ParseBase
    This class is used to parse Jasm 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 Scanner.SyntaxError exceptions and discarding input scanner.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.

    A parser owns several components (scanner, constant-parser, instruction-parser, annotations-parser) to which it delegates certain parsing responsibilities. This parser contains functions to parse the overall form of a class, and any members (fields, methods, inner-classes).

    Syntax errors, should always be caught inside the parser for error recovery.

    • Constructor Detail

      • Parser

        protected Parser​(Environment sf,
                         CFVersion cfVersion)
                  throws java.io.IOException
        Create a parser
        Throws:
        java.io.IOException
    • Method Detail

      • setDebugFlags

        void setDebugFlags​(boolean debugScanner,
                           boolean debugMembers,
                           boolean debugCP,
                           boolean debugAnnot,
                           boolean debugInstr)
      • encodeClassString

        java.lang.String encodeClassString​(java.lang.String classname)
      • parseVersionPkg

        private void parseVersionPkg()
                              throws java.io.IOException
        Parses version in package statements
        Throws:
        java.io.IOException
      • parseVersion

        private void parseVersion()
                           throws java.io.IOException
        Throws:
        java.io.IOException
      • prependPackage

        private java.lang.String prependPackage​(java.lang.String className,
                                                boolean uncond)
      • parseConstDef

        private void parseConstDef()
                            throws java.io.IOException
        Parse constant declaration
        Throws:
        java.io.IOException
      • scanModifier

        private int scanModifier​(int mod)
                          throws java.io.IOException
        Parse the modifiers
        Throws:
        java.io.IOException
      • scanModifiers

        int scanModifiers()
                   throws java.io.IOException
        Throws:
        java.io.IOException
      • parseCPXBootstrapMethod

        private void parseCPXBootstrapMethod()
                                      throws Scanner.SyntaxError,
                                             java.io.IOException
        Parse a (CPX based) BootstrapMethod entry.
        Throws:
        Scanner.SyntaxError
        java.io.IOException
      • parseClasses

        private void parseClasses​(java.util.function.Consumer<java.util.ArrayList<ConstantPool.ConstCell>> classesConsumer)
                           throws Scanner.SyntaxError,
                                  java.io.IOException
        Parse a list of classes belonging to the [NestMembers | PermittedSubclasses] entry
        Throws:
        Scanner.SyntaxError
        java.io.IOException
      • pic_error

        private void pic_error()
      • match

        private void match​(JasmTokens.Token open,
                           JasmTokens.Token close)
                    throws java.io.IOException
        The match() method is used to quickly match opening brackets (ie: '(', '{', or '[') with their closing counter part. This is useful during error recovery.

        Scan to a matching '}', ']' or ')'. The current scanner.token must be a '{', '[' or '(';

        Throws:
        java.io.IOException
      • recoverField

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

        private void parseClass​(int mod)
                         throws java.io.IOException
        Parse a class or interface declaration.
        Throws:
        java.io.IOException
      • parseTypeName

        private java.lang.String parseTypeName()
                                        throws java.io.IOException
        Parses a package or type name in a module statement(s)
        Throws:
        java.io.IOException
      • parseModuleName

        private java.lang.String parseModuleName()
                                          throws java.io.IOException
        Parses a module name in a module statement(s)
        Throws:
        java.io.IOException
      • parseModule

        private void parseModule()
                          throws java.io.IOException
        Parse a module declaration.
        Throws:
        java.io.IOException
      • scanRequires

        private void scanRequires​(java.util.function.BiConsumer<java.lang.String,​java.lang.Integer> action)
                           throws java.io.IOException
        Scans ModuleStatement: requires [transitive] [static] ModuleName ;
        Throws:
        java.io.IOException
      • scanStatement

        private void scanStatement​(java.util.function.Consumer<java.util.Set<java.lang.String>> action,
                                   java.lang.String err)
                            throws java.io.IOException
        Scans ModuleStatement: uses TypeName;
        Throws:
        java.io.IOException
      • scanStatement

        private void scanStatement​(java.util.function.BiConsumer<java.lang.String,​java.util.Set<java.lang.String>> action,
                                   Parser.NameSupplier source,
                                   Parser.NameSupplier target,
                                   JasmTokens.Token startList,
                                   boolean emptyListAllowed,
                                   java.lang.String err)
                            throws java.io.IOException
        Scans Module Statement(s): exports packageName [to ModuleName {, ModuleName}] ; opens packageName [to ModuleName {, ModuleName}] ; provides TypeName with TypeName [,typeName] ;
        Throws:
        java.io.IOException
      • scanList

        private java.util.HashSet<java.lang.String> scanList​(Parser.Method scanMethod,
                                                             Parser.NameSupplier target,
                                                             java.lang.String err,
                                                             boolean onlyOneElement)
                                                      throws java.io.IOException
        Scans the "to" or "with" part of ModuleStatement: exports PackageName [to ModuleName {, ModuleName}] ;, opens packageName [to ModuleName {, ModuleName}] ; provides TypeName with TypeName [,typeName] ; uses TypeName; : [ModuleName {, ModuleName}]; , [TypeName [,typeName]]; or TypeName;
        Throws:
        java.io.IOException
      • parseClassMembers

        private void parseClassMembers()
                                throws java.io.IOException
        Throws:
        java.io.IOException
      • recoverFile

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

        private void endClass()
        End class
      • endModule

        private void endModule()
        End module
      • getClassesData

        final ClassData[] getClassesData()
      • parseJasmPackages

        private void parseJasmPackages()
                                throws java.io.IOException
        Determines whether the JASM file is for a package-info class or for a module-info class.

        creates the correct kind of ClassData accordingly.

        Throws:
        java.io.IOException
      • parseFile

        void parseFile()
        Parse an Jasm file.