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