Home | History | Annotate | Download | only in tree
      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 package org.antlr.runtime.tree;
     29 
     30 import org.antlr.runtime.RecognizerSharedState;
     31 import org.antlr.runtime.RecognitionException;
     32 import org.antlr.runtime.TokenStream;
     33 
     34 /**
     35  Cut-n-paste from material I'm not using in the book anymore (edit later
     36  to make sense):
     37 
     38  Now, how are we going to test these tree patterns against every
     39 subtree in our original tree?  In what order should we visit nodes?
     40 For this application, it turns out we need a simple ``apply once''
     41 rule application strategy and a ``down then up'' tree traversal
     42 strategy.  Let's look at rule application first.
     43 
     44 As we visit each node, we need to see if any of our patterns match. If
     45 a pattern matches, we execute the associated tree rewrite and move on
     46 to the next node. In other words, we only look for a single rule
     47 application opportunity (we'll see below that we sometimes need to
     48 repeatedly apply rules). The following method applies a rule in a @cl
     49 TreeParser (derived from a tree grammar) to a tree:
     50 
     51 here is where weReferenced code/walking/patterns/TreePatternMatcher.java
     52 
     53 It uses reflection to lookup the appropriate rule within the generated
     54 tree parser class (@cl Simplify in this case). Most of the time, the
     55 rule will not match the tree.  To avoid issuing syntax errors and
     56 attempting error recovery, it bumps up the backtracking level.  Upon
     57 failure, the invoked rule immediately returns. If you don't plan on
     58 using this technique in your own ANTLR-based application, don't sweat
     59 the details. This method boils down to ``call a rule to match a tree,
     60 executing any embedded actions and rewrite rules.''
     61 
     62 At this point, we know how to define tree grammar rules and how to
     63 apply them to a particular subtree. The final piece of the tree
     64 pattern matcher is the actual tree traversal. We have to get the
     65 correct node visitation order.  In particular, we need to perform the
     66 scalar-vector multiply transformation on the way down (preorder) and
     67 we need to reduce multiply-by-zero subtrees on the way up (postorder).
     68 
     69 To implement a top-down visitor, we do a depth first walk of the tree,
     70 executing an action in the preorder position. To get a bottom-up
     71 visitor, we execute an action in the postorder position.  ANTLR
     72 provides a standard @cl TreeVisitor class with a depth first search @v
     73 visit method. That method executes either a @m pre or @m post method
     74 or both. In our case, we need to call @m applyOnce in both. On the way
     75 down, we'll look for @r vmult patterns. On the way up,
     76 we'll look for @r mult0 patterns.
     77  */
     78 public class TreeFilter extends TreeParser {
     79     public interface fptr {
     80         public void rule() throws RecognitionException;
     81     }
     82 
     83     protected TokenStream originalTokenStream;
     84     protected TreeAdaptor originalAdaptor;
     85 
     86     public TreeFilter(TreeNodeStream input) {
     87         this(input, new RecognizerSharedState());
     88     }
     89     public TreeFilter(TreeNodeStream input, RecognizerSharedState state) {
     90         super(input, state);
     91         originalAdaptor = input.getTreeAdaptor();
     92         originalTokenStream = input.getTokenStream();
     93     }
     94 
     95     public void applyOnce(Object t, fptr whichRule) {
     96         if ( t==null ) return;
     97         try {
     98             // share TreeParser object but not parsing-related state
     99             state = new RecognizerSharedState();
    100             input = new CommonTreeNodeStream(originalAdaptor, t);
    101             ((CommonTreeNodeStream)input).setTokenStream(originalTokenStream);
    102             setBacktrackingLevel(1);
    103             whichRule.rule();
    104             setBacktrackingLevel(0);
    105         }
    106         catch (RecognitionException e) { ; }
    107     }
    108 
    109     public void downup(Object t) {
    110         TreeVisitor v = new TreeVisitor(new CommonTreeAdaptor());
    111         TreeVisitorAction actions = new TreeVisitorAction() {
    112             public Object pre(Object t)  { applyOnce(t, topdown_fptr); return t; }
    113             public Object post(Object t) { applyOnce(t, bottomup_fptr); return t; }
    114         };
    115         v.visit(t, actions);
    116     }
    117 
    118     fptr topdown_fptr = new fptr() {
    119         public void rule() throws RecognitionException {
    120             topdown();
    121         }
    122     };
    123 
    124     fptr bottomup_fptr = new fptr() {
    125         public void rule() throws RecognitionException {
    126             bottomup();
    127         }
    128     };
    129 
    130     // methods the downup strategy uses to do the up and down rules.
    131     // to override, just define tree grammar rule topdown and turn on
    132     // filter=true.
    133     public void topdown() throws RecognitionException {;}
    134     public void bottomup() throws RecognitionException {;}
    135 }
    136