Class Parser
- java.lang.Object
-
- org.openjdk.asmtools.jasm.ParseBase
-
- org.openjdk.asmtools.jasm.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.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) static classParser.CompilerErrorThe main compile error for the parser(package private) static interfaceParser.Method(package private) static interfaceParser.NameSupplier
-
Field Summary
Fields Modifier and Type Field Description private ParserAnnotationannotParserother parser components(package private) ClassDatacdprivate java.util.ArrayList<AnnotationData>clsAnnttnsprivate java.util.ArrayList<ClassData>clsDataListprivate ParserCPcpParser(package private) CodeAttrcurCodeprivate CFVersioncurrentCFVprivate booleanexplicitcpprivate ParserInstrinstrParserprivate java.util.ArrayList<AnnotationData>memberAnnttnsprivate ModuleAttrmoduleAttributeprivate java.lang.Stringpkgprivate java.util.ArrayList<AnnotationData>pkgAnnttnsprivate java.lang.StringpkgPrefixprotected ConstantPoolpool
-
Constructor Summary
Constructors Modifier Constructor Description protectedParser(Environment sf, CFVersion cfVersion)Create a parser
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description private voidcheckReferenceIndex(int position, Tables.ConstType defaultTag, Tables.ConstType default2Tag)Check the pair reference_kind:reference_index where reference_kind is any from: REF_invokeVirtual, REF_newInvokeSpecial, REF_invokeStatic, REF_invokeSpecial, REF_invokeInterface and reference_index is one of [Empty], Method or InterfaceMethod There are possible entries: ldc Dynamic REF_newInvokeSpecial:InterfaceMethod LdcConDyTwice."": ldc Dynamic REF_invokeInterface:LdcConDyTwice." ": ldc Dynamic REF_newInvokeSpecial:Method LdcConDyTwice." ": ldc MethodHandle REF_newInvokeSpecial:InterfaceMethod LdcConDyTwice." ": ldc MethodHandle REF_invokeInterface:LdcConDyTwice." ": ldc MethodHandle REF_newInvokeSpecial:Method LdcConDyTwice." ": invokedynamic MethodHandle REF_invokeStatic:Method java/lang/invoke/StringConcatFactory.makeConcatWithConstants: invokedynamic MethodHandle REF_invokeStatic:java/lang/invoke/StringConcatFactory.makeConcatWithConstants .... private intcountParams(ConstantPool.ConstCell sigCell)Scan method's signature to determine size of parameters.(package private) java.lang.StringencodeClassString(java.lang.String classname)private voidendClass()End classprivate voidendModule()End module(package private) ClassData[]getClassesData()private voidmatch(JasmTokens.Token open, JasmTokens.Token close)The match() method is used to quickly match opening brackets (ie: '(', '{', or '[') with their closing counter part.private voidparseClass(int mod)Parse a class or interface declaration.private voidparseClasses(java.util.function.Consumer<java.util.ArrayList<ConstantPool.ConstCell>> classesConsumer)Parse a list of classes belonging to the [NestMembers | PermittedSubclasses] entryprivate voidparseClassMembers()(package private) ConstantPool.ConstCellparseClassName(boolean uncond)private voidparseConstDef()Parse constant declarationprivate voidparseCPXBootstrapMethod()Parse a (CPX based) BootstrapMethod entry.private voidparseField(int mod)Parse a field.(package private) voidparseFile()Parse an Jasm file.(package private) java.lang.StringparseIdent()Parse an internal name: identifier.private voidparseInnerClass(int mod)Parse an inner class.private voidparseInnerClass_s1(int mod, ConstantPool.ConstCell nameCell, ConstantPool.ConstCell innerClass, ConstantPool.ConstCell outerClass)private voidparseInnerClass_s2(int mod, ConstantPool.ConstCell nameCell, ConstantPool.ConstCell innerClass, ConstantPool.ConstCell outerClass)private voidparseInnerClass_s3(int mod, ConstantPool.ConstCell nameCell, ConstantPool.ConstCell innerClass, ConstantPool.ConstCell outerClass)(package private) ArgumentparseInt(int size)Parse a signed integer of size bytes long.private voidparseJasmPackages()Determines whether the JASM file is for a package-info class or for a module-info class.(package private) voidparseLocVarDef()Parse a local variable(package private) voidparseLocVarEnd()(package private) ArgumentparseLocVarRef()(package private) voidparseMapItem(DataVector map)private voidparseMethod(int mod)Parse a method.(package private) ConstantPool.ConstCellparseMethodHandle(Tables.SubTag subtag)Parses a field or method reference for method handle.private voidparseModule()Parse a module declaration.private java.lang.StringparseModuleName()Parses a module name in a module statement(s)(package private) ConstantPool.ConstCellparseName()Parse an external name: CPINDEX, string, or identifier.private voidparseNestHost()Parse a NestHost entryprivate voidparseRecord()Parse the Record entry(package private) Tables.SubTagparseSubtag()Parses a sub-tag value in method handle.private java.lang.StringparseTypeName()Parses a package or type name in a module statement(s)(package private) ArgumentparseUInt(int size)Parse an unsigned integer of size bytes long.private voidparseVersion()private voidparseVersionPkg()Parses version in package statementsprivate voidpic_error()private voidpic_tracecreate(int mod, ConstantPool.ConstCell nameCell, ConstantPool.ConstCell innerClass, ConstantPool.ConstCell outerClass)private java.lang.StringprependPackage(java.lang.String className, boolean uncond)private voidrecoverField()Recover after a syntax error in a field.private voidrecoverFile()Recover after a syntax error in the file.private java.util.HashSet<java.lang.String>scanList(Parser.Method scanMethod, Parser.NameSupplier target, java.lang.String err, boolean onlyOneElement)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;private intscanModifier(int mod)Parse the modifiers(package private) intscanModifiers()private voidscanRequires(java.util.function.BiConsumer<java.lang.String,java.lang.Integer> action)Scans ModuleStatement: requires [transitive] [static] ModuleName ;private voidscanStatement(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)Scans Module Statement(s): exports packageName [to ModuleName {, ModuleName}] ; opens packageName [to ModuleName {, ModuleName}] ; provides TypeName with TypeName [,typeName] ;private voidscanStatement(java.util.function.Consumer<java.util.Set<java.lang.String>> action, java.lang.String err)Scans ModuleStatement: uses TypeName;(package private) voidsetDebugFlags(boolean debugScanner, boolean debugMembers, boolean debugCP, boolean debugAnnot, boolean debugInstr)-
Methods inherited from class org.openjdk.asmtools.jasm.ParseBase
debugScan, debugStr, enableDebug, init
-
-
-
-
Field Detail
-
pool
protected ConstantPool pool
-
cd
ClassData cd
-
curCode
CodeAttr curCode
-
clsDataList
private java.util.ArrayList<ClassData> clsDataList
-
pkg
private java.lang.String pkg
-
pkgPrefix
private java.lang.String pkgPrefix
-
pkgAnnttns
private java.util.ArrayList<AnnotationData> pkgAnnttns
-
clsAnnttns
private java.util.ArrayList<AnnotationData> clsAnnttns
-
memberAnnttns
private java.util.ArrayList<AnnotationData> memberAnnttns
-
explicitcp
private boolean explicitcp
-
moduleAttribute
private ModuleAttr moduleAttribute
-
currentCFV
private CFVersion currentCFV
-
annotParser
private ParserAnnotation annotParser
other parser components
-
cpParser
private ParserCP cpParser
-
instrParser
private ParserInstr instrParser
-
-
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.IOExceptionParses version in package statements- Throws:
java.io.IOException
-
parseVersion
private void parseVersion() throws java.io.IOException- Throws:
java.io.IOException
-
parseIdent
java.lang.String parseIdent() throws Scanner.SyntaxError, java.io.IOExceptionParse an internal name: identifier.- Throws:
Scanner.SyntaxErrorjava.io.IOException
-
parseLocVarDef
void parseLocVarDef() throws Scanner.SyntaxError, java.io.IOExceptionParse a local variable- Throws:
Scanner.SyntaxErrorjava.io.IOException
-
parseLocVarRef
Argument parseLocVarRef() throws Scanner.SyntaxError, java.io.IOException
- Throws:
Scanner.SyntaxErrorjava.io.IOException
-
parseLocVarEnd
void parseLocVarEnd() throws Scanner.SyntaxError, java.io.IOException- Throws:
Scanner.SyntaxErrorjava.io.IOException
-
parseMapItem
void parseMapItem(DataVector map) throws Scanner.SyntaxError, java.io.IOException
- Throws:
Scanner.SyntaxErrorjava.io.IOException
-
parseName
ConstantPool.ConstCell parseName() throws Scanner.SyntaxError, java.io.IOException
Parse an external name: CPINDEX, string, or identifier.- Throws:
Scanner.SyntaxErrorjava.io.IOException
-
parseMethodHandle
ConstantPool.ConstCell parseMethodHandle(Tables.SubTag subtag) throws Scanner.SyntaxError, java.io.IOException
Parses a field or method reference for method handle.- Throws:
Scanner.SyntaxErrorjava.io.IOException
-
checkReferenceIndex
private void checkReferenceIndex(int position, Tables.ConstType defaultTag, Tables.ConstType default2Tag)Check the pair reference_kind:reference_index where reference_kind is any from: REF_invokeVirtual, REF_newInvokeSpecial, REF_invokeStatic, REF_invokeSpecial, REF_invokeInterface and reference_index is one of [Empty], Method or InterfaceMethod There are possible entries: ldc Dynamic REF_newInvokeSpecial:InterfaceMethod LdcConDyTwice."": ldc Dynamic REF_invokeInterface:LdcConDyTwice." ": ldc Dynamic REF_newInvokeSpecial:Method LdcConDyTwice." ": ldc MethodHandle REF_newInvokeSpecial:InterfaceMethod LdcConDyTwice." ": ldc MethodHandle REF_invokeInterface:LdcConDyTwice." ": ldc MethodHandle REF_newInvokeSpecial:Method LdcConDyTwice." ": invokedynamic MethodHandle REF_invokeStatic:Method java/lang/invoke/StringConcatFactory.makeConcatWithConstants: invokedynamic MethodHandle REF_invokeStatic:java/lang/invoke/StringConcatFactory.makeConcatWithConstants .... - Parameters:
position- the position in a source filedefaultTag- expected reference_index tag (Method or InterfaceMethod)defaultTag- 2nd expected reference_index tag (Method or InterfaceMethod)
-
parseSubtag
Tables.SubTag parseSubtag() throws Scanner.SyntaxError, java.io.IOException
Parses a sub-tag value in method handle.- Throws:
Scanner.SyntaxErrorjava.io.IOException
-
parseClassName
ConstantPool.ConstCell parseClassName(boolean uncond) throws Scanner.SyntaxError, java.io.IOException
- Throws:
Scanner.SyntaxErrorjava.io.IOException
-
prependPackage
private java.lang.String prependPackage(java.lang.String className, boolean uncond)
-
parseInt
Argument parseInt(int size) throws Scanner.SyntaxError, java.io.IOException
Parse a signed integer of size bytes long. size = 1 or 2- Throws:
Scanner.SyntaxErrorjava.io.IOException
-
parseUInt
Argument parseUInt(int size) throws Scanner.SyntaxError, java.io.IOException
Parse an unsigned integer of size bytes long. size = 1 or 2- Throws:
Scanner.SyntaxErrorjava.io.IOException
-
parseConstDef
private void parseConstDef() throws java.io.IOExceptionParse constant declaration- Throws:
java.io.IOException
-
scanModifier
private int scanModifier(int mod) throws java.io.IOExceptionParse the modifiers- Throws:
java.io.IOException
-
scanModifiers
int scanModifiers() throws java.io.IOException- Throws:
java.io.IOException
-
parseField
private void parseField(int mod) throws Scanner.SyntaxError, java.io.IOExceptionParse a field.- Throws:
Scanner.SyntaxErrorjava.io.IOException
-
countParams
private int countParams(ConstantPool.ConstCell sigCell) throws Scanner.SyntaxError
Scan method's signature to determine size of parameters.- Throws:
Scanner.SyntaxError
-
parseMethod
private void parseMethod(int mod) throws Scanner.SyntaxError, java.io.IOExceptionParse a method.- Throws:
Scanner.SyntaxErrorjava.io.IOException
-
parseCPXBootstrapMethod
private void parseCPXBootstrapMethod() throws Scanner.SyntaxError, java.io.IOExceptionParse a (CPX based) BootstrapMethod entry.- Throws:
Scanner.SyntaxErrorjava.io.IOException
-
parseNestHost
private void parseNestHost() throws Scanner.SyntaxError, java.io.IOExceptionParse a NestHost entry- Throws:
Scanner.SyntaxErrorjava.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.SyntaxErrorjava.io.IOException
-
parseRecord
private void parseRecord() throws Scanner.SyntaxError, java.io.IOExceptionParse the Record entry- Throws:
Scanner.SyntaxErrorjava.io.IOException
-
parseInnerClass
private void parseInnerClass(int mod) throws Scanner.SyntaxError, java.io.IOExceptionParse an inner class.- Throws:
Scanner.SyntaxErrorjava.io.IOException
-
parseInnerClass_s1
private void parseInnerClass_s1(int mod, ConstantPool.ConstCell nameCell, ConstantPool.ConstCell innerClass, ConstantPool.ConstCell outerClass) throws java.io.IOException- Throws:
java.io.IOException
-
parseInnerClass_s2
private void parseInnerClass_s2(int mod, ConstantPool.ConstCell nameCell, ConstantPool.ConstCell innerClass, ConstantPool.ConstCell outerClass) throws java.io.IOException- Throws:
java.io.IOException
-
parseInnerClass_s3
private void parseInnerClass_s3(int mod, ConstantPool.ConstCell nameCell, ConstantPool.ConstCell innerClass, ConstantPool.ConstCell outerClass) throws java.io.IOException- Throws:
java.io.IOException
-
pic_tracecreate
private void pic_tracecreate(int mod, ConstantPool.ConstCell nameCell, ConstantPool.ConstCell innerClass, ConstantPool.ConstCell outerClass)
-
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.IOExceptionRecover after a syntax error in a field. This involves discarding scanner.tokens until an EOF or a possible legal continuation is encountered.- Throws:
Scanner.SyntaxErrorjava.io.IOException
-
parseClass
private void parseClass(int mod) throws java.io.IOExceptionParse a class or interface declaration.- Throws:
java.io.IOException
-
parseTypeName
private java.lang.String parseTypeName() throws java.io.IOExceptionParses a package or type name in a module statement(s)- Throws:
java.io.IOException
-
parseModuleName
private java.lang.String parseModuleName() throws java.io.IOExceptionParses a module name in a module statement(s)- Throws:
java.io.IOException
-
parseModule
private void parseModule() throws java.io.IOExceptionParse 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.IOExceptionScans 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.IOExceptionScans 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.IOExceptionScans 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.IOExceptionRecover 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.IOExceptionDetermines 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.
-
-