]> 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/03/21 14:59:39 $
7   Version:   $Revision: 1.20 $
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 #include "bbtkTranscriptor.h"
33
34 #include <fstream>
35 #include <deque>
36
37 namespace bbtk
38 {
39
40 #ifdef _USE_WXWIDGETS_
41   class WxConsole;
42 #endif
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   class BBTK_EXPORT InterpreterError : public Exception
58   {
59   public:
60     InterpreterError( const std::string& message,
61                       bool in_script_file,
62                       const std::string& script_file,
63                       int script_line 
64                       );
65     InterpreterError( const Exception& excep,
66                       bool in_script_file,
67                       const std::string& script_file,
68                       int script_line 
69                       );
70     ~InterpreterError() throw() {}
71
72     bool IsInScriptFile() const { return mInScriptFile; }
73     const std::string& GetScriptFile() const { return mScriptFile; }
74     int GetScriptLine() const { return mScriptLine; }
75   private:
76     bool mInScriptFile;
77     std::string mScriptFile;
78     int mScriptLine;
79   };
80
81
82   /// 
83   class BBTK_EXPORT Interpreter
84   {
85
86   private:
87
88     /// The enumeration of command codes == Command name
89     typedef enum
90     {
91       cNew,
92       cDelete,
93       cConnect,
94       cExec,
95       cPackage,
96       cEndPackage,
97       cDefine,
98       cEndDefine,
99       cInput,
100       cOutput,
101       cSet,
102       cConfig,  // JPR
103       cReset,   // EED
104       cAuthor, 
105       cCategory, // JPR
106       cDescription,
107       cHelp,
108       cMessage,
109       cInclude,
110       cQuit,
111       cLoad,
112       cUnload,
113       cGraph,
114       cPrint,
115       cIndex,
116       cWorkspace // LG
117     } CommandCodeType;
118
119     /// The structure storing the informations on a command 
120     typedef struct 
121     {
122       std::string category;
123       int argmin, argmax;
124       CommandCodeType code;
125       std::string syntax;
126       std::string help;
127     } CommandInfoType;
128
129     /// The type of dictionnary of commands 
130     typedef std::map<std::string,CommandInfoType> CommandDictType;
131
132   public:
133     /// Constructor
134     Interpreter();
135
136     /// Destructor
137     ~Interpreter();
138
139
140     typedef enum 
141       {
142         Interpreter_OK,
143         Interpreter_ERROR,
144         Interpreter_QUIT
145       }
146       ExitStatus;
147
148     /// Runs the interpretation of a file
149     ExitStatus InterpretFile( const std::string& filename);
150
151     /// Runs the interpretation of a buffer and deletes it !
152     ExitStatus InterpretBuffer( std::stringstream* buffer );
153
154     /// Runs the interpretation of a command
155     ExitStatus InterpretLine( const std::string& line );
156
157
158     /// Launches a command line interpreter (with a prompt)
159     void CommandLineInterpreter();
160
161
162
163     /// Sets the inputs of the workspace :
164     /// the map is passed as is to the Executer
165     void SetInputs(const std::map<std::string,std::string>& m)
166     { mExecuter->SetInputs(m); }
167
168     /// Puts the executer in "no exec" mode,
169     /// which creates but does not execute pipelines.
170     void SetNoExecMode(bool b) { mExecuter->SetNoExecMode(b); }
171
172     ///
173     //typedef Executer::DialogModeType DialogModeType;
174     typedef VirtualExec::DialogModeType DialogModeType;
175
176     void SetDialogMode(DialogModeType t) { mExecuter->SetDialogMode(t); }
177
178     /// Sets the bool that indicates wether we are in command line context
179     void SetCommandLine(bool v = true) { mCommandLine = v; }
180
181     void SetThrow(bool b) { mThrow = b; }
182
183  
184     
185 #ifdef _USE_WXWIDGETS_
186     /// Sets the user of the interpreter (if any)
187     void SetUser(InterpreterUser* c) { mUser = c; }
188     /// Gets the InterpreterUser of this 
189     InterpreterUser* GetUser() { return mUser; }
190     /// Gets the InterpreterUser of this (const)
191     const InterpreterUser* GetUser() const { return mUser; }
192
193 #endif
194
195     /// Gets the Executer 
196     VirtualExec* GetExecuter() { return mExecuter; }
197     /// Gets the Executer (const)
198     const VirtualExec* GetExecuter() const { return mExecuter; }
199
200
201   protected:
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     /// Displays help (entry point of any help)
229     void Help(const std::vector<std::string>& words);
230
231     /// Displays help on all the commands
232     void HelpCommands();
233
234     /// Displays help on a particular command 
235     void HelpCommand( const std::string& command );
236
237     ///
238     void Graph(const std::vector<std::string>& words);
239
240     ///
241     void Config() const; 
242     /// 
243     void Print(const std::string&);
244
245     void Index(const std::string& filename, 
246                const std::string& type = "Initials");
247     ///
248     void FindCommandsWithPrefix( char* buf,
249                                  int n,
250                                  std::vector<std::string>& commands );
251
252
253
254   private:
255
256     /// Opens the file fullPathScriptName 
257     /// includeScriptName is the name as given to the include command 
258     void LoadScript( std::string fullPathScriptName,
259                      std::string includeScriptName);
260
261     /// 
262     void CatchBbtkException( const bbtk::Exception& e );
263     void CatchStdException( const std::exception& e );
264     void CatchUnknownException();
265
266   private:
267
268     //==================================================================
269     // ATTRIBUTES
270
271     /// The command executer used
272     bbtk::VirtualExec* mExecuter;
273
274     /// The user of  the interpreter (0 if none)
275     bbtk::InterpreterUser* mUser;
276
277     /// Vector of open files / buffers (can be stringstream)
278     std::vector<std::istream*> mFile;
279
280     /// Vector of names of open files with full path (as open)
281     std::vector<std::string> mFileName;
282
283     /// Vector of names of open files as given to the include command
284     std::vector<std::string> mIncludeFileName;
285
286     /// Stores the current line number in each open file
287     std::vector<int> mLine;
288
289     /// The dictionnary of commands
290     CommandDictType mCommandDict;
291
292     /// Are we in a command line context ?
293     bool mCommandLine;
294
295     /// The history of commands
296     std::deque< char* > mHistory;
297
298    int bufferNb;
299     
300     bool mThrow;
301
302   };
303   // EO class Interpreter
304
305
306   // The "Quit" exception
307   struct QuitException : public bbtk::Exception
308   {
309     QuitException() : bbtk::Exception("Interpreter","","Quit") {}
310   };
311
312 }
313 #endif