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.tree; 293447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 303447a5916aa62f44de24cc441fc9987116ddff52Andrew Sappersteinimport org.antlr.runtime.Token; 313447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 323447a5916aa62f44de24cc441fc9987116ddff52Andrew Sappersteinimport java.util.List; 333447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 343447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein/** A record of the rules used to match a token sequence. The tokens 353447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * end up as the leaves of this tree and rule nodes are the interior nodes. 363447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * This really adds no functionality, it is just an alias for CommonTree 373447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * that is more meaningful (specific) and holds a String to display for a node. 383447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein */ 393447a5916aa62f44de24cc441fc9987116ddff52Andrew Sappersteinpublic class ParseTree extends BaseTree { 403447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein public Object payload; 413447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein public List hiddenTokens; 423447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 433447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein public ParseTree(Object label) { 443447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein this.payload = label; 453447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein } 463447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 473447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein public Tree dupNode() { 483447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein return null; 493447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein } 503447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 513447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein public int getType() { 523447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein return 0; 533447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein } 543447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 553447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein public String getText() { 563447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein return toString(); 573447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein } 583447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 593447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein public int getTokenStartIndex() { 603447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein return 0; 613447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein } 623447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 633447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein public void setTokenStartIndex(int index) { 643447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein } 653447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 663447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein public int getTokenStopIndex() { 673447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein return 0; 683447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein } 693447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 703447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein public void setTokenStopIndex(int index) { 713447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein } 723447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 733447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein public String toString() { 743447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein if ( payload instanceof Token ) { 753447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein Token t = (Token)payload; 763447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein if ( t.getType() == Token.EOF ) { 773447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein return "<EOF>"; 783447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein } 793447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein return t.getText(); 803447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein } 813447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein return payload.toString(); 823447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein } 833447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 843447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein /** Emit a token and all hidden nodes before. EOF node holds all 853447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * hidden tokens after last real token. 863447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein */ 873447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein public String toStringWithHiddenTokens() { 883447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein StringBuffer buf = new StringBuffer(); 893447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein if ( hiddenTokens!=null ) { 903447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein for (int i = 0; i < hiddenTokens.size(); i++) { 913447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein Token hidden = (Token) hiddenTokens.get(i); 923447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein buf.append(hidden.getText()); 933447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein } 943447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein } 953447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein String nodeText = this.toString(); 963447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein if ( !nodeText.equals("<EOF>") ) buf.append(nodeText); 973447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein return buf.toString(); 983447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein } 993447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 1003447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein /** Print out the leaves of this tree, which means printing original 1013447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * input back out. 1023447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein */ 1033447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein public String toInputString() { 1043447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein StringBuffer buf = new StringBuffer(); 1053447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein _toStringLeaves(buf); 1063447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein return buf.toString(); 1073447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein } 1083447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 1093447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein public void _toStringLeaves(StringBuffer buf) { 1103447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein if ( payload instanceof Token ) { // leaf node token? 1113447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein buf.append(this.toStringWithHiddenTokens()); 1123447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein return; 1133447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein } 1143447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein for (int i = 0; children!=null && i < children.size(); i++) { 1153447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein ParseTree t = (ParseTree)children.get(i); 1163447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein t._toStringLeaves(buf); 1173447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein } 1183447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein } 1193447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein} 120