1 /*=========================================================================
4 Module: $RCSfile: bbtkInterpreter.h,v $ $
6 Date: $Date: 2008/04/22 09:40:11 $
7 Version: $Revision: 1.28 $
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
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(Interpreter);
90 typedef Object Superclass;
92 static Pointer New(const std::string& cpp_file = "");
102 /// Runs the interpretation of a file
103 ExitStatus InterpretFile( const std::string& filename);
105 /// Runs the interpretation of a buffer and deletes it !
106 ExitStatus InterpretBuffer( std::stringstream* buffer );
108 /// Runs the interpretation of a command
109 ExitStatus InterpretLine( const std::string& line );
112 /// Launches a command line interpreter (with a prompt)
113 void CommandLineInterpreter();
117 /// Sets the inputs of the workspace :
118 /// the map is passed as is to the Executer
119 void SetInputs(const std::map<std::string,std::string>& m)
120 { mVirtualExecuter->SetInputs(m); }
122 /// Puts the executer in "no exec" mode,
123 /// which creates but does not execute pipelines.
124 void SetNoExecMode(bool b) { mVirtualExecuter->SetNoExecMode(b); }
127 //typedef Executer::DialogModeType DialogModeType;
128 typedef VirtualExec::DialogModeType DialogModeType;
130 void SetDialogMode(DialogModeType t)
131 { mVirtualExecuter->SetDialogMode(t); }
133 /// Sets the bool that indicates wether we are in command line context
134 void SetCommandLine(bool v = true) { mCommandLine = v; }
136 void SetThrow(bool b) { mThrow = b; }
138 #ifdef _USE_WXWIDGETS_
139 /// Sets the user of the interpreter (if any)
140 void SetUser(InterpreterUser* c) { mUser = c; }
141 /// Gets the InterpreterUser of this
142 InterpreterUser* GetUser() { return mUser; }
143 /// Gets the InterpreterUser of this (const)
144 const InterpreterUser* GetUser() const { return mUser; }
148 /// Gets the Executer
149 VirtualExec::Pointer GetExecuter() const { return mVirtualExecuter; }
154 /// The enumeration of command codes == Command name
188 /// The structure storing the informations on a command
193 CommandCodeType code;
198 /// The type of dictionnary of commands
199 typedef std::map<std::string,CommandInfoType> CommandDictType;
202 /// Interprets a line
203 void InterpretLine( const std::string& line, bool &insideComment );
205 /// Reads a line from prompt
206 void GetLineFromPrompt( std::string& line );
208 /// Splits a line into words
209 void SplitLine ( const std::string& line,
210 std::vector<std::string>& words );
212 /// Executes the right action depending on the command name
213 void InterpretCommand( const std::vector<std::string>& words,
214 CommandInfoType& info );
216 /// Switch to the interpretation of a file
217 void SwitchToFile( const std::string& filename );
219 /// Switch to the interpretation of a stringstream
220 void SwitchToStream( std::stringstream* stream );
222 /// Closes the currently open file
223 void CloseCurrentFile();
225 /// Closes all open files
226 void CloseAllFiles();
231 /// Displays help (entry point of any help)
232 void Help(const std::vector<std::string>& words);
234 /// Displays help on all the commands
237 /// Displays help on a particular command
238 void HelpCommand( const std::string& command );
241 void Graph(const std::vector<std::string>& words);
246 // void Print(const std::string&);
248 void Index(const std::string& filename,
249 const std::string& type = "Initials");
251 void FindCommandsWithPrefix( char* buf,
253 std::vector<std::string>& commands );
255 /// Creates and connects the piece of pipeline which defines a GUI
257 /// Define it as a complex box type with name instanceName+"Type"
258 /// The instance is called instanceName
259 /// and connected to the existing pipeline
260 void NewGUI(const std::string& box,
261 const std::string& instanceName);
263 void ObjectInfo(const std::string& name);
268 Interpreter(const std::string& cpp_file = "");
270 /// Opens the file fullPathScriptName
271 /// includeScriptName is the name as given to the include command
272 void LoadScript( std::string fullPathScriptName,
273 std::string includeScriptName);
276 void CatchBbtkException( const bbtk::Exception& e );
277 void CatchStdException( const std::exception& e );
278 void CatchUnknownException();
282 //==================================================================
285 /// The command executer used
286 bbtk::VirtualExec::Pointer mVirtualExecuter;
287 bbtk::Executer::WeakPointer mRealExecuter;
289 /// The user of the interpreter (0 if none)
290 bbtk::InterpreterUser* mUser;
292 /// Vector of open files / buffers (can be stringstream)
293 std::vector<std::istream*> mFile;
295 /// Vector of names of open files with full path (as open)
296 std::vector<std::string> mFileName;
298 /// Vector of names of files which have been open
299 /// before (and may closed then which are no more in mFileName)
300 /// with full path (as open)
301 std::vector<std::string> mFileNameHistory;
303 /// Vector of names of open files as given to the include command
304 std::vector<std::string> mIncludeFileName;
306 /// Stores the current line number in each open file
307 std::vector<int> mLine;
309 /// The dictionnary of commands
310 CommandDictType mCommandDict;
312 /// Are we in a command line context ?
315 /// The history of commands
316 std::deque< char* > mHistory;
323 // EO class Interpreter
326 // The "Quit" exception
327 struct QuitException : public bbtk::Exception
329 QuitException() : bbtk::Exception("Interpreter","","Quit") {}