1 /*=========================================================================
4 Module: $RCSfile: bbtkInterpreter.h,v $ $
6 Date: $Date: 2008/03/21 14:59:39 $
7 Version: $Revision: 1.20 $
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"
32 #include "bbtkTranscriptor.h"
40 #ifdef _USE_WXWIDGETS_
44 class BBTK_EXPORT InterpreterUser
50 virtual bool InterpreterUserHasOwnHtmlPageViewer() { return false; }
51 virtual void InterpreterUserViewHtmlPage(const std::string&) {}
57 class BBTK_EXPORT InterpreterError : public Exception
60 InterpreterError( const std::string& message,
62 const std::string& script_file,
65 InterpreterError( const Exception& excep,
67 const std::string& script_file,
70 ~InterpreterError() throw() {}
72 bool IsInScriptFile() const { return mInScriptFile; }
73 const std::string& GetScriptFile() const { return mScriptFile; }
74 int GetScriptLine() const { return mScriptLine; }
77 std::string mScriptFile;
83 class BBTK_EXPORT Interpreter
88 /// The enumeration of command codes == Command name
119 /// The structure storing the informations on a command
122 std::string category;
124 CommandCodeType code;
129 /// The type of dictionnary of commands
130 typedef std::map<std::string,CommandInfoType> CommandDictType;
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; }
185 #ifdef _USE_WXWIDGETS_
186 /// Sets the user of the interpreter (if any)
187 void SetUser(InterpreterUser* c) { mUser = c; }
188 /// Gets the InterpreterUser of this
189 InterpreterUser* GetUser() { return mUser; }
190 /// Gets the InterpreterUser of this (const)
191 const InterpreterUser* GetUser() const { return mUser; }
195 /// Gets the Executer
196 VirtualExec* GetExecuter() { return mExecuter; }
197 /// Gets the Executer (const)
198 const VirtualExec* GetExecuter() const { return mExecuter; }
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();
228 /// Displays help (entry point of any help)
229 void Help(const std::vector<std::string>& words);
231 /// Displays help on all the commands
234 /// Displays help on a particular command
235 void HelpCommand( const std::string& command );
238 void Graph(const std::vector<std::string>& words);
243 void Print(const std::string&);
245 void Index(const std::string& filename,
246 const std::string& type = "Initials");
248 void FindCommandsWithPrefix( char* buf,
250 std::vector<std::string>& commands );
256 /// Opens the file fullPathScriptName
257 /// includeScriptName is the name as given to the include command
258 void LoadScript( std::string fullPathScriptName,
259 std::string includeScriptName);
262 void CatchBbtkException( const bbtk::Exception& e );
263 void CatchStdException( const std::exception& e );
264 void CatchUnknownException();
268 //==================================================================
271 /// The command executer used
272 bbtk::VirtualExec* mExecuter;
274 /// The user of the interpreter (0 if none)
275 bbtk::InterpreterUser* mUser;
277 /// Vector of open files / buffers (can be stringstream)
278 std::vector<std::istream*> mFile;
280 /// Vector of names of open files with full path (as open)
281 std::vector<std::string> mFileName;
283 /// Vector of names of open files as given to the include command
284 std::vector<std::string> mIncludeFileName;
286 /// Stores the current line number in each open file
287 std::vector<int> mLine;
289 /// The dictionnary of commands
290 CommandDictType mCommandDict;
292 /// Are we in a command line context ?
295 /// The history of commands
296 std::deque< char* > mHistory;
303 // EO class Interpreter
306 // The "Quit" exception
307 struct QuitException : public bbtk::Exception
309 QuitException() : bbtk::Exception("Interpreter","","Quit") {}