Main Page   Packages   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members  

Lexer.java

00001 package edu.ksu.cis.bandera.abstraction.options.lexer;
00002 
00003 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
00004  * Bandera, a Java(TM) analysis and transformation toolkit           *
00005  * Copyright (C) 1998-2001 SAnToS Laboratories (santos@cis.ksu.edu)  *
00006 
00007  * All rights reserved.                                              *
00008  *                                                                   *
00009  * This work was done as a project in the SAnToS Laboratory,         *
00010  * Department of Computing and Information Sciences, Kansas State    *
00011  * University, USA (http://www.cis.ksu.edu/santos).                  *
00012  * It is understood that any modification not identified as such is  *
00013  * not covered by the preceding statement.                           *
00014  *                                                                   *
00015  * This work is free software; you can redistribute it and/or        *
00016  * modify it under the terms of the GNU Library General Public       *
00017  * License as published by the Free Software Foundation; either      *
00018  * version 2 of the License, or (at your option) any later version.  *
00019  *                                                                   *
00020  * This work is distributed in the hope that it will be useful,      *
00021  * but WITHOUT ANY WARRANTY; without even the implied warranty of    *
00022  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU *
00023  * Library General Public License for more details.                  *
00024  *                                                                   *
00025  * You should have received a copy of the GNU Library General Public *
00026  * License along with this toolkit; if not, write to the             *
00027  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,      *
00028  * Boston, MA  02111-1307, USA.                                      *
00029  *                                                                   *
00030  * Java is a trademark of Sun Microsystems, Inc.                     *
00031  *                                                                   *
00032  * To submit a bug report, send a comment, or get the latest news on *
00033  * this project and other SAnToS projects, please visit the web-site *
00034  *                http://www.cis.ksu.edu/santos                      *
00035  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
00036 /* This file was generated by SableCC (http://www.sable.mcgill.ca/sablecc/). */
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         { // INITIAL
00057             {{9, 9, 1}, {10, 10, 2}, {12, 12, 3}, {13, 13, 4}, {32, 32, 5}, {36, 36, 6}, {40, 40, 7}, {41, 41, 8}, {44, 44, 9}, {46, 46, 10}, {47, 47, 11}, {59, 59, 12}, {65, 90, 13}, {91, 91, 14}, {95, 95, 15}, {97, 98, 13}, {99, 99, 16}, {100, 122, 13}, {123, 123, 17}, {125, 125, 18}, },
00058             {{9, 32, -2}, },
00059             {{9, 32, -2}, },
00060             {{9, 32, -2}, },
00061             {{9, 9, 1}, {10, 10, 19}, {12, 32, -2}, },
00062             {{9, 32, -2}, },
00063             {{36, 36, 20}, {48, 57, 21}, {65, 90, 22}, {95, 95, 23}, {97, 122, 22}, },
00064             {},
00065             {},
00066             {},
00067             {},
00068             {{42, 42, 24}, {47, 47, 25}, },
00069             {},
00070             {{36, 122, -8}, },
00071             {{93, 93, 26}, },
00072             {{36, 122, -8}, },
00073             {{36, 95, -8}, {97, 107, 22}, {108, 108, 27}, {109, 122, 22}, },
00074             {},
00075             {},
00076             {{9, 32, -2}, },
00077             {{36, 122, -8}, },
00078             {{36, 122, -8}, },
00079             {{36, 122, -8}, },
00080             {{36, 122, -8}, },
00081             {{0, 9, 28}, {10, 10, 29}, {11, 12, 28}, {13, 13, 30}, {14, 41, 28}, {42, 42, 31}, {43, 255, 28}, },
00082             {{0, 9, 32}, {10, 10, 33}, {11, 12, 32}, {13, 13, 34}, {14, 255, 32}, },
00083             {},
00084             {{36, 95, -8}, {97, 97, 35}, {98, 122, 22}, },
00085             {{0, 41, -26}, {42, 42, 36}, {43, 255, 28}, },
00086             {{0, 255, -30}, },
00087             {{0, 9, 28}, {10, 10, 37}, {11, 255, -30}, },
00088             {{0, 9, 38}, {10, 10, 39}, {11, 12, 38}, {13, 13, 40}, {14, 41, 38}, {42, 42, 41}, {43, 46, 38}, {47, 47, 42}, {48, 255, 38}, },
00089             {{0, 255, -27}, },
00090             {},
00091             {{10, 10, 43}, },
00092             {{36, 95, -8}, {97, 114, 22}, {115, 115, 44}, {116, 122, 22}, },
00093             {{0, 9, 45}, {10, 10, 46}, {11, 12, 45}, {13, 13, 47}, {14, 41, 45}, {42, 42, 36}, {43, 46, 45}, {47, 47, 48}, {48, 255, 45}, },
00094             {{0, 255, -30}, },
00095             {{0, 9, 49}, {10, 10, 50}, {11, 12, 49}, {13, 13, 51}, {14, 41, 49}, {42, 42, 52}, {43, 255, 49}, },
00096             {{0, 255, -40}, },
00097             {{0, 9, 49}, {10, 10, 53}, {11, 255, -40}, },
00098             {{0, 255, -33}, },
00099             {},
00100             {},
00101             {{36, 114, -37}, {115, 115, 54}, {116, 122, 22}, },
00102             {{0, 9, 55}, {10, 10, 56}, {11, 12, 55}, {13, 13, 57}, {14, 41, 55}, {42, 42, 58}, {43, 255, 55}, },
00103             {{0, 255, -47}, },
00104             {{0, 9, 55}, {10, 10, 59}, {11, 255, -47}, },
00105             {},
00106             {{0, 255, -40}, },
00107             {{0, 255, -40}, },
00108             {{0, 9, 49}, {10, 10, 60}, {11, 255, -40}, },
00109             {{0, 41, -33}, {42, 42, 52}, {43, 255, -33}, },
00110             {{0, 255, -40}, },
00111             {{36, 122, -8}, },
00112             {{0, 255, -47}, },
00113             {{0, 255, -47}, },
00114             {{0, 9, 55}, {10, 10, 61}, {11, 255, -47}, },
00115             {{0, 41, -38}, {42, 42, 58}, {43, 255, -38}, },
00116             {{0, 255, -47}, },
00117             {{0, 255, -40}, },
00118             {{0, 255, -47}, },
00119         }
00120     };*/
00121 
00122     private static int[][] accept;
00123 /*  {
00124         // INITIAL
00125         {0, 0, 0, 0, 0, 0, 13, 6, 7, 11, 9, -1, 10, 13, -1, 13, 13, 4, 5, 0, 13, 13, 13, 13, -1, 3, 8, 13, -1, -1, -1, -1, 3, 3, 3, 13, -1, -1, -1, -1, -1, -1, 2, 3, 13, -1, -1, -1, 1, -1, -1, -1, -1, -1, 12, -1, -1, -1, -1, -1, -1, -1, },
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                 // read gotoTable
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                 // read accept
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 }

Generated at Thu Feb 7 06:49:16 2002 for Bandera by doxygen1.2.10 written by Dimitri van Heesch, © 1997-2001