1 /*=========================================================================
4 Module: $RCSfile: bbtkInterpreter.h,v $ $
6 Date: $Date: 2008/10/02 07:43:20 $
7 Version: $Revision: 1.33 $
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"
31 #include "bbtkExecuter.h"
39 #ifdef _USE_WXWIDGETS_
43 //=======================================================================
44 class BBTK_EXPORT InterpreterUser
48 virtual ~InterpreterUser() {}
50 virtual bool InterpreterUserHasOwnHtmlPageViewer() { return false; }
51 virtual void InterpreterUserViewHtmlPage(const std::string&) {}
55 //=======================================================================
59 //=======================================================================
60 class BBTK_EXPORT InterpreterError : public Exception
63 InterpreterError( const std::string& message,
65 const std::string& script_file,
68 InterpreterError( const Exception& excep,
70 const std::string& script_file,
73 ~InterpreterError() throw() {}
75 bool IsInScriptFile() const { return mInScriptFile; }
76 const std::string& GetScriptFile() const { return mScriptFile; }
77 int GetScriptLine() const { return mScriptLine; }
80 std::string mScriptFile;
83 //=======================================================================
86 //=======================================================================
87 class BBTK_EXPORT Interpreter : public Object
89 BBTK_OBJECT_INTERFACE_NO_CONDES(Interpreter);
90 typedef Object Superclass;
92 static Pointer New(const std::string& cpp_file = "");
93 static Pointer New(VirtualExec::Pointer);
103 /// Runs the interpretation of a file
104 ExitStatus InterpretFile( const std::string& filename);
106 /// Runs the interpretation of a buffer and deletes it !
107 ExitStatus InterpretBuffer( std::stringstream* buffer );
109 /// Runs the interpretation of a command
110 ExitStatus InterpretLine( const std::string& line );
113 /// Launches a command line interpreter (with a prompt)
114 void CommandLineInterpreter();
118 /// Sets the inputs of the workspace :
119 /// the map is passed as is to the Executer
120 void SetInputs(const std::map<std::string,std::string>& m)
121 { mVirtualExecuter->SetInputs(m); }
123 /// Puts the executer in "no exec" mode,
124 /// which creates but does not execute pipelines.
125 void SetNoExecMode(bool m) { mVirtualExecuter->SetNoExecMode(m); }
127 /// Puts the executer in "no error" mode,
128 /// Errors do not stop execution (but warnings are produced)
129 void SetNoErrorMode(bool m) { mVirtualExecuter->SetNoErrorMode(m);}
131 //typedef Executer::DialogModeType DialogModeType;
132 typedef VirtualExec::DialogModeType DialogModeType;
134 void SetDialogMode(DialogModeType t) { mVirtualExecuter->SetDialogMode(t);}
136 /// Sets the bool that indicates wether we are in command line context
137 void SetCommandLine(bool v = true) { mCommandLine = v; }
139 void SetThrow(bool b) { mThrow = b; }
141 #ifdef _USE_WXWIDGETS_
142 /// Sets the user of the interpreter (if any)
143 void SetUser(InterpreterUser* c) { mUser = c; }
144 /// Gets the InterpreterUser of this
145 InterpreterUser* GetUser() { return mUser; }
146 /// Gets the InterpreterUser of this (const)
147 const InterpreterUser* GetUser() const { return mUser; }
151 /// Gets the Executer
152 VirtualExec::Pointer GetExecuter() const { return mVirtualExecuter; }
157 /// The enumeration of command codes == Command name
191 /// The structure storing the informations on a command
196 CommandCodeType code;
201 /// The type of dictionnary of commands
202 typedef std::map<std::string,CommandInfoType> CommandDictType;
205 /// Interprets a line
206 void InterpretLine( const std::string& line, bool &insideComment );
208 /// Reads a line from prompt
209 void GetLineFromPrompt( std::string& line );
211 /// Splits a line into words
212 void SplitLine ( const std::string& line,
213 std::vector<std::string>& words );
215 /// Executes the right action depending on the command name
216 void InterpretCommand( const std::vector<std::string>& words,
217 CommandInfoType& info );
219 /// Switch to the interpretation of a file
220 void SwitchToFile( const std::string& filename );
222 /// Switch to the interpretation of a stringstream
223 void SwitchToStream( std::stringstream* stream );
225 /// Closes the currently open file
226 void CloseCurrentFile();
228 /// Closes all open files
229 void CloseAllFiles();
234 /// Displays help (entry point of any help)
235 void Help(const std::vector<std::string>& words);
237 /// Displays help on all the commands
240 /// Displays help on a particular command
241 void HelpCommand( const std::string& command );
244 void Graph(const std::vector<std::string>& words);
249 // void Print(const std::string&);
251 void Index(const std::string& filename,
252 const std::string& type = "Initials");
254 void FindCommandsWithPrefix( char* buf,
256 std::vector<std::string>& commands );
258 /// Creates and connects the piece of pipeline which defines a GUI
260 /// Define it as a complex box type with name instanceName+"Type"
261 /// The instance is called instanceName
262 /// and connected to the existing pipeline
263 void NewGUI(const std::string& box,
264 const std::string& instanceName);
266 void Debug(const std::string& arg);
271 Interpreter(const std::string& cpp_file = "");
273 Interpreter(VirtualExec::Pointer);
275 void Init(VirtualExec::Pointer, const std::string& cpp_file);
277 /// Opens the file fullPathScriptName
278 /// includeScriptName is the name as given to the include command
279 void LoadScript( std::string fullPathScriptName,
280 std::string includeScriptName);
283 void CatchBbtkException( const bbtk::Exception& e );
284 void CatchStdException( const std::exception& e );
285 void CatchUnknownException();
289 //==================================================================
292 /// The command executer used
293 bbtk::VirtualExec::Pointer mVirtualExecuter;
294 bbtk::Executer::WeakPointer mRealExecuter;
296 /// The user of the interpreter (0 if none)
297 bbtk::InterpreterUser* mUser;
299 /// Vector of open files / buffers (can be stringstream)
300 std::vector<std::istream*> mFile;
302 /// Vector of names of open files with full path (as open)
303 std::vector<std::string> mFileName;
305 /// Vector of names of files which have been open
306 /// before (and may closed then which are no more in mFileName)
307 /// with full path (as open)
308 std::vector<std::string> mFileNameHistory;
310 /// Vector of names of open files as given to the include command
311 std::vector<std::string> mIncludeFileName;
313 /// Stores the current line number in each open file
314 std::vector<int> mLine;
316 /// The dictionnary of commands
317 CommandDictType mCommandDict;
319 /// Are we in a command line context ?
322 /// The history of commands
323 std::deque< char* > mHistory;
333 // EO class Interpreter
336 // The "Quit" exception
337 struct QuitException : public bbtk::Exception
339 QuitException() : bbtk::Exception("Interpreter","","Quit") {}