FileDocCategorySizeDatePackage
ASTUtil.javaAPI DocHibernate 3.2.510222Thu Jun 08 16:04:46 BST 2006org.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)

Fields Summary
Constructors Summary
private ASTUtil()
Private empty constructor. (or else checkstyle says: 'warning: Utility classes should not have a public or default constructor.')

deprecated
(tell clover to ignore this)

	
Methods Summary
public static voidappendSibling(antlr.collections.AST n, antlr.collections.AST s)

		while ( n.getNextSibling() != null ) {
			n = n.getNextSibling();
		}
		n.setNextSibling( s );
	
public static java.util.ListcollectChildren(antlr.collections.AST root, org.hibernate.hql.ast.util.ASTUtil$FilterPredicate predicate)

//		List children = new ArrayList();
//		collectChildren( children, root, predicate );
//		return children;
		return new CollectingNodeVisitor( predicate ).collect( root );
	
private static voidcollectChildren(java.util.List children, antlr.collections.AST root, org.hibernate.hql.ast.util.ASTUtil$FilterPredicate predicate)

		for ( AST n = root.getFirstChild(); n != null; n = n.getNextSibling() ) {
			if ( predicate == null || !predicate.exclude( n ) ) {
				children.add( n );
			}
			collectChildren( children, n, predicate );
		}
	
public static antlr.collections.ASTcreate(antlr.ASTFactory astFactory, int type, java.lang.String text)
Creates a single node AST.

param
astFactory The factory.
param
type The node type.
param
text The node text.
return
AST - A single node tree.

		AST node = astFactory.create( type, text );
		return node;
	
private static antlr.collections.impl.ASTArraycreateAstArray(antlr.ASTFactory factory, int size, int parentType, java.lang.String parentText, antlr.collections.AST child1)

		ASTArray array = new ASTArray( size );
		array.add( factory.create( parentType, parentText ) );
		array.add( child1 );
		return array;
	
