00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
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
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,
00185 cReset,
00186 cAuthor,
00187 cCategory,
00188 cDescription,
00189 cHelp,
00190 cMessage,
00191 cInclude,
00192 cQuit,
00193 cLoad,
00194 cUnload,
00195 cGraph,
00196 cPrint,
00197 cIndex,
00198 cKind,
00199 cNewGUI,
00200 cWorkspace,
00201 cDebug
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
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
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
00347
00348
00349
00350 struct QuitException : public bbtk::Exception
00351 {
00352 QuitException() : bbtk::Exception("Interpreter","","Quit") {}
00353 };
00354
00355 }
00356 #endif