]> 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/09 11:16:57 $
7   Version:   $Revision: 1.25 $
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       cKind, // LG
115       cNewGUI, // LG
116       cWorkspace // LG
117     } CommandCodeType;
118
119     /// The structure storing the informations on a command 
120     typedef struct 
121     {
122       std::string keyword;
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(const std::string& cpp_file = "");
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 #ifdef _USE_WXWIDGETS_
184     /// Sets the user of the interpreter (if any)
185     void SetUser(InterpreterUser* c) { mUser = c; }
186     /// Gets the InterpreterUser of this 
187     InterpreterUser* GetUser() { return mUser; }
188     /// Gets the InterpreterUser of this (const)
189     const InterpreterUser* GetUser() const { return mUser; }
190
191 #endif
192
193     /// Gets the Executer 
194     VirtualExec* GetExecuter() { return mExecuter; }
195     /// Gets the Executer (const)
196     const VirtualExec* GetExecuter() const { return mExecuter; }
197
198
199   protected:
200     /// Interprets a line 
201     void InterpretLine( const std::string& line, bool &insideComment );
202
203     /// Reads a line from prompt
204     void GetLineFromPrompt( std::string& line );
205
206     /// Splits a line into words
207     void SplitLine ( const std::string& line,
208                      std::vector<std::string>& words );
209
210     /// Executes the right action depending on the command name
211     void InterpretCommand( const std::vector<std::string>& words,
212                            CommandInfoType& info );
213
214     /// Switch to the interpretation of a file
215     void SwitchToFile( const std::string& filename );
216
217    /// Switch to the interpretation of a stringstream
218     void SwitchToStream( std::stringstream* stream );
219
220     /// Closes the currently open file
221     void CloseCurrentFile();
222
223     /// Closes all open files
224     void CloseAllFiles();
225
226     /// Displays help (entry point of any help)
227     void Help(const std::vector<std::string>& words);
228
229     /// Displays help on all the commands
230     void HelpCommands();
231
232     /// Displays help on a particular command 
233     void HelpCommand( const std::string& command );
234
235     ///
236     void Graph(const std::vector<std::string>& words);
237
238     ///
239     void Config() const; 
240     /// 
241     //    void Print(const std::string&);
242
243     void Index(const std::string& filename, 
244                const std::string& type = "Initials");
245     ///
246     void FindCommandsWithPrefix( char* buf,
247                                  int n,
248                                  std::vector<std::string>& commands );
249
250     /// Creates and connects the piece of pipeline which defines a GUI 
251     /// for the box box. 
252     /// Define it as a complex box type with name instanceName+"Type"
253     /// The instance is called instanceName
254     /// and connected to the existing pipeline
255     void NewGUI(const std::string& box, 
256                 const std::string& instanceName);
257
258   private:
259
260     /// Opens the file fullPathScriptName 
261     /// includeScriptName is the name as given to the include command 
262     void LoadScript( std::string fullPathScriptName,
263                      std::string includeScriptName);
264
265     /// 
266     void CatchBbtkException( const bbtk::Exception& e );
267     void CatchStdException( const std::exception& e );
268     void CatchUnknownException();
269
270   private:
271
272     //==================================================================
273     // ATTRIBUTES
274
275     /// The command executer used
276     bbtk::VirtualExec* mExecuter;
277
278     /// The user of  the interpreter (0 if none)
279     bbtk::InterpreterUser* mUser;
280
281     /// Vector of open files / buffers (can be stringstream)
282     std::vector<std::istream*> mFile;
283
284     /// Vector of names of open files with full path (as open)
285     std::vector<std::string> mFileName;
286
287     /// Vector of names of files which have been open 
288     /// before (and may closed then which are no more in mFileName)
289     /// with full path (as open)
290     std::vector<std::string> mFileNameHistory;
291
292     /// Vector of names of open files as given to the include command
293     std::vector<std::string> mIncludeFileName;
294
295     /// Stores the current line number in each open file
296     std::vector<int> mLine;
297
298     /// The dictionnary of commands
299     CommandDictType mCommandDict;
300
301     /// Are we in a command line context ?
302     bool mCommandLine;
303
304     /// The history of commands
305     std::deque< char* > mHistory;
306
307    int bufferNb;
308     
309     bool mThrow;
310
311   };
312   // EO class Interpreter
313
314
315   // The "Quit" exception
316   struct QuitException : public bbtk::Exception
317   {
318     QuitException() : bbtk::Exception("Interpreter","","Quit") {}
319   };
320
321 }
322 #endif