FileDocCategorySizeDatePackage
ASTIterator.javaAPI DocGlassfish v2 API2314Wed Aug 30 15:34:04 BST 2006persistence.antlr

ASTIterator

public class ASTIterator extends Object

Fields Summary
protected AST
cursor
protected AST
original
Constructors Summary
public ASTIterator(AST t)



       
        original = cursor = t;
    
Methods Summary
public booleanisSubtree(persistence.antlr.collections.AST t, persistence.antlr.collections.AST sub)
Is 'sub' a subtree of 't' beginning at the root?

        AST sibling;

        // the empty tree is always a subset of any tree.
        if (sub == null) {
            return true;
        }

        // if the tree is empty, return true if the subtree template is too.
        if (t == null) {
            if (sub != null) return false;
            return true;
        }

        // Otherwise, start walking sibling lists.  First mismatch, return false.
        for (sibling = t;
             sibling != null && sub != null;
             sibling = sibling.getNextSibling(), sub = sub.getNextSibling()) {
            // as a quick optimization, check roots first.
            if (sibling.getType() != sub.getType()) return false;
            // if roots match, do full match test on children.
            if (sibling.getFirstChild() != null) {
                if (!isSubtree(sibling.getFirstChild(), sub.getFirstChild())) return false;
            }
        }
        return true;
    
public persistence.antlr.collections.ASTnext(persistence.antlr.collections.AST template)
Find the next subtree with structure and token types equal to those of 'template'.

        AST t = null;
        AST sibling = null;

        if (cursor == null) {	// do nothing if no tree to work on
            return null;
        }

        // Start walking sibling list looking for subtree matches.
        for (; cursor != null; cursor = cursor.getNextSibling()) {
            // as a quick optimization, check roots first.
            if (cursor.getType() == template.getType()) {
                // if roots match, do full match test on children.
                if (cursor.getFirstChild() != null) {
                    if (isSubtree(cursor.getFirstChild(), template.getFirstChild())) {
                        return cursor;
                    }
                }
            }
        }
        return t;