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
00035 public abstract class Lexer extends BaseRecognizer implements TokenSource {
00037 protected CharStream input;
00038
00039 public Lexer() {
00040 }
00041
00042 public Lexer(CharStream input) {
00043 this.input = input;
00044 }
00045
00046 public Lexer(CharStream input, RecognizerSharedState state) {
00047 super(state);
00048 this.input = input;
00049 }
00050
00051 public void reset() {
00052 super.reset();
00053
00054 if ( input!=null ) {
00055 input.seek(0);
00056 }
00057 if ( state==null ) {
00058 return;
00059 }
00060 state.token = null;
00061 state.type = Token.INVALID_TOKEN_TYPE;
00062 state.channel = Token.DEFAULT_CHANNEL;
00063 state.tokenStartCharIndex = -1;
00064 state.tokenStartCharPositionInLine = -1;
00065 state.tokenStartLine = -1;
00066 state.text = null;
00067 }
00068
00072 public Token nextToken() {
00073 while (true) {
00074 state.token = null;
00075 state.channel = Token.DEFAULT_CHANNEL;
00076 state.tokenStartCharIndex = input.index();
00077 state.tokenStartCharPositionInLine = input.getCharPositionInLine();
00078 state.tokenStartLine = input.getLine();
00079 state.text = null;
00080 if ( input.LA(1)==CharStream.EOF ) {
00081 Token eof = new CommonToken((CharStream)input,Token.EOF,
00082 Token.DEFAULT_CHANNEL,
00083 input.index(),input.index());
00084 eof.setLine(getLine());
00085 eof.setCharPositionInLine(getCharPositionInLine());
00086 return eof;
00087 }
00088 try {
00089 mTokens();
00090 if ( state.token==null ) {
00091 emit();
00092 }
00093 else if ( state.token==Token.SKIP_TOKEN ) {
00094 continue;
00095 }
00096 return state.token;
00097 }
00098 catch (NoViableAltException nva) {
00099 reportError(nva);
00100 recover(nva);
00101 }
00102 catch (RecognitionException re) {
00103 reportError(re);
00104
00105 }
00106 }
00107 }
00108
00115 public void skip() {
00116 state.token = Token.SKIP_TOKEN;
00117 }
00118
00120 public abstract void mTokens() throws RecognitionException;
00121
00123 public void setCharStream(CharStream input) {
00124 this.input = null;
00125 reset();
00126 this.input = input;
00127 }
00128
00129 public CharStream getCharStream() {
00130 return this.input;
00131 }
00132
00133 public String getSourceName() {
00134 return input.getSourceName();
00135 }
00136
00142 public void emit(Token token) {
00143 state.token = token;
00144 }
00145
00155 public Token emit() {
00156 Token t = new CommonToken(input, state.type, state.channel, state.tokenStartCharIndex, getCharIndex()-1);
00157 t.setLine(state.tokenStartLine);
00158 t.setText(state.text);
00159 t.setCharPositionInLine(state.tokenStartCharPositionInLine);
00160 emit(t);
00161 return t;
00162 }
00163
00164 public void match(String s) throws MismatchedTokenException {
00165 int i = 0;
00166 while ( i<s.length() ) {
00167 if ( input.LA(1)!=s.charAt(i) ) {
00168 if ( state.backtracking>0 ) {
00169 state.failed = true;
00170 return;
00171 }
00172 MismatchedTokenException mte =
00173 new MismatchedTokenException(s.charAt(i), input);
00174 recover(mte);
00175 throw mte;
00176 }
00177 i++;
00178 input.consume();
00179 state.failed = false;
00180 }
00181 }
00182
00183 public void matchAny() {
00184 input.consume();
00185 }
00186
00187 public void match(int c) throws MismatchedTokenException {
00188 if ( input.LA(1)!=c ) {
00189 if ( state.backtracking>0 ) {
00190 state.failed = true;
00191 return;
00192 }
00193 MismatchedTokenException mte =
00194 new MismatchedTokenException(c, input);
00195 recover(mte);
00196 throw mte;
00197 }
00198 input.consume();
00199 state.failed = false;
00200 }
00201
00202 public void matchRange(int a, int b)
00203 throws MismatchedRangeException
00204 {
00205 if ( input.LA(1)<a || input.LA(1)>b ) {
00206 if ( state.backtracking>0 ) {
00207 state.failed = true;
00208 return;
00209 }
00210 MismatchedRangeException mre =
00211 new MismatchedRangeException(a,b,input);
00212 recover(mre);
00213 throw mre;
00214 }
00215 input.consume();
00216 state.failed = false;
00217 }
00218
00219 public int getLine() {
00220 return input.getLine();
00221 }
00222
00223 public int getCharPositionInLine() {
00224 return input.getCharPositionInLine();
00225 }
00226
00228 public int getCharIndex() {
00229 return input.index();
00230 }
00231
00235 public String getText() {
00236 if ( state.text!=null ) {
00237 return state.text;
00238 }
00239 return input.substring(state.tokenStartCharIndex,getCharIndex()-1);
00240 }
00241
00245 public void setText(String text) {
00246 state.text = text;
00247 }
00248
00249 public void reportError(RecognitionException e) {
00261 displayRecognitionError(this.getTokenNames(), e);
00262 }
00263
00264 public String getErrorMessage(RecognitionException e, String[] tokenNames) {
00265 String msg = null;
00266 if ( e instanceof MismatchedTokenException ) {
00267 MismatchedTokenException mte = (MismatchedTokenException)e;
00268 msg = "mismatched character "+getCharErrorDisplay(e.c)+" expecting "+getCharErrorDisplay(mte.expecting);
00269 }
00270 else if ( e instanceof NoViableAltException ) {
00271 NoViableAltException nvae = (NoViableAltException)e;
00272
00273
00274
00275 msg = "no viable alternative at character "+getCharErrorDisplay(e.c);
00276 }
00277 else if ( e instanceof EarlyExitException ) {
00278 EarlyExitException eee = (EarlyExitException)e;
00279
00280 msg = "required (...)+ loop did not match anything at character "+getCharErrorDisplay(e.c);
00281 }
00282 else if ( e instanceof MismatchedNotSetException ) {
00283 MismatchedNotSetException mse = (MismatchedNotSetException)e;
00284 msg = "mismatched character "+getCharErrorDisplay(e.c)+" expecting set "+mse.expecting;
00285 }
00286 else if ( e instanceof MismatchedSetException ) {
00287 MismatchedSetException mse = (MismatchedSetException)e;
00288 msg = "mismatched character "+getCharErrorDisplay(e.c)+" expecting set "+mse.expecting;
00289 }
00290 else if ( e instanceof MismatchedRangeException ) {
00291 MismatchedRangeException mre = (MismatchedRangeException)e;
00292 msg = "mismatched character "+getCharErrorDisplay(e.c)+" expecting set "+
00293 getCharErrorDisplay(mre.a)+".."+getCharErrorDisplay(mre.b);
00294 }
00295 else {
00296 msg = super.getErrorMessage(e, tokenNames);
00297 }
00298 return msg;
00299 }
00300
00301 public String getCharErrorDisplay(int c) {
00302 String s = String.valueOf((char)c);
00303 switch ( c ) {
00304 case Token.EOF :
00305 s = "<EOF>";
00306 break;
00307 case '\n' :
00308 s = "\\n";
00309 break;
00310 case '\t' :
00311 s = "\\t";
00312 break;
00313 case '\r' :
00314 s = "\\r";
00315 break;
00316 }
00317 return "'"+s+"'";
00318 }
00319
00325 public void recover(RecognitionException re) {
00326
00327
00328 input.consume();
00329 }
00330
00331 public void traceIn(String ruleName, int ruleIndex) {
00332 String inputSymbol = ((char)input.LT(1))+" line="+getLine()+":"+getCharPositionInLine();
00333 super.traceIn(ruleName, ruleIndex, inputSymbol);
00334 }
00335
00336 public void traceOut(String ruleName, int ruleIndex) {
00337 String inputSymbol = ((char)input.LT(1))+" line="+getLine()+":"+getCharPositionInLine();
00338 super.traceOut(ruleName, ruleIndex, inputSymbol);
00339 }
00340 }