13447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein/*
23447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein [The "BSD license"]
33447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein Copyright (c) 2005-2009 Terence Parr
43447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein All rights reserved.
53447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
63447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein Redistribution and use in source and binary forms, with or without
73447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein modification, are permitted provided that the following conditions
83447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein are met:
93447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 1. Redistributions of source code must retain the above copyright
103447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein     notice, this list of conditions and the following disclaimer.
113447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 2. Redistributions in binary form must reproduce the above copyright
123447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein     notice, this list of conditions and the following disclaimer in the
133447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein     documentation and/or other materials provided with the distribution.
143447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 3. The name of the author may not be used to endorse or promote products
153447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein     derived from this software without specific prior written permission.
163447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
173447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
183447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
193447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
203447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
213447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
223447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
233447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
243447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
253447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
263447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
273447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein */
283447a5916aa62f44de24cc441fc9987116ddff52Andrew Sappersteinpackage org.antlr.runtime.tree;
293447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
303447a5916aa62f44de24cc441fc9987116ddff52Andrew Sappersteinimport org.antlr.runtime.RecognizerSharedState;
313447a5916aa62f44de24cc441fc9987116ddff52Andrew Sappersteinimport org.antlr.runtime.RecognitionException;
323447a5916aa62f44de24cc441fc9987116ddff52Andrew Sappersteinimport org.antlr.runtime.TokenStream;
333447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
343447a5916aa62f44de24cc441fc9987116ddff52Andrew Sappersteinpublic class TreeRewriter extends TreeParser {
353447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein    public interface fptr {
363447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein        public Object rule() throws RecognitionException;
373447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein    }
383447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
393447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein    protected boolean showTransformations = false;
403447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
413447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein    protected TokenStream originalTokenStream;
423447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein    protected TreeAdaptor originalAdaptor;
433447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
443447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein    public TreeRewriter(TreeNodeStream input) {
453447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein        this(input, new RecognizerSharedState());
463447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein    }
473447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein    public TreeRewriter(TreeNodeStream input, RecognizerSharedState state) {
483447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein        super(input, state);
493447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein        originalAdaptor = input.getTreeAdaptor();
503447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein        originalTokenStream = input.getTokenStream();
513447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein    }
523447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
533447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein    public Object applyOnce(Object t, fptr whichRule) {
543447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein        if ( t==null ) return null;
553447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein        try {
563447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein            // share TreeParser object but not parsing-related state
573447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein            state = new RecognizerSharedState();
583447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein            input = new CommonTreeNodeStream(originalAdaptor, t);
593447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein            ((CommonTreeNodeStream)input).setTokenStream(originalTokenStream);
603447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein            setBacktrackingLevel(1);
613447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein            TreeRuleReturnScope r = (TreeRuleReturnScope)whichRule.rule();
623447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein            setBacktrackingLevel(0);
633447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein            if ( failed() ) return t;
643447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein            if ( showTransformations &&
653447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein                 r!=null && !t.equals(r.getTree()) && r.getTree()!=null )
663447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein            {
673447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein                reportTransformation(t, r.getTree());
683447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein            }
693447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein            if ( r!=null && r.getTree()!=null ) return r.getTree();
703447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein            else return t;
713447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein        }
723447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein        catch (RecognitionException e) { ; }
733447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein        return t;
743447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein    }
753447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
763447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein    public Object applyRepeatedly(Object t, fptr whichRule) {
773447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein        boolean treeChanged = true;
783447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein        while ( treeChanged ) {
793447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein            Object u = applyOnce(t, whichRule);
803447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein            treeChanged = !t.equals(u);
813447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein            t = u;
823447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein        }
833447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein        return t;
843447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein    }
853447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
863447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein    public Object downup(Object t) { return downup(t, false); }
873447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
883447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein    public Object downup(Object t, boolean showTransformations) {
893447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein        this.showTransformations = showTransformations;
903447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein        TreeVisitor v = new TreeVisitor(new CommonTreeAdaptor());
913447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein        TreeVisitorAction actions = new TreeVisitorAction() {
923447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein            public Object pre(Object t)  { return applyOnce(t, topdown_fptr); }
933447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein            public Object post(Object t) { return applyRepeatedly(t, bottomup_ftpr); }
943447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein        };
953447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein        t = v.visit(t, actions);
963447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein        return t;
973447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein    }
983447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
993447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein    /** Override this if you need transformation tracing to go somewhere
1003447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein     *  other than stdout or if you're not using Tree-derived trees.
1013447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein     */
1023447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein    public void reportTransformation(Object oldTree, Object newTree) {
1033447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein        System.out.println(((Tree)oldTree).toStringTree()+" -> "+
1043447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein                           ((Tree)newTree).toStringTree());
1053447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein    }
1063447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
1073447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein    fptr topdown_fptr = new fptr() {
1083447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein        public Object rule() throws RecognitionException { return topdown(); }
1093447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein    };
1103447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
1113447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein    fptr bottomup_ftpr = new fptr() {
1123447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein        public Object rule() throws RecognitionException { return bottomup(); }
1133447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein    };
1143447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
1153447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein    // methods the downup strategy uses to do the up and down rules.
1163447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein    // to override, just define tree grammar rule topdown and turn on
1173447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein    // filter=true.
1183447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein    public Object topdown() throws RecognitionException { return null; }
1193447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein    public Object bottomup() throws RecognitionException { return null; }
1203447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein}
121