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