00001 /* 00002 [The "BSD license"] 00003 Copyright (c) 2005-2009 Terence Parr 00004 All rights reserved. 00005 00006 Redistribution and use in source and binary forms, with or without 00007 modification, are permitted provided that the following conditions 00008 are met: 00009 1. Redistributions of source code must retain the above copyright 00010 notice, this list of conditions and the following disclaimer. 00011 2. Redistributions in binary form must reproduce the above copyright 00012 notice, this list of conditions and the following disclaimer in the 00013 documentation and/or other materials provided with the distribution. 00014 3. The name of the author may not be used to endorse or promote products 00015 derived from this software without specific prior written permission. 00016 00017 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 00018 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 00019 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 00020 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 00021 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 00022 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 00023 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 00024 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00025 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 00026 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00027 */ 00028 package org.antlr.runtime.tree; 00029 00030 import org.antlr.runtime.Token; 00031 import org.antlr.runtime.CommonToken; 00032 import org.antlr.runtime.misc.FastQueue; 00033 00034 import java.util.Iterator; 00035 00041 public class TreeIterator implements Iterator { 00042 protected TreeAdaptor adaptor; 00043 protected Object root; 00044 protected Object tree; 00045 protected boolean firstTime = true; 00046 00047 // navigation nodes to return during walk and at end 00048 public Object up; 00049 public Object down; 00050 public Object eof; 00051 00055 protected FastQueue nodes; 00056 00057 public TreeIterator(Object tree) { 00058 this(new CommonTreeAdaptor(),tree); 00059 } 00060 00061 public TreeIterator(TreeAdaptor adaptor, Object tree) { 00062 this.adaptor = adaptor; 00063 this.tree = tree; 00064 this.root = tree; 00065 nodes = new FastQueue(); 00066 down = adaptor.create(Token.DOWN, "DOWN"); 00067 up = adaptor.create(Token.UP, "UP"); 00068 eof = adaptor.create(Token.EOF, "EOF"); 00069 } 00070 00071 public void reset() { 00072 firstTime = true; 00073 tree = root; 00074 nodes.clear(); 00075 } 00076 00077 public boolean hasNext() { 00078 if ( firstTime ) return root!=null; 00079 if ( nodes!=null && nodes.size()>0 ) return true; 00080 if ( tree==null ) return false; 00081 if ( adaptor.getChildCount(tree)>0 ) return true; 00082 return adaptor.getParent(tree)!=null; // back at root? 00083 } 00084 00085 public Object next() { 00086 if ( firstTime ) { // initial condition 00087 firstTime = false; 00088 if ( adaptor.getChildCount(tree)==0 ) { // single node tree (special) 00089 nodes.add(eof); 00090 return tree; 00091 } 00092 return tree; 00093 } 00094 // if any queued up, use those first 00095 if ( nodes!=null && nodes.size()>0 ) return nodes.remove(); 00096 00097 // no nodes left? 00098 if ( tree==null ) return eof; 00099 00100 // next node will be child 0 if any children 00101 if ( adaptor.getChildCount(tree)>0 ) { 00102 tree = adaptor.getChild(tree, 0); 00103 nodes.add(tree); // real node is next after DOWN 00104 return down; 00105 } 00106 // if no children, look for next sibling of tree or ancestor 00107 Object parent = adaptor.getParent(tree); 00108 // while we're out of siblings, keep popping back up towards root 00109 while ( parent!=null && 00110 adaptor.getChildIndex(tree)+1 >= adaptor.getChildCount(parent) ) 00111 { 00112 nodes.add(up); // we're moving back up 00113 tree = parent; 00114 parent = adaptor.getParent(tree); 00115 } 00116 // no nodes left? 00117 if ( parent==null ) { 00118 tree = null; // back at root? nothing left then 00119 nodes.add(eof); // add to queue, might have UP nodes in there 00120 return nodes.remove(); 00121 } 00122 00123 // must have found a node with an unvisited sibling 00124 // move to it and return it 00125 int nextSiblingIndex = adaptor.getChildIndex(tree) + 1; 00126 tree = adaptor.getChild(parent, nextSiblingIndex); 00127 nodes.add(tree); // add to queue, might have UP nodes in there 00128 return nodes.remove(); 00129 } 00130 00131 public void remove() { throw new UnsupportedOperationException(); } 00132 }
1.5.5