]> Creatis software - bbtk.git/blob - kernel/src/bbtkInterpreterVirtual.h
29e198cf6ed008fa2a1a9891ce8e36c8f177a11d
[bbtk.git] / kernel / src / bbtkInterpreterVirtual.h
1 /*=========================================================================                                                                               
2   Program:   bbtk
3   Module:    $RCSfile: bbtkInterpreter.h,v $
4   Language:  C++
5   Date:      $Date: 2010/09/14 07:18:47 $
6   Version:   $Revision: 1.41 $
7 =========================================================================*/
8
9 /* ---------------------------------------------------------------------
10
11 * Copyright (c) CREATIS-LRMN (Centre de Recherche en Imagerie Medicale)
12 * Authors : Eduardo Davila, Laurent Guigues, Jean-Pierre Roux
13 *
14 *  This software is governed by the CeCILL-B license under French law and 
15 *  abiding by the rules of distribution of free software. You can  use, 
16 *  modify and/ or redistribute the software under the terms of the CeCILL-B 
17 *  license as circulated by CEA, CNRS and INRIA at the following URL 
18 *  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html 
19 *  or in the file LICENSE.txt.
20 *
21 *  As a counterpart to the access to the source code and  rights to copy,
22 *  modify and redistribute granted by the license, users are provided only
23 *  with a limited warranty  and the software's author,  the holder of the
24 *  economic rights,  and the successive licensors  have only  limited
25 *  liability. 
26 *
27 *  The fact that you are presently reading this means that you have had
28 *  knowledge of the CeCILL-B license and that you accept its terms.
29 * ------------------------------------------------------------------------ */                                                                         
30
31 /**
32  *  \file 
33  *  \brief class Interpreter : The bbtk language interpreter
34  */
35 /**
36  *  \class bbtk::Interpreter 
37  *  \brief The bbtk language interpreter
38  */
39
40 #ifndef __bbtkInterpreterVirtual_h__
41 #define __bbtkInterpreterVirtual_h__
42
43 //#include "bbtkSystem.h"
44
45 #include "bbtkMessageManager.h"
46 #include "bbtkException.h"
47 #include "bbtkObject.h"
48
49
50 #include <fstream>
51 #include <deque>
52
53 // Signal/slot mechanism for 'break' commands
54 #include <boost/signal.hpp>
55 #include <boost/bind.hpp>
56
57 namespace bbtk
58 {
59
60   
61   //=======================================================================
62   class BBTK_EXPORT InterpreterUser
63   {
64   public: 
65     InterpreterUser() {}
66     virtual ~InterpreterUser() {}
67
68     virtual bool InterpreterUserHasOwnHtmlPageViewer() { return false; }
69     virtual void InterpreterUserViewHtmlPage(const std::string&) {}
70
71
72   };
73   //=======================================================================
74
75
76
77   //=======================================================================
78   class BBTK_EXPORT InterpreterException : public Exception
79   {
80   public:
81     InterpreterException( const std::string& message,
82                       bool in_script_file,
83                       const std::string& script_file,
84                       int script_line 
85                       );
86     InterpreterException( const Exception& excep,
87                       bool in_script_file,
88                       const std::string& script_file,
89                       int script_line 
90                       );
91     ~InterpreterException() throw() {}
92
93     bool IsInScriptFile() const { return mInScriptFile; }
94     const std::string& GetScriptFile() const { return mScriptFile; }
95     int GetScriptLine() const { return mScriptLine; }
96   private:
97     bool mInScriptFile;
98     std::string mScriptFile;
99     int mScriptLine;
100   };
101   //=======================================================================
102
103   /*
104   //=======================================================================
105   // The "Quit" exception
106   class BBTK_EXPORT QuitException : public InterpreterError
107   {
108   public:
109     QuitException( bool in_script_file,
110                    const std::string& script_file,
111                    int script_line 
112                    )
113       : InterpreterError("QUIT",in_script_file,script_file,script_line) 
114     {}
115     ~QuitException() throw() {}
116   };
117   //=======================================================================
118   const std::string BREAK("BREAK");
119   //=======================================================================
120   // The "Break" exception
121   class BBTK_EXPORT BreakException : public InterpreterError
122   {
123   public:
124     BreakException( bool in_script_file,
125                     std::string script_file,
126                     int script_line 
127                     )
128       : InterpreterError(BREAK,in_script_file,script_file,script_line) 
129     { std::cout << "BUILDING BREAK"<<std::endl; }
130     ~BreakException() throw() {}
131   };
132   //=======================================================================
133   */
134
135   //=======================================================================
136   class BBTK_EXPORT InterpreterVirtual : public Object
137   {
138     BBTK_OBJECT_INTERFACE_NO_CONDES(InterpreterVirtual);
139     typedef Object Superclass;
140   public:
141           static Pointer New();
142
143     typedef enum 
144       {
145         Interpreter_OK,
146         Interpreter_ERROR,
147         Interpreter_QUIT,
148         Interpreter_BREAK,
149       }
150       ExitStatus;
151
152     /// Runs the interpretation of a file
153     ExitStatus InterpretFile( const std::string& filename, bool source = false);
154
155     /// Runs the interpretation of a buffer and deletes it !
156     ExitStatus InterpretBuffer( std::stringstream* buffer );
157
158     /// Runs the interpretation of a command
159     ExitStatus InterpretLine( const std::string& line );
160
161     /// Runs the interpretation of the currently open streams
162     ExitStatus InterpretCurrentStreams();
163
164     /// Launches a command line interpreter (with a prompt)
165     void CommandLineInterpreter();
166
167
168
169     /// Sets the bool that indicates wether we are in command line context
170     void SetCommandLine(bool v = true) { mCommandLine = v; }
171
172     void SetThrow(bool b) { mThrow = b; }
173
174 #ifdef USE_WXWIDGETS
175     /// Sets the user of the interpreter (if any)
176     void SetUser(InterpreterUser* c) { mUser = c; }
177     /// Gets the InterpreterUser of this 
178     InterpreterUser* GetUser() { return mUser; }
179     /// Gets the InterpreterUser of this (const)
180     const InterpreterUser* GetUser() const { return mUser; }
181
182 #endif
183
184
185           
186           
187     /*
188     // For 'break' commands observation
189     typedef boost::signals::trackable BreakObserverType;
190     typedef boost::signal<void ()>  BreakSignalType;
191     typedef BreakSignalType::slot_function_type BreakCallbackType;
192   
193     // Adds a callback when 'break' command issued
194     void AddBreakObserver( BreakCallbackType );
195     */
196
197   protected:
198
199     /// The enumeration of command codes == Command name
200     typedef enum
201     {
202       cBreak, // LG 12/12/08 : Stops the current script execution (if not exec frozen) - used in tutorial + debugging 
203       cClear, // LG 12/12/08 : Clears the current complex black box (e.g. workspace) - used in tours
204       cNew,
205       cDelete,
206       cConnect,
207       cExec,
208       cPackage,
209       cEndPackage,
210       cDefine,
211       cEndDefine,
212       cInput,
213       cOutput,
214       cSet,
215       cConfig,  // JPR
216       cReset,   // EED
217       cAuthor, 
218       cCategory, // JPR
219       cDescription,
220       cHelp,
221       cMessage,
222       cInclude,
223       cQuit,
224       cLoad,
225       cUnload,
226       cGraph,
227       cPrint,
228       cIndex,
229       cKind, // LG
230       cNewGUI, // LG
231       cWorkspace, // LG
232       cDebug // LG
233     } CommandCodeType;
234
235     /// The structure storing the informations on a command 
236     typedef struct 
237     {
238       std::string keyword;
239       int argmin, argmax;
240       CommandCodeType code;
241       std::string syntax;
242       std::string help;
243     } CommandInfoType;
244
245     /// The type of dictionnary of commands 
246     typedef std::map<std::string,CommandInfoType> CommandDictType;
247
248
249     /// Interprets a line 
250     void DoInterpretLine( const std::string& line ); //, bool &insideComment );
251
252     /// Reads a line from prompt
253     void GetLineFromPrompt( std::string& line );
254
255     /// Splits a line into words
256     void SplitLine ( const std::string& line,
257                      std::vector<std::string>& words );
258
259     /// Executes the right action depending on the command name
260     void InterpretCommand( const std::vector<std::string>& words,
261                            CommandInfoType& info );
262
263     /// Switch to the interpretation of a file
264     void SwitchToFile( const std::string& filename, bool source = false );
265
266    /// Switch to the interpretation of a stringstream
267     void SwitchToStream( std::stringstream* stream );
268
269     /// Closes the currently open file
270     void CloseCurrentFile();
271
272     /// Closes all open files
273     void CloseAllFiles();
274
275     /// Resets all
276     virtual void commandReset();
277
278     /// Displays help (entry point of any help)
279     virtual void commandHelp(const std::string &words);
280     virtual void commandHelp(const std::vector<std::string>& words);
281
282     ///
283     virtual void commandGraph(const std::vector<std::string>& words);
284
285     ///
286     virtual void commandConfig() const; 
287     /// 
288     //    void Print(const std::string&);
289
290     void commandIndex(const std::string& filename,const std::string& type = "Initials");
291     ///
292     void FindCommandsWithPrefix( char* buf,
293                                  int n,
294                                  std::vector<std::string>& commands );
295
296     /// Creates and connects the piece of pipeline which defines a GUI 
297     /// for the box box. 
298     /// Define it as a complex box type with name instanceName+"Type"
299     /// The instance is called instanceName
300     /// and connected to the existing pipeline
301       virtual void commandNewGUI(const std::string& box,const std::string& instanceName);
302       virtual void commandDebug(const std::string& arg);
303           virtual void commandNew(const std::string &boxType, const std::string &boxName);
304           virtual void commandDelete(const std::string &boxName);
305           virtual void commandConnection(const std::string &nodeFrom,const std::string &outputLabel,const std::string &nodeTo,const std::string &inputLabel);
306           virtual void commandPackage(const std::string &packageName);
307           virtual void commandEndPackage();
308           virtual void commandDefine(const std::string &name,const std::string &pack,const std::string &scriptfilename);
309           virtual void commandEndDefine();
310           virtual void commandKind(const std::string &kind);
311           virtual void commandPrint(const std::string &value);
312           virtual void commandExec(const std::string &word);
313           virtual void commandInput(const std::string &name,const std::string &box,const std::string &input,const std::string  &help);
314           virtual void commandOutput(const std::string &name,const std::string &box,const std::string &output,const std::string  &help);
315           virtual void commandSet(const std::string &box,const std::string &input,const std::string &value);
316           virtual void commandAuthor(const std::string &author);
317           virtual void commandCategory(const std::string &categorytype);
318           virtual void commandDescription(const std::string &description);
319           virtual void commandClear();
320           virtual void commandInclude(const std::string &word, bool ok);
321           virtual void commandLoad(const std::string &packageName);
322           virtual void commandUnload(const std::string &packageName);
323           virtual void commandBreak();
324           virtual void commandQuit();
325           virtual void commandMessage();
326           virtual void commandMessage(const std::string &kind,const std::string &levelstr);
327           virtual void SetCurrentFileName(const std::string &fullPathScriptName);
328           virtual void SetTypeOfScript_Application();
329           
330           /// Constructor
331           InterpreterVirtual();
332           
333
334           void Init();
335
336           
337           /// Vector of names of open files with full path (as open)
338           std::vector<std::string> mFileName;
339
340           
341           /// Stores the current line number in each open file
342           std::vector<int> mLine;
343           
344           bool mThrow;
345
346           /// Are we in a command line context ?
347           bool mCommandLine;
348
349           
350           /// Vector of names of files which have been open 
351           /// before (and may closed then which are no more in mFileName)
352           /// with full path (as open)
353           std::vector<std::string> mFileNameHistory;
354
355           /// The user of  the interpreter (0 if none)
356           bbtk::InterpreterUser* mUser;
357   
358           /// The dictionnary of commands
359           CommandDictType mCommandDict;
360           
361           
362   private:
363
364
365
366     /// Opens the file fullPathScriptName 
367     /// includeScriptName is the name as given to the include command 
368     void LoadScript( std::string fullPathScriptName,std::string includeScriptName);
369
370   
371     //==================================================================
372     // ATTRIBUTES
373
374
375
376     /// Vector of open files / buffers (can be stringstream)
377     std::vector<std::istream*> mFile;
378
379
380     /// Vector of names of open files as given to the include command
381     std::vector<std::string> mIncludeFileName;
382
383
384     /// Are we inside a commented-out zone ?
385     bool mInsideComment;
386     
387     /// The current interpreter status
388     ExitStatus mStatus;
389
390     /// The history of commands
391     std::deque< char* > mHistory;
392
393     int bufferNb;
394     
395
396     /// The break signal
397     //    BreakSignalType mBreakSignal;
398
399   protected:
400           ~InterpreterVirtual();
401
402           /// 
403           virtual void CatchInterpreterException( const InterpreterException& e );
404           virtual void CatchBbtkException( const bbtk::Exception& e );
405           virtual void CatchStdException( const std::exception& e );
406           virtual void CatchUnknownException();
407           
408           
409   };
410   // EO class Interpreter
411
412
413
414 }
415 #endif