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; 29 30 import java.io.Serializable; 31 32 public class CommonToken implements Token, Serializable { 33 protected int type; 34 protected int line; 35 protected int charPositionInLine = -1; // set to invalid position 36 protected int channel=DEFAULT_CHANNEL; 37 protected transient CharStream input; 38 39 /** We need to be able to change the text once in a while. If 40 * this is non-null, then getText should return this. Note that 41 * start/stop are not affected by changing this. 42 */ 43 protected String text; 44 45 /** What token number is this from 0..n-1 tokens; < 0 implies invalid index */ 46 protected int index = -1; 47 48 /** The char position into the input buffer where this token starts */ 49 protected int start; 50 51 /** The char position into the input buffer where this token stops */ 52 protected int stop; 53 CommonToken(int type)54 public CommonToken(int type) { 55 this.type = type; 56 } 57 CommonToken(CharStream input, int type, int channel, int start, int stop)58 public CommonToken(CharStream input, int type, int channel, int start, int stop) { 59 this.input = input; 60 this.type = type; 61 this.channel = channel; 62 this.start = start; 63 this.stop = stop; 64 } 65 CommonToken(int type, String text)66 public CommonToken(int type, String text) { 67 this.type = type; 68 this.channel = DEFAULT_CHANNEL; 69 this.text = text; 70 } 71 CommonToken(Token oldToken)72 public CommonToken(Token oldToken) { 73 text = oldToken.getText(); 74 type = oldToken.getType(); 75 line = oldToken.getLine(); 76 index = oldToken.getTokenIndex(); 77 charPositionInLine = oldToken.getCharPositionInLine(); 78 channel = oldToken.getChannel(); 79 input = oldToken.getInputStream(); 80 if ( oldToken instanceof CommonToken ) { 81 start = ((CommonToken)oldToken).start; 82 stop = ((CommonToken)oldToken).stop; 83 } 84 } 85 getType()86 public int getType() { 87 return type; 88 } 89 setLine(int line)90 public void setLine(int line) { 91 this.line = line; 92 } 93 getText()94 public String getText() { 95 if ( text!=null ) { 96 return text; 97 } 98 if ( input==null ) { 99 return null; 100 } 101 if ( start<input.size() && stop<input.size() ) { 102 text = input.substring(start,stop); 103 } 104 else { 105 text = "<EOF>"; 106 } 107 return text; 108 } 109 110 /** Override the text for this token. getText() will return this text 111 * rather than pulling from the buffer. Note that this does not mean 112 * that start/stop indexes are not valid. It means that that input 113 * was converted to a new string in the token object. 114 */ setText(String text)115 public void setText(String text) { 116 this.text = text; 117 } 118 getLine()119 public int getLine() { 120 return line; 121 } 122 getCharPositionInLine()123 public int getCharPositionInLine() { 124 return charPositionInLine; 125 } 126 setCharPositionInLine(int charPositionInLine)127 public void setCharPositionInLine(int charPositionInLine) { 128 this.charPositionInLine = charPositionInLine; 129 } 130 getChannel()131 public int getChannel() { 132 return channel; 133 } 134 setChannel(int channel)135 public void setChannel(int channel) { 136 this.channel = channel; 137 } 138 setType(int type)139 public void setType(int type) { 140 this.type = type; 141 } 142 getStartIndex()143 public int getStartIndex() { 144 return start; 145 } 146 setStartIndex(int start)147 public void setStartIndex(int start) { 148 this.start = start; 149 } 150 getStopIndex()151 public int getStopIndex() { 152 return stop; 153 } 154 setStopIndex(int stop)155 public void setStopIndex(int stop) { 156 this.stop = stop; 157 } 158 getTokenIndex()159 public int getTokenIndex() { 160 return index; 161 } 162 setTokenIndex(int index)163 public void setTokenIndex(int index) { 164 this.index = index; 165 } 166 getInputStream()167 public CharStream getInputStream() { 168 return input; 169 } 170 setInputStream(CharStream input)171 public void setInputStream(CharStream input) { 172 this.input = input; 173 } 174 toString()175 public String toString() { 176 String channelStr = ""; 177 if ( channel>0 ) { 178 channelStr=",channel="+channel; 179 } 180 String txt = getText(); 181 if ( txt!=null ) { 182 txt = txt.replaceAll("\n","\\\\n"); 183 txt = txt.replaceAll("\r","\\\\r"); 184 txt = txt.replaceAll("\t","\\\\t"); 185 } 186 else { 187 txt = "<no text>"; 188 } 189 return "[@"+getTokenIndex()+","+start+":"+stop+"='"+txt+"',<"+type+">"+channelStr+","+line+":"+getCharPositionInLine()+"]"; 190 } 191 } 192