001: /*
002: [The "BSD licence"]
003: Copyright (c) 2005-2006 Terence Parr
004: All rights reserved.
005:
006: Redistribution and use in source and binary forms, with or without
007: modification, are permitted provided that the following conditions
008: are met:
009: 1. Redistributions of source code must retain the above copyright
010: notice, this list of conditions and the following disclaimer.
011: 2. Redistributions in binary form must reproduce the above copyright
012: notice, this list of conditions and the following disclaimer in the
013: documentation and/or other materials provided with the distribution.
014: 3. The name of the author may not be used to endorse or promote products
015: derived from this software without specific prior written permission.
016:
017: THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
018: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
019: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
020: IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
021: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
022: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
023: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
024: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
025: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
026: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027: */
028: package org.antlr.runtime;
029:
030: import java.io.Serializable;
031:
032: public class CommonToken implements Token, Serializable {
033: protected int type;
034: protected int line;
035: protected int charPositionInLine = -1; // set to invalid position
036: protected int channel = DEFAULT_CHANNEL;
037: protected transient CharStream input;
038: /** We need to be able to change the text once in a while. If
039: * this is non-null, then getText should return this. Note that
040: * start/stop are not affected by changing this.
041: */
042: protected String text;
043:
044: /** What token number is this from 0..n-1 tokens; < 0 implies invalid index */
045: protected int index = -1;
046:
047: /** The char position into the input buffer where this token starts */
048: protected int start;
049:
050: /** The char position into the input buffer where this token stops */
051: protected int stop;
052:
053: public CommonToken(int type) {
054: this .type = type;
055: }
056:
057: public CommonToken(CharStream input, int type, int channel,
058: int start, int stop) {
059: this .input = input;
060: this .type = type;
061: this .channel = channel;
062: this .start = start;
063: this .stop = stop;
064: }
065:
066: public CommonToken(int type, String text) {
067: this .type = type;
068: this .channel = DEFAULT_CHANNEL;
069: this .text = text;
070: }
071:
072: public CommonToken(Token oldToken) {
073: text = oldToken.getText();
074: type = oldToken.getType();
075: line = oldToken.getLine();
076: index = oldToken.getTokenIndex();
077: charPositionInLine = oldToken.getCharPositionInLine();
078: channel = oldToken.getChannel();
079: }
080:
081: public int getType() {
082: return type;
083: }
084:
085: public void setLine(int line) {
086: this .line = line;
087: }
088:
089: public String getText() {
090: if (text != null) {
091: return text;
092: }
093: if (input == null) {
094: return null;
095: }
096: text = input.substring(start, stop);
097: return text;
098: }
099:
100: /** Override the text for this token. getText() will return this text
101: * rather than pulling from the buffer. Note that this does not mean
102: * that start/stop indexes are not valid. It means that that input
103: * was converted to a new string in the token object.
104: */
105: public void setText(String text) {
106: this .text = text;
107: }
108:
109: public int getLine() {
110: return line;
111: }
112:
113: public int getCharPositionInLine() {
114: return charPositionInLine;
115: }
116:
117: public void setCharPositionInLine(int charPositionInLine) {
118: this .charPositionInLine = charPositionInLine;
119: }
120:
121: public int getChannel() {
122: return channel;
123: }
124:
125: public void setChannel(int channel) {
126: this .channel = channel;
127: }
128:
129: public void setType(int type) {
130: this .type = type;
131: }
132:
133: public int getStartIndex() {
134: return start;
135: }
136:
137: public void setStartIndex(int start) {
138: this .start = start;
139: }
140:
141: public int getStopIndex() {
142: return stop;
143: }
144:
145: public void setStopIndex(int stop) {
146: this .stop = stop;
147: }
148:
149: public int getTokenIndex() {
150: return index;
151: }
152:
153: public void setTokenIndex(int index) {
154: this .index = index;
155: }
156:
157: public String toString() {
158: String channelStr = "";
159: if (channel > 0) {
160: channelStr = ",channel=" + channel;
161: }
162: String txt = getText();
163: if (txt != null) {
164: txt = txt.replaceAll("\n", "\\\\n");
165: txt = txt.replaceAll("\r", "\\\\r");
166: txt = txt.replaceAll("\t", "\\\\t");
167: } else {
168: txt = "<no text>";
169: }
170: return "[@" + getTokenIndex() + "," + start + ":" + stop + "='"
171: + txt + "',<" + type + ">" + channelStr + "," + line
172: + ":" + getCharPositionInLine() + "]";
173: }
174: }
|