00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 package org.antlr.runtime.tree;
00029
00030 import org.antlr.runtime.RecognizerSharedState;
00031 import org.antlr.runtime.RecognitionException;
00032 import org.antlr.runtime.TokenStream;
00033
00034 public class TreeRewriter extends TreeParser {
00035 public interface fptr {
00036 public Object rule() throws RecognitionException;
00037 }
00038
00039 protected boolean showTransformations = false;
00040
00041 protected TokenStream originalTokenStream;
00042 protected TreeAdaptor originalAdaptor;
00043
00044 public TreeRewriter(TreeNodeStream input) {
00045 this(input, new RecognizerSharedState());
00046 }
00047 public TreeRewriter(TreeNodeStream input, RecognizerSharedState state) {
00048 super(input, state);
00049 originalAdaptor = input.getTreeAdaptor();
00050 originalTokenStream = input.getTokenStream();
00051 }
00052
00053 public Object applyOnce(Object t, fptr whichRule) {
00054 if ( t==null ) return null;
00055 try {
00056
00057 state = new RecognizerSharedState();
00058 input = new CommonTreeNodeStream(originalAdaptor, t);
00059 ((CommonTreeNodeStream)input).setTokenStream(originalTokenStream);
00060 setBacktrackingLevel(1);
00061 TreeRuleReturnScope r = (TreeRuleReturnScope)whichRule.rule();
00062 setBacktrackingLevel(0);
00063 if ( failed() ) return t;
00064 if ( showTransformations &&
00065 r!=null && !t.equals(r.getTree()) && r.getTree()!=null )
00066 {
00067 reportTransformation(t, r.getTree());
00068 }
00069 if ( r!=null && r.getTree()!=null ) return r.getTree();
00070 else return t;
00071 }
00072 catch (RecognitionException e) { ; }
00073 return t;
00074 }
00075
00076 public Object applyRepeatedly(Object t, fptr whichRule) {
00077 boolean treeChanged = true;
00078 while ( treeChanged ) {
00079 Object u = applyOnce(t, whichRule);
00080 treeChanged = !t.equals(u);
00081 t = u;
00082 }
00083 return t;
00084 }
00085
00086 public Object downup(Object t) { return downup(t, false); }
00087
00088 public Object downup(Object t, boolean showTransformations) {
00089 this.showTransformations = showTransformations;
00090 TreeVisitor v = new TreeVisitor(new CommonTreeAdaptor());
00091 TreeVisitorAction actions = new TreeVisitorAction() {
00092 public Object pre(Object t) { return applyOnce(t, topdown_fptr); }
00093 public Object post(Object t) { return applyRepeatedly(t, bottomup_ftpr); }
00094 };
00095 t = v.visit(t, actions);
00096 return t;
00097 }
00098
00102 public void reportTransformation(Object oldTree, Object newTree) {
00103 System.out.println(((Tree)oldTree).toStringTree()+" -> "+
00104 ((Tree)newTree).toStringTree());
00105 }
00106
00107 fptr topdown_fptr = new fptr() {
00108 public Object rule() throws RecognitionException { return topdown(); }
00109 };
00110
00111 fptr bottomup_ftpr = new fptr() {
00112 public Object rule() throws RecognitionException { return bottomup(); }
00113 };
00114
00115
00116
00117
00118 public Object topdown() throws RecognitionException { return null; }
00119 public Object bottomup() throws RecognitionException { return null; }
00120 }