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; }
186 // For 'break' commands observation
187 typedef boost::signals::trackable BreakObserverType;
188 typedef boost::signal<void ()> BreakSignalType;
189 typedef BreakSignalType::slot_function_type BreakCallbackType;
191 // Adds a callback when 'break' command issued
192 void AddBreakObserver( BreakCallbackType );
197 /// The enumeration of command codes == Command name
200 cBreak, // LG 12/12/08 : Stops the current script execution (if not exec frozen) - used in tutorial + debugging
201 cClear, // LG 12/12/08 : Clears the current complex black box (e.g. workspace) - used in tours
233 /// The structure storing the informations on a command
238 CommandCodeType code;
243 /// The type of dictionnary of commands
244 typedef std::map<std::string,CommandInfoType> CommandDictType;
247 /// Interprets a line
248 void DoInterpretLine( const std::string& line ); //, bool &insideComment );
250 /// Reads a line from prompt
251 void GetLineFromPrompt( std::string& line );
253 /// Splits a line into words
254 void SplitLine ( const std::string& line,
255 std::vector<std::string>& words );
257 /// Executes the right action depending on the command name
258 void InterpretCommand( const std::vector<std::string>& words,
259 CommandInfoType& info );
261 /// Switch to the interpretation of a file
262 void SwitchToFile( const std::string& filename, bool source = false );
264 /// Switch to the interpretation of a stringstream
265 void SwitchToStream( std::stringstream* stream );
267 /// Closes the currently open file
268 void CloseCurrentFile();
270 /// Closes all open files
271 void CloseAllFiles();
274 virtual void commandReset();
276 /// Displays help (entry point of any help)
277 virtual void commandHelp(const std::string &words);
278 virtual void commandHelp(const std::vector<std::string>& words);
281 virtual void commandGraph(const std::vector<std::string>& words);
284 virtual void commandConfig() const;
286 // void Print(const std::string&);
288 void commandIndex(const std::string& filename,const std::string& type = "Initials");
290 void FindCommandsWithPrefix( char* buf,
292 std::vector<std::string>& commands );
294 /// Creates and connects the piece of pipeline which defines a GUI
296 /// Define it as a complex box type with name instanceName+"Type"
297 /// The instance is called instanceName
298 /// and connected to the existing pipeline
299 virtual void commandNewGUI(const std::string& box,const std::string& instanceName);
300 virtual void commandDebug(const std::string& arg);
301 virtual void commandNew(const std::string &boxType, const std::string &boxName);
302 virtual void commandDelete(const std::string &boxName);
303 virtual void commandConnection(const std::string &nodeFrom,const std::string &outputLabel,const std::string &nodeTo,const std::string &inputLabel);
304 virtual void commandPackage(const std::string &packageName);
305 virtual void commandEndPackage();
306 virtual void commandDefine(const std::string &name,const std::string &pack,const std::string &scriptfilename);
307 virtual void commandEndDefine();
308 virtual void commandKind(const std::string &kind);
309 virtual void commandPrint(const std::string &value);
310 virtual void commandExec(const std::string &word);
311 virtual void commandInput(const std::string &name,const std::string &box,const std::string &input,const std::string &help);
312 virtual void commandOutput(const std::string &name,const std::string &box,const std::string &output,const std::string &help);
313 virtual void commandSet(const std::string &box,const std::string &input,const std::string &value);
314 virtual void commandAuthor(const std::string &author);
315 virtual void commandCategory(const std::string &categorytype);
316 virtual void commandDescription(const std::string &description);
317 virtual void commandClear();
318 virtual void commandInclude(const std::string &word, bool ok);
319 virtual void commandLoad(const std::string &packageName);
320 virtual void commandUnload(const std::string &packageName);
321 virtual void commandBreak();
322 virtual void commandQuit();
323 virtual void commandMessage();
324 virtual void commandMessage(const std::string &kind,const std::string &levelstr);
325 virtual void SetCurrentFileName(const std::string &fullPathScriptName);
328 InterpreterVirtual();
334 /// Vector of names of open files with full path (as open)
335 std::vector<std::string> mFileName;
338 /// Stores the current line number in each open file
339 std::vector<int> mLine;
343 /// Are we in a command line context ?
347 /// Vector of names of files which have been open
348 /// before (and may closed then which are no more in mFileName)
349 /// with full path (as open)
350 std::vector<std::string> mFileNameHistory;
352 /// The user of the interpreter (0 if none)
353 bbtk::InterpreterUser* mUser;
355 /// The dictionnary of commands
356 CommandDictType mCommandDict;
363 /// Opens the file fullPathScriptName
364 /// includeScriptName is the name as given to the include command
365 void LoadScript( std::string fullPathScriptName,std::string includeScriptName);
368 //==================================================================
373 /// Vector of open files / buffers (can be stringstream)
374 std::vector<std::istream*> mFile;
377 /// Vector of names of open files as given to the include command
378 std::vector<std::string> mIncludeFileName;
381 /// Are we inside a commented-out zone ?
384 /// The current interpreter status
387 /// The history of commands
388 std::deque< char* > mHistory;
394 // BreakSignalType mBreakSignal;
397 ~InterpreterVirtual();
400 virtual void CatchInterpreterException( const InterpreterException& e );
401 virtual void CatchBbtkException( const bbtk::Exception& e );
402 virtual void CatchStdException( const std::exception& e );
403 virtual void CatchUnknownException();
407 // EO class Interpreter