001: package org.antlr.runtime;
002:
003: /** A Token object like we'd use in ANTLR 2.x; has an actual string created
004: * and associated with this object. These objects are needed for imaginary
005: * tree nodes that have payload objects. We need to create a Token object
006: * that has a string; the tree node will point at this token. CommonToken
007: * has indexes into a char stream and hence cannot be used to introduce
008: * new strings.
009: */
010: public class ClassicToken implements Token {
011: protected String text;
012: protected int type;
013: protected int line;
014: protected int charPositionInLine;
015: protected int channel = DEFAULT_CHANNEL;
016:
017: /** What token number is this from 0..n-1 tokens */
018: protected int index;
019:
020: public ClassicToken(int type) {
021: this .type = type;
022: }
023:
024: public ClassicToken(Token oldToken) {
025: text = oldToken.getText();
026: type = oldToken.getType();
027: line = oldToken.getLine();
028: charPositionInLine = oldToken.getCharPositionInLine();
029: channel = oldToken.getChannel();
030: }
031:
032: public ClassicToken(int type, String text) {
033: this .type = type;
034: this .text = text;
035: }
036:
037: public ClassicToken(int type, String text, int channel) {
038: this .type = type;
039: this .text = text;
040: this .channel = channel;
041: }
042:
043: public int getType() {
044: return type;
045: }
046:
047: public void setLine(int line) {
048: this .line = line;
049: }
050:
051: public String getText() {
052: return text;
053: }
054:
055: public void setText(String text) {
056: this .text = text;
057: }
058:
059: public int getLine() {
060: return line;
061: }
062:
063: public int getCharPositionInLine() {
064: return charPositionInLine;
065: }
066:
067: public void setCharPositionInLine(int charPositionInLine) {
068: this .charPositionInLine = charPositionInLine;
069: }
070:
071: public int getChannel() {
072: return channel;
073: }
074:
075: public void setChannel(int channel) {
076: this .channel = channel;
077: }
078:
079: public void setType(int type) {
080: this .type = type;
081: }
082:
083: public int getTokenIndex() {
084: return index;
085: }
086:
087: public void setTokenIndex(int index) {
088: this .index = index;
089: }
090:
091: public String toString() {
092: String channelStr = "";
093: if (channel > 0) {
094: channelStr = ",channel=" + channel;
095: }
096: String txt = getText();
097: if (txt != null) {
098: txt = txt.replaceAll("\n", "\\\\n");
099: txt = txt.replaceAll("\r", "\\\\r");
100: txt = txt.replaceAll("\t", "\\\\t");
101: } else {
102: txt = "<no text>";
103: }
104: return "[@" + getTokenIndex() + ",'" + txt + "',<" + type + ">"
105: + channelStr + "," + line + ":"
106: + getCharPositionInLine() + "]";
107: }
108: }
|