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__
50 //#include "bbtkSystem.h"
52 #include "bbtkMessageManager.h"
53 #include "bbtkException.h"
54 #include "bbtkObject.h"
60 // Signal/slot mechanism for 'break' commands
61 //#include <boost/signals2/signal.hpp>
62 #include <boost/bind.hpp>
68 //=======================================================================
69 class BBTK_EXPORT InterpreterUser
73 virtual ~InterpreterUser() {}
75 virtual bool InterpreterUserHasOwnHtmlPageViewer() { return false; }
76 virtual void InterpreterUserViewHtmlPage(const std::string&) {}
80 //=======================================================================
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 //=======================================================================
111 //=======================================================================
112 // The "Quit" exception
113 class BBTK_EXPORT QuitException : public InterpreterError
116 QuitException( bool in_script_file,
117 const std::string& script_file,
120 : InterpreterError("QUIT",in_script_file,script_file,script_line)
122 ~QuitException() throw() {}
124 //=======================================================================
125 const std::string BREAK("BREAK");
126 //=======================================================================
127 // The "Break" exception
128 class BBTK_EXPORT BreakException : public InterpreterError
131 BreakException( bool in_script_file,
132 std::string script_file,
135 : InterpreterError(BREAK,in_script_file,script_file,script_line)
136 { std::cout << "BUILDING BREAK"<<std::endl; }
137 ~BreakException() throw() {}
139 //=======================================================================
142 //=======================================================================
143 class BBTK_EXPORT InterpreterVirtual : public Object
145 BBTK_OBJECT_INTERFACE_NO_CONDES(InterpreterVirtual);
146 typedef Object Superclass;
148 static Pointer New();
159 /// Runs the interpretation of a file
160 ExitStatus InterpretFile( const std::string& filename, bool source = false);
162 /// Runs the interpretation of a buffer and deletes it !
163 ExitStatus InterpretBuffer( std::stringstream* buffer );
165 /// Runs the interpretation of a command
166 ExitStatus InterpretLine( const std::string& line );
168 /// Runs the interpretation of the currently open streams
169 ExitStatus InterpretCurrentStreams();
171 /// Launches a command line interpreter (with a prompt)
172 void CommandLineInterpreter();
176 /// Sets the bool that indicates wether we are in command line context
177 void SetCommandLine(bool v = true) { mCommandLine = v; }
179 void SetThrow(bool b) { mThrow = b; }
182 /// Sets the user of the interpreter (if any)
183 void SetUser(InterpreterUser* c) { mUser = c; }
184 /// Gets the InterpreterUser of this
185 InterpreterUser* GetUser() { return mUser; }
186 /// Gets the InterpreterUser of this (const)
187 const InterpreterUser* GetUser() const { return mUser; }
195 // For 'break' commands observation
196 typedef boost::signals::trackable BreakObserverType;
197 typedef boost::signal<void ()> BreakSignalType;
198 typedef BreakSignalType::slot_function_type BreakCallbackType;
200 // Adds a callback when 'break' command issued
201 void AddBreakObserver( BreakCallbackType );
206 /// The enumeration of command codes == Command name
209 cBreak, // LG 12/12/08 : Stops the current script execution (if not exec frozen) - used in tutorial + debugging
210 cClear, // LG 12/12/08 : Clears the current complex black box (e.g. workspace) - used in tours
242 /// The structure storing the informations on a command
247 CommandCodeType code;
252 /// The type of dictionnary of commands
253 typedef std::map<std::string,CommandInfoType> CommandDictType;
256 /// Interprets a line
257 void DoInterpretLine( const std::string& line ); //, bool &insideComment );
259 /// Reads a line from prompt
260 void GetLineFromPrompt( std::string& line );
262 /// Splits a line into words
263 void SplitLine ( const std::string& line,
264 std::vector<std::string>& words );
266 /// Executes the right action depending on the command name
267 void InterpretCommand( const std::vector<std::string>& words,
268 CommandInfoType& info );
270 /// Switch to the interpretation of a file
271 void SwitchToFile( const std::string& filename, bool source = false );
273 /// Switch to the interpretation of a stringstream
274 void SwitchToStream( std::stringstream* stream );
276 /// Closes the currently open file
277 void CloseCurrentFile();
279 /// Closes all open files
280 void CloseAllFiles();
283 virtual void commandReset();
285 /// Displays help (entry point of any help)
286 virtual void commandHelp(const std::string &words);
287 virtual void commandHelp(const std::vector<std::string>& words);
290 virtual void commandGraph(const std::vector<std::string>& words);
293 virtual void commandConfig() const;
295 // void Print(const std::string&);
297 void commandIndex(const std::string& filename,const std::string& type = "Initials");
299 void FindCommandsWithPrefix( char* buf,
301 std::vector<std::string>& commands );
303 /// Creates and connects the piece of pipeline which defines a GUI
305 /// Define it as a complex box type with name instanceName+"Type"
306 /// The instance is called instanceName
307 /// and connected to the existing pipeline
308 virtual void commandNewGUI(const std::string& box,const std::string& instanceName);
309 virtual void commandDebug(const std::string& arg);
310 virtual void commandNew(const std::string &boxType, const std::string &boxName);
311 virtual void commandDelete(const std::string &boxName);
312 virtual void commandConnection(const std::string &nodeFrom,const std::string &outputLabel,const std::string &nodeTo,const std::string &inputLabel);
313 virtual void commandPackage(const std::string &packageName);
314 virtual void commandEndPackage();
315 virtual void commandDefine(const std::string &name,const std::string &pack,const std::string &scriptfilename);
316 virtual void commandEndDefine();
317 virtual void commandKind(const std::string &kind);
318 virtual void commandPrint(const std::string &value);
319 virtual void commandExec(const std::string &word);
320 virtual void commandInput(const std::string &name,const std::string &box,const std::string &input,const std::string &help);
321 virtual void commandOutput(const std::string &name,const std::string &box,const std::string &output,const std::string &help);
322 virtual void commandSet(const std::string &box,const std::string &input,const std::string &value);
323 virtual void commandAuthor(const std::string &author);
324 virtual void commandCategory(const std::string &categorytype);
325 virtual void commandDescription(const std::string &description);
326 virtual void commandClear();
327 virtual void commandInclude(const std::string &word, bool ok);
328 virtual void commandLoad(const std::string &packageName);
329 virtual void commandUnload(const std::string &packageName);
330 virtual void commandBreak();
331 virtual void commandQuit();
332 virtual void commandMessage();
333 virtual void commandMessage(const std::string &kind,const std::string &levelstr);
334 virtual void SetCurrentFileName(const std::string &fullPathScriptName);
335 virtual void SetTypeOfScript_Application();
338 InterpreterVirtual();
344 /// Vector of names of open files with full path (as open)
345 std::vector<std::string> mFileName;
348 /// Stores the current line number in each open file
349 std::vector<int> mLine;
353 /// Are we in a command line context ?
357 /// Vector of names of files which have been open
358 /// before (and may closed then which are no more in mFileName)
359 /// with full path (as open)
360 std::vector<std::string> mFileNameHistory;
362 /// The user of the interpreter (0 if none)
363 bbtk::InterpreterUser* mUser;
365 /// The dictionnary of commands
366 CommandDictType mCommandDict;
373 /// Opens the file fullPathScriptName
374 /// includeScriptName is the name as given to the include command
375 void LoadScript( std::string fullPathScriptName,std::string includeScriptName);
378 //==================================================================
383 /// Vector of open files / buffers (can be stringstream)
384 std::vector<std::istream*> mFile;
387 /// Vector of names of open files as given to the include command
388 std::vector<std::string> mIncludeFileName;
391 /// Are we inside a commented-out zone ?
394 /// The current interpreter status
397 /// The history of commands
398 std::deque< char* > mHistory;
404 // BreakSignalType mBreakSignal;
407 ~InterpreterVirtual();
410 virtual void CatchInterpreterException( const InterpreterException& e );
411 virtual void CatchBbtkException( const bbtk::Exception& e );
412 virtual void CatchStdException( const std::exception& e );
413 virtual void CatchUnknownException();
417 // EO class Interpreter