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