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