Home | History | Annotate | Download | only in debug
      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.debug;
     29 
     30 import org.antlr.runtime.RecognitionException;
     31 import org.antlr.runtime.Token;
     32 import org.antlr.runtime.tree.ParseTree;
     33 
     34 import java.util.Stack;
     35 import java.util.ArrayList;
     36 import java.util.List;
     37 
     38 /** This parser listener tracks rule entry/exit and token matches
     39  *  to build a simple parse tree using ParseTree nodes.
     40  */
     41 public class ParseTreeBuilder extends BlankDebugEventListener {
     42 	public static final String EPSILON_PAYLOAD = "<epsilon>";
     43 
     44 	Stack callStack = new Stack();
     45 	List hiddenTokens = new ArrayList();
     46 	int backtracking = 0;
     47 
     48 	public ParseTreeBuilder(String grammarName) {
     49 		ParseTree root = create("<grammar "+grammarName+">");
     50 		callStack.push(root);
     51 	}
     52 
     53 	public ParseTree getTree() {
     54 		return (ParseTree)callStack.elementAt(0);
     55 	}
     56 
     57 	/**  What kind of node to create.  You might want to override
     58 	 *   so I factored out creation here.
     59 	 */
     60 	public ParseTree create(Object payload) {
     61 		return new ParseTree(payload);
     62 	}
     63 
     64 	public ParseTree epsilonNode() {
     65 		return create(EPSILON_PAYLOAD);
     66 	}
     67 
     68 	/** Backtracking or cyclic DFA, don't want to add nodes to tree */
     69 	public void enterDecision(int d, boolean couldBacktrack) { backtracking++; }
     70 	public void exitDecision(int i) { backtracking--; }
     71 
     72 	public void enterRule(String filename, String ruleName) {
     73 		if ( backtracking>0 ) return;
     74 		ParseTree parentRuleNode = (ParseTree)callStack.peek();
     75 		ParseTree ruleNode = create(ruleName);
     76 		parentRuleNode.addChild(ruleNode);
     77 		callStack.push(ruleNode);
     78 	}
     79 
     80 	public void exitRule(String filename, String ruleName) {
     81 		if ( backtracking>0 ) return;
     82 		ParseTree ruleNode = (ParseTree)callStack.peek();
     83 		if ( ruleNode.getChildCount()==0 ) {
     84 			ruleNode.addChild(epsilonNode());
     85 		}
     86 		callStack.pop();
     87 	}
     88 
     89 	public void consumeToken(Token token) {
     90 		if ( backtracking>0 ) return;
     91 		ParseTree ruleNode = (ParseTree)callStack.peek();
     92 		ParseTree elementNode = create(token);
     93 		elementNode.hiddenTokens = this.hiddenTokens;
     94 		this.hiddenTokens = new ArrayList();
     95 		ruleNode.addChild(elementNode);
     96 	}
     97 
     98 	public void consumeHiddenToken(Token token) {
     99 		if ( backtracking>0 ) return;
    100 		hiddenTokens.add(token);
    101 	}
    102 
    103 	public void recognitionException(RecognitionException e) {
    104 		if ( backtracking>0 ) return;
    105 		ParseTree ruleNode = (ParseTree)callStack.peek();
    106 		ParseTree errorNode = create(e);
    107 		ruleNode.addChild(errorNode);
    108 	}
    109 }
    110