]> Creatis software - bbtk.git/blob - kernel/src/bbtkInterpreter.h
*** empty log message ***
[bbtk.git] / kernel / src / bbtkInterpreter.h
1 /*=========================================================================                                                                               
2   Program:   bbtk
3   Module:    $RCSfile: bbtkInterpreter.h,v $
4   Language:  C++
5   Date:      $Date: 2008/12/12 12:11:21 $
6   Version:   $Revision: 1.38 $
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 __bbtkInterpreter_h__
41 #define __bbtkInterpreter_h__
42
43 #include "bbtkVirtualExec.h"
44 #include "bbtkExecuter.h"
45
46 #include <fstream>
47 #include <deque>
48
49 namespace bbtk
50 {
51
52 #ifdef USE_WXWIDGETS
53   class WxConsole;
54 #endif
55   
56   //=======================================================================
57   class BBTK_EXPORT InterpreterUser
58   {
59   public: 
60     InterpreterUser() {}
61     virtual ~InterpreterUser() {}
62
63     virtual bool InterpreterUserHasOwnHtmlPageViewer() { return false; }
64     virtual void InterpreterUserViewHtmlPage(const std::string&) {}
65
66
67   };
68   //=======================================================================
69
70
71
72   //=======================================================================
73   class BBTK_EXPORT InterpreterError : public Exception
74   {
75   public:
76     InterpreterError( const std::string& message,
77                       bool in_script_file,
78                       const std::string& script_file,
79                       int script_line 
80                       );
81     InterpreterError( const Exception& excep,
82                       bool in_script_file,
83                       const std::string& script_file,
84                       int script_line 
85                       );
86     ~InterpreterError() throw() {}
87
88     bool IsInScriptFile() const { return mInScriptFile; }
89     const std::string& GetScriptFile() const { return mScriptFile; }
90     int GetScriptLine() const { return mScriptLine; }
91   private:
92     bool mInScriptFile;
93     std::string mScriptFile;
94     int mScriptLine;
95   };
96   //=======================================================================
97
98
99   //=======================================================================
100   class BBTK_EXPORT Interpreter : public Object
101   {
102     BBTK_OBJECT_INTERFACE_NO_CONDES(Interpreter);
103     typedef Object Superclass;
104   public:
105     static Pointer New(const std::string& cpp_file = "");
106     static Pointer New(VirtualExec::Pointer);
107
108     typedef enum 
109       {
110         Interpreter_OK,
111         Interpreter_ERROR,
112         Interpreter_QUIT
113       }
114       ExitStatus;
115
116     /// Runs the interpretation of a file
117     ExitStatus InterpretFile( const std::string& filename, bool source = false);
118
119     /// Runs the interpretation of a buffer and deletes it !
120     ExitStatus InterpretBuffer( std::stringstream* buffer );
121
122     /// Runs the interpretation of a command
123     ExitStatus InterpretLine( const std::string& line );
124
125
126     /// Launches a command line interpreter (with a prompt)
127     void CommandLineInterpreter();
128
129
130
131     /// Sets the inputs of the workspace :
132     /// the map is passed as is to the Executer
133     void SetInputs(const std::map<std::string,std::string>& m)
134     { mVirtualExecuter->SetInputs(m); }
135
136     /// Puts the executer in "no exec" mode,
137     /// which creates but does not execute pipelines.
138     void SetNoExecMode(bool m) { mVirtualExecuter->SetNoExecMode(m); }
139
140     /// Puts the executer in "no error" mode, 
141     /// Errors do not stop execution (but warnings are produced)
142     void SetNoErrorMode(bool m) { mVirtualExecuter->SetNoErrorMode(m);}
143     ///
144     //typedef Executer::DialogModeType DialogModeType;
145     typedef VirtualExec::DialogModeType DialogModeType;
146
147     void SetDialogMode(DialogModeType t) { mVirtualExecuter->SetDialogMode(t);}
148
149     /// Sets the bool that indicates wether we are in command line context
150     void SetCommandLine(bool v = true) { mCommandLine = v; }
151
152     void SetThrow(bool b) { mThrow = b; }
153
154 #ifdef USE_WXWIDGETS
155     /// Sets the user of the interpreter (if any)
156     void SetUser(InterpreterUser* c) { mUser = c; }
157     /// Gets the InterpreterUser of this 
158     InterpreterUser* GetUser() { return mUser; }
159     /// Gets the InterpreterUser of this (const)
160     const InterpreterUser* GetUser() const { return mUser; }
161
162 #endif
163
164     /// Gets the Executer 
165     VirtualExec::Pointer GetExecuter() const { return mVirtualExecuter; }
166
167
168   protected:
169
170     /// The enumeration of command codes == Command name
171     typedef enum
172     {
173       cClear,
174       cNew,
175       cDelete,
176       cConnect,
177       cExec,
178       cPackage,
179       cEndPackage,
180       cDefine,
181       cEndDefine,
182       cInput,
183       cOutput,
184       cSet,
185       cConfig,  // JPR
186       cReset,   // EED
187       cAuthor, 
188       cCategory, // JPR
189       cDescription,
190       cHelp,
191       cMessage,
192       cInclude,
193       cQuit,
194       cLoad,
195       cUnload,
196       cGraph,
197       cPrint,
198       cIndex,
199       cKind, // LG
200       cNewGUI, // LG
201       cWorkspace, // LG
202       cDebug // LG
203     } CommandCodeType;
204
205     /// The structure storing the informations on a command 
206     typedef struct 
207     {
208       std::string keyword;
209       int argmin, argmax;
210       CommandCodeType code;
211       std::string syntax;
212       std::string help;
213     } CommandInfoType;
214
215     /// The type of dictionnary of commands 
216     typedef std::map<std::string,CommandInfoType> CommandDictType;
217
218
219     /// Interprets a line 
220     void InterpretLine( const std::string& line, bool &insideComment );
221
222     /// Reads a line from prompt
223     void GetLineFromPrompt( std::string& line );
224
225     /// Splits a line into words
226     void SplitLine ( const std::string& line,
227                      std::vector<std::string>& words );
228
229     /// Executes the right action depending on the command name
230     void InterpretCommand( const std::vector<std::string>& words,
231                            CommandInfoType& info );
232
233     /// Switch to the interpretation of a file
234     void SwitchToFile( const std::string& filename, bool source = false );
235
236    /// Switch to the interpretation of a stringstream
237     void SwitchToStream( std::stringstream* stream );
238
239     /// Closes the currently open file
240     void CloseCurrentFile();
241
242     /// Closes all open files
243     void CloseAllFiles();
244
245     /// Resets all
246     void Reset();
247
248     /// Displays help (entry point of any help)
249     void Help(const std::vector<std::string>& words);
250
251     /// Displays help on all the commands
252     void HelpCommands();
253
254     /// Displays help on a particular command 
255     void HelpCommand( const std::string& command );
256
257     ///
258     void Graph(const std::vector<std::string>& words);
259
260     ///
261     void Config() const; 
262     /// 
263     //    void Print(const std::string&);
264
265     void Index(const std::string& filename, 
266                const std::string& type = "Initials");
267     ///
268     void FindCommandsWithPrefix( char* buf,
269                                  int n,
270                                  std::vector<std::string>& commands );
271
272     /// Creates and connects the piece of pipeline which defines a GUI 
273     /// for the box box. 
274     /// Define it as a complex box type with name instanceName+"Type"
275     /// The instance is called instanceName
276     /// and connected to the existing pipeline
277     void NewGUI(const std::string& box, 
278                 const std::string& instanceName);
279
280     void Debug(const std::string& arg);
281
282   private:
283
284     /// Constructor
285     Interpreter(const std::string& cpp_file = "");
286
287     Interpreter(VirtualExec::Pointer);
288
289     void Init(VirtualExec::Pointer, const std::string& cpp_file);
290
291     /// Opens the file fullPathScriptName 
292     /// includeScriptName is the name as given to the include command 
293     void LoadScript( std::string fullPathScriptName,
294                      std::string includeScriptName);
295
296     /// 
297     void CatchInterpreterException( const InterpreterError& e );
298     void CatchBbtkException( const bbtk::Exception& e );
299     void CatchStdException( const std::exception& e );
300     void CatchUnknownException();
301
302
303   
304     //==================================================================
305     // ATTRIBUTES
306
307     /// The command executer used
308     bbtk::VirtualExec::Pointer mVirtualExecuter;
309     bbtk::Executer::WeakPointer mRealExecuter;
310
311     /// The user of  the interpreter (0 if none)
312     bbtk::InterpreterUser* mUser;
313
314     /// Vector of open files / buffers (can be stringstream)
315     std::vector<std::istream*> mFile;
316
317     /// Vector of names of open files with full path (as open)
318     std::vector<std::string> mFileName;
319
320     /// Vector of names of files which have been open 
321     /// before (and may closed then which are no more in mFileName)
322     /// with full path (as open)
323     std::vector<std::string> mFileNameHistory;
324
325     /// Vector of names of open files as given to the include command
326     std::vector<std::string> mIncludeFileName;
327
328     /// Stores the current line number in each open file
329     std::vector<int> mLine;
330
331     /// The dictionnary of commands
332     CommandDictType mCommandDict;
333
334     /// Are we in a command line context ?
335     bool mCommandLine;
336
337     /// The history of commands
338     std::deque< char* > mHistory;
339
340    int bufferNb;
341     
342     bool mThrow;
343
344         protected:
345         ~Interpreter();
346
347   };
348   // EO class Interpreter
349
350
351   // The "Quit" exception
352   struct QuitException : public bbtk::Exception
353   {
354     QuitException() : bbtk::Exception("Interpreter","","Quit") {}
355   };
356
357 }
358 #endif