org.hibernate.hql.ast.util
Class ASTUtil

java.lang.Object
  extended by org.hibernate.hql.ast.util.ASTUtil

public final class ASTUtil
extends Object

Provides utility methods for AST traversal and manipulation.

Author:
Joshua Davis (pgmjsd@sourceforge.net)

Nested Class Summary
static interface ASTUtil.FilterPredicate
          Filters nodes out of a tree.
static class ASTUtil.IncludePredicate
          A predicate that uses inclusion, rather than exclusion semantics.
 
Method Summary
static void appendSibling(antlr.collections.AST n, antlr.collections.AST s)
           
static List collectChildren(antlr.collections.AST root, ASTUtil.FilterPredicate predicate)
           
static antlr.collections.AST create(antlr.ASTFactory astFactory, int type, String text)
          Creates a single node AST.
static antlr.collections.AST createBinarySubtree(antlr.ASTFactory factory, int parentType, String parentText, antlr.collections.AST child1, antlr.collections.AST child2)
          Creates a 'binary operator' subtree, given the information about the parent and the two child nodex.
static antlr.collections.AST createParent(antlr.ASTFactory factory, int parentType, String parentText, antlr.collections.AST child)
          Creates a single parent of the specified child (i.e.
static antlr.collections.AST createSibling(antlr.ASTFactory astFactory, int type, String text, antlr.collections.AST prevSibling)
          Creates a single node AST as a sibling.
static antlr.collections.AST createTree(antlr.ASTFactory factory, antlr.collections.AST[] nestedChildren)
           
static antlr.collections.AST findPreviousSibling(antlr.collections.AST parent, antlr.collections.AST child)
          Find the previous sibling in the parent for the given child.
static antlr.collections.AST findTypeInChildren(antlr.collections.AST parent, int type)
          Finds the first node of the specified type in the chain of children.
static String getDebugString(antlr.collections.AST n)
          Returns the 'list' representation with some brackets around it for debugging.
static antlr.collections.AST getLastChild(antlr.collections.AST n)
          Returns the last direct child of 'n'.
static String getPathText(antlr.collections.AST n)
           
static boolean hasExactlyOneChild(antlr.collections.AST n)
           
static void insertChild(antlr.collections.AST parent, antlr.collections.AST child)
          Inserts the child as the first child of the parent, all other children are shifted over to the 'right'.
static antlr.collections.AST insertSibling(antlr.collections.AST node, antlr.collections.AST prevSibling)
           
static boolean isDirectChild(antlr.collections.AST fixture, antlr.collections.AST test)
          Determine if a given node (test) is a direct (throtle to one level down) child of another given node (fixture).
static boolean isSubtreeChild(antlr.collections.AST fixture, antlr.collections.AST test)
          Determine if a given node (test) is contained anywhere in the subtree of another given node (fixture).
static void makeSiblingOfParent(antlr.collections.AST parent, antlr.collections.AST child)
          Makes the child node a sibling of the parent, reconnecting all siblings.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

create

public static antlr.collections.AST create(antlr.ASTFactory astFactory,
                                           int type,
                                           String text)
Creates a single node AST.

Parameters:
astFactory - The factory.
type - The node type.
text - The node text.
Returns:
AST - A single node tree.

createSibling

public static antlr.collections.AST createSibling(antlr.ASTFactory astFactory,
                                                  int type,
                                                  String text,
                                                  antlr.collections.AST prevSibling)
Creates a single node AST as a sibling.

Parameters:
astFactory - The factory.
type - The node type.
text - The node text.
prevSibling - The previous sibling.
Returns:
AST - A single node tree.

insertSibling

public static antlr.collections.AST insertSibling(antlr.collections.AST node,
                                                  antlr.collections.AST prevSibling)

createBinarySubtree

public static antlr.collections.AST createBinarySubtree(antlr.ASTFactory factory,
                                                        int parentType,
                                                        String parentText,
                                                        antlr.collections.AST child1,
                                                        antlr.collections.AST child2)
Creates a 'binary operator' subtree, given the information about the parent and the two child nodex.

Parameters:
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.
Returns:
AST - A new sub-tree of the form "(parent child1 child2)"

createParent

public static antlr.collections.AST createParent(antlr.ASTFactory factory,
                                                 int parentType,
                                                 String parentText,
                                                 antlr.collections.AST child)
Creates a single parent of the specified child (i.e. a 'unary operator' subtree).

Parameters:
factory - The AST factory.
parentType - The type of the parent node.
parentText - The text of the parent node.
child - The child.
Returns:
AST - A new sub-tree of the form "(parent child)"

createTree

public static antlr.collections.AST createTree(antlr.ASTFactory factory,
                                               antlr.collections.AST[] nestedChildren)

findTypeInChildren

public static antlr.collections.AST findTypeInChildren(antlr.collections.AST parent,
                                                       int type)
Finds the first node of the specified type in the chain of children.

Parameters:
parent - The parent
type - The type to find.
Returns:
The first node of the specified type, or null if not found.

getLastChild

public static antlr.collections.AST getLastChild(antlr.collections.AST n)
Returns the last direct child of 'n'.

Parameters:
n - The parent
Returns:
The last direct child of 'n'.

getDebugString

public static String getDebugString(antlr.collections.AST n)
Returns the 'list' representation with some brackets around it for debugging.

Parameters:
n - The tree.
Returns:
The list representation of the tree.

findPreviousSibling

public static antlr.collections.AST findPreviousSibling(antlr.collections.AST parent,
                                                        antlr.collections.AST child)
Find the previous sibling in the parent for the given child.

Parameters:
parent - the parent node
child - the child to find the previous sibling of
Returns:
the previous sibling of the child

isDirectChild

public static boolean isDirectChild(antlr.collections.AST fixture,
                                    antlr.collections.AST test)
Determine if a given node (test) is a direct (throtle to one level down) child of another given node (fixture).

Parameters:
fixture - The node against which to testto be checked for children.
test - The node to be tested as being a child of the parent.
Returns:
True if test is contained in the fixtures's direct children; false otherwise.

isSubtreeChild

public static boolean isSubtreeChild(antlr.collections.AST fixture,
                                     antlr.collections.AST test)
Determine if a given node (test) is contained anywhere in the subtree of another given node (fixture).

Parameters:
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.
Returns:
True if child is contained in the parent's collection of children.

makeSiblingOfParent

public static void makeSiblingOfParent(antlr.collections.AST parent,
                                       antlr.collections.AST child)
Makes the child node a sibling of the parent, reconnecting all siblings.

Parameters:
parent - the parent
child - the child

getPathText

public static String getPathText(antlr.collections.AST n)

hasExactlyOneChild

public static boolean hasExactlyOneChild(antlr.collections.AST n)

appendSibling

public static void appendSibling(antlr.collections.AST n,
                                 antlr.collections.AST s)

insertChild

public static void insertChild(antlr.collections.AST parent,
                               antlr.collections.AST child)
Inserts the child as the first child of the parent, all other children are shifted over to the 'right'.

Parameters:
parent - the parent
child - the new first child

collectChildren

public static List collectChildren(antlr.collections.AST root,
                                   ASTUtil.FilterPredicate predicate)