]> Creatis software - bbtk.git/blob - kernel/src/bbtkInterpreter.h
4b6669cf7ae00ee788e4f72158754dac4e67dcec
[bbtk.git] / kernel / src / bbtkInterpreter.h
1 /*=========================================================================                                                                               
2   Program:   bbtk
3   Module:    $RCSfile: bbtkInterpreter.h,v $
4   Language:  C++
5   Date:      $Date: 2008/11/26 12:36:43 $
6   Version:   $Revision: 1.36 $
7 =========================================================================*/
8
9 /* ---------------------------------------------------------------------
10
11 * Copyright (c) CREATIS-LRMN (Centre de Recherche en Imagerie Medicale)
12 * Authors : Eduardo Davila, Laurent Guigues, Jean-Pierre Roux
13 *
14 *  This software is governed by the CeCILL-B license under French law and 
15 *  abiding by the rules of distribution of free software. You can  use, 
16 *  modify and/ or redistribute the software under the terms of the CeCILL-B 
17 *  license as circulated by CEA, CNRS and INRIA at the following URL 
18 *  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html 
19 *  or in the file LICENSE.txt.
20 *
21 *  As a counterpart to the access to the source code and  rights to copy,
22 *  modify and redistribute granted by the license, users are provided only
23 *  with a limited warranty  and the software's author,  the holder of the
24 *  economic rights,  and the successive licensors  have only  limited
25 *  liability. 
26 *
27 *  The fact that you are presently reading this means that you have had
28 *  knowledge of the CeCILL-B license and that you accept its terms.
29 * ------------------------------------------------------------------------ */                                                                         
30
31 /**
32  *  \file 
33  *  \brief class Interpreter : The bbtk language interpreter
34  */
35 /**
36  *  \class bbtk::Interpreter 
37  *  \brief The bbtk language interpreter
38  */
39
40 #ifndef __bbtkInterpreter_h__
41 #define __bbtkInterpreter_h__
42
43 #include "bbtkVirtualExec.h"
44 #include "bbtkExecuter.h"
45
46 #include <fstream>
47 #include <deque>
48
49 namespace bbtk
50 {
51
52 #ifdef _USE_WXWIDGETS_
53   class WxConsole;
54 #endif
55   
56   //=======================================================================
57   class BBTK_EXPORT InterpreterUser
58   {
59   public: 
60     InterpreterUser() {}
61     virtual ~InterpreterUser() {}
62
63     virtual bool InterpreterUserHasOwnHtmlPageViewer() { return false; }
64     virtual void InterpreterUserViewHtmlPage(const std::string&) {}
65
66
67   };
68   //=======================================================================
69
70
71
72   //=======================================================================
73   class BBTK_EXPORT InterpreterError : public Exception
74   {
75   public:
76     InterpreterError( const std::string& message,
77                       bool in_script_file,
78                       const std::string& script_file,
79                       int script_line 
80                       );
81     InterpreterError( const Exception& excep,
82                       bool in_script_file,
83                       const std::string& script_file,
84                       int script_line 
85                       );
86     ~InterpreterError() throw() {}
87
88     bool IsInScriptFile() const { return mInScriptFile; }
89     const std::string& GetScriptFile() const { return mScriptFile; }
90     int GetScriptLine() const { return mScriptLine; }
91   private:
92     bool mInScriptFile;
93     std::string mScriptFile;
94     int mScriptLine;
95   };
96   //=======================================================================
97
98
99   //=======================================================================
100   class BBTK_EXPORT Interpreter : public Object
101   {
102     BBTK_OBJECT_INTERFACE_NO_CONDES(Interpreter);
103     typedef Object Superclass;
104   public:
105     static Pointer New(const std::string& cpp_file = "");
106     static Pointer New(VirtualExec::Pointer);
107
108     typedef enum 
109       {
110         Interpreter_OK,
111         Interpreter_ERROR,
112         Interpreter_QUIT
113       }
114       ExitStatus;
115
116     /// Runs the interpretation of a file
117     ExitStatus InterpretFile( const std::string& filename, bool source = false);
118
119     /// Runs the interpretation of a buffer and deletes it !
120     ExitStatus InterpretBuffer( std::stringstream* buffer );
121
122     /// Runs the interpretation of a command
123     ExitStatus InterpretLine( const std::string& line );
124
125
126     /// Launches a command line interpreter (with a prompt)
127     void CommandLineInterpreter();
128
129
130
131     /// Sets the inputs of the workspace :
132     /// the map is passed as is to the Executer
133     void SetInputs(const std::map<std::string,std::string>& m)
134     { mVirtualExecuter->SetInputs(m); }
135
136     /// Puts the executer in "no exec" mode,
137     /// which creates but does not execute pipelines.
138     void SetNoExecMode(bool m) { mVirtualExecuter->SetNoExecMode(m); }
139
140     /// Puts the executer in "no error" mode, 
141     /// Errors do not stop execution (but warnings are produced)
142     void SetNoErrorMode(bool m) { mVirtualExecuter->SetNoErrorMode(m);}
143     ///
144     //typedef Executer::DialogModeType DialogModeType;
145     typedef VirtualExec::DialogModeType DialogModeType;
146
147     void SetDialogMode(DialogModeType t) { mVirtualExecuter->SetDialogMode(t);}
148
149     /// Sets the bool that indicates wether we are in command line context
150     void SetCommandLine(bool v = true) { mCommandLine = v; }
151
152     void SetThrow(bool b) { mThrow = b; }
153
154 #ifdef _USE_WXWIDGETS_
155     /// Sets the user of the interpreter (if any)
156     void SetUser(InterpreterUser* c) { mUser = c; }
157     /// Gets the InterpreterUser of this 
158     InterpreterUser* GetUser() { return mUser; }
159     /// Gets the InterpreterUser of this (const)
160     const InterpreterUser* GetUser() const { return mUser; }
161
162 #endif
163
164     /// Gets the Executer 
165     VirtualExec::Pointer GetExecuter() const { return mVirtualExecuter; }
166
167
168   protected:
169
170     /// The enumeration of command codes == Command name
171     typedef enum
172     {
173       cNew,
174       cDelete,
175       cConnect,
176       cExec,
177       cPackage,
178       cEndPackage,
179       cDefine,
180       cEndDefine,
181       cInput,
182       cOutput,
183       cSet,
184       cConfig,  // JPR
185       cReset,   // EED
186       cAuthor, 
187       cCategory, // JPR
188       cDescription,
189       cHelp,
190       cMessage,
191       cInclude,
192       cQuit,
193       cLoad,
194       cUnload,
195       cGraph,
196       cPrint,
197       cIndex,
198       cKind, // LG
199       cNewGUI, // LG
200       cWorkspace, // LG
201       cDebug // LG
202     } CommandCodeType;
203
204     /// The structure storing the informations on a command 
205     typedef struct 
206     {
207       std::string keyword;
208       int argmin, argmax;
209       CommandCodeType code;
210       std::string syntax;
211       std::string help;
212     } CommandInfoType;
213
214     /// The type of dictionnary of commands 
215     typedef std::map<std::string,CommandInfoType> CommandDictType;
216
217
218     /// Interprets a line 
219     void InterpretLine( const std::string& line, bool &insideComment );
220
221     /// Reads a line from prompt
222     void GetLineFromPrompt( std::string& line );
223
224     /// Splits a line into words
225     void SplitLine ( const std::string& line,
226                      std::vector<std::string>& words );
227
228     /// Executes the right action depending on the command name
229     void InterpretCommand( const std::vector<std::string>& words,
230                            CommandInfoType& info );
231
232     /// Switch to the interpretation of a file
233     void SwitchToFile( const std::string& filename, bool source = false );
234
235    /// Switch to the interpretation of a stringstream
236     void SwitchToStream( std::stringstream* stream );
237
238     /// Closes the currently open file
239     void CloseCurrentFile();
240
241     /// Closes all open files
242     void CloseAllFiles();
243
244     /// Resets all
245     void Reset();
246
247     /// Displays help (entry point of any help)
248     void Help(const std::vector<std::string>& words);
249
250     /// Displays help on all the commands
251     void HelpCommands();
252
253     /// Displays help on a particular command 
254     void HelpCommand( const std::string& command );
255
256     ///
257     void Graph(const std::vector<std::string>& words);
258
259     ///
260     void Config() const; 
261     /// 
262     //    void Print(const std::string&);
263
264     void Index(const std::string& filename, 
265                const std::string& type = "Initials");
266     ///
267     void FindCommandsWithPrefix( char* buf,
268                                  int n,
269                                  std::vector<std::string>& commands );
270
271     /// Creates and connects the piece of pipeline which defines a GUI 
272     /// for the box box. 
273     /// Define it as a complex box type with name instanceName+"Type"
274     /// The instance is called instanceName
275     /// and connected to the existing pipeline
276     void NewGUI(const std::string& box, 
277                 const std::string& instanceName);
278
279     void Debug(const std::string& arg);
280
281   private:
282
283     /// Constructor
284     Interpreter(const std::string& cpp_file = "");
285
286     Interpreter(VirtualExec::Pointer);
287
288     void Init(VirtualExec::Pointer, const std::string& cpp_file);
289
290     /// Opens the file fullPathScriptName 
291     /// includeScriptName is the name as given to the include command 
292     void LoadScript( std::string fullPathScriptName,
293                      std::string includeScriptName);
294
295     /// 
296     void CatchInterpreterException( const InterpreterError& e );
297     void CatchBbtkException( const bbtk::Exception& e );
298     void CatchStdException( const std::exception& e );
299     void CatchUnknownException();
300
301
302   
303     //==================================================================
304     // ATTRIBUTES
305
306     /// The command executer used
307     bbtk::VirtualExec::Pointer mVirtualExecuter;
308     bbtk::Executer::WeakPointer mRealExecuter;
309
310     /// The user of  the interpreter (0 if none)
311     bbtk::InterpreterUser* mUser;
312
313     /// Vector of open files / buffers (can be stringstream)
314     std::vector<std::istream*> mFile;
315
316     /// Vector of names of open files with full path (as open)
317     std::vector<std::string> mFileName;
318
319     /// Vector of names of files which have been open 
320     /// before (and may closed then which are no more in mFileName)
321     /// with full path (as open)
322     std::vector<std::string> mFileNameHistory;
323
324     /// Vector of names of open files as given to the include command
325     std::vector<std::string> mIncludeFileName;
326
327     /// Stores the current line number in each open file
328     std::vector<int> mLine;
329
330     /// The dictionnary of commands
331     CommandDictType mCommandDict;
332
333     /// Are we in a command line context ?
334     bool mCommandLine;
335
336     /// The history of commands
337     std::deque< char* > mHistory;
338
339    int bufferNb;
340     
341     bool mThrow;
342
343         protected:
344         ~Interpreter();
345
346   };
347   // EO class Interpreter
348
349
350   // The "Quit" exception
351   struct QuitException : public bbtk::Exception
352   {
353     QuitException() : bbtk::Exception("Interpreter","","Quit") {}
354   };
355
356 }
357 #endif