2 # ---------------------------------------------------------------------
4 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
6 # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
7 # Previous Authors : Laurent Guigues, Jean-Pierre Roux
8 # CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
10 # This software is governed by the CeCILL-B license under French law and
11 # abiding by the rules of distribution of free software. You can use,
12 # modify and/ or redistribute the software under the terms of the CeCILL-B
13 # license as circulated by CEA, CNRS and INRIA at the following URL
14 # http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
15 # or in the file LICENSE.txt.
17 # As a counterpart to the access to the source code and rights to copy,
18 # modify and redistribute granted by the license, users are provided only
19 # with a limited warranty and the software's author, the holder of the
20 # economic rights, and the successive licensors have only limited
23 # The fact that you are presently reading this means that you have had
24 # knowledge of the CeCILL-B license and that you accept its terms.
25 # ------------------------------------------------------------------------ */
28 /*=========================================================================
30 Module: $RCSfile: bbtkInterpreter.h,v $
32 Date: $Date: 2012/11/16 08:49:01 $
33 Version: $Revision: 1.46 $
34 =========================================================================*/
40 * \brief class Interpreter : The bbtk language interpreter
43 * \class bbtk::Interpreter
44 * \brief The bbtk language interpreter
47 #ifndef __bbtkInterpreter_h__
48 #define __bbtkInterpreter_h__
50 #include "bbtkVirtualExec.h"
51 #include "bbtkExecuter.h"
52 #include "bbtkInterpreterVirtual.h"
57 // Signal/slot mechanism for 'break' commands
58 //#include <boost/signal.hpp>
59 //#include <boost/bind.hpp>
69 //=======================================================================
70 class BBTK_EXPORT InterpreterUser
74 virtual ~InterpreterUser() {}
76 virtual bool InterpreterUserHasOwnHtmlPageViewer() { return false; }
77 virtual void InterpreterUserViewHtmlPage(const std::string&) {}
79 //=======================================================================
84 //=======================================================================
85 class BBTK_EXPORT InterpreterException : public Exception
88 InterpreterException( const std::string& message,
90 const std::string& script_file,
93 InterpreterException( const Exception& excep,
95 const std::string& script_file,
98 ~InterpreterException() throw() {}
100 bool IsInScriptFile() const { return mInScriptFile; }
101 const std::string& GetScriptFile() const { return mScriptFile; }
102 int GetScriptLine() const { return mScriptLine; }
105 std::string mScriptFile;
108 //=======================================================================
114 //=======================================================================
115 // The "Quit" exception
116 class BBTK_EXPORT QuitException : public InterpreterError
119 QuitException( bool in_script_file,
120 const std::string& script_file,
123 : InterpreterError("QUIT",in_script_file,script_file,script_line)
125 ~QuitException() throw() {}
127 //=======================================================================
128 const std::string BREAK("BREAK");
129 //=======================================================================
130 // The "Break" exception
131 class BBTK_EXPORT BreakException : public InterpreterError
134 BreakException( bool in_script_file,
135 std::string script_file,
138 : InterpreterError(BREAK,in_script_file,script_file,script_line)
139 { std::cout << "BUILDING BREAK"<<std::endl; }
140 ~BreakException() throw() {}
142 //=======================================================================
145 //=======================================================================
146 class BBTK_EXPORT Interpreter : public InterpreterVirtual
148 BBTK_OBJECT_INTERFACE_NO_CONDES(Interpreter);
149 typedef Object InterpreterVirtual;
151 static Pointer New(const std::string& cpp_file = "");
152 static Pointer New(VirtualExec::Pointer);
164 /// Runs the interpretation of a file
165 ExitStatus InterpretFile( const std::string& filename, bool source = false);
167 /// Runs the interpretation of a buffer and deletes it !
168 ExitStatus InterpretBuffer( std::stringstream* buffer );
170 /// Runs the interpretation of a command
171 ExitStatus InterpretLine( const std::string& line );
173 /// Runs the interpretation of the currently open streams
174 ExitStatus InterpretCurrentStreams();
176 /// Launches a command line interpreter (with a prompt)
177 void CommandLineInterpreter();
181 /// Sets the inputs of the workspace :
182 /// the map is passed as is to the Executer
183 void SetInputs(const std::map<std::string,std::string>& m)
184 { mVirtualExecuter->SetInputs(m); }
186 /// Puts the executer in "no exec" mode,
187 /// which creates but does not execute pipelines.
188 void SetNoExecMode(bool m) { mVirtualExecuter->SetNoExecMode(m); }
190 /// Puts the executer in "no error" mode,
191 /// Errors do not stop execution (but warnings are produced)
192 void SetNoErrorMode(bool m) { mVirtualExecuter->SetNoErrorMode(m);}
194 //typedef Executer::DialogModeType DialogModeType;
195 typedef VirtualExec::DialogModeType DialogModeType;
197 void SetDialogMode(DialogModeType t) { mVirtualExecuter->SetDialogMode(t);}
200 /// Sets the bool that indicates wether we are in command line context
201 void SetCommandLine(bool v = true) { mCommandLine = v; }
203 void SetThrow(bool b) { mThrow = b; }
206 /// Sets the user of the interpreter (if any)
207 void SetUser(InterpreterUser* c) { mUser = c; }
208 /// Gets the InterpreterUser of this
209 InterpreterUser* GetUser() { return mUser; }
210 /// Gets the InterpreterUser of this (const)
211 const InterpreterUser* GetUser() const { return mUser; }
216 /// Gets the Executer
217 VirtualExec::Pointer GetExecuter() const { return mVirtualExecuter; }
220 // For 'break' commands observation
221 typedef boost::signals::trackable BreakObserverType;
222 typedef boost::signal<void ()> BreakSignalType;
223 typedef BreakSignalType::slot_function_type BreakCallbackType;
225 // Adds a callback when 'break' command issued
226 void AddBreakObserver( BreakCallbackType );
232 /// The enumeration of command codes == Command name
235 cBreak, // LG 12/12/08 : Stops the current script execution (if not exec frozen) - used in tutorial + debugging
236 cClear, // LG 12/12/08 : Clears the current complex black box (e.g. workspace) - used in tours
268 /// The structure storing the informations on a command
273 CommandCodeType code;
278 /// The type of dictionnary of commands
279 typedef std::map<std::string,CommandInfoType> CommandDictType;
282 /// Interprets a line
283 void DoInterpretLine( const std::string& line ); //, bool &insideComment );
285 /// Reads a line from prompt
286 void GetLineFromPrompt( std::string& line );
288 /// Splits a line into words
289 void SplitLine ( const std::string& line,
290 std::vector<std::string>& words );
292 /// Executes the right action depending on the command name
293 void InterpretCommand( const std::vector<std::string>& words,
294 CommandInfoType& info );
296 /// Switch to the interpretation of a file
297 void SwitchToFile( const std::string& filename, bool source = false );
299 /// Switch to the interpretation of a stringstream
300 void SwitchToStream( std::stringstream* stream );
302 /// Closes the currently open file
303 void CloseCurrentFile();
305 /// Closes all open files
306 void CloseAllFiles();
310 virtual void commandReset();
313 /// Displays help (entry point of any help)
314 virtual void commandHelp(const std::vector<std::string>& words);
316 /// Displays help on all the commands
320 /// Displays help on a particular command
321 virtual void commandHelp( const std::string& command );
324 virtual void commandGraph(const std::vector<std::string>& words);
327 virtual void commandConfig() const;
329 // void Print(const std::string&);
331 virtual void commandIndex(const std::string& filename,
332 const std::string& type = "Initials");
336 void FindCommandsWithPrefix( char* buf,
338 std::vector<std::string>& commands );
341 /// Creates and connects the piece of pipeline which defines a GUI
343 /// Define it as a complex box type with name instanceName+"Type"
344 /// The instance is called instanceName
345 /// and connected to the existing pipeline
346 virtual void commandNewGUI(const std::string& box,const std::string& instanceName);
347 virtual void commandDebug(const std::string& arg);
348 virtual void commandNew(const std::string &boxType,const std::string &boxName);
349 virtual void commandDelete(const std::string &boxName);
350 virtual void commandConnection(const std::string &nodeFrom,const std::string &outputLabel,const std::string &nodeTo,const std::string &inputLabel);
351 virtual void commandPackage(const std::string &packageName);
352 virtual void commandEndPackage();
353 virtual void commandDefine(const std::string &name,const std::string &pack,const std::string &scriptfilename);
354 virtual void commandEndDefine();
355 virtual void commandKind(const std::string &kind);
356 virtual void commandPrint(const std::string &value);
357 virtual void commandExec(const std::string &word);
358 virtual void commandInput(const std::string &name,const std::string &box,const std::string &input,const std::string &help);
359 virtual void commandOutput(const std::string &name,const std::string &box,const std::string &output,const std::string &help);
360 virtual void commandSet(const std::string &box,const std::string &input,const std::string &value);
361 virtual void commandAuthor(const std::string &author);
362 virtual void commandCategory(const std::string &categorytype);
363 virtual void commandDescription(const std::string &description);
364 virtual void commandClear();
365 virtual void commandInclude(const std::string &word, bool ok);
366 virtual void commandLoad(const std::string &packageName);
367 virtual void commandUnload(const std::string &packageName);
368 virtual void commandBreak();
369 virtual void commandQuit();
370 virtual void commandMessage();
371 virtual void commandMessage(const std::string &kind,const std::string &levelstr);
372 virtual void SetCurrentFileName(const std::string &fullPathScriptName);
373 virtual void SetTypeOfScript_Application();
378 Interpreter(const std::string& cpp_file = "");
379 Interpreter(VirtualExec::Pointer);
384 void Init(VirtualExec::Pointer, const std::string& cpp_file);
387 /// Opens the file fullPathScriptName
388 /// includeScriptName is the name as given to the include command
389 void LoadScript( std::string fullPathScriptName,
390 std::string includeScriptName);
392 //==================================================================
395 /// The command executer used
396 bbtk::VirtualExec::Pointer mVirtualExecuter;
397 bbtk::Executer::WeakPointer mRealExecuter;
400 /// The user of the interpreter (0 if none)
401 bbtk::InterpreterUser* mUser;
402 /// Vector of open files / buffers (can be stringstream)
403 std::vector<std::istream*> mFile;
405 /// Vector of names of open files with full path (as open)
406 std::vector<std::string> mFileName;
408 /// Vector of names of files which have been open
409 /// before (and may closed then which are no more in mFileName)
410 /// with full path (as open)
411 std::vector<std::string> mFileNameHistory;
413 /// Vector of names of open files as given to the include command
414 std::vector<std::string> mIncludeFileName;
416 /// Stores the current line number in each open file
417 std::vector<int> mLine;
419 /// The dictionnary of commands
420 CommandDictType mCommandDict;
422 /// Are we in a command line context ?
425 /// Are we inside a commented-out zone ?
428 /// The current interpreter status
431 /// The history of commands
432 std::deque< char* > mHistory;
439 // BreakSignalType mBreakSignal;
447 virtual void CatchInterpreterException( const InterpreterException& e );
448 virtual void CatchBbtkException( const bbtk::Exception& e );
449 virtual void CatchStdException( const std::exception& e );
450 virtual void CatchUnknownException();
454 // EO class Interpreter