00001 // Torc - Copyright 2011 University of Southern California. All Rights Reserved. 00002 // $HeadURL: https://torc-isi.svn.sourceforge.net/svnroot/torc-isi/branches/staging/0.9/src/torc/generic/parser/Driver.hpp $ 00003 // $Id: Driver.hpp 10 2011-10-12 18:40:16Z nsteiner $ 00004 00005 // This program is free software: you can redistribute it and/or modify it under the terms of the 00006 // GNU General Public License as published by the Free Software Foundation, either version 3 of the 00007 // License, or (at your option) any later version. 00008 // 00009 // This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 00010 // without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See 00011 // the GNU General Public License for more details. 00012 // 00013 // You should have received a copy of the GNU General Public License along with this program. If 00014 // not, see <http://www.gnu.org/licenses/>. 00015 00016 // FILE : driver.hpp 00017 // DATE : 08-July-2010 00018 // DESCRIPTION : Declaration of the torc::generic::Driver class 00019 // REVISION HISTORY: 00020 // SI REVISION AUTHOR CHANGES PRs 00021 // [0] Initial Version Niladri 00022 // [1] First Version Santanu 00023 00024 #ifndef TORC_GENERIC_PARSER_DRIVER_HPP 00025 #define TORC_GENERIC_PARSER_DRIVER_HPP 00026 00027 #include <string> 00028 #include "torc/generic/om/PointerTypes.hpp" 00029 #include "torc/generic/parser/ParserPointerTypes.hpp" 00030 #include "torc/generic/util/Error.hpp" 00031 00032 //BOOST 00033 #include <boost/shared_ptr.hpp> 00034 00035 // forward declaration 00036 namespace torc{ namespace generic { class EdifContext; } } 00037 namespace torc{ namespace generic { class Scanner; } } 00038 00039 namespace torc { 00040 namespace generic { 00041 00042 /** The Driver class brings together all components. It creates an instance of 00043 * the Parser and Scanner classes and connects them. Then the input stream is 00044 * fed into the scanner object and the parser gets it's token 00045 * sequence. Furthermore the driver object is available in the grammar rules as 00046 * a parameter. Therefore the driver class contains a reference to the 00047 * structure into which the parsed data is saved. */ 00048 class Driver { 00049 public: 00050 /** Invoke the scanner and parser for a stream. 00051 * @param inStream input stream 00052 * @param inStreamName stream name for error messages 00053 * @return true if successfully parsed 00054 */ 00055 bool 00056 parseStream(std::istream& inStream, 00057 const std::string& inStreamName = "stream input"); 00058 00059 /** Invoke the scanner and parser on an input string. 00060 * @param inString input string 00061 * @param inStreamName stream name for error messages 00062 * @return true if successfully parsed 00063 */ 00064 bool 00065 parseString(const std::string& inString, 00066 const std::string& inStreamName = "string stream"); 00067 00068 /** Invoke the scanner and parser on a file. Use parseStream with a 00069 * std::ifstream if detection of file reading errors is required. 00070 * @param inFileName input file name 00071 * @return true if successfully parsed 00072 */ 00073 bool 00074 parseFile(const std::string& inFileName); 00075 00076 // To demonstrate pure handling of parse errors, instead of 00077 // simply dumping them on the standard error output, we will pass 00078 // them to the driver using the following two member functions. 00079 00080 /** Error handling with associated line number. This can be modified to 00081 * output the error e.g. to a dialog box. */ 00082 void 00083 error(const class location& inLocation, const std::string& inMessage); 00084 00085 /** General error handling. This can be modified to output the error 00086 * e.g. to a dialog box. */ 00087 void error(const std::string& inMessage); 00088 00089 inline Scanner * 00090 getLexer() const throw(); 00091 00092 //Note: Let the address be returned .. this is used by the parser 00093 inline std::string & 00094 getStreamName() throw(); 00095 00096 inline EdifContextSharedPtr 00097 getContext() const throw(); 00098 00099 inline Error 00100 getParserError() const throw(); 00101 00102 inline bool 00103 getIsParserErrorSet() const throw(); 00104 00105 void 00106 setParserError( 00107 const Error & inSource) throw(); 00108 00109 /// construct a new parser driver context 00110 Driver(const EdifContextSharedPtr &inEdifCntx); 00111 00112 ~Driver() throw(); 00113 00114 private: 00115 bool mTraceScanning; 00116 bool mTraceParsing; 00117 std::string mStreamName; 00118 00119 /** Pointer to the current lexer instance, this is used to connect the 00120 * parser to the scanner. It is used in the yylex macro. */ 00121 Scanner* mLexer; 00122 00123 /** Reference to the Edif context filled during parsing of the 00124 * Edif file. */ 00125 EdifContextSharedPtr mEdifCntx; 00126 00127 /** Reference to the Error object */ 00128 ErrorSharedPtr mErrorObj; 00129 }; 00130 00131 inline Scanner * 00132 Driver::getLexer() const throw() 00133 { 00134 return mLexer; 00135 } 00136 00137 inline std::string & 00138 Driver::getStreamName() throw() { 00139 return mStreamName; 00140 } 00141 00142 inline EdifContextSharedPtr 00143 Driver::getContext() const throw() { 00144 return mEdifCntx; 00145 } 00146 00147 inline Error 00148 Driver::getParserError() const throw() { 00149 return *mErrorObj; 00150 } 00151 00152 inline bool 00153 Driver::getIsParserErrorSet() const throw() { 00154 return mErrorObj; 00155 } 00156 00157 } // namespace generic 00158 } // namespace torc 00159 00160 #endif // TORC_GENERIC_PARSER_DRIVER_HPP