1 /*=========================================================================
3 Module: $RCSfile: bbtkInterpreter.h,v $
5 Date: $Date: 2008/12/12 12:11:21 $
6 Version: $Revision: 1.38 $
7 =========================================================================*/
9 /* ---------------------------------------------------------------------
11 * Copyright (c) CREATIS-LRMN (Centre de Recherche en Imagerie Medicale)
12 * Authors : Eduardo Davila, Laurent Guigues, Jean-Pierre Roux
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.
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
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 * ------------------------------------------------------------------------ */
33 * \brief class Interpreter : The bbtk language interpreter
36 * \class bbtk::Interpreter
37 * \brief The bbtk language interpreter
40 #ifndef __bbtkInterpreter_h__
41 #define __bbtkInterpreter_h__
43 #include "bbtkVirtualExec.h"
44 #include "bbtkExecuter.h"
56 //=======================================================================
57 class BBTK_EXPORT InterpreterUser
61 virtual ~InterpreterUser() {}
63 virtual bool InterpreterUserHasOwnHtmlPageViewer() { return false; }
64 virtual void InterpreterUserViewHtmlPage(const std::string&) {}
68 //=======================================================================
72 //=======================================================================
73 class BBTK_EXPORT InterpreterError : public Exception
76 InterpreterError( const std::string& message,
78 const std::string& script_file,
81 InterpreterError( const Exception& excep,
83 const std::string& script_file,
86 ~InterpreterError() throw() {}
88 bool IsInScriptFile() const { return mInScriptFile; }
89 const std::string& GetScriptFile() const { return mScriptFile; }
90 int GetScriptLine() const { return mScriptLine; }
93 std::string mScriptFile;
96 //=======================================================================
99 //=======================================================================
100 class BBTK_EXPORT Interpreter : public Object
102 BBTK_OBJECT_INTERFACE_NO_CONDES(Interpreter);
103 typedef Object Superclass;
105 static Pointer New(const std::string& cpp_file = "");
106 static Pointer New(VirtualExec::Pointer);
116 /// Runs the interpretation of a file
117 ExitStatus InterpretFile( const std::string& filename, bool source = false);
119 /// Runs the interpretation of a buffer and deletes it !
120 ExitStatus InterpretBuffer( std::stringstream* buffer );
122 /// Runs the interpretation of a command
123 ExitStatus InterpretLine( const std::string& line );
126 /// Launches a command line interpreter (with a prompt)
127 void CommandLineInterpreter();
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); }
136 /// Puts the executer in "no exec" mode,
137 /// which creates but does not execute pipelines.
138 void SetNoExecMode(bool m) { mVirtualExecuter->SetNoExecMode(m); }
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);}
144 //typedef Executer::DialogModeType DialogModeType;
145 typedef VirtualExec::DialogModeType DialogModeType;
147 void SetDialogMode(DialogModeType t) { mVirtualExecuter->SetDialogMode(t);}
149 /// Sets the bool that indicates wether we are in command line context
150 void SetCommandLine(bool v = true) { mCommandLine = v; }
152 void SetThrow(bool b) { mThrow = b; }
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; }
164 /// Gets the Executer
165 VirtualExec::Pointer GetExecuter() const { return mVirtualExecuter; }
170 /// The enumeration of command codes == Command name
205 /// The structure storing the informations on a command
210 CommandCodeType code;
215 /// The type of dictionnary of commands
216 typedef std::map<std::string,CommandInfoType> CommandDictType;
219 /// Interprets a line
220 void InterpretLine( const std::string& line, bool &insideComment );
222 /// Reads a line from prompt
223 void GetLineFromPrompt( std::string& line );
225 /// Splits a line into words
226 void SplitLine ( const std::string& line,
227 std::vector<std::string>& words );
229 /// Executes the right action depending on the command name
230 void InterpretCommand( const std::vector<std::string>& words,
231 CommandInfoType& info );
233 /// Switch to the interpretation of a file
234 void SwitchToFile( const std::string& filename, bool source = false );
236 /// Switch to the interpretation of a stringstream
237 void SwitchToStream( std::stringstream* stream );
239 /// Closes the currently open file
240 void CloseCurrentFile();
242 /// Closes all open files
243 void CloseAllFiles();
248 /// Displays help (entry point of any help)
249 void Help(const std::vector<std::string>& words);
251 /// Displays help on all the commands
254 /// Displays help on a particular command
255 void HelpCommand( const std::string& command );
258 void Graph(const std::vector<std::string>& words);
263 // void Print(const std::string&);
265 void Index(const std::string& filename,
266 const std::string& type = "Initials");
268 void FindCommandsWithPrefix( char* buf,
270 std::vector<std::string>& commands );
272 /// Creates and connects the piece of pipeline which defines a GUI
274 /// Define it as a complex box type with name instanceName+"Type"
275 /// The instance is called instanceName
276 /// and connected to the existing pipeline
277 void NewGUI(const std::string& box,
278 const std::string& instanceName);
280 void Debug(const std::string& arg);
285 Interpreter(const std::string& cpp_file = "");
287 Interpreter(VirtualExec::Pointer);
289 void Init(VirtualExec::Pointer, const std::string& cpp_file);
291 /// Opens the file fullPathScriptName
292 /// includeScriptName is the name as given to the include command
293 void LoadScript( std::string fullPathScriptName,
294 std::string includeScriptName);
297 void CatchInterpreterException( const InterpreterError& e );
298 void CatchBbtkException( const bbtk::Exception& e );
299 void CatchStdException( const std::exception& e );
300 void CatchUnknownException();
304 //==================================================================
307 /// The command executer used
308 bbtk::VirtualExec::Pointer mVirtualExecuter;
309 bbtk::Executer::WeakPointer mRealExecuter;
311 /// The user of the interpreter (0 if none)
312 bbtk::InterpreterUser* mUser;
314 /// Vector of open files / buffers (can be stringstream)
315 std::vector<std::istream*> mFile;
317 /// Vector of names of open files with full path (as open)
318 std::vector<std::string> mFileName;
320 /// Vector of names of files which have been open
321 /// before (and may closed then which are no more in mFileName)
322 /// with full path (as open)
323 std::vector<std::string> mFileNameHistory;
325 /// Vector of names of open files as given to the include command
326 std::vector<std::string> mIncludeFileName;
328 /// Stores the current line number in each open file
329 std::vector<int> mLine;
331 /// The dictionnary of commands
332 CommandDictType mCommandDict;
334 /// Are we in a command line context ?
337 /// The history of commands
338 std::deque< char* > mHistory;
348 // EO class Interpreter
351 // The "Quit" exception
352 struct QuitException : public bbtk::Exception
354 QuitException() : bbtk::Exception("Interpreter","","Quit") {}