]> Creatis software - bbtk.git/blob - kernel/src/bbtkInterpreter.h
#2710 BBTK Bug New Normal - package wx box ComboBox refresh error
[bbtk.git] / kernel / src / bbtkInterpreter.h
1 /*
2  # ---------------------------------------------------------------------
3  #
4  # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
5  #                        pour la SantÈ)
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
9  #
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.
16  #
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
21  #  liability.
22  #
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  # ------------------------------------------------------------------------ */
26
27
28 /*=========================================================================
29   Program:   bbtk
30   Module:    $RCSfile: bbtkInterpreter.h,v $
31   Language:  C++
32   Date:      $Date: 2012/11/16 08:49:01 $
33   Version:   $Revision: 1.46 $
34 =========================================================================*/
35
36
37
38 /**
39  *  \file 
40  *  \brief class Interpreter : The bbtk language interpreter
41  */
42 /**
43  *  \class bbtk::Interpreter 
44  *  \brief The bbtk language interpreter
45  */
46
47 #ifndef __bbtkInterpreter_h__
48 #define __bbtkInterpreter_h__
49
50 #include "bbtkVirtualExec.h"
51 #include "bbtkExecuter.h"
52 #include "bbtkInterpreterVirtual.h"
53
54 #include <fstream>
55 #include <deque>
56
57 // Signal/slot mechanism for 'break' commands
58 //#include <boost/signal.hpp>
59 //#include <boost/bind.hpp>
60
61 namespace bbtk
62 {
63
64 #ifdef USE_WXWIDGETS
65   class WxConsole;
66 #endif
67   
68 /* EED Borrame  
69   //=======================================================================
70   class BBTK_EXPORT InterpreterUser
71   {
72   public: 
73     InterpreterUser() {}
74     virtual ~InterpreterUser() {}
75
76     virtual bool InterpreterUserHasOwnHtmlPageViewer() { return false; }
77     virtual void InterpreterUserViewHtmlPage(const std::string&) {}
78   };
79   //=======================================================================
80 */
81         
82
83 /* EED Borrame
84   //=======================================================================
85   class BBTK_EXPORT InterpreterException : public Exception
86   {
87   public:
88     InterpreterException( const std::string& message,
89                       bool in_script_file,
90                       const std::string& script_file,
91                       int script_line 
92                       );
93     InterpreterException( const Exception& excep,
94                       bool in_script_file,
95                       const std::string& script_file,
96                       int script_line 
97                       );
98     ~InterpreterException() throw() {}
99
100     bool IsInScriptFile() const { return mInScriptFile; }
101     const std::string& GetScriptFile() const { return mScriptFile; }
102     int GetScriptLine() const { return mScriptLine; }
103   private:
104     bool mInScriptFile;
105     std::string mScriptFile;
106     int mScriptLine;
107   };
108   //=======================================================================
109 */
110         
111         
112         
113   /*
114   //=======================================================================
115   // The "Quit" exception
116   class BBTK_EXPORT QuitException : public InterpreterError
117   {
118   public:
119     QuitException( bool in_script_file,
120                    const std::string& script_file,
121                    int script_line 
122                    )
123       : InterpreterError("QUIT",in_script_file,script_file,script_line) 
124     {}
125     ~QuitException() throw() {}
126   };
127   //=======================================================================
128   const std::string BREAK("BREAK");
129   //=======================================================================
130   // The "Break" exception
131   class BBTK_EXPORT BreakException : public InterpreterError
132   {
133   public:
134     BreakException( bool in_script_file,
135                     std::string script_file,
136                     int script_line 
137                     )
138       : InterpreterError(BREAK,in_script_file,script_file,script_line) 
139     { std::cout << "BUILDING BREAK"<<std::endl; }
140     ~BreakException() throw() {}
141   };
142   //=======================================================================
143   */
144
145   //=======================================================================
146   class BBTK_EXPORT Interpreter : public InterpreterVirtual
147   {
148     BBTK_OBJECT_INTERFACE_NO_CONDES(Interpreter);
149     typedef Object InterpreterVirtual;
150   public:
151     static Pointer New(const std::string& cpp_file = "");
152     static Pointer New(VirtualExec::Pointer);
153
154 /* EED Borrame    
155     typedef enum 
156       {
157         Interpreter_OK,
158         Interpreter_ERROR,
159         Interpreter_QUIT,
160         Interpreter_BREAK,
161       }
162       ExitStatus;
163
164     /// Runs the interpretation of a file
165     ExitStatus InterpretFile( const std::string& filename, bool source = false);
166
167     /// Runs the interpretation of a buffer and deletes it !
168     ExitStatus InterpretBuffer( std::stringstream* buffer );
169
170     /// Runs the interpretation of a command
171     ExitStatus InterpretLine( const std::string& line );
172
173     /// Runs the interpretation of the currently open streams
174     ExitStatus InterpretCurrentStreams();
175
176     /// Launches a command line interpreter (with a prompt)
177     void CommandLineInterpreter();
178 */
179
180
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); }
185
186     /// Puts the executer in "no exec" mode,
187     /// which creates but does not execute pipelines.
188     void SetNoExecMode(bool m) { mVirtualExecuter->SetNoExecMode(m); }
189
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);}
193     ///
194     //typedef Executer::DialogModeType DialogModeType;
195     typedef VirtualExec::DialogModeType DialogModeType;
196
197     void SetDialogMode(DialogModeType t) { mVirtualExecuter->SetDialogMode(t);}
198
199 /*EED Borrame     
200     /// Sets the bool that indicates wether we are in command line context
201     void SetCommandLine(bool v = true) { mCommandLine = v; }
202
203     void SetThrow(bool b) { mThrow = b; }
204  
205 #ifdef USE_WXWIDGETS
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; }
212
213 #endif
214  */
215
216     /// Gets the Executer 
217     VirtualExec::Pointer GetExecuter() const { return mVirtualExecuter; }
218
219     /*
220     // For 'break' commands observation
221     typedef boost::signals::trackable BreakObserverType;
222     typedef boost::signal<void ()>  BreakSignalType;
223     typedef BreakSignalType::slot_function_type BreakCallbackType;
224   
225     // Adds a callback when 'break' command issued
226     void AddBreakObserver( BreakCallbackType );
227     */
228
229   protected:
230
231 /* EED Borrame    
232     /// The enumeration of command codes == Command name
233     typedef enum
234     {
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
237       cNew,
238       cDelete,
239       cConnect,
240       cExec,
241       cPackage,
242       cEndPackage,
243       cDefine,
244       cEndDefine,
245       cInput,
246       cOutput,
247       cSet,
248       cConfig,  // JPR
249       cReset,   // EED
250       cAuthor, 
251       cCategory, // JPR
252       cDescription,
253       cHelp,
254       cMessage,
255       cInclude,
256       cQuit,
257       cLoad,
258       cUnload,
259       cGraph,
260       cPrint,
261       cIndex,
262       cKind, // LG
263       cNewGUI, // LG
264       cWorkspace, // LG
265       cDebug // LG
266     } CommandCodeType;
267
268     /// The structure storing the informations on a command 
269     typedef struct 
270     {
271       std::string keyword;
272       int argmin, argmax;
273       CommandCodeType code;
274       std::string syntax;
275       std::string help;
276     } CommandInfoType;
277
278     /// The type of dictionnary of commands 
279     typedef std::map<std::string,CommandInfoType> CommandDictType;
280
281
282     /// Interprets a line 
283     void DoInterpretLine( const std::string& line ); //, bool &insideComment );
284
285     /// Reads a line from prompt
286     void GetLineFromPrompt( std::string& line );
287
288     /// Splits a line into words
289     void SplitLine ( const std::string& line,
290                      std::vector<std::string>& words );
291
292     /// Executes the right action depending on the command name
293     void InterpretCommand( const std::vector<std::string>& words,
294                            CommandInfoType& info );
295
296     /// Switch to the interpretation of a file
297     void SwitchToFile( const std::string& filename, bool source = false );
298
299    /// Switch to the interpretation of a stringstream
300     void SwitchToStream( std::stringstream* stream );
301
302     /// Closes the currently open file
303     void CloseCurrentFile();
304
305     /// Closes all open files
306     void CloseAllFiles();
307 */
308           
309     /// Resets all
310     virtual void commandReset();
311
312           
313     /// Displays help (entry point of any help)
314     virtual void commandHelp(const std::vector<std::string>& words);
315
316     /// Displays help on all the commands
317     void HelpCommands();
318
319           
320     /// Displays help on a particular command 
321     virtual void commandHelp( const std::string& command );
322
323     ///
324     virtual void commandGraph(const std::vector<std::string>& words);
325
326     ///
327     virtual void commandConfig() const; 
328     /// 
329     //    void Print(const std::string&);
330
331     virtual void commandIndex(const std::string& filename, 
332                const std::string& type = "Initials");
333
334 /*EED Borrame     
335     ///
336     void FindCommandsWithPrefix( char* buf,
337                                  int n,
338                                  std::vector<std::string>& commands );
339 */
340           
341     /// Creates and connects the piece of pipeline which defines a GUI 
342     /// for the box box. 
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();
374
375           
376           
377           /// Constructor
378           Interpreter(const std::string& cpp_file = "");
379           Interpreter(VirtualExec::Pointer);
380           
381   private:
382
383                   
384     void Init(VirtualExec::Pointer, const std::string& cpp_file);
385
386 /*EED  Borrame 
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);
391 */  
392     //==================================================================
393     // ATTRIBUTES
394
395     /// The command executer used
396     bbtk::VirtualExec::Pointer mVirtualExecuter;
397     bbtk::Executer::WeakPointer mRealExecuter;
398
399 /*EED Borrame     
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;
404
405     /// Vector of names of open files with full path (as open)
406     std::vector<std::string> mFileName;
407
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;
412
413     /// Vector of names of open files as given to the include command
414     std::vector<std::string> mIncludeFileName;
415
416     /// Stores the current line number in each open file
417     std::vector<int> mLine;
418
419     /// The dictionnary of commands
420     CommandDictType mCommandDict;
421
422     /// Are we in a command line context ?
423     bool mCommandLine;
424
425     /// Are we inside a commented-out zone ?
426     bool mInsideComment;
427     
428     /// The current interpreter status
429     ExitStatus mStatus;
430
431     /// The history of commands
432     std::deque< char* > mHistory;
433
434     int bufferNb;
435     
436     bool mThrow;
437
438     /// The break signal
439     //    BreakSignalType mBreakSignal;
440  
441  */
442
443   protected:
444           ~Interpreter();
445
446           /// 
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();
451           
452           
453   };
454   // EO class Interpreter
455
456
457
458 }
459 #endif