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