[antlr-interest] How to change AST node order in Java ?
Gerald B. Rosenberg
gbr at newtechlaw.com
Tue Nov 13 00:22:28 PST 2007
It is just a list. Simple solution is to add whatever methods you need.
At 11:23 PM 11/12/2007, you wrote:
>Curtis Clauson wrote:
>>Jaak Niit wrote:
>>>Is there solution to change AST node order in Java ?
>>>
>>>If current node have children A, B, C, D and I want to change
>>>order to A, D, B, C.
>>>BaseTree has methods deleteChild(int i) and addChild(Tree t),
>>>but addChild(int offset, Tree t) is missing.
>>>
>>>I want to write something like
>>>BaseTree D = t.getChild(3);
>>>t.deleteChild(3);
>>>t.addChild(1,D);
>>>
>>>So problem is how to add node in middle of list.
>>
>>The default object used for an AntLr tree is CommonTree which
>>sub-classes BaseTree which implement the Tree interface. BaseTree
>>extends the Tree interface with, among others, the method
>> void setChild(int i, BaseTree t)
>I have extended CommonTree and use ASTLabelType already, but
>setChild replaces i-th element in list, but I need to add.
>When I look to void addChild(Tree t) source code then I need
>children.add(offset, t) instead of children.add(t).
>
>I thought that this is so common problem that it should have simple
>solution :-)
>>You can simply cast your tree object to BaseTree or CommonTree, or
>>specify the returned type in the grammar with the ASTLabelType option.
>>
>>I hope that helps.
>>-- Curtis
>
>
>--
># Jaak Niit
-------------- next part --------------
package net.certiv.antlrdt.core.parser;
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
import org.antlr.runtime.Token;
import org.antlr.runtime.tree.CommonTree;
/**
* Convenience class just to provide some of tree manipulation functionality otherwise
* missing from the Common Tree base class.
*
* @author Gerald B. Rosenberg
*/
public class AstTreeAdapter extends CommonTree {
public AstTreeAdapter(Token t) {
super(t);
}
/** Create a child list of the correct type */
@SuppressWarnings("unchecked")
@Override
protected List<AstNode> createChildrenList() {
children = new ArrayList<AstNode>();
return children;
}
/** Returns the specified child of this node */
public AstNode getChild(int i) {
return (AstNode) super.getChild(i);
}
/** Deletes the specified child, returning the deleted child */
public AstNode deleteChild(int idx) {
return (AstNode) super.deleteChild(idx);
}
/** Returns whether this node has children */
public boolean hasChildren() {
return getChildCount() > 0;
}
/** ArrayList of the children of this node */
@SuppressWarnings("unchecked")
public ArrayList<AstNode> getChildren() {
return (ArrayList<AstNode>) children;
}
/** First child of this node */
public AstNode getFirstChild() {
return getChild(0);
}
/** Last child of this node */
public AstNode getLastChild() {
return getChild(getChildCount() - 1);
}
/** Index of child of this node */
public int indexOfChild(AstNode node) {
if (getChildren() != null) {
return getChildren().indexOf(node);
}
return -1;
}
/** Insert at this index */
public void insertChild(int idx, AstNode child) {
if (getChildren() == null) createChildrenList();
getChildren().add(idx, child);
}
/** Remove the last child of this node */
public AstNode deleteLastChild() {
if (getChildCount() > 0) {
return getChildren().remove(getChildCount() - 1);
}
return null;
}
/** Return a list iterator for the children of this node */
public ListIterator<AstNode> listIterator() {
return getChildren().listIterator();
}
}
-------------- next part --------------
----
Gerald B. Rosenberg
Certiv Analytics
www.certiv.net
More information about the antlr-interest
mailing list