1 /*=========================================================================
4 Module: $RCSfile: bbtkInterpreter.h,v $ $
6 Date: $Date: 2008/03/21 11:44:37 $
7 Version: $Revision: 1.19 $
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.
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.
17 =========================================================================*/
20 * \brief class Interpreter : The bbtk language interpreter
23 * \class bbtk::Interpreter
24 * \brief The bbtk language interpreter
27 #ifndef __bbtkInterpreter_h__
28 #define __bbtkInterpreter_h__
30 #include "bbtkVirtualExec.h"
31 #include "bbtkExecuter.h"
32 #include "bbtkTranscriptor.h"
40 #ifdef _USE_WXWIDGETS_
44 class BBTK_EXPORT InterpreterUser
50 virtual bool InterpreterUserHasOwnHtmlPageViewer() { return false; }
51 virtual void InterpreterUserViewHtmlPage(const std::string&) {}
56 class BBTK_EXPORT Interpreter
61 /// The enumeration of command codes == Command name
92 /// The structure storing the informations on a command
102 /// The type of dictionnary of commands
103 typedef std::map<std::string,CommandInfoType> CommandDictType;
121 /// Runs the interpretation of a file
122 ExitStatus InterpretFile( const std::string& filename);
124 /// Runs the interpretation of a buffer and deletes it !
125 ExitStatus InterpretBuffer( std::stringstream* buffer );
127 /// Runs the interpretation of a command
128 ExitStatus InterpretLine( const std::string& line );
131 /// Launches a command line interpreter (with a prompt)
132 void CommandLineInterpreter();
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); }
141 /// Puts the executer in "no exec" mode,
142 /// which creates but does not execute pipelines.
143 void SetNoExecMode(bool b) { mExecuter->SetNoExecMode(b); }
146 //typedef Executer::DialogModeType DialogModeType;
147 typedef VirtualExec::DialogModeType DialogModeType;
149 void SetDialogMode(DialogModeType t) { mExecuter->SetDialogMode(t); }
151 /// Sets the bool that indicates wether we are in command line context
152 void SetCommandLine(bool v = true) { mCommandLine = v; }
154 void SetThrow(bool b) { mThrow = b; }
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; }
168 /// Gets the Executer
169 VirtualExec* GetExecuter() { return mExecuter; }
170 /// Gets the Executer (const)
171 const VirtualExec* GetExecuter() const { return mExecuter; }
175 /// Interprets a line
176 void InterpretLine( const std::string& line, bool &insideComment );
178 /// Reads a line from prompt
179 void GetLineFromPrompt( std::string& line );
181 /// Splits a line into words
182 void SplitLine ( const std::string& line,
183 std::vector<std::string>& words );
185 /// Executes the right action depending on the command name
186 void InterpretCommand( const std::vector<std::string>& words,
187 CommandInfoType& info );
189 /// Switch to the interpretation of a file
190 void SwitchToFile( const std::string& filename );
192 /// Switch to the interpretation of a stringstream
193 void SwitchToStream( std::stringstream* stream );
195 /// Closes the currently open file
196 void CloseCurrentFile();
198 /// Closes all open files
199 void CloseAllFiles();
201 /// Displays help (entry point of any help)
202 void Help(const std::vector<std::string>& words);
204 /// Displays help on all the commands
207 /// Displays help on a particular command
208 void HelpCommand( const std::string& command );
211 void Graph(const std::vector<std::string>& words);
216 void Print(const std::string&);
218 void Index(const std::string& filename,
219 const std::string& type = "Initials");
221 void FindCommandsWithPrefix( char* buf,
223 std::vector<std::string>& commands );
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);
236 //==================================================================
239 /// The command executer used
240 bbtk::VirtualExec* mExecuter;
242 /// The user of the interpreter (0 if none)
243 bbtk::InterpreterUser* mUser;
245 /// Vector of open files / buffers (can be stringstream)
246 std::vector<std::istream*> mFile;
248 /// Vector of names of open files with full path (as open)
249 std::vector<std::string> mFileName;
251 /// Vector of names of open files as given to the include command
252 std::vector<std::string> mIncludeFileName;
254 /// Stores the current line number in each open file
255 std::vector<int> mLine;
257 /// The dictionnary of commands
258 CommandDictType mCommandDict;
260 /// Are we in a command line context ?
263 /// The history of commands
264 std::deque< char* > mHistory;
271 // EO class Interpreter
274 // The "Quit" exception
275 struct QuitException : public bbtk::Exception
277 QuitException() : bbtk::Exception("Interpreter","","Quit") {}