public static antlr.collections.ASTcreateBinarySubtree(antlr.ASTFactory factory, int parentType, java.lang.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.

param
factory The AST factory.
param
parentType The type of the parent node.
param
parentText The text of the parent node.
param
child1 The first child.
param
child2 The second child.
return
AST - A new sub-tree of the form "(parent child1 child2)"

		ASTArray array = createAstArray( factory, 3, parentType, parentText, child1 );
		array.add( child2 );
		return factory.make( array );
	
public static antlr.collections.ASTcreateParent(antlr.ASTFactory factory, int parentType, java.lang.String parentText, antlr.collections.AST child)
Creates a single parent of the specified child (i.e. a 'unary operator' subtree).

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

		ASTArray array = createAstArray( factory, 2, parentType, parentText, child );
		return factory.make( array );
	
public static antlr.collections.ASTcreateSibling(antlr.ASTFactory astFactory, int type, java.lang.String text, antlr.collections.AST prevSibling)
Creates a single node AST as a sibling.

param
astFactory The factory.
param
type The node type.
param
text The node text.
param
prevSibling The previous sibling.
return
AST - A single node tree.

		AST node = astFactory.create( type, text );
		node.setNextSibling( prevSibling.getNextSibling() );
		prevSibling.setNextSibling( node );
		return node;
	
public static antlr.collections.ASTcreateTree(antlr.ASTFactory factory, antlr.collections.AST[] nestedChildren)

		AST[] array = new AST[2];
		int limit = nestedChildren.length - 1;
		for ( int i = limit; i >= 0; i-- ) {
			if ( i != limit ) {
				array[1] = nestedChildren[i + 1];
				array[0] = nestedChildren[i];
				factory.make( array );
			}
		}
		return array[0];
	
public static antlr.collections.ASTfindPreviousSibling(antlr.collections.AST parent, antlr.collections.AST child)
Find the previous sibling in the parent for the given child.

param
parent the parent node
param
child the child to find the previous sibling of
return
the previous sibling of the child

		AST prev = null;
		AST n = parent.getFirstChild();
		while ( n != null ) {
			if ( n == child ) {
				return prev;
			}
			prev = n;
			n = n.getNextSibling();
		}
		throw new IllegalArgumentException( "Child not found in parent!" );
	
public static antlr.collections.ASTfindTypeInChildren(antlr.collections.AST parent, int type)
Finds the first node of the specified type in the chain of children.

param
parent The parent
param
type The type to find.
return
The first node of the specified type, or null if not found.

		AST n = parent.getFirstChild();
		while ( n != null && n.getType() != type ) {
			n = n.getNextSibling();
		}
		return n;
	
public static java.lang.StringgetDebugString(antlr.collections.AST n)
Returns the 'list' representation with some brackets around it for debugging.

param
n The tree.
return
The list representation of the tree.

		StringBuffer buf = new StringBuffer();
		buf.append( "[ " );
		buf.append( ( n == null ) ? "{null}" : n.toStringTree() );
		buf.append( " ]" );
		return buf.toString();
	
public static antlr.collections.ASTgetLastChild(antlr.collections.AST n)
Returns the last direct child of 'n'.

param
n The parent
return
The last direct child of 'n'.

		return getLastSibling( n.getFirstChild() );
	
private static antlr.collections.ASTgetLastSibling(antlr.collections.AST a)
Returns the last sibling of 'a'.

param
a The sibling.
return
The last sibling of 'a'.

		AST last = null;
		while ( a != null ) {
			last = a;
			a = a.getNextSibling();
		}
		return last;
	
public static java.lang.StringgetPathText(antlr.collections.AST n)

		StringBuffer buf = new StringBuffer();
		getPathText( buf, n );
		return buf.toString();
	
private static voidgetPathText(java.lang.StringBuffer buf, antlr.collections.AST n)

		AST firstChild = n.getFirstChild();
		// If the node has a first child, recurse into the first child.
		if ( firstChild != null ) {
			getPathText( buf, firstChild );
		}
		// Append the text of the current node.
		buf.append( n.getText() );
		// If there is a second child (RHS), recurse into that child.
		if ( firstChild != null && firstChild.getNextSibling() != null ) {
			getPathText( buf, firstChild.getNextSibling() );
		}
	
public static booleanhasExactlyOneChild(antlr.collections.AST n)

		return n != null && n.getFirstChild() != null && n.getFirstChild().getNextSibling() == null;
	
public static voidinsertChild(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'.

param
parent the parent
param
child the new first child

		if ( parent.getFirstChild() == null ) {
			parent.setFirstChild( child );
		}
		else {
			AST n = parent.getFirstChild();
			parent.setFirstChild( child );
			child.setNextSibling( n );
		}
	
public static antlr.collections.ASTinsertSibling(antlr.collections.AST node, antlr.collections.AST prevSibling)

		node.setNextSibling( prevSibling.getNextSibling() );
		prevSibling.setNextSibling( node );
		return node;
	
public static booleanisDirectChild(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).

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

		AST n = fixture.getFirstChild();
		while ( n != null ) {
			if ( n == test ) {
				return true;
			}
			n = n.getNextSibling();
		}
		return false;
	
public static booleanisSubtreeChild(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).

param
fixture The node against which to testto be checked for children.
param
test The node to be tested as being a subtree child of the parent.
return
True if child is contained in the parent's collection of children.

		AST n = fixture.getFirstChild();
		while ( n != null ) {
			if ( n == test ) {
				return true;
			}
			if ( n.getFirstChild() != null && isSubtreeChild( n, test ) ) {
				return true;
			}
			n = n.getNextSibling();
		}
		return false;
	
public static voidmakeSiblingOfParent(antlr.collections.AST parent, antlr.collections.AST child)
Makes the child node a sibling of the parent, reconnecting all siblings.

param
parent the parent
param
child the child

		AST prev = findPreviousSibling( parent, child );
		if ( prev != null ) {
			prev.setNextSibling( child.getNextSibling() );
		}
		else { // child == parent.getFirstChild()
			parent.setFirstChild( child.getNextSibling() );
		}
		child.setNextSibling( parent.getNextSibling() );
		parent.setNextSibling( child );