00001 package edu.ksu.cis.bandera.abstraction.options.lexer;
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
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 import java.io.*;
00039 import java.util.*;
00040 import edu.ksu.cis.bandera.abstraction.options.node.*;
00041
00042 public class Lexer
00043 {
00044 protected Token token;
00045 protected State state = State.INITIAL;
00046
00047 private PushbackReader in;
00048 private int line;
00049 private int pos;
00050 private boolean cr;
00051 private boolean eof;
00052 private final StringBuffer text = new StringBuffer();
00053
00054 private static int[][][][] gotoTable;
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122 private static int[][] accept;
00123
00124
00125
00126
00127
00128
00129 public static class State
00130 {
00131 public final static State INITIAL = new State(0);
00132
00133 private int id;
00134
00135 private State(int id)
00136 {
00137 this.id = id;
00138 }
00139
00140 public int id()
00141 {
00142 return id;
00143 }
00144 }
00145 public Lexer(PushbackReader in)
00146 {
00147 this.in = in;
00148
00149 if(gotoTable == null)
00150 {
00151 try
00152 {
00153 DataInputStream s = new DataInputStream(
00154 new BufferedInputStream(
00155 Lexer.class.getResourceAsStream("lexer.dat")));
00156
00157
00158 int length = s.readInt();
00159 gotoTable = new int[length][][][];
00160 for(int i = 0; i < gotoTable.length; i++)
00161 {
00162 length = s.readInt();
00163 gotoTable[i] = new int[length][][];
00164 for(int j = 0; j < gotoTable[i].length; j++)
00165 {
00166 length = s.readInt();
00167 gotoTable[i][j] = new int[length][3];
00168 for(int k = 0; k < gotoTable[i][j].length; k++)
00169 {
00170 for(int l = 0; l < 3; l++)
00171 {
00172 gotoTable[i][j][k][l] = s.readInt();
00173 }
00174 }
00175 }
00176 }
00177
00178
00179 length = s.readInt();
00180 accept = new int[length][];
00181 for(int i = 0; i < accept.length; i++)
00182 {
00183 length = s.readInt();
00184 accept[i] = new int[length];
00185 for(int j = 0; j < accept[i].length; j++)
00186 {
00187 accept[i][j] = s.readInt();
00188 }
00189 }
00190
00191 s.close();
00192 }
00193 catch(Exception e)
00194 {
00195 throw new RuntimeException("Unable to read lexer.dat.");
00196 }
00197 }
00198 }
00199 protected void filter() throws LexerException, IOException
00200 {
00201 }
00202 private int getChar() throws IOException
00203 {
00204 if(eof)
00205 {
00206 return -1;
00207 }
00208
00209 int result = in.read();
00210
00211 if(result == -1)
00212 {
00213 eof = true;
00214 }
00215
00216 return result;
00217 }
00218 private String getText(int acceptLength)
00219 {
00220 StringBuffer s = new StringBuffer(acceptLength);
00221 for(int i = 0; i < acceptLength; i++)
00222 {
00223 s.append(text.charAt(i));
00224 }
00225
00226 return s.toString();
00227 }
00228 protected Token getToken() throws IOException, LexerException
00229 {
00230 int dfa_state = 0;
00231
00232 int start_pos = pos;
00233 int start_line = line;
00234
00235 int accept_state = -1;
00236 int accept_token = -1;
00237 int accept_length = -1;
00238 int accept_pos = -1;
00239 int accept_line = -1;
00240
00241 int[][][] gotoTable = this.gotoTable[state.id()];
00242 int[] accept = this.accept[state.id()];
00243 text.setLength(0);
00244
00245 while(true)
00246 {
00247 int c = getChar();
00248
00249 if(c != -1)
00250 {
00251 switch(c)
00252 {
00253 case 10:
00254 if(cr)
00255 {
00256 cr = false;
00257 }
00258 else
00259 {
00260 line++;
00261 pos = 0;
00262 }
00263 break;
00264 case 13:
00265 line++;
00266 pos = 0;
00267 cr = true;
00268 break;
00269 default:
00270 pos++;
00271 cr = false;
00272 break;
00273 };
00274
00275 text.append((char) c);
00276
00277 do
00278 {
00279 int oldState = (dfa_state < -1) ? (-2 -dfa_state) : dfa_state;
00280
00281 dfa_state = -1;
00282
00283 int[][] tmp1 = gotoTable[oldState];
00284 int low = 0;
00285 int high = tmp1.length - 1;
00286
00287 while(low <= high)
00288 {
00289 int middle = (low + high) / 2;
00290 int[] tmp2 = tmp1[middle];
00291
00292 if(c < tmp2[0])
00293 {
00294 high = middle - 1;
00295 }
00296 else if(c > tmp2[1])
00297 {
00298 low = middle + 1;
00299 }
00300 else
00301 {
00302 dfa_state = tmp2[2];
00303 break;
00304 }
00305 }
00306 }while(dfa_state < -1);
00307 }
00308 else
00309 {
00310 dfa_state = -1;
00311 }
00312
00313 if(dfa_state >= 0)
00314 {
00315 if(accept[dfa_state] != -1)
00316 {
00317 accept_state = dfa_state;
00318 accept_token = accept[dfa_state];
00319 accept_length = text.length();
00320 accept_pos = pos;
00321 accept_line = line;
00322 }
00323 }
00324 else
00325 {
00326 if(accept_state != -1)
00327 {
00328 switch(accept_token)
00329 {
00330 case 0:
00331 {
00332 Token token = new0(
00333 getText(accept_length),
00334 start_line + 1,
00335 start_pos + 1);
00336 pushBack(accept_length);
00337 pos = accept_pos;
00338 line = accept_line;
00339 return token;
00340 }
00341 case 1:
00342 {
00343 Token token = new1(
00344 getText(accept_length),
00345 start_line + 1,
00346 start_pos + 1);
00347 pushBack(accept_length);
00348 pos = accept_pos;
00349 line = accept_line;
00350 return token;
00351 }
00352 case 2:
00353 {
00354 Token token = new2(
00355 getText(accept_length),
00356 start_line + 1,
00357 start_pos + 1);
00358 pushBack(accept_length);
00359 pos = accept_pos;
00360 line = accept_line;
00361 return token;
00362 }
00363 case 3:
00364 {
00365 Token token = new3(
00366 getText(accept_length),
00367 start_line + 1,
00368 start_pos + 1);
00369 pushBack(accept_length);
00370 pos = accept_pos;
00371 line = accept_line;
00372 return token;
00373 }
00374 case 4:
00375 {
00376 Token token = new4(
00377 start_line + 1,
00378 start_pos + 1);
00379 pushBack(accept_length);
00380 pos = accept_pos;
00381 line = accept_line;
00382 return token;
00383 }
00384 case 5:
00385 {
00386 Token token = new5(
00387 start_line + 1,
00388 start_pos + 1);
00389 pushBack(accept_length);
00390 pos = accept_pos;
00391 line = accept_line;
00392 return token;
00393 }
00394 case 6:
00395 {
00396 Token token = new6(
00397 start_line + 1,
00398 start_pos + 1);
00399 pushBack(accept_length);
00400 pos = accept_pos;
00401 line = accept_line;
00402 return token;
00403 }
00404 case 7:
00405 {
00406 Token token = new7(
00407 start_line + 1,
00408 start_pos + 1);
00409 pushBack(accept_length);
00410 pos = accept_pos;
00411 line = accept_line;
00412 return token;
00413 }
00414 case 8:
00415 {
00416 Token token = new8(
00417 start_line + 1,
00418 start_pos + 1);
00419 pushBack(accept_length);
00420 pos = accept_pos;
00421 line = accept_line;
00422 return token;
00423 }
00424 case 9:
00425 {
00426 Token token = new9(
00427 start_line + 1,
00428 start_pos + 1);
00429 pushBack(accept_length);
00430 pos = accept_pos;
00431 line = accept_line;
00432 return token;
00433 }
00434 case 10:
00435 {
00436 Token token = new10(
00437 start_line + 1,
00438 start_pos + 1);
00439 pushBack(accept_length);
00440 pos = accept_pos;
00441 line = accept_line;
00442 return token;
00443 }
00444 case 11:
00445 {
00446 Token token = new11(
00447 start_line + 1,
00448 start_pos + 1);
00449 pushBack(accept_length);
00450 pos = accept_pos;
00451 line = accept_line;
00452 return token;
00453 }
00454 case 12:
00455 {
00456 Token token = new12(
00457 start_line + 1,
00458 start_pos + 1);
00459 pushBack(accept_length);
00460 pos = accept_pos;
00461 line = accept_line;
00462 return token;
00463 }
00464 case 13:
00465 {
00466 Token token = new13(
00467 getText(accept_length),
00468 start_line + 1,
00469 start_pos + 1);
00470 pushBack(accept_length);
00471 pos = accept_pos;
00472 line = accept_line;
00473 return token;
00474 }
00475 }
00476 }
00477 else
00478 {
00479 if(text.length() > 0)
00480 {
00481 throw new LexerException(
00482 "[" + (start_line + 1) + "," + (start_pos + 1) + "]" +
00483 " Unknown token: " + text);
00484 }
00485 else
00486 {
00487 EOF token = new EOF(
00488 start_line + 1,
00489 start_pos + 1);
00490 return token;
00491 }
00492 }
00493 }
00494 }
00495 }
00496 Token new0(String text, int line, int pos) { return new TWhiteSpace(text, line, pos); }
00497 Token new1(String text, int line, int pos) { return new TTraditionalComment(text, line, pos); }
00498 Token new10(int line, int pos) { return new TSemicolon(line, pos); }
00499 Token new11(int line, int pos) { return new TComma(line, pos); }
00500 Token new12(int line, int pos) { return new TCls(line, pos); }
00501 Token new13(String text, int line, int pos) { return new TId(text, line, pos); }
00502 Token new2(String text, int line, int pos) { return new TDocumentationComment(text, line, pos); }
00503 Token new3(String text, int line, int pos) { return new TEndOfLineComment(text, line, pos); }
00504 Token new4(int line, int pos) { return new TLBrace(line, pos); }
00505 Token new5(int line, int pos) { return new TRBrace(line, pos); }
00506 Token new6(int line, int pos) { return new TLParen(line, pos); }
00507 Token new7(int line, int pos) { return new TRParen(line, pos); }
00508 Token new8(int line, int pos) { return new TDim(line, pos); }
00509 Token new9(int line, int pos) { return new TDot(line, pos); }
00510 public Token next() throws LexerException, IOException
00511 {
00512 while(token == null)
00513 {
00514 token = getToken();
00515 filter();
00516 }
00517
00518 Token result = token;
00519 token = null;
00520 return result;
00521 }
00522 public Token peek() throws LexerException, IOException
00523 {
00524 while(token == null)
00525 {
00526 token = getToken();
00527 filter();
00528 }
00529
00530 return token;
00531 }
00532 private void pushBack(int acceptLength) throws IOException
00533 {
00534 int length = text.length();
00535 for(int i = length - 1; i >= acceptLength; i--)
00536 {
00537 eof = false;
00538
00539 in.unread(text.charAt(i));
00540 }
00541 }
00542 protected void unread(Token token) throws IOException
00543 {
00544 String text = token.getText();
00545 int length = text.length();
00546
00547 for(int i = length - 1; i >= 0; i--)
00548 {
00549 eof = false;
00550
00551 in.unread(text.charAt(i));
00552 }
00553
00554 pos = token.getPos() - 1;
00555 line = token.getLine() - 1;
00556 }
00557 }