1 /*=========================================================================
3 Module: $RCSfile: bbtkInterpreter.h,v $
5 Date: $Date: 2010/09/14 07:18:47 $
6 Version: $Revision: 1.41 $
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 __bbtkInterpreterVirtual_h__
41 #define __bbtkInterpreterVirtual_h__
43 //#include "bbtkSystem.h"
45 #include "bbtkMessageManager.h"
46 #include "bbtkException.h"
47 #include "bbtkObject.h"
53 // Signal/slot mechanism for 'break' commands
54 #include <boost/signal.hpp>
55 #include <boost/bind.hpp>
61 //=======================================================================
62 class BBTK_EXPORT InterpreterUser
66 virtual ~InterpreterUser() {}
68 virtual bool InterpreterUserHasOwnHtmlPageViewer() { return false; }
69 virtual void InterpreterUserViewHtmlPage(const std::string&) {}
73 //=======================================================================
77 //=======================================================================
78 class BBTK_EXPORT InterpreterException : public Exception
81 InterpreterException( const std::string& message,
83 const std::string& script_file,
86 InterpreterException( const Exception& excep,
88 const std::string& script_file,
91 ~InterpreterException() throw() {}
93 bool IsInScriptFile() const { return mInScriptFile; }
94 const std::string& GetScriptFile() const { return mScriptFile; }
95 int GetScriptLine() const { return mScriptLine; }
98 std::string mScriptFile;
101 //=======================================================================
104 //=======================================================================
105 // The "Quit" exception
106 class BBTK_EXPORT QuitException : public InterpreterError
109 QuitException( bool in_script_file,
110 const std::string& script_file,
113 : InterpreterError("QUIT",in_script_file,script_file,script_line)
115 ~QuitException() throw() {}
117 //=======================================================================
118 const std::string BREAK("BREAK");
119 //=======================================================================
120 // The "Break" exception
121 class BBTK_EXPORT BreakException : public InterpreterError
124 BreakException( bool in_script_file,
125 std::string script_file,
128 : InterpreterError(BREAK,in_script_file,script_file,script_line)
129 { std::cout << "BUILDING BREAK"<<std::endl; }
130 ~BreakException() throw() {}
132 //=======================================================================
135 //=======================================================================
136 class BBTK_EXPORT InterpreterVirtual : public Object
138 BBTK_OBJECT_INTERFACE_NO_CONDES(InterpreterVirtual);
139 typedef Object Superclass;
141 static Pointer New();
152 /// Runs the interpretation of a file
153 ExitStatus InterpretFile( const std::string& filename, bool source = false);
155 /// Runs the interpretation of a buffer and deletes it !
156 ExitStatus InterpretBuffer( std::stringstream* buffer );
158 /// Runs the interpretation of a command
159 ExitStatus InterpretLine( const std::string& line );
161 /// Runs the interpretation of the currently open streams
162 ExitStatus InterpretCurrentStreams();
164 /// Launches a command line interpreter (with a prompt)
165 void CommandLineInterpreter();
169 /// Sets the bool that indicates wether we are in command line context
170 void SetCommandLine(bool v = true) { mCommandLine = v; }
172 void SetThrow(bool b) { mThrow = b; }
175 /// Sets the user of the interpreter (if any)
176 void SetUser(InterpreterUser* c) { mUser = c; }
177 /// Gets the InterpreterUser of this
178 InterpreterUser* GetUser() { return mUser; }
179 /// Gets the InterpreterUser of this (const)
180 const InterpreterUser* GetUser() const { return mUser; }
188 // For 'break' commands observation
189 typedef boost::signals::trackable BreakObserverType;
190 typedef boost::signal<void ()> BreakSignalType;
191 typedef BreakSignalType::slot_function_type BreakCallbackType;
193 // Adds a callback when 'break' command issued
194 void AddBreakObserver( BreakCallbackType );
199 /// The enumeration of command codes == Command name
202 cBreak, // LG 12/12/08 : Stops the current script execution (if not exec frozen) - used in tutorial + debugging
203 cClear, // LG 12/12/08 : Clears the current complex black box (e.g. workspace) - used in tours
235 /// The structure storing the informations on a command
240 CommandCodeType code;
245 /// The type of dictionnary of commands
246 typedef std::map<std::string,CommandInfoType> CommandDictType;
249 /// Interprets a line
250 void DoInterpretLine( const std::string& line ); //, bool &insideComment );
252 /// Reads a line from prompt
253 void GetLineFromPrompt( std::string& line );
255 /// Splits a line into words
256 void SplitLine ( const std::string& line,
257 std::vector<std::string>& words );
259 /// Executes the right action depending on the command name
260 void InterpretCommand( const std::vector<std::string>& words,
261 CommandInfoType& info );
263 /// Switch to the interpretation of a file
264 void SwitchToFile( const std::string& filename, bool source = false );
266 /// Switch to the interpretation of a stringstream
267 void SwitchToStream( std::stringstream* stream );
269 /// Closes the currently open file
270 void CloseCurrentFile();
272 /// Closes all open files
273 void CloseAllFiles();
276 virtual void commandReset();
278 /// Displays help (entry point of any help)
279 virtual void commandHelp(const std::string &words);
280 virtual void commandHelp(const std::vector<std::string>& words);
283 virtual void commandGraph(const std::vector<std::string>& words);
286 virtual void commandConfig() const;
288 // void Print(const std::string&);
290 void commandIndex(const std::string& filename,const std::string& type = "Initials");
292 void FindCommandsWithPrefix( char* buf,
294 std::vector<std::string>& commands );
296 /// Creates and connects the piece of pipeline which defines a GUI
298 /// Define it as a complex box type with name instanceName+"Type"
299 /// The instance is called instanceName
300 /// and connected to the existing pipeline
301 virtual void commandNewGUI(const std::string& box,const std::string& instanceName);
302 virtual void commandDebug(const std::string& arg);
303 virtual void commandNew(const std::string &boxType, const std::string &boxName);
304 virtual void commandDelete(const std::string &boxName);
305 virtual void commandConnection(const std::string &nodeFrom,const std::string &outputLabel,const std::string &nodeTo,const std::string &inputLabel);
306 virtual void commandPackage(const std::string &packageName);
307 virtual void commandEndPackage();
308 virtual void commandDefine(const std::string &name,const std::string &pack,const std::string &scriptfilename);
309 virtual void commandEndDefine();
310 virtual void commandKind(const std::string &kind);
311 virtual void commandPrint(const std::string &value);
312 virtual void commandExec(const std::string &word);
313 virtual void commandInput(const std::string &name,const std::string &box,const std::string &input,const std::string &help);
314 virtual void commandOutput(const std::string &name,const std::string &box,const std::string &output,const std::string &help);
315 virtual void commandSet(const std::string &box,const std::string &input,const std::string &value);
316 virtual void commandAuthor(const std::string &author);
317 virtual void commandCategory(const std::string &categorytype);
318 virtual void commandDescription(const std::string &description);
319 virtual void commandClear();
320 virtual void commandInclude(const std::string &word, bool ok);
321 virtual void commandLoad(const std::string &packageName);
322 virtual void commandUnload(const std::string &packageName);
323 virtual void commandBreak();
324 virtual void commandQuit();
325 virtual void commandMessage();
326 virtual void commandMessage(const std::string &kind,const std::string &levelstr);
327 virtual void SetCurrentFileName(const std::string &fullPathScriptName);
328 virtual void SetTypeOfScript_Application();
331 InterpreterVirtual();
337 /// Vector of names of open files with full path (as open)
338 std::vector<std::string> mFileName;
341 /// Stores the current line number in each open file
342 std::vector<int> mLine;
346 /// Are we in a command line context ?
350 /// Vector of names of files which have been open
351 /// before (and may closed then which are no more in mFileName)
352 /// with full path (as open)
353 std::vector<std::string> mFileNameHistory;
355 /// The user of the interpreter (0 if none)
356 bbtk::InterpreterUser* mUser;
358 /// The dictionnary of commands
359 CommandDictType mCommandDict;
366 /// Opens the file fullPathScriptName
367 /// includeScriptName is the name as given to the include command
368 void LoadScript( std::string fullPathScriptName,std::string includeScriptName);
371 //==================================================================
376 /// Vector of open files / buffers (can be stringstream)
377 std::vector<std::istream*> mFile;
380 /// Vector of names of open files as given to the include command
381 std::vector<std::string> mIncludeFileName;
384 /// Are we inside a commented-out zone ?
387 /// The current interpreter status
390 /// The history of commands
391 std::deque< char* > mHistory;
397 // BreakSignalType mBreakSignal;
400 ~InterpreterVirtual();
403 virtual void CatchInterpreterException( const InterpreterException& e );
404 virtual void CatchBbtkException( const bbtk::Exception& e );
405 virtual void CatchStdException( const std::exception& e );
406 virtual void CatchUnknownException();
410 // EO class Interpreter