1 /*=========================================================================
3 Module: $RCSfile: bbtkInterpreter.h,v $
5 Date: $Date: 2008/12/15 09:04:47 $
6 Version: $Revision: 1.39 $
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"
49 // Signal/slot mechanism for 'break' commands
50 //#include <boost/signal.hpp>
51 //#include <boost/bind.hpp>
60 //=======================================================================
61 class BBTK_EXPORT InterpreterUser
65 virtual ~InterpreterUser() {}
67 virtual bool InterpreterUserHasOwnHtmlPageViewer() { return false; }
68 virtual void InterpreterUserViewHtmlPage(const std::string&) {}
72 //=======================================================================
76 //=======================================================================
77 class BBTK_EXPORT InterpreterException : public Exception
80 InterpreterException( const std::string& message,
82 const std::string& script_file,
85 InterpreterException( const Exception& excep,
87 const std::string& script_file,
90 ~InterpreterException() throw() {}
92 bool IsInScriptFile() const { return mInScriptFile; }
93 const std::string& GetScriptFile() const { return mScriptFile; }
94 int GetScriptLine() const { return mScriptLine; }
97 std::string mScriptFile;
100 //=======================================================================
103 //=======================================================================
104 // The "Quit" exception
105 class BBTK_EXPORT QuitException : public InterpreterError
108 QuitException( bool in_script_file,
109 const std::string& script_file,
112 : InterpreterError("QUIT",in_script_file,script_file,script_line)
114 ~QuitException() throw() {}
116 //=======================================================================
117 const std::string BREAK("BREAK");
118 //=======================================================================
119 // The "Break" exception
120 class BBTK_EXPORT BreakException : public InterpreterError
123 BreakException( bool in_script_file,
124 std::string script_file,
127 : InterpreterError(BREAK,in_script_file,script_file,script_line)
128 { std::cout << "BUILDING BREAK"<<std::endl; }
129 ~BreakException() throw() {}
131 //=======================================================================
134 //=======================================================================
135 class BBTK_EXPORT Interpreter : public Object
137 BBTK_OBJECT_INTERFACE_NO_CONDES(Interpreter);
138 typedef Object Superclass;
140 static Pointer New(const std::string& cpp_file = "");
141 static Pointer New(VirtualExec::Pointer);
152 /// Runs the interpretation of a file
153 ExitStatus InterpretFile( const std::string& filename, bool source = false);
155 /// Runs the interpretation of a buffer and deletes it !
156 ExitStatus InterpretBuffer( std::stringstream* buffer );
158 /// Runs the interpretation of a command
159 ExitStatus InterpretLine( const std::string& line );
161 /// Runs the interpretation of the currently open streams
162 ExitStatus InterpretCurrentStreams();
164 /// Launches a command line interpreter (with a prompt)
165 void CommandLineInterpreter();
169 /// Sets the inputs of the workspace :
170 /// the map is passed as is to the Executer
171 void SetInputs(const std::map<std::string,std::string>& m)
172 { mVirtualExecuter->SetInputs(m); }
174 /// Puts the executer in "no exec" mode,
175 /// which creates but does not execute pipelines.
176 void SetNoExecMode(bool m) { mVirtualExecuter->SetNoExecMode(m); }
178 /// Puts the executer in "no error" mode,
179 /// Errors do not stop execution (but warnings are produced)
180 void SetNoErrorMode(bool m) { mVirtualExecuter->SetNoErrorMode(m);}
182 //typedef Executer::DialogModeType DialogModeType;
183 typedef VirtualExec::DialogModeType DialogModeType;
185 void SetDialogMode(DialogModeType t) { mVirtualExecuter->SetDialogMode(t);}
187 /// Sets the bool that indicates wether we are in command line context
188 void SetCommandLine(bool v = true) { mCommandLine = v; }
190 void SetThrow(bool b) { mThrow = b; }
193 /// Sets the user of the interpreter (if any)
194 void SetUser(InterpreterUser* c) { mUser = c; }
195 /// Gets the InterpreterUser of this
196 InterpreterUser* GetUser() { return mUser; }
197 /// Gets the InterpreterUser of this (const)
198 const InterpreterUser* GetUser() const { return mUser; }
202 /// Gets the Executer
203 VirtualExec::Pointer GetExecuter() const { return mVirtualExecuter; }
206 // For 'break' commands observation
207 typedef boost::signals::trackable BreakObserverType;
208 typedef boost::signal<void ()> BreakSignalType;
209 typedef BreakSignalType::slot_function_type BreakCallbackType;
211 // Adds a callback when 'break' command issued
212 void AddBreakObserver( BreakCallbackType );
217 /// The enumeration of command codes == Command name
220 cBreak, // LG 12/12/08 : Stops the current script execution (if not exec frozen) - used in tutorial + debugging
221 cClear, // LG 12/12/08 : Clears the current complex black box (e.g. workspace) - used in tours
253 /// The structure storing the informations on a command
258 CommandCodeType code;
263 /// The type of dictionnary of commands
264 typedef std::map<std::string,CommandInfoType> CommandDictType;
267 /// Interprets a line
268 void DoInterpretLine( const std::string& line ); //, bool &insideComment );
270 /// Reads a line from prompt
271 void GetLineFromPrompt( std::string& line );
273 /// Splits a line into words
274 void SplitLine ( const std::string& line,
275 std::vector<std::string>& words );
277 /// Executes the right action depending on the command name
278 void InterpretCommand( const std::vector<std::string>& words,
279 CommandInfoType& info );
281 /// Switch to the interpretation of a file
282 void SwitchToFile( const std::string& filename, bool source = false );
284 /// Switch to the interpretation of a stringstream
285 void SwitchToStream( std::stringstream* stream );
287 /// Closes the currently open file
288 void CloseCurrentFile();
290 /// Closes all open files
291 void CloseAllFiles();
296 /// Displays help (entry point of any help)
297 void Help(const std::vector<std::string>& words);
299 /// Displays help on all the commands
302 /// Displays help on a particular command
303 void HelpCommand( const std::string& command );
306 void Graph(const std::vector<std::string>& words);
311 // void Print(const std::string&);
313 void Index(const std::string& filename,
314 const std::string& type = "Initials");
316 void FindCommandsWithPrefix( char* buf,
318 std::vector<std::string>& commands );
320 /// Creates and connects the piece of pipeline which defines a GUI
322 /// Define it as a complex box type with name instanceName+"Type"
323 /// The instance is called instanceName
324 /// and connected to the existing pipeline
325 void NewGUI(const std::string& box,
326 const std::string& instanceName);
328 void Debug(const std::string& arg);
334 Interpreter(const std::string& cpp_file = "");
336 Interpreter(VirtualExec::Pointer);
338 void Init(VirtualExec::Pointer, const std::string& cpp_file);
340 /// Opens the file fullPathScriptName
341 /// includeScriptName is the name as given to the include command
342 void LoadScript( std::string fullPathScriptName,
343 std::string includeScriptName);
346 void CatchInterpreterException( const InterpreterException& e );
347 void CatchBbtkException( const bbtk::Exception& e );
348 void CatchStdException( const std::exception& e );
349 void CatchUnknownException();
353 //==================================================================
356 /// The command executer used
357 bbtk::VirtualExec::Pointer mVirtualExecuter;
358 bbtk::Executer::WeakPointer mRealExecuter;
360 /// The user of the interpreter (0 if none)
361 bbtk::InterpreterUser* mUser;
363 /// Vector of open files / buffers (can be stringstream)
364 std::vector<std::istream*> mFile;
366 /// Vector of names of open files with full path (as open)
367 std::vector<std::string> mFileName;
369 /// Vector of names of files which have been open
370 /// before (and may closed then which are no more in mFileName)
371 /// with full path (as open)
372 std::vector<std::string> mFileNameHistory;
374 /// Vector of names of open files as given to the include command
375 std::vector<std::string> mIncludeFileName;
377 /// Stores the current line number in each open file
378 std::vector<int> mLine;
380 /// The dictionnary of commands
381 CommandDictType mCommandDict;
383 /// Are we in a command line context ?
386 /// Are we inside a commented-out zone ?
389 /// The current interpreter status
392 /// The history of commands
393 std::deque< char* > mHistory;
400 // BreakSignalType mBreakSignal;
406 // EO class Interpreter