bbtkInterpreter.h

Go to the documentation of this file.
00001 /*=========================================================================                                                                               
00002   Program:   bbtk
00003   Module:    $RCSfile: bbtkInterpreter.h,v $
00004   Language:  C++
00005   Date:      $Date: 2008/10/17 08:18:13 $
00006   Version:   $Revision: 1.35 $
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 
00046 #include <fstream>
00047 #include <deque>
00048 
00049 namespace bbtk
00050 {
00051 
00052 #ifdef _USE_WXWIDGETS_
00053   class WxConsole;
00054 #endif
00055   
00056   //=======================================================================
00057   class BBTK_EXPORT InterpreterUser
00058   {
00059   public: 
00060     InterpreterUser() {}
00061     virtual ~InterpreterUser() {}
00062 
00063     virtual bool InterpreterUserHasOwnHtmlPageViewer() { return false; }
00064     virtual void InterpreterUserViewHtmlPage(const std::string&) {}
00065 
00066 
00067   };
00068   //=======================================================================
00069 
00070 
00071 
00072   //=======================================================================
00073   class BBTK_EXPORT InterpreterError : public Exception
00074   {
00075   public:
00076     InterpreterError( const std::string& message,
00077                       bool in_script_file,
00078                       const std::string& script_file,
00079                       int script_line 
00080                       );
00081     InterpreterError( const Exception& excep,
00082                       bool in_script_file,
00083                       const std::string& script_file,
00084                       int script_line 
00085                       );
00086     ~InterpreterError() throw() {}
00087 
00088     bool IsInScriptFile() const { return mInScriptFile; }
00089     const std::string& GetScriptFile() const { return mScriptFile; }
00090     int GetScriptLine() const { return mScriptLine; }
00091   private:
00092     bool mInScriptFile;
00093     std::string mScriptFile;
00094     int mScriptLine;
00095   };
00096   //=======================================================================
00097 
00098 
00099   //=======================================================================
00100   class BBTK_EXPORT Interpreter : public Object
00101   {
00102     BBTK_OBJECT_INTERFACE_NO_CONDES(Interpreter);
00103     typedef Object Superclass;
00104   public:
00105     static Pointer New(const std::string& cpp_file = "");
00106     static Pointer New(VirtualExec::Pointer);
00107 
00108     typedef enum 
00109       {
00110         Interpreter_OK,
00111         Interpreter_ERROR,
00112         Interpreter_QUIT
00113       }
00114       ExitStatus;
00115 
00117     ExitStatus InterpretFile( const std::string& filename, bool source = false);
00118 
00120     ExitStatus InterpretBuffer( std::stringstream* buffer );
00121 
00123     ExitStatus InterpretLine( const std::string& line );
00124 
00125 
00127     void CommandLineInterpreter();
00128 
00129 
00130 
00133     void SetInputs(const std::map<std::string,std::string>& m)
00134     { mVirtualExecuter->SetInputs(m); }
00135 
00138     void SetNoExecMode(bool m) { mVirtualExecuter->SetNoExecMode(m); }
00139 
00142     void SetNoErrorMode(bool m) { mVirtualExecuter->SetNoErrorMode(m);}
00144     //typedef Executer::DialogModeType DialogModeType;
00145     typedef VirtualExec::DialogModeType DialogModeType;
00146 
00147     void SetDialogMode(DialogModeType t) { mVirtualExecuter->SetDialogMode(t);}
00148 
00150     void SetCommandLine(bool v = true) { mCommandLine = v; }
00151 
00152     void SetThrow(bool b) { mThrow = b; }
00153 
00154 #ifdef _USE_WXWIDGETS_
00156     void SetUser(InterpreterUser* c) { mUser = c; }
00158     InterpreterUser* GetUser() { return mUser; }
00160     const InterpreterUser* GetUser() const { return mUser; }
00161 
00162 #endif
00163 
00165     VirtualExec::Pointer GetExecuter() const { return mVirtualExecuter; }
00166 
00167 
00168   protected:
00169 
00171     typedef enum
00172     {
00173       cNew,
00174       cDelete,
00175       cConnect,
00176       cExec,
00177       cPackage,
00178       cEndPackage,
00179       cDefine,
00180       cEndDefine,
00181       cInput,
00182       cOutput,
00183       cSet,
00184       cConfig,  // JPR
00185       cReset,   // EED
00186       cAuthor, 
00187       cCategory, // JPR
00188       cDescription,
00189       cHelp,
00190       cMessage,
00191       cInclude,
00192       cQuit,
00193       cLoad,
00194       cUnload,
00195       cGraph,
00196       cPrint,
00197       cIndex,
00198       cKind, // LG
00199       cNewGUI, // LG
00200       cWorkspace, // LG
00201       cDebug // LG
00202     } CommandCodeType;
00203 
00205     typedef struct 
00206     {
00207       std::string keyword;
00208       int argmin, argmax;
00209       CommandCodeType code;
00210       std::string syntax;
00211       std::string help;
00212     } CommandInfoType;
00213 
00215     typedef std::map<std::string,CommandInfoType> CommandDictType;
00216 
00217 
00219     void InterpretLine( const std::string& line, bool &insideComment );
00220 
00222     void GetLineFromPrompt( std::string& line );
00223 
00225     void SplitLine ( const std::string& line,
00226                      std::vector<std::string>& words );
00227 
00229     void InterpretCommand( const std::vector<std::string>& words,
00230                            CommandInfoType& info );
00231 
00233     void SwitchToFile( const std::string& filename, bool source = false );
00234 
00236     void SwitchToStream( std::stringstream* stream );
00237 
00239     void CloseCurrentFile();
00240 
00242     void CloseAllFiles();
00243 
00245     void Reset();
00246 
00248     void Help(const std::vector<std::string>& words);
00249 
00251     void HelpCommands();
00252 
00254     void HelpCommand( const std::string& command );
00255 
00257     void Graph(const std::vector<std::string>& words);
00258 
00260     void Config() const; 
00262     //    void Print(const std::string&);
00263 
00264     void Index(const std::string& filename, 
00265                const std::string& type = "Initials");
00267     void FindCommandsWithPrefix( char* buf,
00268                                  int n,
00269                                  std::vector<std::string>& commands );
00270 
00276     void NewGUI(const std::string& box, 
00277                 const std::string& instanceName);
00278 
00279     void Debug(const std::string& arg);
00280 
00281   private:
00282 
00284     Interpreter(const std::string& cpp_file = "");
00285 
00286     Interpreter(VirtualExec::Pointer);
00287 
00288     void Init(VirtualExec::Pointer, const std::string& cpp_file);
00289 
00292     void LoadScript( std::string fullPathScriptName,
00293                      std::string includeScriptName);
00294 
00296     void CatchBbtkException( const bbtk::Exception& e );
00297     void CatchStdException( const std::exception& e );
00298     void CatchUnknownException();
00299 
00300 
00301   
00302     //==================================================================
00303     // ATTRIBUTES
00304 
00306     bbtk::VirtualExec::Pointer mVirtualExecuter;
00307     bbtk::Executer::WeakPointer mRealExecuter;
00308 
00310     bbtk::InterpreterUser* mUser;
00311 
00313     std::vector<std::istream*> mFile;
00314 
00316     std::vector<std::string> mFileName;
00317 
00321     std::vector<std::string> mFileNameHistory;
00322 
00324     std::vector<std::string> mIncludeFileName;
00325 
00327     std::vector<int> mLine;
00328 
00330     CommandDictType mCommandDict;
00331 
00333     bool mCommandLine;
00334 
00336     std::deque< char* > mHistory;
00337 
00338    int bufferNb;
00339     
00340     bool mThrow;
00341 
00342         protected:
00343         ~Interpreter();
00344 
00345   };
00346   // EO class Interpreter
00347 
00348 
00349   // The "Quit" exception
00350   struct QuitException : public bbtk::Exception
00351   {
00352     QuitException() : bbtk::Exception("Interpreter","","Quit") {}
00353   };
00354 
00355 }
00356 #endif

Generated on Wed Nov 12 11:37:08 2008 for BBTK by  doxygen 1.5.6