1 /*
2  [The "BSD license"]
3  Copyright (c) 2005-2009 Terence Parr
4  All rights reserved.
5 
6  Redistribution and use in source and binary forms, with or without
7  modification, are permitted provided that the following conditions
8  are met:
9  1. Redistributions of source code must retain the above copyright
10      notice, this list of conditions and the following disclaimer.
11  2. Redistributions in binary form must reproduce the above copyright
12      notice, this list of conditions and the following disclaimer in the
13      documentation and/or other materials provided with the distribution.
14  3. The name of the author may not be used to endorse or promote products
15      derived from this software without specific prior written permission.
16 
17  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 package org.antlr.runtime.tree;
29 
30 import org.antlr.runtime.Token;
31 import org.antlr.runtime.misc.FastQueue;
32 
33 import java.util.Iterator;
34 
35 /** Return a node stream from a doubly-linked tree whose nodes
36  *  know what child index they are.  No remove() is supported.
37  *
38  *  Emit navigation nodes (DOWN, UP, and EOF) to let show tree structure.
39  */
40 public class TreeIterator implements Iterator<Object> {
41     protected TreeAdaptor adaptor;
42     protected Object root;
43     protected Object tree;
44     protected boolean firstTime = true;
45 
46     // navigation nodes to return during walk and at end
47     public Object up;
48     public Object down;
49     public Object eof;
50 
51     /** If we emit UP/DOWN nodes, we need to spit out multiple nodes per
52      *  next() call.
53      */
54     protected FastQueue<Object> nodes;
55 
TreeIterator(Object tree)56     public TreeIterator(Object tree) {
57         this(new CommonTreeAdaptor(),tree);
58     }
59 
TreeIterator(TreeAdaptor adaptor, Object tree)60     public TreeIterator(TreeAdaptor adaptor, Object tree) {
61         this.adaptor = adaptor;
62         this.tree = tree;
63         this.root = tree;
64         nodes = new FastQueue<Object>();
65         down = adaptor.create(Token.DOWN, "DOWN");
66         up = adaptor.create(Token.UP, "UP");
67         eof = adaptor.create(Token.EOF, "EOF");
68     }
69 
reset()70     public void reset() {
71         firstTime = true;
72         tree = root;
73         nodes.clear();
74     }
75 
76 	@Override
hasNext()77     public boolean hasNext() {
78         if ( firstTime ) return root!=null;
79         if ( nodes!=null && nodes.size()>0 ) return true;
80         if ( tree==null ) return false;
81         if ( adaptor.getChildCount(tree)>0 ) return true;
82         return adaptor.getParent(tree)!=null; // back at root?
83     }
84 
85 	@Override
next()86     public Object next() {
87         if ( firstTime ) { // initial condition
88             firstTime = false;
89             if ( adaptor.getChildCount(tree)==0 ) { // single node tree (special)
90                 nodes.add(eof);
91                 return tree;
92             }
93             return tree;
94         }
95         // if any queued up, use those first
96         if ( nodes!=null && nodes.size()>0 ) return nodes.remove();
97 
98         // no nodes left?
99         if ( tree==null ) return eof;
100 
101         // next node will be child 0 if any children
102         if ( adaptor.getChildCount(tree)>0 ) {
103             tree = adaptor.getChild(tree, 0);
104             nodes.add(tree); // real node is next after DOWN
105             return down;
106         }
107         // if no children, look for next sibling of tree or ancestor
108         Object parent = adaptor.getParent(tree);
109         // while we're out of siblings, keep popping back up towards root
110         while ( parent!=null &&
111                 adaptor.getChildIndex(tree)+1 >= adaptor.getChildCount(parent) )
112         {
113             nodes.add(up); // we're moving back up
114             tree = parent;
115             parent = adaptor.getParent(tree);
116         }
117         // no nodes left?
118         if ( parent==null ) {
119             tree = null; // back at root? nothing left then
120             nodes.add(eof); // add to queue, might have UP nodes in there
121             return nodes.remove();
122         }
123 
124         // must have found a node with an unvisited sibling
125         // move to it and return it
126         int nextSiblingIndex = adaptor.getChildIndex(tree) + 1;
127         tree = adaptor.getChild(parent, nextSiblingIndex);
128         nodes.add(tree); // add to queue, might have UP nodes in there
129         return nodes.remove();
130     }
131 
132 	@Override
remove()133     public void remove() { throw new UnsupportedOperationException(); }
134 }
135