]> Creatis software - bbtk.git/blob - kernel/src/bbtkInterpreterVirtual.h
no message
[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     // For 'break' commands observation
187     typedef boost::signals::trackable BreakObserverType;
188     typedef boost::signal<void ()>  BreakSignalType;
189     typedef BreakSignalType::slot_function_type BreakCallbackType;
190   
191     // Adds a callback when 'break' command issued
192     void AddBreakObserver( BreakCallbackType );
193     */
194
195   protected:
196
197     /// The enumeration of command codes == Command name
198     typedef enum
199     {
200       cBreak, // LG 12/12/08 : Stops the current script execution (if not exec frozen) - used in tutorial + debugging 
201       cClear, // LG 12/12/08 : Clears the current complex black box (e.g. workspace) - used in tours
202       cNew,
203       cDelete,
204       cConnect,
205       cExec,
206       cPackage,
207       cEndPackage,
208       cDefine,
209       cEndDefine,
210       cInput,
211       cOutput,
212       cSet,
213       cConfig,  // JPR
214       cReset,   // EED
215       cAuthor, 
216       cCategory, // JPR
217       cDescription,
218       cHelp,
219       cMessage,
220       cInclude,
221       cQuit,
222       cLoad,
223       cUnload,
224       cGraph,
225       cPrint,
226       cIndex,
227       cKind, // LG
228       cNewGUI, // LG
229       cWorkspace, // LG
230       cDebug // LG
231     } CommandCodeType;
232
233     /// The structure storing the informations on a command 
234     typedef struct 
235     {
236       std::string keyword;
237       int argmin, argmax;
238       CommandCodeType code;
239       std::string syntax;
240       std::string help;
241     } CommandInfoType;
242
243     /// The type of dictionnary of commands 
244     typedef std::map<std::string,CommandInfoType> CommandDictType;
245
246
247     /// Interprets a line 
248     void DoInterpretLine( const std::string& line ); //, bool &insideComment );
249
250     /// Reads a line from prompt
251     void GetLineFromPrompt( std::string& line );
252
253     /// Splits a line into words
254     void SplitLine ( const std::string& line,
255                      std::vector<std::string>& words );
256
257     /// Executes the right action depending on the command name
258     void InterpretCommand( const std::vector<std::string>& words,
259                            CommandInfoType& info );
260
261     /// Switch to the interpretation of a file
262     void SwitchToFile( const std::string& filename, bool source = false );
263
264    /// Switch to the interpretation of a stringstream
265     void SwitchToStream( std::stringstream* stream );
266
267     /// Closes the currently open file
268     void CloseCurrentFile();
269
270     /// Closes all open files
271     void CloseAllFiles();
272
273     /// Resets all
274     virtual void commandReset();
275
276     /// Displays help (entry point of any help)
277     virtual void commandHelp(const std::string words);
278     virtual void commandHelp(const std::vector<std::string>& words);
279
280     ///
281     virtual void commandGraph(const std::vector<std::string>& words);
282
283     ///
284     virtual void commandConfig() const; 
285     /// 
286     //    void Print(const std::string&);
287
288     void commandIndex(const std::string& filename,const std::string& type = "Initials");
289     ///
290     void FindCommandsWithPrefix( char* buf,
291                                  int n,
292                                  std::vector<std::string>& commands );
293
294     /// Creates and connects the piece of pipeline which defines a GUI 
295     /// for the box box. 
296     /// Define it as a complex box type with name instanceName+"Type"
297     /// The instance is called instanceName
298     /// and connected to the existing pipeline
299       virtual void commandNewGUI(const std::string& box,const std::string& instanceName);
300       virtual void commandDebug(const std::string& arg);
301           virtual void commandNew(std::string boxType, std::string boxName);
302           virtual void commandDelete(std::string boxName);
303           virtual void commandConnection(std::string nodeFrom, std::string outputLabel, std::string nodeTo, std::string inputLabel);
304           virtual void commandPackage(std::string packageName);
305           virtual void commandEndPackage();
306           virtual void commandDefine(std::string name, std::string pack, std::string scriptfilename);
307           virtual void commandEndDefine();
308           virtual void commandKind(std::string kind);
309           virtual void commandPrint(std::string value);
310           virtual void commandExec(std::string word);
311           virtual void commandInput(std::string name, std::string box, std::string input,std::string  help);
312           virtual void commandOutput(std::string name, std::string box, std::string output,std::string  help);
313           virtual void commandSet(std::string box, std::string input, std::string value);
314           virtual void commandAuthor(std::string author);
315           virtual void commandCategory(std::string categorytype);
316           virtual void commandDescription(std::string description);
317           virtual void commandClear();
318           virtual void commandInclude(std::string word, bool ok);
319           virtual void commandLoad(std::string packageName);
320           virtual void commandUnload(std::string packageName);
321           virtual void commandBreak();
322           virtual void commandQuit();
323           virtual void commandMessage();
324           virtual void commandMessage(std::string kind, std::string levelstr);
325           virtual void SetCurrentFileName(std::string fullPathScriptName);
326
327           /// Constructor
328           InterpreterVirtual();
329           
330           
331   private:
332
333
334     void Init();
335
336     /// Opens the file fullPathScriptName 
337     /// includeScriptName is the name as given to the include command 
338     void LoadScript( std::string fullPathScriptName,std::string includeScriptName);
339
340   
341     //==================================================================
342     // ATTRIBUTES
343
344
345     /// The user of  the interpreter (0 if none)
346     bbtk::InterpreterUser* mUser;
347
348     /// Vector of open files / buffers (can be stringstream)
349     std::vector<std::istream*> mFile;
350
351     /// Vector of names of open files with full path (as open)
352     std::vector<std::string> mFileName;
353
354     /// Vector of names of files which have been open 
355     /// before (and may closed then which are no more in mFileName)
356     /// with full path (as open)
357     std::vector<std::string> mFileNameHistory;
358
359     /// Vector of names of open files as given to the include command
360     std::vector<std::string> mIncludeFileName;
361
362     /// Stores the current line number in each open file
363     std::vector<int> mLine;
364
365     /// The dictionnary of commands
366     CommandDictType mCommandDict;
367
368     /// Are we in a command line context ?
369     bool mCommandLine;
370
371     /// Are we inside a commented-out zone ?
372     bool mInsideComment;
373     
374     /// The current interpreter status
375     ExitStatus mStatus;
376
377     /// The history of commands
378     std::deque< char* > mHistory;
379
380     int bufferNb;
381     
382     bool mThrow;
383
384     /// The break signal
385     //    BreakSignalType mBreakSignal;
386
387   protected:
388           ~InterpreterVirtual();
389
390           /// 
391           virtual void CatchInterpreterException( const InterpreterException& e );
392           virtual void CatchBbtkException( const bbtk::Exception& e );
393           virtual void CatchStdException( const std::exception& e );
394           virtual void CatchUnknownException();
395           
396           
397   };
398   // EO class Interpreter
399
400
401
402 }
403 #endif