00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 package org.antlr.runtime;
00029
00030 import java.io.Serializable;
00031
00032 public class CommonToken implements Token, Serializable {
00033 protected int type;
00034 protected int line;
00035 protected int charPositionInLine = -1;
00036 protected int channel=DEFAULT_CHANNEL;
00037 protected transient CharStream input;
00038
00043 protected String text;
00044
00046 protected int index = -1;
00047
00049 protected int start;
00050
00052 protected int stop;
00053
00054 public CommonToken(int type) {
00055 this.type = type;
00056 }
00057
00058 public CommonToken(CharStream input, int type, int channel, int start, int stop) {
00059 this.input = input;
00060 this.type = type;
00061 this.channel = channel;
00062 this.start = start;
00063 this.stop = stop;
00064 }
00065
00066 public CommonToken(int type, String text) {
00067 this.type = type;
00068 this.channel = DEFAULT_CHANNEL;
00069 this.text = text;
00070 }
00071
00072 public CommonToken(Token oldToken) {
00073 text = oldToken.getText();
00074 type = oldToken.getType();
00075 line = oldToken.getLine();
00076 index = oldToken.getTokenIndex();
00077 charPositionInLine = oldToken.getCharPositionInLine();
00078 channel = oldToken.getChannel();
00079 input = oldToken.getInputStream();
00080 if ( oldToken instanceof CommonToken ) {
00081 start = ((CommonToken)oldToken).start;
00082 stop = ((CommonToken)oldToken).stop;
00083 }
00084 }
00085
00086 public int getType() {
00087 return type;
00088 }
00089
00090 public void setLine(int line) {
00091 this.line = line;
00092 }
00093
00094 public String getText() {
00095 if ( text!=null ) {
00096 return text;
00097 }
00098 if ( input==null ) {
00099 return null;
00100 }
00101 if ( start<input.size() && stop<input.size() ) {
00102 text = input.substring(start,stop);
00103 }
00104 else {
00105 text = "<EOF>";
00106 }
00107 return text;
00108 }
00109
00115 public void setText(String text) {
00116 this.text = text;
00117 }
00118
00119 public int getLine() {
00120 return line;
00121 }
00122
00123 public int getCharPositionInLine() {
00124 return charPositionInLine;
00125 }
00126
00127 public void setCharPositionInLine(int charPositionInLine) {
00128 this.charPositionInLine = charPositionInLine;
00129 }
00130
00131 public int getChannel() {
00132 return channel;
00133 }
00134
00135 public void setChannel(int channel) {
00136 this.channel = channel;
00137 }
00138
00139 public void setType(int type) {
00140 this.type = type;
00141 }
00142
00143 public int getStartIndex() {
00144 return start;
00145 }
00146
00147 public void setStartIndex(int start) {
00148 this.start = start;
00149 }
00150
00151 public int getStopIndex() {
00152 return stop;
00153 }
00154
00155 public void setStopIndex(int stop) {
00156 this.stop = stop;
00157 }
00158
00159 public int getTokenIndex() {
00160 return index;
00161 }
00162
00163 public void setTokenIndex(int index) {
00164 this.index = index;
00165 }
00166
00167 public CharStream getInputStream() {
00168 return input;
00169 }
00170
00171 public void setInputStream(CharStream input) {
00172 this.input = input;
00173 }
00174
00175 public String toString() {
00176 String channelStr = "";
00177 if ( channel>0 ) {
00178 channelStr=",channel="+channel;
00179 }
00180 String txt = getText();
00181 if ( txt!=null ) {
00182 txt = txt.replaceAll("\n","\\\\n");
00183 txt = txt.replaceAll("\r","\\\\r");
00184 txt = txt.replaceAll("\t","\\\\t");
00185 }
00186 else {
00187 txt = "<no text>";
00188 }
00189 return "[@"+getTokenIndex()+","+start+":"+stop+"='"+txt+"',<"+type+">"+channelStr+","+line+":"+getCharPositionInLine()+"]";
00190 }
00191 }