1 /*=========================================================================
4 Module: $RCSfile: bbtkInterpreter.h,v $ $
6 Date: $Date: 2008/03/26 14:47:36 $
7 Version: $Revision: 1.23 $
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 class BBTK_EXPORT InterpreterUser
48 virtual bool InterpreterUserHasOwnHtmlPageViewer() { return false; }
49 virtual void InterpreterUserViewHtmlPage(const std::string&) {}
55 class BBTK_EXPORT InterpreterError : public Exception
58 InterpreterError( const std::string& message,
60 const std::string& script_file,
63 InterpreterError( const Exception& excep,
65 const std::string& script_file,
68 ~InterpreterError() throw() {}
70 bool IsInScriptFile() const { return mInScriptFile; }
71 const std::string& GetScriptFile() const { return mScriptFile; }
72 int GetScriptLine() const { return mScriptLine; }
75 std::string mScriptFile;
81 class BBTK_EXPORT Interpreter
86 /// The enumeration of command codes == Command name
117 /// The structure storing the informations on a command
120 std::string category;
122 CommandCodeType code;
127 /// The type of dictionnary of commands
128 typedef std::map<std::string,CommandInfoType> CommandDictType;
132 Interpreter(const std::string& cpp_file = "");
146 /// Runs the interpretation of a file
147 ExitStatus InterpretFile( const std::string& filename);
149 /// Runs the interpretation of a buffer and deletes it !
150 ExitStatus InterpretBuffer( std::stringstream* buffer );
152 /// Runs the interpretation of a command
153 ExitStatus InterpretLine( const std::string& line );
156 /// Launches a command line interpreter (with a prompt)
157 void CommandLineInterpreter();
161 /// Sets the inputs of the workspace :
162 /// the map is passed as is to the Executer
163 void SetInputs(const std::map<std::string,std::string>& m)
164 { mExecuter->SetInputs(m); }
166 /// Puts the executer in "no exec" mode,
167 /// which creates but does not execute pipelines.
168 void SetNoExecMode(bool b) { mExecuter->SetNoExecMode(b); }
171 //typedef Executer::DialogModeType DialogModeType;
172 typedef VirtualExec::DialogModeType DialogModeType;
174 void SetDialogMode(DialogModeType t) { mExecuter->SetDialogMode(t); }
176 /// Sets the bool that indicates wether we are in command line context
177 void SetCommandLine(bool v = true) { mCommandLine = v; }
179 void SetThrow(bool b) { mThrow = b; }
181 #ifdef _USE_WXWIDGETS_
182 /// Sets the user of the interpreter (if any)
183 void SetUser(InterpreterUser* c) { mUser = c; }
184 /// Gets the InterpreterUser of this
185 InterpreterUser* GetUser() { return mUser; }
186 /// Gets the InterpreterUser of this (const)
187 const InterpreterUser* GetUser() const { return mUser; }
191 /// Gets the Executer
192 VirtualExec* GetExecuter() { return mExecuter; }
193 /// Gets the Executer (const)
194 const VirtualExec* GetExecuter() const { return mExecuter; }
198 /// Interprets a line
199 void InterpretLine( const std::string& line, bool &insideComment );
201 /// Reads a line from prompt
202 void GetLineFromPrompt( std::string& line );
204 /// Splits a line into words
205 void SplitLine ( const std::string& line,
206 std::vector<std::string>& words );
208 /// Executes the right action depending on the command name
209 void InterpretCommand( const std::vector<std::string>& words,
210 CommandInfoType& info );
212 /// Switch to the interpretation of a file
213 void SwitchToFile( const std::string& filename );
215 /// Switch to the interpretation of a stringstream
216 void SwitchToStream( std::stringstream* stream );
218 /// Closes the currently open file
219 void CloseCurrentFile();
221 /// Closes all open files
222 void CloseAllFiles();
224 /// Displays help (entry point of any help)
225 void Help(const std::vector<std::string>& words);
227 /// Displays help on all the commands
230 /// Displays help on a particular command
231 void HelpCommand( const std::string& command );
234 void Graph(const std::vector<std::string>& words);
239 // void Print(const std::string&);
241 void Index(const std::string& filename,
242 const std::string& type = "Initials");
244 void FindCommandsWithPrefix( char* buf,
246 std::vector<std::string>& commands );
252 /// Opens the file fullPathScriptName
253 /// includeScriptName is the name as given to the include command
254 void LoadScript( std::string fullPathScriptName,
255 std::string includeScriptName);
258 void CatchBbtkException( const bbtk::Exception& e );
259 void CatchStdException( const std::exception& e );
260 void CatchUnknownException();
264 //==================================================================
267 /// The command executer used
268 bbtk::VirtualExec* mExecuter;
270 /// The user of the interpreter (0 if none)
271 bbtk::InterpreterUser* mUser;
273 /// Vector of open files / buffers (can be stringstream)
274 std::vector<std::istream*> mFile;
276 /// Vector of names of open files with full path (as open)
277 std::vector<std::string> mFileName;
279 /// Vector of names of open files as given to the include command
280 std::vector<std::string> mIncludeFileName;
282 /// Stores the current line number in each open file
283 std::vector<int> mLine;
285 /// The dictionnary of commands
286 CommandDictType mCommandDict;
288 /// Are we in a command line context ?
291 /// The history of commands
292 std::deque< char* > mHistory;
299 // EO class Interpreter
302 // The "Quit" exception
303 struct QuitException : public bbtk::Exception
305 QuitException() : bbtk::Exception("Interpreter","","Quit") {}