1 /*=========================================================================
3 Module: $RCSfile: bbtkInterpreter.h,v $
5 Date: $Date: 2010/09/18 22:31:40 $
6 Version: $Revision: 1.44 $
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"
45 #include "bbtkInterpreterVirtual.h"
50 // Signal/slot mechanism for 'break' commands
51 //#include <boost/signal.hpp>
52 //#include <boost/bind.hpp>
62 //=======================================================================
63 class BBTK_EXPORT InterpreterUser
67 virtual ~InterpreterUser() {}
69 virtual bool InterpreterUserHasOwnHtmlPageViewer() { return false; }
70 virtual void InterpreterUserViewHtmlPage(const std::string&) {}
72 //=======================================================================
77 //=======================================================================
78 class BBTK_EXPORT InterpreterException : public Exception
81 InterpreterException( const std::string& message,
83 const std::string& script_file,
86 InterpreterException( const Exception& excep,
88 const std::string& script_file,
91 ~InterpreterException() throw() {}
93 bool IsInScriptFile() const { return mInScriptFile; }
94 const std::string& GetScriptFile() const { return mScriptFile; }
95 int GetScriptLine() const { return mScriptLine; }
98 std::string mScriptFile;
101 //=======================================================================
107 //=======================================================================
108 // The "Quit" exception
109 class BBTK_EXPORT QuitException : public InterpreterError
112 QuitException( bool in_script_file,
113 const std::string& script_file,
116 : InterpreterError("QUIT",in_script_file,script_file,script_line)
118 ~QuitException() throw() {}
120 //=======================================================================
121 const std::string BREAK("BREAK");
122 //=======================================================================
123 // The "Break" exception
124 class BBTK_EXPORT BreakException : public InterpreterError
127 BreakException( bool in_script_file,
128 std::string script_file,
131 : InterpreterError(BREAK,in_script_file,script_file,script_line)
132 { std::cout << "BUILDING BREAK"<<std::endl; }
133 ~BreakException() throw() {}
135 //=======================================================================
138 //=======================================================================
139 class BBTK_EXPORT Interpreter : public InterpreterVirtual
141 BBTK_OBJECT_INTERFACE_NO_CONDES(Interpreter);
142 typedef Object InterpreterVirtual;
144 static Pointer New(const std::string& cpp_file = "");
145 static Pointer New(VirtualExec::Pointer);
157 /// Runs the interpretation of a file
158 ExitStatus InterpretFile( const std::string& filename, bool source = false);
160 /// Runs the interpretation of a buffer and deletes it !
161 ExitStatus InterpretBuffer( std::stringstream* buffer );
163 /// Runs the interpretation of a command
164 ExitStatus InterpretLine( const std::string& line );
166 /// Runs the interpretation of the currently open streams
167 ExitStatus InterpretCurrentStreams();
169 /// Launches a command line interpreter (with a prompt)
170 void CommandLineInterpreter();
174 /// Sets the inputs of the workspace :
175 /// the map is passed as is to the Executer
176 void SetInputs(const std::map<std::string,std::string>& m)
177 { mVirtualExecuter->SetInputs(m); }
179 /// Puts the executer in "no exec" mode,
180 /// which creates but does not execute pipelines.
181 void SetNoExecMode(bool m) { mVirtualExecuter->SetNoExecMode(m); }
183 /// Puts the executer in "no error" mode,
184 /// Errors do not stop execution (but warnings are produced)
185 void SetNoErrorMode(bool m) { mVirtualExecuter->SetNoErrorMode(m);}
187 //typedef Executer::DialogModeType DialogModeType;
188 typedef VirtualExec::DialogModeType DialogModeType;
190 void SetDialogMode(DialogModeType t) { mVirtualExecuter->SetDialogMode(t);}
193 /// Sets the bool that indicates wether we are in command line context
194 void SetCommandLine(bool v = true) { mCommandLine = v; }
196 void SetThrow(bool b) { mThrow = b; }
199 /// Sets the user of the interpreter (if any)
200 void SetUser(InterpreterUser* c) { mUser = c; }
201 /// Gets the InterpreterUser of this
202 InterpreterUser* GetUser() { return mUser; }
203 /// Gets the InterpreterUser of this (const)
204 const InterpreterUser* GetUser() const { return mUser; }
209 /// Gets the Executer
210 VirtualExec::Pointer GetExecuter() const { return mVirtualExecuter; }
213 // For 'break' commands observation
214 typedef boost::signals::trackable BreakObserverType;
215 typedef boost::signal<void ()> BreakSignalType;
216 typedef BreakSignalType::slot_function_type BreakCallbackType;
218 // Adds a callback when 'break' command issued
219 void AddBreakObserver( BreakCallbackType );
225 /// The enumeration of command codes == Command name
228 cBreak, // LG 12/12/08 : Stops the current script execution (if not exec frozen) - used in tutorial + debugging
229 cClear, // LG 12/12/08 : Clears the current complex black box (e.g. workspace) - used in tours
261 /// The structure storing the informations on a command
266 CommandCodeType code;
271 /// The type of dictionnary of commands
272 typedef std::map<std::string,CommandInfoType> CommandDictType;
275 /// Interprets a line
276 void DoInterpretLine( const std::string& line ); //, bool &insideComment );
278 /// Reads a line from prompt
279 void GetLineFromPrompt( std::string& line );
281 /// Splits a line into words
282 void SplitLine ( const std::string& line,
283 std::vector<std::string>& words );
285 /// Executes the right action depending on the command name
286 void InterpretCommand( const std::vector<std::string>& words,
287 CommandInfoType& info );
289 /// Switch to the interpretation of a file
290 void SwitchToFile( const std::string& filename, bool source = false );
292 /// Switch to the interpretation of a stringstream
293 void SwitchToStream( std::stringstream* stream );
295 /// Closes the currently open file
296 void CloseCurrentFile();
298 /// Closes all open files
299 void CloseAllFiles();
303 virtual void commandReset();
306 /// Displays help (entry point of any help)
307 virtual void commandHelp(const std::vector<std::string>& words);
309 /// Displays help on all the commands
313 /// Displays help on a particular command
314 virtual void commandHelp( const std::string& command );
317 virtual void commandGraph(const std::vector<std::string>& words);
320 virtual void commandConfig() const;
322 // void Print(const std::string&);
324 virtual void commandIndex(const std::string& filename,
325 const std::string& type = "Initials");
329 void FindCommandsWithPrefix( char* buf,
331 std::vector<std::string>& commands );
334 /// Creates and connects the piece of pipeline which defines a GUI
336 /// Define it as a complex box type with name instanceName+"Type"
337 /// The instance is called instanceName
338 /// and connected to the existing pipeline
339 virtual void commandNewGUI(const std::string& box,const std::string& instanceName);
340 virtual void commandDebug(const std::string& arg);
341 virtual void commandNew(const std::string &boxType,const std::string &boxName);
342 virtual void commandDelete(const std::string &boxName);
343 virtual void commandConnection(const std::string &nodeFrom,const std::string &outputLabel,const std::string &nodeTo,const std::string &inputLabel);
344 virtual void commandPackage(const std::string &packageName);
345 virtual void commandEndPackage();
346 virtual void commandDefine(const std::string &name,const std::string &pack,const std::string &scriptfilename);
347 virtual void commandEndDefine();
348 virtual void commandKind(const std::string &kind);
349 virtual void commandPrint(const std::string &value);
350 virtual void commandExec(const std::string &word);
351 virtual void commandInput(const std::string &name,const std::string &box,const std::string &input,const std::string &help);
352 virtual void commandOutput(const std::string &name,const std::string &box,const std::string &output,const std::string &help);
353 virtual void commandSet(const std::string &box,const std::string &input,const std::string &value);
354 virtual void commandAuthor(const std::string &author);
355 virtual void commandCategory(const std::string &categorytype);
356 virtual void commandDescription(const std::string &description);
357 virtual void commandClear();
358 virtual void commandInclude(const std::string &word, bool ok);
359 virtual void commandLoad(const std::string &packageName);
360 virtual void commandUnload(const std::string &packageName);
361 virtual void commandBreak();
362 virtual void commandQuit();
363 virtual void commandMessage();
364 virtual void commandMessage(const std::string &kind,const std::string &levelstr);
365 virtual void SetCurrentFileName(const std::string &fullPathScriptName);
369 Interpreter(const std::string& cpp_file = "");
370 Interpreter(VirtualExec::Pointer);
375 void Init(VirtualExec::Pointer, const std::string& cpp_file);
378 /// Opens the file fullPathScriptName
379 /// includeScriptName is the name as given to the include command
380 void LoadScript( std::string fullPathScriptName,
381 std::string includeScriptName);
383 //==================================================================
386 /// The command executer used
387 bbtk::VirtualExec::Pointer mVirtualExecuter;
388 bbtk::Executer::WeakPointer mRealExecuter;
391 /// The user of the interpreter (0 if none)
392 bbtk::InterpreterUser* mUser;
393 /// Vector of open files / buffers (can be stringstream)
394 std::vector<std::istream*> mFile;
396 /// Vector of names of open files with full path (as open)
397 std::vector<std::string> mFileName;
399 /// Vector of names of files which have been open
400 /// before (and may closed then which are no more in mFileName)
401 /// with full path (as open)
402 std::vector<std::string> mFileNameHistory;
404 /// Vector of names of open files as given to the include command
405 std::vector<std::string> mIncludeFileName;
407 /// Stores the current line number in each open file
408 std::vector<int> mLine;
410 /// The dictionnary of commands
411 CommandDictType mCommandDict;
413 /// Are we in a command line context ?
416 /// Are we inside a commented-out zone ?
419 /// The current interpreter status
422 /// The history of commands
423 std::deque< char* > mHistory;
430 // BreakSignalType mBreakSignal;
438 virtual void CatchInterpreterException( const InterpreterException& e );
439 virtual void CatchBbtkException( const bbtk::Exception& e );
440 virtual void CatchStdException( const std::exception& e );
441 virtual void CatchUnknownException();
445 // EO class Interpreter