]> Creatis software - bbtk.git/blob - kernel/src/bbtkInterpreter.h
*** empty log message ***
[bbtk.git] / kernel / src / bbtkInterpreter.h
1 /*=========================================================================
2
3   Program:   bbtk
4   Module:    $RCSfile: bbtkInterpreter.h,v $ $
5   Language:  C++
6   Date:      $Date: 2008/07/23 11:46:11 $
7   Version:   $Revision: 1.32 $
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);
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 b) { mVirtualExecuter->SetNoExecMode(b); }
126
127     ///
128     //typedef Executer::DialogModeType DialogModeType;
129     typedef VirtualExec::DialogModeType DialogModeType;
130
131     void SetDialogMode(DialogModeType t) 
132     { mVirtualExecuter->SetDialogMode(t); }
133
134     /// Sets the bool that indicates wether we are in command line context
135     void SetCommandLine(bool v = true) { mCommandLine = v; }
136
137     void SetThrow(bool b) { mThrow = b; }
138
139 #ifdef _USE_WXWIDGETS_
140     /// Sets the user of the interpreter (if any)
141     void SetUser(InterpreterUser* c) { mUser = c; }
142     /// Gets the InterpreterUser of this 
143     InterpreterUser* GetUser() { return mUser; }
144     /// Gets the InterpreterUser of this (const)
145     const InterpreterUser* GetUser() const { return mUser; }
146
147 #endif
148
149     /// Gets the Executer 
150     VirtualExec::Pointer GetExecuter() const { return mVirtualExecuter; }
151
152
153   protected:
154
155     /// The enumeration of command codes == Command name
156     typedef enum
157     {
158       cNew,
159       cDelete,
160       cConnect,
161       cExec,
162       cPackage,
163       cEndPackage,
164       cDefine,
165       cEndDefine,
166       cInput,
167       cOutput,
168       cSet,
169       cConfig,  // JPR
170       cReset,   // EED
171       cAuthor, 
172       cCategory, // JPR
173       cDescription,
174       cHelp,
175       cMessage,
176       cInclude,
177       cQuit,
178       cLoad,
179       cUnload,
180       cGraph,
181       cPrint,
182       cIndex,
183       cKind, // LG
184       cNewGUI, // LG
185       cWorkspace, // LG
186       cDebug // LG
187     } CommandCodeType;
188
189     /// The structure storing the informations on a command 
190     typedef struct 
191     {
192       std::string keyword;
193       int argmin, argmax;
194       CommandCodeType code;
195       std::string syntax;
196       std::string help;
197     } CommandInfoType;
198
199     /// The type of dictionnary of commands 
200     typedef std::map<std::string,CommandInfoType> CommandDictType;
201
202
203     /// Interprets a line 
204     void InterpretLine( const std::string& line, bool &insideComment );
205
206     /// Reads a line from prompt
207     void GetLineFromPrompt( std::string& line );
208
209     /// Splits a line into words
210     void SplitLine ( const std::string& line,
211                      std::vector<std::string>& words );
212
213     /// Executes the right action depending on the command name
214     void InterpretCommand( const std::vector<std::string>& words,
215                            CommandInfoType& info );
216
217     /// Switch to the interpretation of a file
218     void SwitchToFile( const std::string& filename );
219
220    /// Switch to the interpretation of a stringstream
221     void SwitchToStream( std::stringstream* stream );
222
223     /// Closes the currently open file
224     void CloseCurrentFile();
225
226     /// Closes all open files
227     void CloseAllFiles();
228
229     /// Resets all
230     void Reset();
231
232     /// Displays help (entry point of any help)
233     void Help(const std::vector<std::string>& words);
234
235     /// Displays help on all the commands
236     void HelpCommands();
237
238     /// Displays help on a particular command 
239     void HelpCommand( const std::string& command );
240
241     ///
242     void Graph(const std::vector<std::string>& words);
243
244     ///
245     void Config() const; 
246     /// 
247     //    void Print(const std::string&);
248
249     void Index(const std::string& filename, 
250                const std::string& type = "Initials");
251     ///
252     void FindCommandsWithPrefix( char* buf,
253                                  int n,
254                                  std::vector<std::string>& commands );
255
256     /// Creates and connects the piece of pipeline which defines a GUI 
257     /// for the box box. 
258     /// Define it as a complex box type with name instanceName+"Type"
259     /// The instance is called instanceName
260     /// and connected to the existing pipeline
261     void NewGUI(const std::string& box, 
262                 const std::string& instanceName);
263
264     void Debug(const std::string& arg);
265
266   private:
267
268     /// Constructor
269     Interpreter(const std::string& cpp_file = "");
270
271     Interpreter(VirtualExec::Pointer);
272
273     void Init(VirtualExec::Pointer, const std::string& cpp_file);
274
275     /// Opens the file fullPathScriptName 
276     /// includeScriptName is the name as given to the include command 
277     void LoadScript( std::string fullPathScriptName,
278                      std::string includeScriptName);
279
280     /// 
281     void CatchBbtkException( const bbtk::Exception& e );
282     void CatchStdException( const std::exception& e );
283     void CatchUnknownException();
284
285
286   
287     //==================================================================
288     // ATTRIBUTES
289
290     /// The command executer used
291     bbtk::VirtualExec::Pointer mVirtualExecuter;
292     bbtk::Executer::WeakPointer mRealExecuter;
293
294     /// The user of  the interpreter (0 if none)
295     bbtk::InterpreterUser* mUser;
296
297     /// Vector of open files / buffers (can be stringstream)
298     std::vector<std::istream*> mFile;
299
300     /// Vector of names of open files with full path (as open)
301     std::vector<std::string> mFileName;
302
303     /// Vector of names of files which have been open 
304     /// before (and may closed then which are no more in mFileName)
305     /// with full path (as open)
306     std::vector<std::string> mFileNameHistory;
307
308     /// Vector of names of open files as given to the include command
309     std::vector<std::string> mIncludeFileName;
310
311     /// Stores the current line number in each open file
312     std::vector<int> mLine;
313
314     /// The dictionnary of commands
315     CommandDictType mCommandDict;
316
317     /// Are we in a command line context ?
318     bool mCommandLine;
319
320     /// The history of commands
321     std::deque< char* > mHistory;
322
323    int bufferNb;
324     
325     bool mThrow;
326
327         protected:
328         ~Interpreter();
329
330   };
331   // EO class Interpreter
332
333
334   // The "Quit" exception
335   struct QuitException : public bbtk::Exception
336   {
337     QuitException() : bbtk::Exception("Interpreter","","Quit") {}
338   };
339
340 }
341 #endif