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: 2010/09/14 07:18:47 $
33 Version: $Revision: 1.41 $
34 =========================================================================*/
40 * \brief class Interpreter : The bbtk language interpreter
43 * \class bbtk::Interpreter
44 * \brief The bbtk language interpreter
47 #ifndef __bbtkInterpreterVirtual_h__
48 #define __bbtkInterpreterVirtual_h__
51 // Signal/slot mechanism for 'break' commands
52 //#include <boost/signals2/signal.hpp>
53 #include <boost/bind.hpp>
55 //#include "bbtkSystem.h"
57 #include "bbtkMessageManager.h"
58 #include "bbtkException.h"
59 #include "bbtkObject.h"
70 //=======================================================================
71 class BBTK_EXPORT InterpreterUser
75 virtual ~InterpreterUser() {}
77 virtual bool InterpreterUserHasOwnHtmlPageViewer() { return false; }
78 virtual void InterpreterUserViewHtmlPage(const std::string&) {}
82 //=======================================================================
86 //=======================================================================
87 class BBTK_EXPORT InterpreterException : public Exception
90 InterpreterException( const std::string& message,
92 const std::string& script_file,
95 InterpreterException( const Exception& excep,
97 const std::string& script_file,
100 ~InterpreterException() throw() {}
102 bool IsInScriptFile() const { return mInScriptFile; }
103 const std::string& GetScriptFile() const { return mScriptFile; }
104 int GetScriptLine() const { return mScriptLine; }
107 std::string mScriptFile;
110 //=======================================================================
113 //=======================================================================
114 // The "Quit" exception
115 class BBTK_EXPORT QuitException : public InterpreterError
118 QuitException( bool in_script_file,
119 const std::string& script_file,
122 : InterpreterError("QUIT",in_script_file,script_file,script_line)
124 ~QuitException() throw() {}
126 //=======================================================================
127 const std::string BREAK("BREAK");
128 //=======================================================================
129 // The "Break" exception
130 class BBTK_EXPORT BreakException : public InterpreterError
133 BreakException( bool in_script_file,
134 std::string script_file,
137 : InterpreterError(BREAK,in_script_file,script_file,script_line)
138 { std::cout << "BUILDING BREAK"<<std::endl; }
139 ~BreakException() throw() {}
141 //=======================================================================
144 //=======================================================================
145 class BBTK_EXPORT InterpreterVirtual : public Object
147 BBTK_OBJECT_INTERFACE_NO_CONDES(InterpreterVirtual);
148 typedef Object Superclass;
150 static Pointer New();
161 /// Runs the interpretation of a file
162 ExitStatus InterpretFile( const std::string& filename, bool source = false);
164 /// Runs the interpretation of a buffer and deletes it !
165 ExitStatus InterpretBuffer( std::stringstream* buffer );
167 /// Runs the interpretation of a command
168 ExitStatus InterpretLine( const std::string& line );
170 /// Runs the interpretation of the currently open streams
171 ExitStatus InterpretCurrentStreams();
173 /// Launches a command line interpreter (with a prompt)
174 void CommandLineInterpreter();
178 /// Sets the bool that indicates wether we are in command line context
179 void SetCommandLine(bool v = true) { mCommandLine = v; }
181 void SetThrow(bool b) { mThrow = b; }
184 /// Sets the user of the interpreter (if any)
185 void SetUser(InterpreterUser* c) { mUser = c; }
186 /// Gets the InterpreterUser of this
187 InterpreterUser* GetUser() { return mUser; }
188 /// Gets the InterpreterUser of this (const)
189 const InterpreterUser* GetUser() const { return mUser; }
197 // For 'break' commands observation
198 typedef boost::signals::trackable BreakObserverType;
199 typedef boost::signal<void ()> BreakSignalType;
200 typedef BreakSignalType::slot_function_type BreakCallbackType;
202 // Adds a callback when 'break' command issued
203 void AddBreakObserver( BreakCallbackType );
208 /// The enumeration of command codes == Command name
211 cBreak, // LG 12/12/08 : Stops the current script execution (if not exec frozen) - used in tutorial + debugging
212 cClear, // LG 12/12/08 : Clears the current complex black box (e.g. workspace) - used in tours
244 /// The structure storing the informations on a command
249 CommandCodeType code;
254 /// The type of dictionnary of commands
255 typedef std::map<std::string,CommandInfoType> CommandDictType;
258 /// Interprets a line
259 void DoInterpretLine( const std::string& line ); //, bool &insideComment );
261 /// Reads a line from prompt
262 void GetLineFromPrompt( std::string& line );
264 /// Splits a line into words
265 void SplitLine ( const std::string& line,
266 std::vector<std::string>& words );
268 /// Executes the right action depending on the command name
269 void InterpretCommand( const std::vector<std::string>& words,
270 CommandInfoType& info );
272 /// Switch to the interpretation of a file
273 void SwitchToFile( const std::string& filename, bool source = false );
275 /// Switch to the interpretation of a stringstream
276 void SwitchToStream( std::stringstream* stream );
278 /// Closes the currently open file
279 void CloseCurrentFile();
281 /// Closes all open files
282 void CloseAllFiles();
285 virtual void commandReset();
287 /// Displays help (entry point of any help)
288 virtual void commandHelp(const std::string &words);
289 virtual void commandHelp(const std::vector<std::string>& words);
292 virtual void commandGraph(const std::vector<std::string>& words);
295 virtual void commandConfig() const;
297 // void Print(const std::string&);
299 void commandIndex(const std::string& filename,const std::string& type = "Initials");
301 void FindCommandsWithPrefix( char* buf,
303 std::vector<std::string>& commands );
305 /// Creates and connects the piece of pipeline which defines a GUI
307 /// Define it as a complex box type with name instanceName+"Type"
308 /// The instance is called instanceName
309 /// and connected to the existing pipeline
310 virtual void commandNewGUI(const std::string& box,const std::string& instanceName);
311 virtual void commandDebug(const std::string& arg);
312 virtual void commandNew(const std::string &boxType, const std::string &boxName);
313 virtual void commandDelete(const std::string &boxName);
314 virtual void commandConnection(const std::string &nodeFrom,const std::string &outputLabel,const std::string &nodeTo,const std::string &inputLabel);
315 virtual void commandPackage(const std::string &packageName);
316 virtual void commandEndPackage();
317 virtual void commandDefine(const std::string &name,const std::string &pack,const std::string &scriptfilename);
318 virtual void commandEndDefine();
319 virtual void commandKind(const std::string &kind);
320 virtual void commandPrint(const std::string &value);
321 virtual void commandExec(const std::string &word);
322 virtual void commandInput(const std::string &name,const std::string &box,const std::string &input,const std::string &help);
323 virtual void commandOutput(const std::string &name,const std::string &box,const std::string &output,const std::string &help);
324 virtual void commandSet(const std::string &box,const std::string &input,const std::string &value);
325 virtual void commandAuthor(const std::string &author);
326 virtual void commandCategory(const std::string &categorytype);
327 virtual void commandDescription(const std::string &description);
328 virtual void commandClear();
329 virtual void commandInclude(const std::string &word, bool ok);
330 virtual void commandLoad(const std::string &packageName);
331 virtual void commandUnload(const std::string &packageName);
332 virtual void commandBreak();
333 virtual void commandQuit();
334 virtual void commandMessage();
335 virtual void commandMessage(const std::string &kind,const std::string &levelstr);
336 virtual void SetCurrentFileName(const std::string &fullPathScriptName);
337 virtual void SetTypeOfScript_Application();
340 InterpreterVirtual();
346 /// Vector of names of open files with full path (as open)
347 std::vector<std::string> mFileName;
350 /// Stores the current line number in each open file
351 std::vector<int> mLine;
355 /// Are we in a command line context ?
359 /// Vector of names of files which have been open
360 /// before (and may closed then which are no more in mFileName)
361 /// with full path (as open)
362 std::vector<std::string> mFileNameHistory;
364 /// The user of the interpreter (0 if none)
365 bbtk::InterpreterUser* mUser;
367 /// The dictionnary of commands
368 CommandDictType mCommandDict;
375 /// Opens the file fullPathScriptName
376 /// includeScriptName is the name as given to the include command
377 void LoadScript( std::string fullPathScriptName,std::string includeScriptName);
380 //==================================================================
385 /// Vector of open files / buffers (can be stringstream)
386 std::vector<std::istream*> mFile;
389 /// Vector of names of open files as given to the include command
390 std::vector<std::string> mIncludeFileName;
393 /// Are we inside a commented-out zone ?
396 /// The current interpreter status
399 /// The history of commands
400 std::deque< char* > mHistory;
406 // BreakSignalType mBreakSignal;
409 ~InterpreterVirtual();
412 virtual void CatchInterpreterException( const InterpreterException& e );
413 virtual void CatchBbtkException( const bbtk::Exception& e );
414 virtual void CatchStdException( const std::exception& e );
415 virtual void CatchUnknownException();
419 // EO class Interpreter