1 /*=========================================================================
4 Module: $RCSfile: bbtkInterpreter.h,v $ $
6 Date: $Date: 2008/07/23 11:46:11 $
7 Version: $Revision: 1.32 $
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 b) { mVirtualExecuter->SetNoExecMode(b); }
128 //typedef Executer::DialogModeType DialogModeType;
129 typedef VirtualExec::DialogModeType DialogModeType;
131 void SetDialogMode(DialogModeType t)
132 { mVirtualExecuter->SetDialogMode(t); }
134 /// Sets the bool that indicates wether we are in command line context
135 void SetCommandLine(bool v = true) { mCommandLine = v; }
137 void SetThrow(bool b) { mThrow = b; }
139 #ifdef _USE_WXWIDGETS_
140 /// Sets the user of the interpreter (if any)
141 void SetUser(InterpreterUser* c) { mUser = c; }
142 /// Gets the InterpreterUser of this
143 InterpreterUser* GetUser() { return mUser; }
144 /// Gets the InterpreterUser of this (const)
145 const InterpreterUser* GetUser() const { return mUser; }
149 /// Gets the Executer
150 VirtualExec::Pointer GetExecuter() const { return mVirtualExecuter; }
155 /// The enumeration of command codes == Command name
189 /// The structure storing the informations on a command
194 CommandCodeType code;
199 /// The type of dictionnary of commands
200 typedef std::map<std::string,CommandInfoType> CommandDictType;
203 /// Interprets a line
204 void InterpretLine( const std::string& line, bool &insideComment );
206 /// Reads a line from prompt
207 void GetLineFromPrompt( std::string& line );
209 /// Splits a line into words
210 void SplitLine ( const std::string& line,
211 std::vector<std::string>& words );
213 /// Executes the right action depending on the command name
214 void InterpretCommand( const std::vector<std::string>& words,
215 CommandInfoType& info );
217 /// Switch to the interpretation of a file
218 void SwitchToFile( const std::string& filename );
220 /// Switch to the interpretation of a stringstream
221 void SwitchToStream( std::stringstream* stream );
223 /// Closes the currently open file
224 void CloseCurrentFile();
226 /// Closes all open files
227 void CloseAllFiles();
232 /// Displays help (entry point of any help)
233 void Help(const std::vector<std::string>& words);
235 /// Displays help on all the commands
238 /// Displays help on a particular command
239 void HelpCommand( const std::string& command );
242 void Graph(const std::vector<std::string>& words);
247 // void Print(const std::string&);
249 void Index(const std::string& filename,
250 const std::string& type = "Initials");
252 void FindCommandsWithPrefix( char* buf,
254 std::vector<std::string>& commands );
256 /// Creates and connects the piece of pipeline which defines a GUI
258 /// Define it as a complex box type with name instanceName+"Type"
259 /// The instance is called instanceName
260 /// and connected to the existing pipeline
261 void NewGUI(const std::string& box,
262 const std::string& instanceName);
264 void Debug(const std::string& arg);
269 Interpreter(const std::string& cpp_file = "");
271 Interpreter(VirtualExec::Pointer);
273 void Init(VirtualExec::Pointer, const std::string& cpp_file);
275 /// Opens the file fullPathScriptName
276 /// includeScriptName is the name as given to the include command
277 void LoadScript( std::string fullPathScriptName,
278 std::string includeScriptName);
281 void CatchBbtkException( const bbtk::Exception& e );
282 void CatchStdException( const std::exception& e );
283 void CatchUnknownException();
287 //==================================================================
290 /// The command executer used
291 bbtk::VirtualExec::Pointer mVirtualExecuter;
292 bbtk::Executer::WeakPointer mRealExecuter;
294 /// The user of the interpreter (0 if none)
295 bbtk::InterpreterUser* mUser;
297 /// Vector of open files / buffers (can be stringstream)
298 std::vector<std::istream*> mFile;
300 /// Vector of names of open files with full path (as open)
301 std::vector<std::string> mFileName;
303 /// Vector of names of files which have been open
304 /// before (and may closed then which are no more in mFileName)
305 /// with full path (as open)
306 std::vector<std::string> mFileNameHistory;
308 /// Vector of names of open files as given to the include command
309 std::vector<std::string> mIncludeFileName;
311 /// Stores the current line number in each open file
312 std::vector<int> mLine;
314 /// The dictionnary of commands
315 CommandDictType mCommandDict;
317 /// Are we in a command line context ?
320 /// The history of commands
321 std::deque< char* > mHistory;
331 // EO class Interpreter
334 // The "Quit" exception
335 struct QuitException : public bbtk::Exception
337 QuitException() : bbtk::Exception("Interpreter","","Quit") {}