]> Creatis software - bbtk.git/blob - kernel/src/bbtkInterpreterVirtual.h
#3205 BBTK Feature New Normal - branch vtk7itk4wx3-mxecc
[bbtk.git] / kernel / src / bbtkInterpreterVirtual.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: 2010/09/14 07:18:47 $
33   Version:   $Revision: 1.41 $
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 __bbtkInterpreterVirtual_h__
48 #define __bbtkInterpreterVirtual_h__
49
50
51 // Signal/slot mechanism for 'break' commands
52 //#include <boost/signals2/signal.hpp>
53 #include <boost/bind.hpp>
54
55 //#include "bbtkSystem.h"
56
57 #include "bbtkMessageManager.h"
58 #include "bbtkException.h"
59 #include "bbtkObject.h"
60
61
62 #include <fstream>
63 #include <deque>
64
65
66 namespace bbtk
67 {
68
69   
70   //=======================================================================
71   class BBTK_EXPORT InterpreterUser
72   {
73   public: 
74     InterpreterUser() {}
75     virtual ~InterpreterUser() {}
76
77     virtual bool InterpreterUserHasOwnHtmlPageViewer() { return false; }
78     virtual void InterpreterUserViewHtmlPage(const std::string&) {}
79
80
81   };
82   //=======================================================================
83
84
85
86   //=======================================================================
87   class BBTK_EXPORT InterpreterException : public Exception
88   {
89   public:
90     InterpreterException( const std::string& message,
91                       bool in_script_file,
92                       const std::string& script_file,
93                       int script_line 
94                       );
95     InterpreterException( const Exception& excep,
96                       bool in_script_file,
97                       const std::string& script_file,
98                       int script_line 
99                       );
100     ~InterpreterException() throw() {}
101
102     bool IsInScriptFile() const { return mInScriptFile; }
103     const std::string& GetScriptFile() const { return mScriptFile; }
104     int GetScriptLine() const { return mScriptLine; }
105   private:
106     bool mInScriptFile;
107     std::string mScriptFile;
108     int mScriptLine;
109   };
110   //=======================================================================
111
112   /*
113   //=======================================================================
114   // The "Quit" exception
115   class BBTK_EXPORT QuitException : public InterpreterError
116   {
117   public:
118     QuitException( bool in_script_file,
119                    const std::string& script_file,
120                    int script_line 
121                    )
122       : InterpreterError("QUIT",in_script_file,script_file,script_line) 
123     {}
124     ~QuitException() throw() {}
125   };
126   //=======================================================================
127   const std::string BREAK("BREAK");
128   //=======================================================================
129   // The "Break" exception
130   class BBTK_EXPORT BreakException : public InterpreterError
131   {
132   public:
133     BreakException( bool in_script_file,
134                     std::string script_file,
135                     int script_line 
136                     )
137       : InterpreterError(BREAK,in_script_file,script_file,script_line) 
138     { std::cout << "BUILDING BREAK"<<std::endl; }
139     ~BreakException() throw() {}
140   };
141   //=======================================================================
142   */
143
144   //=======================================================================
145   class BBTK_EXPORT InterpreterVirtual : public Object
146   {
147     BBTK_OBJECT_INTERFACE_NO_CONDES(InterpreterVirtual);
148     typedef Object Superclass;
149   public:
150           static Pointer New();
151
152     typedef enum 
153       {
154         Interpreter_OK,
155         Interpreter_ERROR,
156         Interpreter_QUIT,
157         Interpreter_BREAK,
158       }
159       ExitStatus;
160
161     /// Runs the interpretation of a file
162     ExitStatus InterpretFile( const std::string& filename, bool source = false);
163
164     /// Runs the interpretation of a buffer and deletes it !
165     ExitStatus InterpretBuffer( std::stringstream* buffer );
166
167     /// Runs the interpretation of a command
168     ExitStatus InterpretLine( const std::string& line );
169
170     /// Runs the interpretation of the currently open streams
171     ExitStatus InterpretCurrentStreams();
172
173     /// Launches a command line interpreter (with a prompt)
174     void CommandLineInterpreter();
175
176
177
178     /// Sets the bool that indicates wether we are in command line context
179     void SetCommandLine(bool v = true) { mCommandLine = v; }
180
181     void SetThrow(bool b) { mThrow = b; }
182
183 #ifdef USE_WXWIDGETS
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; }
190
191 #endif
192
193
194           
195           
196     /*
197     // For 'break' commands observation
198     typedef boost::signals::trackable BreakObserverType;
199     typedef boost::signal<void ()>  BreakSignalType;
200     typedef BreakSignalType::slot_function_type BreakCallbackType;
201   
202     // Adds a callback when 'break' command issued
203     void AddBreakObserver( BreakCallbackType );
204     */
205
206   protected:
207
208     /// The enumeration of command codes == Command name
209     typedef enum
210     {
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
213       cNew,
214       cDelete,
215       cConnect,
216       cExec,
217       cPackage,
218       cEndPackage,
219       cDefine,
220       cEndDefine,
221       cInput,
222       cOutput,
223       cSet,
224       cConfig,  // JPR
225       cReset,   // EED
226       cAuthor, 
227       cCategory, // JPR
228       cDescription,
229       cHelp,
230       cMessage,
231       cInclude,
232       cQuit,
233       cLoad,
234       cUnload,
235       cGraph,
236       cPrint,
237       cIndex,
238       cKind, // LG
239       cNewGUI, // LG
240       cWorkspace, // LG
241       cDebug // LG
242     } CommandCodeType;
243
244     /// The structure storing the informations on a command 
245     typedef struct 
246     {
247       std::string keyword;
248       int argmin, argmax;
249       CommandCodeType code;
250       std::string syntax;
251       std::string help;
252     } CommandInfoType;
253
254     /// The type of dictionnary of commands 
255     typedef std::map<std::string,CommandInfoType> CommandDictType;
256
257
258     /// Interprets a line 
259     void DoInterpretLine( const std::string& line ); //, bool &insideComment );
260
261     /// Reads a line from prompt
262     void GetLineFromPrompt( std::string& line );
263
264     /// Splits a line into words
265     void SplitLine ( const std::string& line,
266                      std::vector<std::string>& words );
267
268     /// Executes the right action depending on the command name
269     void InterpretCommand( const std::vector<std::string>& words,
270                            CommandInfoType& info );
271
272     /// Switch to the interpretation of a file
273     void SwitchToFile( const std::string& filename, bool source = false );
274
275    /// Switch to the interpretation of a stringstream
276     void SwitchToStream( std::stringstream* stream );
277
278     /// Closes the currently open file
279     void CloseCurrentFile();
280
281     /// Closes all open files
282     void CloseAllFiles();
283
284     /// Resets all
285     virtual void commandReset();
286
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);
290
291     ///
292     virtual void commandGraph(const std::vector<std::string>& words);
293
294     ///
295     virtual void commandConfig() const; 
296     /// 
297     //    void Print(const std::string&);
298
299     void commandIndex(const std::string& filename,const std::string& type = "Initials");
300     ///
301     void FindCommandsWithPrefix( char* buf,
302                                  int n,
303                                  std::vector<std::string>& commands );
304
305     /// Creates and connects the piece of pipeline which defines a GUI 
306     /// for the box box. 
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();
338           
339           /// Constructor
340           InterpreterVirtual();
341           
342
343           void Init();
344
345           
346           /// Vector of names of open files with full path (as open)
347           std::vector<std::string> mFileName;
348
349           
350           /// Stores the current line number in each open file
351           std::vector<int> mLine;
352           
353           bool mThrow;
354
355           /// Are we in a command line context ?
356           bool mCommandLine;
357
358           
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;
363
364           /// The user of  the interpreter (0 if none)
365           bbtk::InterpreterUser* mUser;
366   
367           /// The dictionnary of commands
368           CommandDictType mCommandDict;
369           
370           
371   private:
372
373
374
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);
378
379   
380     //==================================================================
381     // ATTRIBUTES
382
383
384
385     /// Vector of open files / buffers (can be stringstream)
386     std::vector<std::istream*> mFile;
387
388
389     /// Vector of names of open files as given to the include command
390     std::vector<std::string> mIncludeFileName;
391
392
393     /// Are we inside a commented-out zone ?
394     bool mInsideComment;
395     
396     /// The current interpreter status
397     ExitStatus mStatus;
398
399     /// The history of commands
400     std::deque< char* > mHistory;
401
402     int bufferNb;
403     
404
405     /// The break signal
406     //    BreakSignalType mBreakSignal;
407
408   protected:
409           ~InterpreterVirtual();
410
411           /// 
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();
416           
417           
418   };
419   // EO class Interpreter
420
421
422
423 }
424 #endif