1 /*=========================================================================
4 Module: $RCSfile: bbtkInterpreter.h,v $ $
6 Date: $Date: 2008/04/08 06:59:30 $
7 Version: $Revision: 1.24 $
9 Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
10 l'Image). All rights reserved. See Doc/License.txt or
11 http://www.creatis.insa-lyon.fr/Public/bbtk/License.html for details.
13 This software is distributed WITHOUT ANY WARRANTY; without even
14 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 PURPOSE. See the above copyright notices for more information.
17 =========================================================================*/
20 * \brief class Interpreter : The bbtk language interpreter
23 * \class bbtk::Interpreter
24 * \brief The bbtk language interpreter
27 #ifndef __bbtkInterpreter_h__
28 #define __bbtkInterpreter_h__
30 #include "bbtkVirtualExec.h"
38 #ifdef _USE_WXWIDGETS_
42 class BBTK_EXPORT InterpreterUser
48 virtual bool InterpreterUserHasOwnHtmlPageViewer() { return false; }
49 virtual void InterpreterUserViewHtmlPage(const std::string&) {}
55 class BBTK_EXPORT InterpreterError : public Exception
58 InterpreterError( const std::string& message,
60 const std::string& script_file,
63 InterpreterError( const Exception& excep,
65 const std::string& script_file,
68 ~InterpreterError() throw() {}
70 bool IsInScriptFile() const { return mInScriptFile; }
71 const std::string& GetScriptFile() const { return mScriptFile; }
72 int GetScriptLine() const { return mScriptLine; }
75 std::string mScriptFile;
81 class BBTK_EXPORT Interpreter
86 /// The enumeration of command codes == Command name
119 /// The structure storing the informations on a command
124 CommandCodeType code;
129 /// The type of dictionnary of commands
130 typedef std::map<std::string,CommandInfoType> CommandDictType;
134 Interpreter(const std::string& cpp_file = "");
148 /// Runs the interpretation of a file
149 ExitStatus InterpretFile( const std::string& filename);
151 /// Runs the interpretation of a buffer and deletes it !
152 ExitStatus InterpretBuffer( std::stringstream* buffer );
154 /// Runs the interpretation of a command
155 ExitStatus InterpretLine( const std::string& line );
158 /// Launches a command line interpreter (with a prompt)
159 void CommandLineInterpreter();
163 /// Sets the inputs of the workspace :
164 /// the map is passed as is to the Executer
165 void SetInputs(const std::map<std::string,std::string>& m)
166 { mExecuter->SetInputs(m); }
168 /// Puts the executer in "no exec" mode,
169 /// which creates but does not execute pipelines.
170 void SetNoExecMode(bool b) { mExecuter->SetNoExecMode(b); }
173 //typedef Executer::DialogModeType DialogModeType;
174 typedef VirtualExec::DialogModeType DialogModeType;
176 void SetDialogMode(DialogModeType t) { mExecuter->SetDialogMode(t); }
178 /// Sets the bool that indicates wether we are in command line context
179 void SetCommandLine(bool v = true) { mCommandLine = v; }
181 void SetThrow(bool b) { mThrow = b; }
183 #ifdef _USE_WXWIDGETS_
184 /// Sets the user of the interpreter (if any)
185 void SetUser(InterpreterUser* c) { mUser = c; }
186 /// Gets the InterpreterUser of this
187 InterpreterUser* GetUser() { return mUser; }
188 /// Gets the InterpreterUser of this (const)
189 const InterpreterUser* GetUser() const { return mUser; }
193 /// Gets the Executer
194 VirtualExec* GetExecuter() { return mExecuter; }
195 /// Gets the Executer (const)
196 const VirtualExec* GetExecuter() const { return mExecuter; }
200 /// Interprets a line
201 void InterpretLine( const std::string& line, bool &insideComment );
203 /// Reads a line from prompt
204 void GetLineFromPrompt( std::string& line );
206 /// Splits a line into words
207 void SplitLine ( const std::string& line,
208 std::vector<std::string>& words );
210 /// Executes the right action depending on the command name
211 void InterpretCommand( const std::vector<std::string>& words,
212 CommandInfoType& info );
214 /// Switch to the interpretation of a file
215 void SwitchToFile( const std::string& filename );
217 /// Switch to the interpretation of a stringstream
218 void SwitchToStream( std::stringstream* stream );
220 /// Closes the currently open file
221 void CloseCurrentFile();
223 /// Closes all open files
224 void CloseAllFiles();
226 /// Displays help (entry point of any help)
227 void Help(const std::vector<std::string>& words);
229 /// Displays help on all the commands
232 /// Displays help on a particular command
233 void HelpCommand( const std::string& command );
236 void Graph(const std::vector<std::string>& words);
241 // void Print(const std::string&);
243 void Index(const std::string& filename,
244 const std::string& type = "Initials");
246 void FindCommandsWithPrefix( char* buf,
248 std::vector<std::string>& commands );
250 /// Creates and connects the piece of pipeline which defines a GUI
252 /// Define it as a complex box type with name instanceName+"Type"
253 /// The instance is called instanceName
254 /// and connected to the existing pipeline
255 void NewGUI(const std::string& box,
256 const std::string& instanceName);
260 /// Opens the file fullPathScriptName
261 /// includeScriptName is the name as given to the include command
262 void LoadScript( std::string fullPathScriptName,
263 std::string includeScriptName);
266 void CatchBbtkException( const bbtk::Exception& e );
267 void CatchStdException( const std::exception& e );
268 void CatchUnknownException();
272 //==================================================================
275 /// The command executer used
276 bbtk::VirtualExec* mExecuter;
278 /// The user of the interpreter (0 if none)
279 bbtk::InterpreterUser* mUser;
281 /// Vector of open files / buffers (can be stringstream)
282 std::vector<std::istream*> mFile;
284 /// Vector of names of open files with full path (as open)
285 std::vector<std::string> mFileName;
287 /// Vector of names of open files as given to the include command
288 std::vector<std::string> mIncludeFileName;
290 /// Stores the current line number in each open file
291 std::vector<int> mLine;
293 /// The dictionnary of commands
294 CommandDictType mCommandDict;
296 /// Are we in a command line context ?
299 /// The history of commands
300 std::deque< char* > mHistory;
307 // EO class Interpreter
310 // The "Quit" exception
311 struct QuitException : public bbtk::Exception
313 QuitException() : bbtk::Exception("Interpreter","","Quit") {}