Methods Summary |
---|
public void | addASTChild(persistence.antlr.ASTPair currentAST, persistence.antlr.collections.AST child)Add a child to the current AST
if (child != null) {
if (currentAST.root == null) {
// Make new child the current root
currentAST.root = child;
}
else {
if (currentAST.child == null) {
// Add new child to current root
currentAST.root.setFirstChild(child);
}
else {
currentAST.child.setNextSibling(child);
}
}
// Make new child the current child
currentAST.child = child;
currentAST.advanceChildToEnd();
}
|
public persistence.antlr.collections.AST | create(persistence.antlr.collections.AST tr)Create a new empty AST node; if the user did not specify
an AST node type, then create a default one: CommonAST.
if (tr == null) return null; // create(null) == null
AST t = create(tr.getType());
if ( t!=null ) {
t.initialize(tr);
}
return t;
|
public persistence.antlr.collections.AST | create(persistence.antlr.Token tok)
AST t = create(tok.getType());
if ( t!=null ) {
t.initialize(tok);
}
return t;
|
public persistence.antlr.collections.AST | create(persistence.antlr.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().
AST t = createUsingCtor(tok,className);
return t;
|
public persistence.antlr.collections.AST | create(java.lang.String className)
Class c = null;
try {
c = Class.forName(className);
}
catch (Exception e) {
throw new IllegalArgumentException("Invalid class, "+className);
}
return create(c);
|
protected persistence.antlr.collections.AST | create(java.lang.Class c)
AST t = null;
try {
t = (AST)c.newInstance(); // make a new one
}
catch (Exception e) {
error("Can't create AST Node " + c.getName());
return null;
}
return t;
|
public persistence.antlr.collections.AST | create()Create a new empty AST node; if the user did not specify
an AST node type, then create a default one: CommonAST.
return create(Token.INVALID_TYPE);
|
public persistence.antlr.collections.AST | create(int type)
Class c = getASTNodeType(type);
AST t = create(c);
if ( t!=null ) {
t.initialize(type, "");
}
return t;
|
public persistence.antlr.collections.AST | create(int type, java.lang.String txt)
AST t = create(type);
if ( t!=null ) {
t.initialize(type, txt);
}
return t;
|
public persistence.antlr.collections.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.
AST t = create(className);
if ( t!=null ) {
t.initialize(type, txt);
}
return t;
|
protected persistence.antlr.collections.AST | createUsingCtor(persistence.antlr.Token token, java.lang.String className)
Class c = null;
AST t = null;
try {
c = Class.forName(className);
Class[] tokenArgType = new Class[] { persistence.antlr.Token.class };
try {
Constructor ctor = c.getConstructor(tokenArgType);
t = (AST)ctor.newInstance(new Object[]{token}); // make a new one
}
catch (NoSuchMethodException e){
// just do the regular thing if you can't find the ctor
// Your AST must have default ctor to use this.
t = create(c);
if ( t!=null ) {
t.initialize(token);
}
}
}
catch (Exception e) {
throw new IllegalArgumentException("Invalid class or can't make instance, "+className);
}
return t;
|
public persistence.antlr.collections.AST | dup(persistence.antlr.collections.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.
if ( t==null ) {
return null;
}
AST dup_t = create(t.getClass());
dup_t.initialize(t);
return dup_t;
|
public persistence.antlr.collections.AST | dupList(persistence.antlr.collections.AST t)Duplicate tree including siblings of root.
AST result = dupTree(t); // if t == null, then result==null
AST nt = result;
while (t != null) { // for each sibling of the root
t = t.getNextSibling();
nt.setNextSibling(dupTree(t)); // dup each subtree, building new tree
nt = nt.getNextSibling();
}
return result;
|
public persistence.antlr.collections.AST | dupTree(persistence.antlr.collections.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.
AST result = dup(t); // make copy of root
// copy all children of root.
if (t != null) {
result.setFirstChild(dupList(t.getFirstChild()));
}
return result;
|
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 persistence.antlr.Tool.
System.err.println(e);
|
public java.lang.Class | getASTNodeType(int tokenType)For a given token type, what is the AST node object type to create
for it?
// try node specific class
if ( tokenTypeToASTClassMap!=null ) {
Class c = (Class)tokenTypeToASTClassMap.get(new Integer(tokenType));
if ( c!=null ) {
return c;
}
}
// try a global specified class
if (theASTNodeTypeClass != null) {
return theASTNodeTypeClass;
}
// default to the common type
return CommonAST.class;
|
public java.util.Hashtable | getTokenTypeToASTClassMap()
return tokenTypeToASTClassMap;
|
public persistence.antlr.collections.AST | make(persistence.antlr.collections.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).
if (nodes == null || nodes.length == 0) return null;
AST root = nodes[0];
AST tail = null;
if (root != null) {
root.setFirstChild(null); // don't leave any old pointers set
}
// link in children;
for (int i = 1; i < nodes.length; i++) {
if (nodes[i] == null) continue; // ignore null nodes
if (root == null) {
// Set the root and set it up for a flat list
root = tail = nodes[i];
}
else if (tail == null) {
root.setFirstChild(nodes[i]);
tail = root.getFirstChild();
}
else {
tail.setNextSibling(nodes[i]);
tail = tail.getNextSibling();
}
// Chase tail to last sibling
while (tail.getNextSibling() != null) {
tail = tail.getNextSibling();
}
}
return root;
|
public persistence.antlr.collections.AST | make(persistence.antlr.collections.impl.ASTArray nodes)Make a tree from a list of nodes, where the nodes are contained
in an ASTArray object
return make(nodes.array);
|
public void | makeASTRoot(persistence.antlr.ASTPair currentAST, persistence.antlr.collections.AST root)Make an AST the root of current AST
if (root != null) {
// Add the current root as a child of new root
root.addChild(currentAST.root);
// The new current child is the last sibling of the old root
currentAST.child = currentAST.root;
currentAST.advanceChildToEnd();
// Set the new root
currentAST.root = root;
}
|
public void | setASTNodeClass(java.lang.String t)
theASTNodeType = t;
try {
theASTNodeTypeClass = Class.forName(t); // get class def
}
catch (Exception e) {
// either class not found,
// class is interface/abstract, or
// class or initializer is not accessible.
error("Can't find/access AST Node type" + t);
}
|
public void | setASTNodeType(java.lang.String t)Specify the type of node to create during tree building.
setASTNodeClass(t);
|
public void | setTokenTypeASTNodeType(int tokenType, java.lang.String className)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.
if ( tokenTypeToASTClassMap==null ) {
tokenTypeToASTClassMap = new Hashtable();
}
if ( className==null ) {
tokenTypeToASTClassMap.remove(new Integer(tokenType));
return;
}
Class c = null;
try {
c = Class.forName(className);
tokenTypeToASTClassMap.put(new Integer(tokenType), c);
}
catch (Exception e) {
throw new IllegalArgumentException("Invalid class, "+className);
}
|
public void | setTokenTypeToASTClassMap(java.util.Hashtable tokenTypeToClassMap)
this.tokenTypeToASTClassMap = tokenTypeToClassMap;
|