~ubuntu-branches/ubuntu/wily/libhibernate3-java/wily-proposed

« back to all changes in this revision

Viewing changes to src/org/hibernate/hql/ast/util/ASTIterator.java

  • Committer: Bazaar Package Importer
  • Author(s): Torsten Werner
  • Date: 2007-10-14 14:43:34 UTC
  • Revision ID: james.westby@ubuntu.com-20071014144334-eamc8i0q10gs1aro
Tags: upstream-3.2.5
ImportĀ upstreamĀ versionĀ 3.2.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// $Id: ASTIterator.java 7460 2005-07-12 20:27:29Z steveebersole $
 
2
package org.hibernate.hql.ast.util;
 
3
 
 
4
import java.util.Iterator;
 
5
import java.util.LinkedList;
 
6
 
 
7
import antlr.collections.AST;
 
8
 
 
9
/**
 
10
 * Depth first iteration of an ANTLR AST.
 
11
 *
 
12
 * @author josh Sep 25, 2004 7:44:39 AM
 
13
 */
 
14
public class ASTIterator implements Iterator {
 
15
        private AST next, current;
 
16
        private LinkedList parents = new LinkedList();
 
17
 
 
18
        public void remove() {
 
19
                throw new UnsupportedOperationException( "remove() is not supported" );
 
20
        }
 
21
 
 
22
        public boolean hasNext() {
 
23
                return next != null;
 
24
        }
 
25
 
 
26
        public Object next() {
 
27
                return nextNode();
 
28
        }
 
29
 
 
30
        public ASTIterator(AST tree) {
 
31
                next = tree;
 
32
                down();
 
33
        }
 
34
 
 
35
        public AST nextNode() {
 
36
                current = next;
 
37
                if ( next != null ) {
 
38
                        AST nextSibling = next.getNextSibling();
 
39
                        if ( nextSibling == null ) {
 
40
                                next = pop();
 
41
                        }
 
42
                        else {
 
43
                                next = nextSibling;
 
44
                                down();
 
45
                        }
 
46
                }
 
47
                return current;
 
48
        }
 
49
 
 
50
        private void down() {
 
51
                while ( next != null && next.getFirstChild() != null ) {
 
52
                        push( next );
 
53
                        next = next.getFirstChild();
 
54
                }
 
55
        }
 
56
 
 
57
        private void push(AST parent) {
 
58
                parents.addFirst( parent );
 
59
        }
 
60
 
 
61
        private AST pop() {
 
62
                if ( parents.size() == 0 ) {
 
63
                        return null;
 
64
                }
 
65
                else {
 
66
                        return ( AST ) parents.removeFirst();
 
67
                }
 
68
        }
 
69
 
 
70
}