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 */
28package org.antlr.runtime.tree;
29
30import org.antlr.runtime.Token;
31import org.antlr.runtime.CommonToken;
32import org.antlr.runtime.misc.FastQueue;
33
34import java.util.Iterator;
35
36/** Return a node stream from a doubly-linked tree whose nodes
37 *  know what child index they are.  No remove() is supported.
38 *
39 *  Emit navigation nodes (DOWN, UP, and EOF) to let show tree structure.
40 */
41public class TreeIterator implements Iterator {
42    protected TreeAdaptor adaptor;
43    protected Object root;
44    protected Object tree;
45    protected boolean firstTime = true;
46
47    // navigation nodes to return during walk and at end
48    public Object up;
49    public Object down;
50    public Object eof;
51
52    /** If we emit UP/DOWN nodes, we need to spit out multiple nodes per
53     *  next() call.
54     */
55    protected FastQueue nodes;
56
57    public TreeIterator(Object tree) {
58        this(new CommonTreeAdaptor(),tree);
59    }
60
61    public TreeIterator(TreeAdaptor adaptor, Object tree) {
62        this.adaptor = adaptor;
63        this.tree = tree;
64        this.root = tree;
65        nodes = new FastQueue();
66        down = adaptor.create(Token.DOWN, "DOWN");
67        up = adaptor.create(Token.UP, "UP");
68        eof = adaptor.create(Token.EOF, "EOF");
69    }
70
71    public void reset() {
72        firstTime = true;
73        tree = root;
74        nodes.clear();
75    }
76
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    public Object next() {
86        if ( firstTime ) { // initial condition
87            firstTime = false;
88            if ( adaptor.getChildCount(tree)==0 ) { // single node tree (special)
89                nodes.add(eof);
90                return tree;
91            }
92            return tree;
93        }
94        // if any queued up, use those first
95        if ( nodes!=null && nodes.size()>0 ) return nodes.remove();
96
97        // no nodes left?
98        if ( tree==null ) return eof;
99
100        // next node will be child 0 if any children
101        if ( adaptor.getChildCount(tree)>0 ) {
102            tree = adaptor.getChild(tree, 0);
103            nodes.add(tree); // real node is next after DOWN
104            return down;
105        }
106        // if no children, look for next sibling of tree or ancestor
107        Object parent = adaptor.getParent(tree);
108        // while we're out of siblings, keep popping back up towards root
109        while ( parent!=null &&
110                adaptor.getChildIndex(tree)+1 >= adaptor.getChildCount(parent) )
111        {
112            nodes.add(up); // we're moving back up
113            tree = parent;
114            parent = adaptor.getParent(tree);
115        }
116        // no nodes left?
117        if ( parent==null ) {
118            tree = null; // back at root? nothing left then
119            nodes.add(eof); // add to queue, might have UP nodes in there
120            return nodes.remove();
121        }
122
123        // must have found a node with an unvisited sibling
124        // move to it and return it
125        int nextSiblingIndex = adaptor.getChildIndex(tree) + 1;
126        tree = adaptor.getChild(parent, nextSiblingIndex);
127        nodes.add(tree); // add to queue, might have UP nodes in there
128        return nodes.remove();
129    }
130
131    public void remove() { throw new UnsupportedOperationException(); }
132}
133