GenericScanner.java
Go to the documentation of this file.
00001 /*
00002  * (c) copyright 2008, Technische Universitaet Graz and Technische Universitaet Wien
00003  *
00004  * This file is part of jdiagengine.
00005  *
00006  * jdiagengine is free software: you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation, either version 3 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * jdiagengine is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  * You should have received a copy of the GNU General Public License
00016  * along with jdiagengine. If not, see <http://www.gnu.org/licenses/>.
00017  *
00018  * Authors: Joerg Weber, Franz Wotawa
00019  * Contact: jweber@ist.tugraz.at (preferred), or fwotawa@ist.tugraz.at
00020  *
00021  */
00022 
00023 
00036 package theoremprover;
00037 
00038 import java.lang.*;    // Java language classes 
00039 import java.awt.*;     // User Interface components
00040 import java.io.*;      // IO specific classes (File, Stream,..)
00041 import java.awt.event.*;   // Event handling
00042 
00043 import theoremprover.*;
00044 
00045 
00046 public class GenericScanner extends Object
00047 {
00048 
00049   // Instance variables ...
00050 
00051   protected String source;
00052   protected char nextChar;
00053   protected int position;
00054   protected StringBuffer charBuffer;
00055 
00056   // Instance creation and initialization
00057 
00058   public GenericScanner()
00059   {
00060     super();
00061     initialize();
00062   }
00063 
00064   public GenericScanner(String str)
00065   {
00066     super();
00067     initialize();
00068     source = str;
00069     getNextChar();
00070   }
00071 
00072   public static GenericScanner parseFile (String file)
00073   {
00074     GenericScanner gs;
00075  
00076     FileInputStream freader;
00077     try{
00078       freader = new FileInputStream(file);}
00079     catch (FileNotFoundException e) {
00080       return null;
00081     }
00082 
00083     try {
00084       byte[] chars = new byte[freader.available()];
00085       freader.read(chars);
00086       freader.close();
00087       String str = new String(chars);
00088       return new GenericScanner(str);}
00089     catch (IOException e){
00090       return null;
00091     }
00092   }
00093 
00094   public void initialize()
00095   {
00096     source = "";
00097     nextChar = endOfInput();
00098     position = 0;
00099     charBuffer = new StringBuffer();
00100   }
00101 
00102   // Accessing
00103 
00104   public String source()
00105   {
00106     return source;
00107   }
00108 
00109   public int position()
00110   {
00111     return position;
00112   }
00113 
00114 
00115   // Private methods
00116 
00117   public char endOfInput ()
00118   {
00119     return 0;
00120   }
00121 
00122   public boolean isEndOfInput ()
00123   {
00124     return (nextChar == endOfInput());
00125   }
00126 
00127   public boolean isDelimiter (char c)
00128   {
00129     return false; // This method should be overwritten by my subclasses..
00130   }
00131 
00132   public String bufferValue ()
00133   {
00134     return charBuffer.toString();
00135   }
00136 
00137   // Public methods
00138 
00139   public void initSource (String str)
00140   {
00141     source = str;
00142     position = 0;
00143     getNextChar();
00144   }
00145 
00146   public char getNextChar ()
00147   {
00148       if (position < source.length()) {
00149           nextChar = source.charAt(position);
00150           charBuffer.append(nextChar);
00151           position = position + 1; }
00152       else {
00153           charBuffer.append('\u0000');
00154           nextChar = endOfInput(); }
00155       return nextChar;
00156   }
00157 
00158   public GenericToken scanSource(String str)
00159   {
00160       initSource(str);
00161       return scanToken();
00162   }
00163 
00164   public void overReadSpaces()
00165   {
00166     if (isEndOfInput()) { return; }
00167     while (Character.isWhitespace(nextChar)) {
00168       getNextChar();
00169       if (isEndOfInput()) { return; }}
00170     charBuffer = new StringBuffer((new Character(nextChar)).toString());
00171   }
00172 
00173   public GenericToken scanToken ()
00174   {
00175     GenericToken actualToken;
00176     String str;
00177     
00178     overReadSpaces();
00179 
00180     // Ignore comments
00181 
00182     if (nextChar == '%') {
00183         while ((nextChar != '\n') && (nextChar != '\r')) {
00184             getNextChar();
00185             if (isEndOfInput()) { return new EOIToken("EOI",position); }
00186         }
00187         charBuffer = new StringBuffer();
00188         getNextChar();
00189         return scanToken();
00190     }
00191 
00192     // Classify tokens
00193 
00194     int oldPos = position;
00195 
00196     if (isEndOfInput()) {
00197       return new EOIToken("EOI",oldPos); }
00198 
00199     if (Character.isDigit(nextChar)) {
00200       return scanNumber(); }
00201 
00202     if (Character.isLetter(nextChar) || (nextChar == '!')) {
00203       return scanIdOrKeyword(); }
00204 
00205     if ((nextChar == ',') || (nextChar == '.') ||
00206         (nextChar == ')') || (nextChar == '(')) {
00207       str = bufferValue();
00208       actualToken = new DelimiterToken(str,oldPos);
00209       charBuffer = new StringBuffer();
00210       getNextChar();
00211       return actualToken;}
00212 
00213     if (nextChar == ':') {
00214       getNextChar();
00215       if (nextChar == '-') {
00216         actualToken = new DelimiterToken(bufferValue(),oldPos);
00217         charBuffer = new StringBuffer();
00218         getNextChar();
00219       }
00220       else {
00221         // Lexical Error detected
00222         actualToken = new ErrorToken(":- expected", position);
00223         charBuffer = new StringBuffer();
00224         getNextChar();
00225       }
00226       return actualToken; }
00227 
00228     if (nextChar == '-') {
00229       getNextChar();
00230       if (nextChar == '>') {
00231         actualToken = new DelimiterToken(bufferValue(),oldPos);
00232         charBuffer = new StringBuffer();
00233         getNextChar();
00234       }
00235       else {
00236         if (Character.isDigit(nextChar)) {
00237           actualToken = scanNumber();
00238         } else {
00239           // Lexical Error detected
00240           charBuffer = new StringBuffer();
00241           getNextChar();
00242           actualToken = new ErrorToken("-> or number expected", position);
00243         }
00244       }
00245       return actualToken; }
00246 
00247     if (nextChar == '"') {
00248       return scanString(); }
00249     
00250     if (nextChar == '\'') {
00251       return scanCharacter(); }
00252     
00253     // Lexical Error detected
00254     charBuffer = new StringBuffer();
00255     getNextChar();
00256     return new ErrorToken("No token recognized", position);
00257   }
00258 
00259   public GenericToken scanNumber ()
00260   {
00261     boolean floatFlag = false;
00262     String str;
00263 
00264     getNextChar();
00265     while (Character.isDigit(nextChar)) {
00266       getNextChar(); }
00267     if (nextChar == '.') {
00268       floatFlag = true;
00269       getNextChar();
00270       while (Character.isDigit(nextChar)) {
00271         getNextChar(); } }
00272     str = bufferValue().substring(0,bufferValue().length() - 1);
00273     charBuffer = new StringBuffer((new Character(nextChar)).toString());
00274     if (floatFlag) {
00275       return new FloatToken(str, position);
00276     } else {
00277       return new IntegerToken(str, position);
00278     }
00279   }
00280 
00281   public GenericToken scanIdOrKeyword ()
00282   {
00283     String str; 
00284 
00285     getNextChar();
00286     if (nextChar == '!') getNextChar();
00287     while (Character.isLetterOrDigit(nextChar) || (nextChar == '_')) {
00288       getNextChar(); }
00289     str = bufferValue().substring(0,bufferValue().length() - 1);
00290     charBuffer = new StringBuffer((new Character(nextChar)).toString());
00291     return new IdentifierToken(str, position);
00292   }
00293 
00294   public GenericToken scanString ()
00295   {
00296     String str;
00297 
00298     boolean flag = true;
00299     while (flag ) {
00300         getNextChar();
00301         if (nextChar == '\"') {
00302             getNextChar();
00303             if (nextChar != '\"') {
00304                 flag = false; 
00305             }
00306         }
00307         if (isEndOfInput()) {
00308             return new ErrorToken("eoi detected while scanning string", position);
00309         }
00310     }
00311   
00312     str = bufferValue().substring(1,bufferValue().length() - 2);
00313     charBuffer = new StringBuffer((new Character(nextChar)).toString());
00314     return new StringToken(str, position);
00315   }
00316 
00317   public GenericToken scanCharacter ()
00318   {
00319     getNextChar();
00320     char ch = nextChar;
00321     getNextChar();
00322     if (nextChar == '\'') {
00323       charBuffer = new StringBuffer();
00324       getNextChar();
00325       return new CharacterToken(new Character(ch).toString(),position);
00326     } else {
00327       // Lexical error detected
00328       charBuffer = new StringBuffer();
00329       getNextChar();
00330       return new ErrorToken("character token expected",position);
00331     }
00332   }
00333 
00334 }
00335 


tug_ist_diagnosis_engine
Author(s): Safdar Zaman, Gerald Steinbauer
autogenerated on Mon Jan 6 2014 11:51:16