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.RecognizerSharedState;
31import org.antlr.runtime.RecognitionException;
32import org.antlr.runtime.TokenStream;
33
34public class TreeRewriter extends TreeParser {
35    public interface fptr {
36        public Object rule() throws RecognitionException;
37    }
38
39    protected boolean showTransformations = false;
40
41    protected TokenStream originalTokenStream;
42    protected TreeAdaptor originalAdaptor;
43
44    public TreeRewriter(TreeNodeStream input) {
45        this(input, new RecognizerSharedState());
46    }
47    public TreeRewriter(TreeNodeStream input, RecognizerSharedState state) {
48        super(input, state);
49        originalAdaptor = input.getTreeAdaptor();
50        originalTokenStream = input.getTokenStream();
51    }
52
53    public Object applyOnce(Object t, fptr whichRule) {
54        if ( t==null ) return null;
55        try {
56            // share TreeParser object but not parsing-related state
57            state = new RecognizerSharedState();
58            input = new CommonTreeNodeStream(originalAdaptor, t);
59            ((CommonTreeNodeStream)input).setTokenStream(originalTokenStream);
60            setBacktrackingLevel(1);
61            TreeRuleReturnScope r = (TreeRuleReturnScope)whichRule.rule();
62            setBacktrackingLevel(0);
63            if ( failed() ) return t;
64            if ( showTransformations &&
65                 r!=null && !t.equals(r.getTree()) && r.getTree()!=null )
66            {
67                reportTransformation(t, r.getTree());
68            }
69            if ( r!=null && r.getTree()!=null ) return r.getTree();
70            else return t;
71        }
72        catch (RecognitionException e) { ; }
73        return t;
74    }
75
76    public Object applyRepeatedly(Object t, fptr whichRule) {
77        boolean treeChanged = true;
78        while ( treeChanged ) {
79            Object u = applyOnce(t, whichRule);
80            treeChanged = !t.equals(u);
81            t = u;
82        }
83        return t;
84    }
85
86    public Object downup(Object t) { return downup(t, false); }
87
88    public Object downup(Object t, boolean showTransformations) {
89        this.showTransformations = showTransformations;
90        TreeVisitor v = new TreeVisitor(new CommonTreeAdaptor());
91        TreeVisitorAction actions = new TreeVisitorAction() {
92            public Object pre(Object t)  { return applyOnce(t, topdown_fptr); }
93            public Object post(Object t) { return applyRepeatedly(t, bottomup_ftpr); }
94        };
95        t = v.visit(t, actions);
96        return t;
97    }
98
99    /** Override this if you need transformation tracing to go somewhere
100     *  other than stdout or if you're not using Tree-derived trees.
101     */
102    public void reportTransformation(Object oldTree, Object newTree) {
103        System.out.println(((Tree)oldTree).toStringTree()+" -> "+
104                           ((Tree)newTree).toStringTree());
105    }
106
107    fptr topdown_fptr = new fptr() {
108        public Object rule() throws RecognitionException { return topdown(); }
109    };
110
111    fptr bottomup_ftpr = new fptr() {
112        public Object rule() throws RecognitionException { return bottomup(); }
113    };
114
115    // methods the downup strategy uses to do the up and down rules.
116    // to override, just define tree grammar rule topdown and turn on
117    // filter=true.
118    public Object topdown() throws RecognitionException { return null; }
119    public Object bottomup() throws RecognitionException { return null; }
120}
121