torc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Driver.hpp
Go to the documentation of this file.
1 // Torc - Copyright 2011-2013 University of Southern California. All Rights Reserved.
2 // $HeadURL$
3 // $Id$
4 
5 // This program is free software: you can redistribute it and/or modify it under the terms of the
6 // GNU General Public License as published by the Free Software Foundation, either version 3 of the
7 // License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
10 // without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
11 // the GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License along with this program. If
14 // not, see <http://www.gnu.org/licenses/>.
15 
16 // FILE : driver.hpp
17 // DATE : 08-July-2010
18 // DESCRIPTION : Declaration of the torc::generic::Driver class
19 // REVISION HISTORY:
20 // SI REVISION AUTHOR CHANGES PRs
21 // [0] Initial Version Niladri
22 // [1] First Version Santanu
23 
24 #ifndef TORC_GENERIC_EDIF_DRIVER_HPP
25 #define TORC_GENERIC_EDIF_DRIVER_HPP
26 
27 #include <string>
30 #include "torc/generic/Error.hpp"
31 
32 //BOOST
33 #include <boost/shared_ptr.hpp>
34 
35 // forward declaration
36 namespace torc { namespace generic { class EdifContext; } }
37 namespace torc { namespace generic { class Scanner; } }
38 
39 namespace torc {
40 namespace generic {
41 
42 /** The Driver class brings together all components. It creates an instance of
43  * the Parser and Scanner classes and connects them. Then the input stream is
44  * fed into the scanner object and the parser gets it's token
45  * sequence. Furthermore the driver object is available in the grammar rules as
46  * a parameter. Therefore the driver class contains a reference to the
47  * structure into which the parsed data is saved. */
48 class Driver {
49 public:
50  /** Invoke the scanner and parser for a stream.
51  * @param inStream input stream
52  * @param inStreamName stream name for error messages
53  * @return true if successfully parsed
54  */
55  bool parseStream(std::istream& inStream, const std::string& inStreamName = "stream input");
56 
57  /** Invoke the scanner and parser on an input string.
58  * @param inString input string
59  * @param inStreamName stream name for error messages
60  * @return true if successfully parsed
61  */
62  bool parseString(const std::string& inString, const std::string& inStreamName
63  = "string stream");
64 
65  /** Invoke the scanner and parser on a file. Use parseStream with a
66  * std::ifstream if detection of file reading errors is required.
67  * @param inFileName input file name
68  * @return true if successfully parsed
69  */
70  bool parseFile(const std::string& inFileName);
71 
72  // To demonstrate pure handling of parse errors, instead of
73  // simply dumping them on the standard error output, we will pass
74  // them to the driver using the following two member functions.
75 
76  /** Error handling with associated line number. This can be modified to
77  * output the error e.g. to a dialog box. */
78  void error(const class location& inLocation, const std::string& inMessage);
79 
80  /** General error handling. This can be modified to output the error
81  * e.g. to a dialog box. */
82  void error(const std::string& inMessage);
83 
84  inline Scanner* getLexer() const;
85 
86  //Note: Let the address be returned .. this is used by the parser
87  inline std::string& getStreamName();
88 
89  inline EdifContextSharedPtr getContext() const;
90 
91  inline Error getParserError() const;
92 
93  inline bool getIsParserErrorSet() const;
94 
95  void setParserError(const Error& inSource);
96 
97  /// construct a new parser driver context
98  Driver(const EdifContextSharedPtr& inEdifCntx);
99 
100  ~Driver() throw ();
101 
102 private:
106 
107  /** Pointer to the current lexer instance, this is used to connect the
108  * parser to the scanner. It is used in the yylex macro. */
110 
111  /** Reference to the Edif context filled during parsing of the
112  * Edif file. */
114 
115  /** Reference to the Error object */
117 };
118 
119 inline Scanner* Driver::getLexer() const {
120  return mLexer;
121 }
122 
124  return mStreamName;
125 }
126 
128  return mEdifCntx;
129 }
130 
132  return *mErrorObj;
133 }
134 
135 inline bool Driver::getIsParserErrorSet() const {
136  return mErrorObj;
137 }
138 
139 } // namespace generic
140 } // namespace torc
141 
142 #endif // TORC_GENERIC_EDIF_DRIVER_HPP
boost::shared_ptr< EdifContext > EdifContextSharedPtr
Driver(const EdifContextSharedPtr &inEdifCntx)
construct a new parser driver context
Definition: Driver.cpp:47
ErrorSharedPtr mErrorObj
Definition: Driver.hpp:116
boost::shared_ptr< Error > ErrorSharedPtr
std::string string
The Error object thrown by different methods of EdifOM.
Definition: Error.hpp:41
Error getParserError() const
Definition: Driver.hpp:131
std::string mStreamName
Definition: Driver.hpp:105
EdifContextSharedPtr mEdifCntx
Definition: Driver.hpp:113
bool parseStream(std::istream &inStream, const std::string &inStreamName="stream input")
Definition: Driver.cpp:52
bool parseFile(const std::string &inFileName)
Definition: Driver.cpp:64
EdifContextSharedPtr getContext() const
Definition: Driver.hpp:127
Scanner * getLexer() const
Definition: Driver.hpp:119
void error(const class location &inLocation, const std::string &inMessage)
void setParserError(const Error &inSource)
Definition: Driver.cpp:85
std::string & getStreamName()
Definition: Driver.hpp:123
bool parseString(const std::string &inString, const std::string &inStreamName="string stream")
Definition: Driver.cpp:72
bool getIsParserErrorSet() const
Definition: Driver.hpp:135