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 if ( oldToken instanceof CommonToken ) {
00080 start = ((CommonToken)oldToken).start;
00081 stop = ((CommonToken)oldToken).stop;
00082 }
00083 }
00084
00085 public int getType() {
00086 return type;
00087 }
00088
00089 public void setLine(int line) {
00090 this.line = line;
00091 }
00092
00093 public String getText() {
00094 if ( text!=null ) {
00095 return text;
00096 }
00097 if ( input==null ) {
00098 return null;
00099 }
00100 text = input.substring(start,stop);
00101 return text;
00102 }
00103
00109 public void setText(String text) {
00110 this.text = text;
00111 }
00112
00113 public int getLine() {
00114 return line;
00115 }
00116
00117 public int getCharPositionInLine() {
00118 return charPositionInLine;
00119 }
00120
00121 public void setCharPositionInLine(int charPositionInLine) {
00122 this.charPositionInLine = charPositionInLine;
00123 }
00124
00125 public int getChannel() {
00126 return channel;
00127 }
00128
00129 public void setChannel(int channel) {
00130 this.channel = channel;
00131 }
00132
00133 public void setType(int type) {
00134 this.type = type;
00135 }
00136
00137 public int getStartIndex() {
00138 return start;
00139 }
00140
00141 public void setStartIndex(int start) {
00142 this.start = start;
00143 }
00144
00145 public int getStopIndex() {
00146 return stop;
00147 }
00148
00149 public void setStopIndex(int stop) {
00150 this.stop = stop;
00151 }
00152
00153 public int getTokenIndex() {
00154 return index;
00155 }
00156
00157 public void setTokenIndex(int index) {
00158 this.index = index;
00159 }
00160
00161 public CharStream getInputStream() {
00162 return input;
00163 }
00164
00165 public void setInputStream(CharStream input) {
00166 this.input = input;
00167 }
00168
00169 public String toString() {
00170 String channelStr = "";
00171 if ( channel>0 ) {
00172 channelStr=",channel="+channel;
00173 }
00174 String txt = getText();
00175 if ( txt!=null ) {
00176 txt = txt.replaceAll("\n","\\\\n");
00177 txt = txt.replaceAll("\r","\\\\r");
00178 txt = txt.replaceAll("\t","\\\\t");
00179 }
00180 else {
00181 txt = "<no text>";
00182 }
00183 return "[@"+getTokenIndex()+","+start+":"+stop+"='"+txt+"',<"+type+">"+channelStr+","+line+":"+getCharPositionInLine()+"]";
00184 }
00185 }