Provides utility methods for AST traversal and manipulation.
appendSibling
public static void appendSibling(AST n,
AST s)
create
public static AST create(ASTFactory astFactory,
int type,
String text)
Creates a single node AST.
astFactory
- The factory.type
- The node type.text
- The node text.
- AST - A single node tree.
createBinarySubtree
public static AST createBinarySubtree(ASTFactory factory,
int parentType,
String parentText,
AST child1,
AST child2)
Creates a 'binary operator' subtree, given the information about the
parent and the two child nodex.
factory
- The AST factory.parentType
- The type of the parent node.parentText
- The text of the parent node.child1
- The first child.child2
- The second child.
- AST - A new sub-tree of the form "(parent child1 child2)"
createParent
public static AST createParent(ASTFactory factory,
int parentType,
String parentText,
AST child)
Creates a single parent of the specified child (i.e. a 'unary operator'
subtree).
factory
- The AST factory.parentType
- The type of the parent node.parentText
- The text of the parent node.child
- The child.
- AST - A new sub-tree of the form "(parent child)"
createSibling
public static AST createSibling(ASTFactory astFactory,
int type,
String text,
AST prevSibling)
Creates a single node AST as a sibling.
astFactory
- The factory.type
- The node type.text
- The node text.prevSibling
- The previous sibling.
- AST - A single node tree.
createTree
public static AST createTree(ASTFactory factory,
AST[] nestedChildren)
findPreviousSibling
public static AST findPreviousSibling(AST parent,
AST child)
Find the previous sibling in the parent for the given child.
parent
- the parent nodechild
- the child to find the previous sibling of
- the previous sibling of the child
findTypeInChildren
public static AST findTypeInChildren(AST parent,
int type)
Finds the first node of the specified type in the chain of children.
parent
- The parenttype
- The type to find.
- The first node of the specified type, or null if not found.
getDebugString
public static String getDebugString(AST n)
Returns the 'list' representation with some brackets around it for debugging.
- The list representation of the tree.
getLastChild
public static AST getLastChild(AST n)
Returns the last direct child of 'n'.
- The last direct child of 'n'.
getPathText
public static String getPathText(AST n)
hasExactlyOneChild
public static boolean hasExactlyOneChild(AST n)
insertChild
public static void insertChild(AST parent,
AST child)
Inserts the child as the first child of the parent, all other children are shifted over to the 'right'.
parent
- the parentchild
- the new first child
insertSibling
public static AST insertSibling(AST node,
AST prevSibling)
isDirectChild
public static boolean isDirectChild(AST fixture,
AST test)
Determine if a given node (test) is a direct (throtle to one level down)
child of another given node (fixture).
fixture
- The node against which to testto be checked for children.test
- The node to be tested as being a child of the parent.
- True if test is contained in the fixtures's direct children;
false otherwise.
isSubtreeChild
public static boolean isSubtreeChild(AST fixture,
AST test)
Determine if a given node (test) is contained anywhere in the subtree
of another given node (fixture).
fixture
- The node against which to testto be checked for children.test
- The node to be tested as being a subtree child of the parent.
- True if child is contained in the parent's collection of children.
makeSiblingOfParent
public static void makeSiblingOfParent(AST parent,
AST child)
Makes the child node a sibling of the parent, reconnecting all siblings.
parent
- the parentchild
- the child