1 /*=========================================================================
3 Module: $RCSfile: bbtkInterpreter.h,v $
5 Date: $Date: 2008/11/26 12:36:43 $
6 Version: $Revision: 1.36 $
7 =========================================================================*/
9 /* ---------------------------------------------------------------------
11 * Copyright (c) CREATIS-LRMN (Centre de Recherche en Imagerie Medicale)
12 * Authors : Eduardo Davila, Laurent Guigues, Jean-Pierre Roux
14 * This software is governed by the CeCILL-B license under French law and
15 * abiding by the rules of distribution of free software. You can use,
16 * modify and/ or redistribute the software under the terms of the CeCILL-B
17 * license as circulated by CEA, CNRS and INRIA at the following URL
18 * http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
19 * or in the file LICENSE.txt.
21 * As a counterpart to the access to the source code and rights to copy,
22 * modify and redistribute granted by the license, users are provided only
23 * with a limited warranty and the software's author, the holder of the
24 * economic rights, and the successive licensors have only limited
27 * The fact that you are presently reading this means that you have had
28 * knowledge of the CeCILL-B license and that you accept its terms.
29 * ------------------------------------------------------------------------ */
33 * \brief class Interpreter : The bbtk language interpreter
36 * \class bbtk::Interpreter
37 * \brief The bbtk language interpreter
40 #ifndef __bbtkInterpreter_h__
41 #define __bbtkInterpreter_h__
43 #include "bbtkVirtualExec.h"
44 #include "bbtkExecuter.h"
52 #ifdef _USE_WXWIDGETS_
56 //=======================================================================
57 class BBTK_EXPORT InterpreterUser
61 virtual ~InterpreterUser() {}
63 virtual bool InterpreterUserHasOwnHtmlPageViewer() { return false; }
64 virtual void InterpreterUserViewHtmlPage(const std::string&) {}
68 //=======================================================================
72 //=======================================================================
73 class BBTK_EXPORT InterpreterError : public Exception
76 InterpreterError( const std::string& message,
78 const std::string& script_file,
81 InterpreterError( const Exception& excep,
83 const std::string& script_file,
86 ~InterpreterError() throw() {}
88 bool IsInScriptFile() const { return mInScriptFile; }
89 const std::string& GetScriptFile() const { return mScriptFile; }
90 int GetScriptLine() const { return mScriptLine; }
93 std::string mScriptFile;
96 //=======================================================================
99 //=======================================================================
100 class BBTK_EXPORT Interpreter : public Object
102 BBTK_OBJECT_INTERFACE_NO_CONDES(Interpreter);
103 typedef Object Superclass;
105 static Pointer New(const std::string& cpp_file = "");
106 static Pointer New(VirtualExec::Pointer);
116 /// Runs the interpretation of a file
117 ExitStatus InterpretFile( const std::string& filename, bool source = false);
119 /// Runs the interpretation of a buffer and deletes it !
120 ExitStatus InterpretBuffer( std::stringstream* buffer );
122 /// Runs the interpretation of a command
123 ExitStatus InterpretLine( const std::string& line );
126 /// Launches a command line interpreter (with a prompt)
127 void CommandLineInterpreter();
131 /// Sets the inputs of the workspace :
132 /// the map is passed as is to the Executer
133 void SetInputs(const std::map<std::string,std::string>& m)
134 { mVirtualExecuter->SetInputs(m); }
136 /// Puts the executer in "no exec" mode,
137 /// which creates but does not execute pipelines.
138 void SetNoExecMode(bool m) { mVirtualExecuter->SetNoExecMode(m); }
140 /// Puts the executer in "no error" mode,
141 /// Errors do not stop execution (but warnings are produced)
142 void SetNoErrorMode(bool m) { mVirtualExecuter->SetNoErrorMode(m);}
144 //typedef Executer::DialogModeType DialogModeType;
145 typedef VirtualExec::DialogModeType DialogModeType;
147 void SetDialogMode(DialogModeType t) { mVirtualExecuter->SetDialogMode(t);}
149 /// Sets the bool that indicates wether we are in command line context
150 void SetCommandLine(bool v = true) { mCommandLine = v; }
152 void SetThrow(bool b) { mThrow = b; }
154 #ifdef _USE_WXWIDGETS_
155 /// Sets the user of the interpreter (if any)
156 void SetUser(InterpreterUser* c) { mUser = c; }
157 /// Gets the InterpreterUser of this
158 InterpreterUser* GetUser() { return mUser; }
159 /// Gets the InterpreterUser of this (const)
160 const InterpreterUser* GetUser() const { return mUser; }
164 /// Gets the Executer
165 VirtualExec::Pointer GetExecuter() const { return mVirtualExecuter; }
170 /// The enumeration of command codes == Command name
204 /// The structure storing the informations on a command
209 CommandCodeType code;
214 /// The type of dictionnary of commands
215 typedef std::map<std::string,CommandInfoType> CommandDictType;
218 /// Interprets a line
219 void InterpretLine( const std::string& line, bool &insideComment );
221 /// Reads a line from prompt
222 void GetLineFromPrompt( std::string& line );
224 /// Splits a line into words
225 void SplitLine ( const std::string& line,
226 std::vector<std::string>& words );
228 /// Executes the right action depending on the command name
229 void InterpretCommand( const std::vector<std::string>& words,
230 CommandInfoType& info );
232 /// Switch to the interpretation of a file
233 void SwitchToFile( const std::string& filename, bool source = false );
235 /// Switch to the interpretation of a stringstream
236 void SwitchToStream( std::stringstream* stream );
238 /// Closes the currently open file
239 void CloseCurrentFile();
241 /// Closes all open files
242 void CloseAllFiles();
247 /// Displays help (entry point of any help)
248 void Help(const std::vector<std::string>& words);
250 /// Displays help on all the commands
253 /// Displays help on a particular command
254 void HelpCommand( const std::string& command );
257 void Graph(const std::vector<std::string>& words);
262 // void Print(const std::string&);
264 void Index(const std::string& filename,
265 const std::string& type = "Initials");
267 void FindCommandsWithPrefix( char* buf,
269 std::vector<std::string>& commands );
271 /// Creates and connects the piece of pipeline which defines a GUI
273 /// Define it as a complex box type with name instanceName+"Type"
274 /// The instance is called instanceName
275 /// and connected to the existing pipeline
276 void NewGUI(const std::string& box,
277 const std::string& instanceName);
279 void Debug(const std::string& arg);
284 Interpreter(const std::string& cpp_file = "");
286 Interpreter(VirtualExec::Pointer);
288 void Init(VirtualExec::Pointer, const std::string& cpp_file);
290 /// Opens the file fullPathScriptName
291 /// includeScriptName is the name as given to the include command
292 void LoadScript( std::string fullPathScriptName,
293 std::string includeScriptName);
296 void CatchInterpreterException( const InterpreterError& e );
297 void CatchBbtkException( const bbtk::Exception& e );
298 void CatchStdException( const std::exception& e );
299 void CatchUnknownException();
303 //==================================================================
306 /// The command executer used
307 bbtk::VirtualExec::Pointer mVirtualExecuter;
308 bbtk::Executer::WeakPointer mRealExecuter;
310 /// The user of the interpreter (0 if none)
311 bbtk::InterpreterUser* mUser;
313 /// Vector of open files / buffers (can be stringstream)
314 std::vector<std::istream*> mFile;
316 /// Vector of names of open files with full path (as open)
317 std::vector<std::string> mFileName;
319 /// Vector of names of files which have been open
320 /// before (and may closed then which are no more in mFileName)
321 /// with full path (as open)
322 std::vector<std::string> mFileNameHistory;
324 /// Vector of names of open files as given to the include command
325 std::vector<std::string> mIncludeFileName;
327 /// Stores the current line number in each open file
328 std::vector<int> mLine;
330 /// The dictionnary of commands
331 CommandDictType mCommandDict;
333 /// Are we in a command line context ?
336 /// The history of commands
337 std::deque< char* > mHistory;
347 // EO class Interpreter
350 // The "Quit" exception
351 struct QuitException : public bbtk::Exception
353 QuitException() : bbtk::Exception("Interpreter","","Quit") {}