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