00001 /* 00002 [The "BSD license"] 00003 Copyright (c) 2005-2009 Terence Parr 00004 All rights reserved. 00005 00006 Redistribution and use in source and binary forms, with or without 00007 modification, are permitted provided that the following conditions 00008 are met: 00009 1. Redistributions of source code must retain the above copyright 00010 notice, this list of conditions and the following disclaimer. 00011 2. Redistributions in binary form must reproduce the above copyright 00012 notice, this list of conditions and the following disclaimer in the 00013 documentation and/or other materials provided with the distribution. 00014 3. The name of the author may not be used to endorse or promote products 00015 derived from this software without specific prior written permission. 00016 00017 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 00018 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 00019 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 00020 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 00021 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 00022 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 00023 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 00024 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00025 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 00026 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 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 00078 public class TreeFilter extends TreeParser { 00079 public interface fptr { 00080 public void rule() throws RecognitionException; 00081 } 00082 00083 protected TokenStream originalTokenStream; 00084 protected TreeAdaptor originalAdaptor; 00085 00086 public TreeFilter(TreeNodeStream input) { 00087 this(input, new RecognizerSharedState()); 00088 } 00089 public TreeFilter(TreeNodeStream input, RecognizerSharedState state) { 00090 super(input, state); 00091 originalAdaptor = input.getTreeAdaptor(); 00092 originalTokenStream = input.getTokenStream(); 00093 } 00094 00095 public void applyOnce(Object t, fptr whichRule) { 00096 if ( t==null ) return; 00097 try { 00098 // share TreeParser object but not parsing-related state 00099 state = new RecognizerSharedState(); 00100 input = new CommonTreeNodeStream(originalAdaptor, t); 00101 ((CommonTreeNodeStream)input).setTokenStream(originalTokenStream); 00102 setBacktrackingLevel(1); 00103 whichRule.rule(); 00104 setBacktrackingLevel(0); 00105 } 00106 catch (RecognitionException e) { ; } 00107 } 00108 00109 public void downup(Object t) { 00110 TreeVisitor v = new TreeVisitor(new CommonTreeAdaptor()); 00111 TreeVisitorAction actions = new TreeVisitorAction() { 00112 public Object pre(Object t) { applyOnce(t, topdown_fptr); return t; } 00113 public Object post(Object t) { applyOnce(t, bottomup_fptr); return t; } 00114 }; 00115 v.visit(t, actions); 00116 } 00117 00118 fptr topdown_fptr = new fptr() { 00119 public void rule() throws RecognitionException { 00120 topdown(); 00121 } 00122 }; 00123 00124 fptr bottomup_fptr = new fptr() { 00125 public void rule() throws RecognitionException { 00126 bottomup(); 00127 } 00128 }; 00129 00130 // methods the downup strategy uses to do the up and down rules. 00131 // to override, just define tree grammar rule topdown and turn on 00132 // filter=true. 00133 public void topdown() throws RecognitionException {;} 00134 public void bottomup() throws RecognitionException {;} 00135 }
1.5.5