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 00039 package theoremprover; 00040 00041 import java.lang.*; // Java language classes 00042 import java.awt.*; // User Interface components 00043 import java.io.*; // IO specific classes (File, Stream,..) 00044 import java.awt.event.*; // Event handling 00045 00046 import theoremprover.*; 00047 00048 00049 public class GenericParser extends Object 00050 { 00051 00052 // Instance variables ... 00053 00054 protected GenericScanner scanner; 00055 protected String source; 00056 protected GenericToken actualToken; 00057 protected Object result; 00058 protected String errorMessage; 00059 00060 // Instance creation and initialization 00061 00062 GenericParser() 00063 { 00064 errorMessage = null; 00065 scanner = defaultScanner(); 00066 source = ""; 00067 actualToken = null; 00068 result = null; 00069 } 00070 00071 GenericParser(String str) 00072 { 00073 errorMessage = null; 00074 scanner = defaultScanner(); 00075 source = str; 00076 actualToken = null; 00077 result = null; 00078 } 00079 00080 public GenericScanner defaultScanner() 00081 { 00082 return new GenericScanner(); 00083 } 00084 00085 // Accessing 00086 00087 public Object result() 00088 { 00089 return result; 00090 } 00091 00092 public String errorMessage() 00093 { 00094 return errorMessage; 00095 } 00096 00097 // Parsing 00098 00099 public boolean parse(String str) 00100 // Returns true if the parser accepts the given string and 00101 // false otherwise 00102 { 00103 boolean noerror = true; 00104 errorMessage = null; 00105 result = defaultResult(); 00106 source = str; 00107 actualToken = scanner.scanSource(str); 00108 try { 00109 parse(); 00110 } catch (ParserErrorException e) { 00111 errorMessage = e.getMessage(); 00112 noerror = false; 00113 } 00114 return noerror; 00115 } 00116 00117 public Object defaultResult() 00118 { 00119 return null; 00120 } 00121 00122 public boolean parseFile (String file) 00123 { 00124 FileInputStream freader; 00125 String str; 00126 try{ 00127 freader = new FileInputStream(file); 00128 } catch (FileNotFoundException e) { 00129 return false; 00130 } 00131 try { 00132 byte[] chars = new byte[freader.available()]; 00133 freader.read(chars); 00134 freader.close(); 00135 str = new String(chars); 00136 } catch (IOException e){ 00137 return false; 00138 } 00139 return parse(str); 00140 } 00141 00142 00143 // Private parsing 00144 00145 public void parse () throws ParserErrorException {} 00146 00147 public void nextToken () throws ParserErrorException 00148 { 00149 if (actualToken.isEOI()) { 00150 // Do nothing after dedecting the end of the input 00151 } else { 00152 actualToken = scanner.scanToken(); 00153 } 00154 00155 if (actualToken.isErrorToken()) { 00156 throw new ParserErrorException( 00157 "Lexical Error at position " + 00158 Integer.toString(actualToken.position())); 00159 } 00160 } 00161 00162 public void errorDetected () throws ParserErrorException 00163 { 00164 throw new ParserErrorException( 00165 "Parser Error [ " + 00166 actualToken.value() + 00167 " ] at position " + 00168 Integer.toString(actualToken.position())); 00169 } 00170 00171 public void errorDetected (String str) throws ParserErrorException 00172 { 00173 throw new ParserErrorException( 00174 "Parser Error [ " + 00175 actualToken.value() + 00176 " ] at position " + 00177 Integer.toString(actualToken.position()) + 00178 " [ " + str + " ] "); 00179 } 00180 00181 }