Package antlr
Class ASTFactory
- java.lang.Object
-
- antlr.ASTFactory
-
public class ASTFactory extends java.lang.Object
AST Support code shared by TreeParser and Parser. We use delegation to share code (and have only one bit of code to maintain) rather than subclassing or superclassing (forces AST support code to be loaded even when you don't want to do AST stuff). Typically, setASTNodeType is used to specify the homogeneous type of node to create, but you can override create to make heterogeneous nodes etc...
-
-
Field Summary
Fields Modifier and Type Field Description protected java.lang.String
theASTNodeType
Name of AST class to create during tree construction.protected java.lang.Class
theASTNodeTypeClass
protected java.util.Hashtable
tokenTypeToASTClassMap
How to specify the classname to create for a particular token type.
-
Constructor Summary
Constructors Constructor Description ASTFactory()
ASTFactory(java.util.Hashtable tokenTypeToClassMap)
Create factory with a specific mapping from token type to Java AST node type.
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description void
addASTChild(ASTPair currentAST, AST child)
Add a child to the current ASTAST
create()
Create a new empty AST node; if the user did not specify an AST node type, then create a default one: CommonAST.AST
create(int type)
AST
create(int type, java.lang.String txt)
AST
create(int type, java.lang.String txt, java.lang.String className)
Create an AST node with the token type and text passed in, but with a specific Java object type.AST
create(AST tr)
Create a new empty AST node; if the user did not specify an AST node type, then create a default one: CommonAST.AST
create(Token tok)
AST
create(Token tok, java.lang.String className)
ANTLR generates reference to this when you reference a token that has a specified heterogeneous AST node type.protected AST
create(java.lang.Class c)
AST
create(java.lang.String className)
protected AST
createUsingCtor(Token token, java.lang.String className)
AST
dup(AST t)
Copy a single node with same Java AST objec type.AST
dupList(AST t)
Duplicate tree including siblings of root.AST
dupTree(AST t)
Duplicate a tree, assuming this is a root node of a tree-- duplicate that node and what's below; ignore siblings of root node.void
error(java.lang.String e)
To change where error messages go, can subclass/override this method and then setASTFactory in Parser and TreeParser.java.lang.Class
getASTNodeType(int tokenType)
For a given token type, what is the AST node object type to create for it?java.util.Hashtable
getTokenTypeToASTClassMap()
AST
make(AST[] nodes)
Make a tree from a list of nodes.AST
make(ASTArray nodes)
Make a tree from a list of nodes, where the nodes are contained in an ASTArray objectvoid
makeASTRoot(ASTPair currentAST, AST root)
Make an AST the root of current ASTvoid
setASTNodeClass(java.lang.Class c)
void
setASTNodeClass(java.lang.String t)
void
setASTNodeType(java.lang.String t)
Deprecated.since 2.7.1void
setTokenTypeASTNodeType(int tokenType, java.lang.String className)
Specify an "override" for the Java AST object created for a specific token.void
setTokenTypeToASTClassMap(java.util.Hashtable tokenTypeToClassMap)
-
-
-
Field Detail
-
theASTNodeType
protected java.lang.String theASTNodeType
Name of AST class to create during tree construction. Null implies that the create method should create a default AST type such as CommonAST. This is for homogeneous nodes.
-
theASTNodeTypeClass
protected java.lang.Class theASTNodeTypeClass
-
tokenTypeToASTClassMap
protected java.util.Hashtable tokenTypeToASTClassMap
How to specify the classname to create for a particular token type. Note that ANTLR allows you to say, for example, tokens { PLUS; ... } and it tracks everything statically. #[PLUS] will make you a PLUSNode w/o use of this table. For tokens that ANTLR cannot track statically like #[i], you can use this table to map PLUS (Integer) -> PLUSNode (Class) etc... ANTLR sets the class map from the tokens {...} section via the ASTFactory(Hashtable) ctor in antlr.Parser.
-
-
Method Detail
-
setTokenTypeASTNodeType
public void setTokenTypeASTNodeType(int tokenType, java.lang.String className) throws java.lang.IllegalArgumentException
Specify an "override" for the Java AST object created for a specific token. It is provided as a convenience so you can specify node types dynamically. ANTLR sets the token type mapping automatically from the tokens{...} section, but you can change that mapping with this method. ANTLR does it's best to statically determine the node type for generating parsers, but it cannot deal with dynamic values like #[LT(1)]. In this case, it relies on the mapping. Beware differences in the tokens{...} section and what you set via this method. Make sure they are the same. Set className to null to remove the mapping.- Throws:
java.lang.IllegalArgumentException
- Since:
- 2.7.2
-
getASTNodeType
public java.lang.Class getASTNodeType(int tokenType)
For a given token type, what is the AST node object type to create for it?- Since:
- 2.7.2
-
create
public AST create()
Create a new empty AST node; if the user did not specify an AST node type, then create a default one: CommonAST.
-
create
public AST create(int type)
-
create
public AST create(int type, java.lang.String txt)
-
create
public AST create(int type, java.lang.String txt, java.lang.String className)
Create an AST node with the token type and text passed in, but with a specific Java object type. Typically called when you say @[PLUS,"+",PLUSNode] in an antlr action.- Since:
- 2.7.2
-
create
public AST create(AST tr)
Create a new empty AST node; if the user did not specify an AST node type, then create a default one: CommonAST.
-
create
public AST create(Token tok, java.lang.String className)
ANTLR generates reference to this when you reference a token that has a specified heterogeneous AST node type. This is also a special case node creation routine for backward compatibility. Before, ANTLR generated "new T(tokenObject)" and so I must call the appropriate constructor not T().- Since:
- 2.7.2
-
create
public AST create(java.lang.String className)
- Since:
- 2.7.2
-
createUsingCtor
protected AST createUsingCtor(Token token, java.lang.String className)
- Since:
- 2.7.2
-
create
protected AST create(java.lang.Class c)
- Since:
- 2.7.2
-
dup
public AST dup(AST t)
Copy a single node with same Java AST objec type. Ignore the tokenType->Class mapping since you know the type of the node, t.getClass(), and doing a dup. clone() is not used because we want all AST creation to go thru the factory so creation can be tracked. Returns null if t is null.
-
dupTree
public AST dupTree(AST t)
Duplicate a tree, assuming this is a root node of a tree-- duplicate that node and what's below; ignore siblings of root node.
-
make
public AST make(AST[] nodes)
Make a tree from a list of nodes. The first element in the array is the root. If the root is null, then the tree is a simple list not a tree. Handles null children nodes correctly. For example, build(a, b, null, c) yields tree (a b c). build(null,a,b) yields tree (nil a b).
-
make
public AST make(ASTArray nodes)
Make a tree from a list of nodes, where the nodes are contained in an ASTArray object
-
makeASTRoot
public void makeASTRoot(ASTPair currentAST, AST root)
Make an AST the root of current AST
-
setASTNodeClass
public void setASTNodeClass(java.lang.Class c)
-
setASTNodeClass
public void setASTNodeClass(java.lang.String t)
-
setASTNodeType
public void setASTNodeType(java.lang.String t)
Deprecated.since 2.7.1Specify the type of node to create during tree building.
-
getTokenTypeToASTClassMap
public java.util.Hashtable getTokenTypeToASTClassMap()
-
setTokenTypeToASTClassMap
public void setTokenTypeToASTClassMap(java.util.Hashtable tokenTypeToClassMap)
-
error
public void error(java.lang.String e)
To change where error messages go, can subclass/override this method and then setASTFactory in Parser and TreeParser. This method removes a prior dependency on class antlr.Tool.
-
-