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.debug; 293447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 303447a5916aa62f44de24cc441fc9987116ddff52Andrew Sappersteinimport org.antlr.runtime.RecognitionException; 313447a5916aa62f44de24cc441fc9987116ddff52Andrew Sappersteinimport org.antlr.runtime.Token; 323447a5916aa62f44de24cc441fc9987116ddff52Andrew Sappersteinimport org.antlr.runtime.tree.ParseTree; 333447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 343447a5916aa62f44de24cc441fc9987116ddff52Andrew Sappersteinimport java.util.Stack; 353447a5916aa62f44de24cc441fc9987116ddff52Andrew Sappersteinimport java.util.ArrayList; 363447a5916aa62f44de24cc441fc9987116ddff52Andrew Sappersteinimport java.util.List; 373447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 383447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein/** This parser listener tracks rule entry/exit and token matches 393447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * to build a simple parse tree using ParseTree nodes. 403447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein */ 413447a5916aa62f44de24cc441fc9987116ddff52Andrew Sappersteinpublic class ParseTreeBuilder extends BlankDebugEventListener { 423447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein public static final String EPSILON_PAYLOAD = "<epsilon>"; 433447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 443447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein Stack callStack = new Stack(); 453447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein List hiddenTokens = new ArrayList(); 463447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein int backtracking = 0; 473447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 483447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein public ParseTreeBuilder(String grammarName) { 493447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein ParseTree root = create("<grammar "+grammarName+">"); 503447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein callStack.push(root); 513447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein } 523447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 533447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein public ParseTree getTree() { 543447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein return (ParseTree)callStack.elementAt(0); 553447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein } 563447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 573447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein /** What kind of node to create. You might want to override 583447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * so I factored out creation here. 593447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein */ 603447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein public ParseTree create(Object payload) { 613447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein return new ParseTree(payload); 623447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein } 633447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 643447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein public ParseTree epsilonNode() { 653447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein return create(EPSILON_PAYLOAD); 663447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein } 673447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 683447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein /** Backtracking or cyclic DFA, don't want to add nodes to tree */ 693447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein public void enterDecision(int d, boolean couldBacktrack) { backtracking++; } 703447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein public void exitDecision(int i) { backtracking--; } 713447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 723447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein public void enterRule(String filename, String ruleName) { 733447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein if ( backtracking>0 ) return; 743447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein ParseTree parentRuleNode = (ParseTree)callStack.peek(); 753447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein ParseTree ruleNode = create(ruleName); 763447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein parentRuleNode.addChild(ruleNode); 773447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein callStack.push(ruleNode); 783447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein } 793447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 803447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein public void exitRule(String filename, String ruleName) { 813447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein if ( backtracking>0 ) return; 823447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein ParseTree ruleNode = (ParseTree)callStack.peek(); 833447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein if ( ruleNode.getChildCount()==0 ) { 843447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein ruleNode.addChild(epsilonNode()); 853447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein } 863447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein callStack.pop(); 873447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein } 883447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 893447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein public void consumeToken(Token token) { 903447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein if ( backtracking>0 ) return; 913447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein ParseTree ruleNode = (ParseTree)callStack.peek(); 923447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein ParseTree elementNode = create(token); 933447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein elementNode.hiddenTokens = this.hiddenTokens; 943447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein this.hiddenTokens = new ArrayList(); 953447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein ruleNode.addChild(elementNode); 963447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein } 973447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 983447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein public void consumeHiddenToken(Token token) { 993447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein if ( backtracking>0 ) return; 1003447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein hiddenTokens.add(token); 1013447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein } 1023447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 1033447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein public void recognitionException(RecognitionException e) { 1043447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein if ( backtracking>0 ) return; 1053447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein ParseTree ruleNode = (ParseTree)callStack.peek(); 1063447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein ParseTree errorNode = create(e); 1073447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein ruleNode.addChild(errorNode); 1083447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein } 1093447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein} 110