00001 /* 00002 [The "BSD licence"] 00003 Copyright (c) 2005-2008 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.Token; 00031 00038 public class CommonTree extends BaseTree { 00040 public Token token; 00041 00045 protected int startIndex=-1, stopIndex=-1; 00046 00048 public CommonTree parent; 00049 00051 public int childIndex = -1; 00052 00053 public CommonTree() { } 00054 00055 public CommonTree(CommonTree node) { 00056 super(node); 00057 this.token = node.token; 00058 this.startIndex = node.startIndex; 00059 this.stopIndex = node.stopIndex; 00060 } 00061 00062 public CommonTree(Token t) { 00063 this.token = t; 00064 } 00065 00066 public Token getToken() { 00067 return token; 00068 } 00069 00070 public Tree dupNode() { 00071 return new CommonTree(this); 00072 } 00073 00074 public boolean isNil() { 00075 return token==null; 00076 } 00077 00078 public int getType() { 00079 if ( token==null ) { 00080 return Token.INVALID_TOKEN_TYPE; 00081 } 00082 return token.getType(); 00083 } 00084 00085 public String getText() { 00086 if ( token==null ) { 00087 return null; 00088 } 00089 return token.getText(); 00090 } 00091 00092 public int getLine() { 00093 if ( token==null || token.getLine()==0 ) { 00094 if ( getChildCount()>0 ) { 00095 return getChild(0).getLine(); 00096 } 00097 return 0; 00098 } 00099 return token.getLine(); 00100 } 00101 00102 public int getCharPositionInLine() { 00103 if ( token==null || token.getCharPositionInLine()==-1 ) { 00104 if ( getChildCount()>0 ) { 00105 return getChild(0).getCharPositionInLine(); 00106 } 00107 return 0; 00108 } 00109 return token.getCharPositionInLine(); 00110 } 00111 00112 public int getTokenStartIndex() { 00113 if ( startIndex==-1 && token!=null ) { 00114 return token.getTokenIndex(); 00115 } 00116 return startIndex; 00117 } 00118 00119 public void setTokenStartIndex(int index) { 00120 startIndex = index; 00121 } 00122 00123 public int getTokenStopIndex() { 00124 if ( stopIndex==-1 && token!=null ) { 00125 return token.getTokenIndex(); 00126 } 00127 return stopIndex; 00128 } 00129 00130 public void setTokenStopIndex(int index) { 00131 stopIndex = index; 00132 } 00133 00134 public int getChildIndex() { 00135 return childIndex; 00136 } 00137 00138 public Tree getParent() { 00139 return parent; 00140 } 00141 00142 public void setParent(Tree t) { 00143 this.parent = (CommonTree)t; 00144 } 00145 00146 public void setChildIndex(int index) { 00147 this.childIndex = index; 00148 } 00149 00150 public String toString() { 00151 if ( isNil() ) { 00152 return "nil"; 00153 } 00154 if ( getType()==Token.INVALID_TOKEN_TYPE ) { 00155 return "<errornode>"; 00156 } 00157 if ( token==null ) { 00158 return null; 00159 } 00160 return token.getText(); 00161 } 00162 }
1.5.5