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 return Token.EOF_TOKEN;
00082 }
00083 try {
00084 mTokens();
00085 if ( state.token==null ) {
00086 emit();
00087 }
00088 else if ( state.token==Token.SKIP_TOKEN ) {
00089 continue;
00090 }
00091 return state.token;
00092 }
00093 catch (NoViableAltException nva) {
00094 reportError(nva);
00095 recover(nva);
00096 }
00097 catch (RecognitionException re) {
00098 reportError(re);
00099
00100 }
00101 }
00102 }
00103
00110 public void skip() {
00111 state.token = Token.SKIP_TOKEN;
00112 }
00113
00115 public abstract void mTokens() throws RecognitionException;
00116
00118 public void setCharStream(CharStream input) {
00119 this.input = null;
00120 reset();
00121 this.input = input;
00122 }
00123
00124 public CharStream getCharStream() {
00125 return this.input;
00126 }
00127
00128 public String getSourceName() {
00129 return input.getSourceName();
00130 }
00131
00137 public void emit(Token token) {
00138 state.token = token;
00139 }
00140
00150 public Token emit() {
00151 Token t = new CommonToken(input, state.type, state.channel, state.tokenStartCharIndex, getCharIndex()-1);
00152 t.setLine(state.tokenStartLine);
00153 t.setText(state.text);
00154 t.setCharPositionInLine(state.tokenStartCharPositionInLine);
00155 emit(t);
00156 return t;
00157 }
00158
00159 public void match(String s) throws MismatchedTokenException {
00160 int i = 0;
00161 while ( i<s.length() ) {
00162 if ( input.LA(1)!=s.charAt(i) ) {
00163 if ( state.backtracking>0 ) {
00164 state.failed = true;
00165 return;
00166 }
00167 MismatchedTokenException mte =
00168 new MismatchedTokenException(s.charAt(i), input);
00169 recover(mte);
00170 throw mte;
00171 }
00172 i++;
00173 input.consume();
00174 state.failed = false;
00175 }
00176 }
00177
00178 public void matchAny() {
00179 input.consume();
00180 }
00181
00182 public void match(int c) throws MismatchedTokenException {
00183 if ( input.LA(1)!=c ) {
00184 if ( state.backtracking>0 ) {
00185 state.failed = true;
00186 return;
00187 }
00188 MismatchedTokenException mte =
00189 new MismatchedTokenException(c, input);
00190 recover(mte);
00191 throw mte;
00192 }
00193 input.consume();
00194 state.failed = false;
00195 }
00196
00197 public void matchRange(int a, int b)
00198 throws MismatchedRangeException
00199 {
00200 if ( input.LA(1)<a || input.LA(1)>b ) {
00201 if ( state.backtracking>0 ) {
00202 state.failed = true;
00203 return;
00204 }
00205 MismatchedRangeException mre =
00206 new MismatchedRangeException(a,b,input);
00207 recover(mre);
00208 throw mre;
00209 }
00210 input.consume();
00211 state.failed = false;
00212 }
00213
00214 public int getLine() {
00215 return input.getLine();
00216 }
00217
00218 public int getCharPositionInLine() {
00219 return input.getCharPositionInLine();
00220 }
00221
00223 public int getCharIndex() {
00224 return input.index();
00225 }
00226
00230 public String getText() {
00231 if ( state.text!=null ) {
00232 return state.text;
00233 }
00234 return input.substring(state.tokenStartCharIndex,getCharIndex()-1);
00235 }
00236
00240 public void setText(String text) {
00241 state.text = text;
00242 }
00243
00244 public void reportError(RecognitionException e) {
00256 displayRecognitionError(this.getTokenNames(), e);
00257 }
00258
00259 public String getErrorMessage(RecognitionException e, String[] tokenNames) {
00260 String msg = null;
00261 if ( e instanceof MismatchedTokenException ) {
00262 MismatchedTokenException mte = (MismatchedTokenException)e;
00263 msg = "mismatched character "+getCharErrorDisplay(e.c)+" expecting "+getCharErrorDisplay(mte.expecting);
00264 }
00265 else if ( e instanceof NoViableAltException ) {
00266 NoViableAltException nvae = (NoViableAltException)e;
00267
00268
00269
00270 msg = "no viable alternative at character "+getCharErrorDisplay(e.c);
00271 }
00272 else if ( e instanceof EarlyExitException ) {
00273 EarlyExitException eee = (EarlyExitException)e;
00274
00275 msg = "required (...)+ loop did not match anything at character "+getCharErrorDisplay(e.c);
00276 }
00277 else if ( e instanceof MismatchedNotSetException ) {
00278 MismatchedNotSetException mse = (MismatchedNotSetException)e;
00279 msg = "mismatched character "+getCharErrorDisplay(e.c)+" expecting set "+mse.expecting;
00280 }
00281 else if ( e instanceof MismatchedSetException ) {
00282 MismatchedSetException mse = (MismatchedSetException)e;
00283 msg = "mismatched character "+getCharErrorDisplay(e.c)+" expecting set "+mse.expecting;
00284 }
00285 else if ( e instanceof MismatchedRangeException ) {
00286 MismatchedRangeException mre = (MismatchedRangeException)e;
00287 msg = "mismatched character "+getCharErrorDisplay(e.c)+" expecting set "+
00288 getCharErrorDisplay(mre.a)+".."+getCharErrorDisplay(mre.b);
00289 }
00290 else {
00291 msg = super.getErrorMessage(e, tokenNames);
00292 }
00293 return msg;
00294 }
00295
00296 public String getCharErrorDisplay(int c) {
00297 String s = String.valueOf((char)c);
00298 switch ( c ) {
00299 case Token.EOF :
00300 s = "<EOF>";
00301 break;
00302 case '\n' :
00303 s = "\\n";
00304 break;
00305 case '\t' :
00306 s = "\\t";
00307 break;
00308 case '\r' :
00309 s = "\\r";
00310 break;
00311 }
00312 return "'"+s+"'";
00313 }
00314
00320 public void recover(RecognitionException re) {
00321
00322
00323 input.consume();
00324 }
00325
00326 public void traceIn(String ruleName, int ruleIndex) {
00327 String inputSymbol = ((char)input.LT(1))+" line="+getLine()+":"+getCharPositionInLine();
00328 super.traceIn(ruleName, ruleIndex, inputSymbol);
00329 }
00330
00331 public void traceOut(String ruleName, int ruleIndex) {
00332 String inputSymbol = ((char)input.LT(1))+" line="+getLine()+":"+getCharPositionInLine();
00333 super.traceOut(ruleName, ruleIndex, inputSymbol);
00334 }
00335 }