00001 /*========================================================================= 00002 Program: bbtk 00003 Module: $RCSfile: bbtkInterpreter.h,v $ 00004 Language: C++ 00005 Date: $Date: 2010/09/18 22:31:40 $ 00006 Version: $Revision: 1.44 $ 00007 =========================================================================*/ 00008 00009 /* --------------------------------------------------------------------- 00010 00011 * Copyright (c) CREATIS-LRMN (Centre de Recherche en Imagerie Medicale) 00012 * Authors : Eduardo Davila, Laurent Guigues, Jean-Pierre Roux 00013 * 00014 * This software is governed by the CeCILL-B license under French law and 00015 * abiding by the rules of distribution of free software. You can use, 00016 * modify and/ or redistribute the software under the terms of the CeCILL-B 00017 * license as circulated by CEA, CNRS and INRIA at the following URL 00018 * http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html 00019 * or in the file LICENSE.txt. 00020 * 00021 * As a counterpart to the access to the source code and rights to copy, 00022 * modify and redistribute granted by the license, users are provided only 00023 * with a limited warranty and the software's author, the holder of the 00024 * economic rights, and the successive licensors have only limited 00025 * liability. 00026 * 00027 * The fact that you are presently reading this means that you have had 00028 * knowledge of the CeCILL-B license and that you accept its terms. 00029 * ------------------------------------------------------------------------ */ 00030 00040 #ifndef __bbtkInterpreter_h__ 00041 #define __bbtkInterpreter_h__ 00042 00043 #include "bbtkVirtualExec.h" 00044 #include "bbtkExecuter.h" 00045 #include "bbtkInterpreterVirtual.h" 00046 00047 #include <fstream> 00048 #include <deque> 00049 00050 // Signal/slot mechanism for 'break' commands 00051 //#include <boost/signal.hpp> 00052 //#include <boost/bind.hpp> 00053 00054 namespace bbtk 00055 { 00056 00057 #ifdef USE_WXWIDGETS 00058 class WxConsole; 00059 #endif 00060 00061 /* EED Borrame 00062 //======================================================================= 00063 class BBTK_EXPORT InterpreterUser 00064 { 00065 public: 00066 InterpreterUser() {} 00067 virtual ~InterpreterUser() {} 00068 00069 virtual bool InterpreterUserHasOwnHtmlPageViewer() { return false; } 00070 virtual void InterpreterUserViewHtmlPage(const std::string&) {} 00071 }; 00072 //======================================================================= 00073 */ 00074 00075 00076 /* EED Borrame 00077 //======================================================================= 00078 class BBTK_EXPORT InterpreterException : public Exception 00079 { 00080 public: 00081 InterpreterException( const std::string& message, 00082 bool in_script_file, 00083 const std::string& script_file, 00084 int script_line 00085 ); 00086 InterpreterException( const Exception& excep, 00087 bool in_script_file, 00088 const std::string& script_file, 00089 int script_line 00090 ); 00091 ~InterpreterException() throw() {} 00092 00093 bool IsInScriptFile() const { return mInScriptFile; } 00094 const std::string& GetScriptFile() const { return mScriptFile; } 00095 int GetScriptLine() const { return mScriptLine; } 00096 private: 00097 bool mInScriptFile; 00098 std::string mScriptFile; 00099 int mScriptLine; 00100 }; 00101 //======================================================================= 00102 */ 00103 00104 00105 00106 /* 00107 //======================================================================= 00108 // The "Quit" exception 00109 class BBTK_EXPORT QuitException : public InterpreterError 00110 { 00111 public: 00112 QuitException( bool in_script_file, 00113 const std::string& script_file, 00114 int script_line 00115 ) 00116 : InterpreterError("QUIT",in_script_file,script_file,script_line) 00117 {} 00118 ~QuitException() throw() {} 00119 }; 00120 //======================================================================= 00121 const std::string BREAK("BREAK"); 00122 //======================================================================= 00123 // The "Break" exception 00124 class BBTK_EXPORT BreakException : public InterpreterError 00125 { 00126 public: 00127 BreakException( bool in_script_file, 00128 std::string script_file, 00129 int script_line 00130 ) 00131 : InterpreterError(BREAK,in_script_file,script_file,script_line) 00132 { std::cout << "BUILDING BREAK"<<std::endl; } 00133 ~BreakException() throw() {} 00134 }; 00135 //======================================================================= 00136 */ 00137 00138 //======================================================================= 00139 class BBTK_EXPORT Interpreter : public InterpreterVirtual 00140 { 00141 BBTK_OBJECT_INTERFACE_NO_CONDES(Interpreter); 00142 typedef Object InterpreterVirtual; 00143 public: 00144 static Pointer New(const std::string& cpp_file = ""); 00145 static Pointer New(VirtualExec::Pointer); 00146 00147 /* EED Borrame 00148 typedef enum 00149 { 00150 Interpreter_OK, 00151 Interpreter_ERROR, 00152 Interpreter_QUIT, 00153 Interpreter_BREAK, 00154 } 00155 ExitStatus; 00156 00158 ExitStatus InterpretFile( const std::string& filename, bool source = false); 00159 00161 ExitStatus InterpretBuffer( std::stringstream* buffer ); 00162 00164 ExitStatus InterpretLine( const std::string& line ); 00165 00167 ExitStatus InterpretCurrentStreams(); 00168 00170 void CommandLineInterpreter(); 00171 */ 00172 00173 00176 void SetInputs(const std::map<std::string,std::string>& m) 00177 { mVirtualExecuter->SetInputs(m); } 00178 00181 void SetNoExecMode(bool m) { mVirtualExecuter->SetNoExecMode(m); } 00182 00185 void SetNoErrorMode(bool m) { mVirtualExecuter->SetNoErrorMode(m);} 00187 //typedef Executer::DialogModeType DialogModeType; 00188 typedef VirtualExec::DialogModeType DialogModeType; 00189 00190 void SetDialogMode(DialogModeType t) { mVirtualExecuter->SetDialogMode(t);} 00191 00192 /*EED Borrame 00194 void SetCommandLine(bool v = true) { mCommandLine = v; } 00195 00196 void SetThrow(bool b) { mThrow = b; } 00197 00198 #ifdef USE_WXWIDGETS 00200 void SetUser(InterpreterUser* c) { mUser = c; } 00202 InterpreterUser* GetUser() { return mUser; } 00204 const InterpreterUser* GetUser() const { return mUser; } 00205 00206 #endif 00207 */ 00208 00210 VirtualExec::Pointer GetExecuter() const { return mVirtualExecuter; } 00211 00212 /* 00213 // For 'break' commands observation 00214 typedef boost::signals::trackable BreakObserverType; 00215 typedef boost::signal<void ()> BreakSignalType; 00216 typedef BreakSignalType::slot_function_type BreakCallbackType; 00217 00218 // Adds a callback when 'break' command issued 00219 void AddBreakObserver( BreakCallbackType ); 00220 */ 00221 00222 protected: 00223 00224 /* EED Borrame 00226 typedef enum 00227 { 00228 cBreak, // LG 12/12/08 : Stops the current script execution (if not exec frozen) - used in tutorial + debugging 00229 cClear, // LG 12/12/08 : Clears the current complex black box (e.g. workspace) - used in tours 00230 cNew, 00231 cDelete, 00232 cConnect, 00233 cExec, 00234 cPackage, 00235 cEndPackage, 00236 cDefine, 00237 cEndDefine, 00238 cInput, 00239 cOutput, 00240 cSet, 00241 cConfig, // JPR 00242 cReset, // EED 00243 cAuthor, 00244 cCategory, // JPR 00245 cDescription, 00246 cHelp, 00247 cMessage, 00248 cInclude, 00249 cQuit, 00250 cLoad, 00251 cUnload, 00252 cGraph, 00253 cPrint, 00254 cIndex, 00255 cKind, // LG 00256 cNewGUI, // LG 00257 cWorkspace, // LG 00258 cDebug // LG 00259 } CommandCodeType; 00260 00262 typedef struct 00263 { 00264 std::string keyword; 00265 int argmin, argmax; 00266 CommandCodeType code; 00267 std::string syntax; 00268 std::string help; 00269 } CommandInfoType; 00270 00272 typedef std::map<std::string,CommandInfoType> CommandDictType; 00273 00274 00276 void DoInterpretLine( const std::string& line ); //, bool &insideComment ); 00277 00279 void GetLineFromPrompt( std::string& line ); 00280 00282 void SplitLine ( const std::string& line, 00283 std::vector<std::string>& words ); 00284 00286 void InterpretCommand( const std::vector<std::string>& words, 00287 CommandInfoType& info ); 00288 00290 void SwitchToFile( const std::string& filename, bool source = false ); 00291 00293 void SwitchToStream( std::stringstream* stream ); 00294 00296 void CloseCurrentFile(); 00297 00299 void CloseAllFiles(); 00300 */ 00301 00303 virtual void commandReset(); 00304 00305 00307 virtual void commandHelp(const std::vector<std::string>& words); 00308 00310 void HelpCommands(); 00311 00312 00314 virtual void commandHelp( const std::string& command ); 00315 00317 virtual void commandGraph(const std::vector<std::string>& words); 00318 00320 virtual void commandConfig() const; 00322 // void Print(const std::string&); 00323 00324 virtual void commandIndex(const std::string& filename, 00325 const std::string& type = "Initials"); 00326 00327 /*EED Borrame 00329 void FindCommandsWithPrefix( char* buf, 00330 int n, 00331 std::vector<std::string>& commands ); 00332 */ 00333 00339 virtual void commandNewGUI(const std::string& box,const std::string& instanceName); 00340 virtual void commandDebug(const std::string& arg); 00341 virtual void commandNew(const std::string &boxType,const std::string &boxName); 00342 virtual void commandDelete(const std::string &boxName); 00343 virtual void commandConnection(const std::string &nodeFrom,const std::string &outputLabel,const std::string &nodeTo,const std::string &inputLabel); 00344 virtual void commandPackage(const std::string &packageName); 00345 virtual void commandEndPackage(); 00346 virtual void commandDefine(const std::string &name,const std::string &pack,const std::string &scriptfilename); 00347 virtual void commandEndDefine(); 00348 virtual void commandKind(const std::string &kind); 00349 virtual void commandPrint(const std::string &value); 00350 virtual void commandExec(const std::string &word); 00351 virtual void commandInput(const std::string &name,const std::string &box,const std::string &input,const std::string &help); 00352 virtual void commandOutput(const std::string &name,const std::string &box,const std::string &output,const std::string &help); 00353 virtual void commandSet(const std::string &box,const std::string &input,const std::string &value); 00354 virtual void commandAuthor(const std::string &author); 00355 virtual void commandCategory(const std::string &categorytype); 00356 virtual void commandDescription(const std::string &description); 00357 virtual void commandClear(); 00358 virtual void commandInclude(const std::string &word, bool ok); 00359 virtual void commandLoad(const std::string &packageName); 00360 virtual void commandUnload(const std::string &packageName); 00361 virtual void commandBreak(); 00362 virtual void commandQuit(); 00363 virtual void commandMessage(); 00364 virtual void commandMessage(const std::string &kind,const std::string &levelstr); 00365 virtual void SetCurrentFileName(const std::string &fullPathScriptName); 00366 00367 00369 Interpreter(const std::string& cpp_file = ""); 00370 Interpreter(VirtualExec::Pointer); 00371 00372 private: 00373 00374 00375 void Init(VirtualExec::Pointer, const std::string& cpp_file); 00376 00377 /*EED Borrame 00380 void LoadScript( std::string fullPathScriptName, 00381 std::string includeScriptName); 00382 */ 00383 //================================================================== 00384 // ATTRIBUTES 00385 00387 bbtk::VirtualExec::Pointer mVirtualExecuter; 00388 bbtk::Executer::WeakPointer mRealExecuter; 00389 00390 /*EED Borrame 00392 bbtk::InterpreterUser* mUser; 00394 std::vector<std::istream*> mFile; 00395 00397 std::vector<std::string> mFileName; 00398 00402 std::vector<std::string> mFileNameHistory; 00403 00405 std::vector<std::string> mIncludeFileName; 00406 00408 std::vector<int> mLine; 00409 00411 CommandDictType mCommandDict; 00412 00414 bool mCommandLine; 00415 00417 bool mInsideComment; 00418 00420 ExitStatus mStatus; 00421 00423 std::deque< char* > mHistory; 00424 00425 int bufferNb; 00426 00427 bool mThrow; 00428 00430 // BreakSignalType mBreakSignal; 00431 00432 */ 00433 00434 protected: 00435 ~Interpreter(); 00436 00438 virtual void CatchInterpreterException( const InterpreterException& e ); 00439 virtual void CatchBbtkException( const bbtk::Exception& e ); 00440 virtual void CatchStdException( const std::exception& e ); 00441 virtual void CatchUnknownException(); 00442 00443 00444 }; 00445 // EO class Interpreter 00446 00447 00448 00449 } 00450 #endif