it.twenfir.antlr.ast.AstNode Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of twenfir-antlr Show documentation
Show all versions of twenfir-antlr Show documentation
Add-ons for the ANTLR parser generator
package it.twenfir.antlr.ast;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
/**
* The base class of AST nodes.
* Provides access to children nodes and to source code location information.
* Position is expressed as source code line and character as well as start and end tokens.
*/
public abstract class AstNode {
private Location location;
private AstNode parent;
private List children = new ArrayList<>();
/**
* Constructor.
*
* @param location the node's position within the source file
*/
public AstNode(Location location) {
this.location = location;
}
/**
* Return the node's parent.
*
* @return the node's parent
*/
public AstNode getParent() {
return parent;
}
/**
* Set the node's parent.
*
* @param parent the node's parent
*/
void setParent(AstNode parent) {
this.parent = parent;
}
/**
* Return the line where the node's text is located in the source code.
*
* @return the line number
*/
public int getLine() {
return location.getLine();
}
/**
* Return the position in the line where the node's text is located in the source code.
*
* @return the position in the line
*/
public int getPos() {
return location.getPos();
}
/**
* Return the start token in the ANTLR token stream.
*
* @return the start token
*/
public int getStart() {
return location.getStart();
}
/**
* Return the end token in the ANTLR token stream.
*
* @return the end token
*/
public int getEnd() {
return location.getEnd();
}
/**
* Add a child node.
*
* @param child the child node
*/
public void addChild(AstNode child) {
if ( child != null ) {
children.add(child);
child.setParent(this);
}
}
/**
* Add a collection of children nodes.
*
* @param the children's type
* @param children the children nodes
*/
public void addChildren(Collection children) {
if ( children != null ) {
for ( N n : children ) {
children.add(n);
n.setParent(this);
}
}
}
/**
* Return all the node's children.
*
* @return an iterator over the node's children
*/
public Iterator getChildren() {
return children.iterator();
}
/**
* Return a single child node of the specified type, assumed to be the only such node.
*
* @param the type of the requested child
* @param clazz the class object of the requested child node
* @return the child node, or null
if none exists
*/
public ChildT getChild(Class clazz) {
ChildrenIterator iter = new ChildrenIterator(getChildren(), clazz);
return iter.hasNext() ? iter.next() : null;
}
/**
* Return all the children nodes of the specified type.
*
* @param the type of the requested children
* @param clazz the class object of the requested children
* @return an iterator over children of the specified type
*/
public Iterator getChildren(Class clazz) {
return new ChildrenIterator(getChildren(), clazz);
}
/**
* Return a single descendant node of the specified type, assumed to be the only such node.
*
* @param the type of the requested child
* @param clazz the class object of the requested child node
* @return the descendant node, or null
if none exists
*/
public ChildT getDescendant(Class clazz) {
ChildrenIterator iter = new ChildrenIterator(new WalkerIterator(this), clazz);
return iter.hasNext() ? iter.next() : null;
}
/**
* Return all the descendant nodes of the specified type.
*
* @param the type of the requested children
* @param clazz the class object of the requested children
* @return an iterator over descendants of the specified type
*/
public Iterator getDescendants(Class clazz) {
return new ChildrenIterator(new WalkerIterator(this), clazz);
}
/**
* Default implementation of accept
for AstVisitor
's.
*
* @param the type returned by the visitor
* @param visitor the visitor
* @return the result of the visit
*/
public ValueT accept(AstVisitor extends ValueT> visitor) {
return visitor.visit(this);
}
/**
* Return the class name of the node.
*
* @return the class name
*/
public String getType() {
return this.getClass().getSimpleName();
}
}