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
29package org.antlr.runtime.tree;
30
31/** Do a depth first walk of a tree, applying pre() and post() actions
32 *  as we discover and finish nodes.
33 */
34public class TreeVisitor {
35    protected TreeAdaptor adaptor;
36
37    public TreeVisitor(TreeAdaptor adaptor) {
38        this.adaptor = adaptor;
39    }
40    public TreeVisitor() { this(new CommonTreeAdaptor()); }
41
42    /** Visit every node in tree t and trigger an action for each node
43     *  before/after having visited all of its children.
44     *  Execute both actions even if t has no children.
45     *  If a child visit yields a new child, it can update its
46     *  parent's child list or just return the new child.  The
47     *  child update code works even if the child visit alters its parent
48     *  and returns the new tree.
49     *
50     *  Return result of applying post action to this node.
51     */
52    public Object visit(Object t, TreeVisitorAction action) {
53        // System.out.println("visit "+((Tree)t).toStringTree());
54        boolean isNil = adaptor.isNil(t);
55        if ( action!=null && !isNil ) {
56            t = action.pre(t); // if rewritten, walk children of new t
57        }
58        for (int i=0; i<adaptor.getChildCount(t); i++) {
59            Object child = adaptor.getChild(t, i);
60            Object visitResult = visit(child, action);
61            Object childAfterVisit = adaptor.getChild(t, i);
62            if ( visitResult !=  childAfterVisit ) { // result & child differ?
63                adaptor.setChild(t, i, visitResult);
64            }
65        }
66        if ( action!=null && !isNil ) t = action.post(t);
67        return t;
68    }
69}
70