1 /*=========================================================================
4 Module: $RCSfile: bbtkInterpreter.h,v $ $
6 Date: $Date: 2008/04/22 08:29:09 $
7 Version: $Revision: 1.27 $
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 //=======================================================================
43 class BBTK_EXPORT InterpreterUser
49 virtual bool InterpreterUserHasOwnHtmlPageViewer() { return false; }
50 virtual void InterpreterUserViewHtmlPage(const std::string&) {}
54 //=======================================================================
58 //=======================================================================
59 class BBTK_EXPORT InterpreterError : public Exception
62 InterpreterError( const std::string& message,
64 const std::string& script_file,
67 InterpreterError( const Exception& excep,
69 const std::string& script_file,
72 ~InterpreterError() throw() {}
74 bool IsInScriptFile() const { return mInScriptFile; }
75 const std::string& GetScriptFile() const { return mScriptFile; }
76 int GetScriptLine() const { return mScriptLine; }
79 std::string mScriptFile;
82 //=======================================================================
85 //=======================================================================
86 class BBTK_EXPORT Interpreter : public Object
88 BBTK_OBJECT_INTERFACE(Interpreter);
89 typedef Object Superclass;
91 static Pointer New(const std::string& cpp_file = "");
101 /// Runs the interpretation of a file
102 ExitStatus InterpretFile( const std::string& filename);
104 /// Runs the interpretation of a buffer and deletes it !
105 ExitStatus InterpretBuffer( std::stringstream* buffer );
107 /// Runs the interpretation of a command
108 ExitStatus InterpretLine( const std::string& line );
111 /// Launches a command line interpreter (with a prompt)
112 void CommandLineInterpreter();
116 /// Sets the inputs of the workspace :
117 /// the map is passed as is to the Executer
118 void SetInputs(const std::map<std::string,std::string>& m)
119 { mExecuter->SetInputs(m); }
121 /// Puts the executer in "no exec" mode,
122 /// which creates but does not execute pipelines.
123 void SetNoExecMode(bool b) { mExecuter->SetNoExecMode(b); }
126 //typedef Executer::DialogModeType DialogModeType;
127 typedef VirtualExec::DialogModeType DialogModeType;
129 void SetDialogMode(DialogModeType t) { mExecuter->SetDialogMode(t); }
131 /// Sets the bool that indicates wether we are in command line context
132 void SetCommandLine(bool v = true) { mCommandLine = v; }
134 void SetThrow(bool b) { mThrow = b; }
136 #ifdef _USE_WXWIDGETS_
137 /// Sets the user of the interpreter (if any)
138 void SetUser(InterpreterUser* c) { mUser = c; }
139 /// Gets the InterpreterUser of this
140 InterpreterUser* GetUser() { return mUser; }
141 /// Gets the InterpreterUser of this (const)
142 const InterpreterUser* GetUser() const { return mUser; }
146 /// Gets the Executer
147 VirtualExec::Pointer GetExecuter() const { return mExecuter; }
152 /// The enumeration of command codes == Command name
186 /// The structure storing the informations on a command
191 CommandCodeType code;
196 /// The type of dictionnary of commands
197 typedef std::map<std::string,CommandInfoType> CommandDictType;
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();
229 /// Displays help (entry point of any help)
230 void Help(const std::vector<std::string>& words);
232 /// Displays help on all the commands
235 /// Displays help on a particular command
236 void HelpCommand( const std::string& command );
239 void Graph(const std::vector<std::string>& words);
244 // void Print(const std::string&);
246 void Index(const std::string& filename,
247 const std::string& type = "Initials");
249 void FindCommandsWithPrefix( char* buf,
251 std::vector<std::string>& commands );
253 /// Creates and connects the piece of pipeline which defines a GUI
255 /// Define it as a complex box type with name instanceName+"Type"
256 /// The instance is called instanceName
257 /// and connected to the existing pipeline
258 void NewGUI(const std::string& box,
259 const std::string& instanceName);
261 void ObjectInfo(const std::string& name);
266 Interpreter(const std::string& cpp_file = "");
268 /// Opens the file fullPathScriptName
269 /// includeScriptName is the name as given to the include command
270 void LoadScript( std::string fullPathScriptName,
271 std::string includeScriptName);
274 void CatchBbtkException( const bbtk::Exception& e );
275 void CatchStdException( const std::exception& e );
276 void CatchUnknownException();
280 //==================================================================
283 /// The command executer used
284 bbtk::VirtualExec::Pointer mExecuter;
286 /// The user of the interpreter (0 if none)
287 bbtk::InterpreterUser* mUser;
289 /// Vector of open files / buffers (can be stringstream)
290 std::vector<std::istream*> mFile;
292 /// Vector of names of open files with full path (as open)
293 std::vector<std::string> mFileName;
295 /// Vector of names of files which have been open
296 /// before (and may closed then which are no more in mFileName)
297 /// with full path (as open)
298 std::vector<std::string> mFileNameHistory;
300 /// Vector of names of open files as given to the include command
301 std::vector<std::string> mIncludeFileName;
303 /// Stores the current line number in each open file
304 std::vector<int> mLine;
306 /// The dictionnary of commands
307 CommandDictType mCommandDict;
309 /// Are we in a command line context ?
312 /// The history of commands
313 std::deque< char* > mHistory;
320 // EO class Interpreter
323 // The "Quit" exception
324 struct QuitException : public bbtk::Exception
326 QuitException() : bbtk::Exception("Interpreter","","Quit") {}