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 __bbtkInterpreterVirtual_h__
00041 #define __bbtkInterpreterVirtual_h__
00042
00043
00044
00045 #include "bbtkMessageManager.h"
00046 #include "bbtkException.h"
00047 #include "bbtkObject.h"
00048
00049
00050 #include <fstream>
00051 #include <deque>
00052
00053
00054 #include <boost/signal.hpp>
00055 #include <boost/bind.hpp>
00056
00057 namespace bbtk
00058 {
00059
00060
00061
00062 class BBTK_EXPORT InterpreterUser
00063 {
00064 public:
00065 InterpreterUser() {}
00066 virtual ~InterpreterUser() {}
00067
00068 virtual bool InterpreterUserHasOwnHtmlPageViewer() { return false; }
00069 virtual void InterpreterUserViewHtmlPage(const std::string&) {}
00070
00071
00072 };
00073
00074
00075
00076
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
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136 class BBTK_EXPORT InterpreterVirtual : public Object
00137 {
00138 BBTK_OBJECT_INTERFACE_NO_CONDES(InterpreterVirtual);
00139 typedef Object Superclass;
00140 public:
00141 static Pointer New();
00142
00143 typedef enum
00144 {
00145 Interpreter_OK,
00146 Interpreter_ERROR,
00147 Interpreter_QUIT,
00148 Interpreter_BREAK,
00149 }
00150 ExitStatus;
00151
00153 ExitStatus InterpretFile( const std::string& filename, bool source = false);
00154
00156 ExitStatus InterpretBuffer( std::stringstream* buffer );
00157
00159 ExitStatus InterpretLine( const std::string& line );
00160
00162 ExitStatus InterpretCurrentStreams();
00163
00165 void CommandLineInterpreter();
00166
00167
00168
00170 void SetCommandLine(bool v = true) { mCommandLine = v; }
00171
00172 void SetThrow(bool b) { mThrow = b; }
00173
00174 #ifdef USE_WXWIDGETS
00176 void SetUser(InterpreterUser* c) { mUser = c; }
00178 InterpreterUser* GetUser() { return mUser; }
00180 const InterpreterUser* GetUser() const { return mUser; }
00181
00182 #endif
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195 protected:
00196
00198 typedef enum
00199 {
00200 cBreak,
00201 cClear,
00202 cNew,
00203 cDelete,
00204 cConnect,
00205 cExec,
00206 cPackage,
00207 cEndPackage,
00208 cDefine,
00209 cEndDefine,
00210 cInput,
00211 cOutput,
00212 cSet,
00213 cConfig,
00214 cReset,
00215 cAuthor,
00216 cCategory,
00217 cDescription,
00218 cHelp,
00219 cMessage,
00220 cInclude,
00221 cQuit,
00222 cLoad,
00223 cUnload,
00224 cGraph,
00225 cPrint,
00226 cIndex,
00227 cKind,
00228 cNewGUI,
00229 cWorkspace,
00230 cDebug
00231 } CommandCodeType;
00232
00234 typedef struct
00235 {
00236 std::string keyword;
00237 int argmin, argmax;
00238 CommandCodeType code;
00239 std::string syntax;
00240 std::string help;
00241 } CommandInfoType;
00242
00244 typedef std::map<std::string,CommandInfoType> CommandDictType;
00245
00246
00248 void DoInterpretLine( const std::string& line );
00249
00251 void GetLineFromPrompt( std::string& line );
00252
00254 void SplitLine ( const std::string& line,
00255 std::vector<std::string>& words );
00256
00258 void InterpretCommand( const std::vector<std::string>& words,
00259 CommandInfoType& info );
00260
00262 void SwitchToFile( const std::string& filename, bool source = false );
00263
00265 void SwitchToStream( std::stringstream* stream );
00266
00268 void CloseCurrentFile();
00269
00271 void CloseAllFiles();
00272
00274 virtual void commandReset();
00275
00277 virtual void commandHelp(const std::string &words);
00278 virtual void commandHelp(const std::vector<std::string>& words);
00279
00281 virtual void commandGraph(const std::vector<std::string>& words);
00282
00284 virtual void commandConfig() const;
00286
00287
00288 void commandIndex(const std::string& filename,const std::string& type = "Initials");
00290 void FindCommandsWithPrefix( char* buf,
00291 int n,
00292 std::vector<std::string>& commands );
00293
00299 virtual void commandNewGUI(const std::string& box,const std::string& instanceName);
00300 virtual void commandDebug(const std::string& arg);
00301 virtual void commandNew(const std::string &boxType, const std::string &boxName);
00302 virtual void commandDelete(const std::string &boxName);
00303 virtual void commandConnection(const std::string &nodeFrom,const std::string &outputLabel,const std::string &nodeTo,const std::string &inputLabel);
00304 virtual void commandPackage(const std::string &packageName);
00305 virtual void commandEndPackage();
00306 virtual void commandDefine(const std::string &name,const std::string &pack,const std::string &scriptfilename);
00307 virtual void commandEndDefine();
00308 virtual void commandKind(const std::string &kind);
00309 virtual void commandPrint(const std::string &value);
00310 virtual void commandExec(const std::string &word);
00311 virtual void commandInput(const std::string &name,const std::string &box,const std::string &input,const std::string &help);
00312 virtual void commandOutput(const std::string &name,const std::string &box,const std::string &output,const std::string &help);
00313 virtual void commandSet(const std::string &box,const std::string &input,const std::string &value);
00314 virtual void commandAuthor(const std::string &author);
00315 virtual void commandCategory(const std::string &categorytype);
00316 virtual void commandDescription(const std::string &description);
00317 virtual void commandClear();
00318 virtual void commandInclude(const std::string &word, bool ok);
00319 virtual void commandLoad(const std::string &packageName);
00320 virtual void commandUnload(const std::string &packageName);
00321 virtual void commandBreak();
00322 virtual void commandQuit();
00323 virtual void commandMessage();
00324 virtual void commandMessage(const std::string &kind,const std::string &levelstr);
00325 virtual void SetCurrentFileName(const std::string &fullPathScriptName);
00326
00328 InterpreterVirtual();
00329
00330
00331 void Init();
00332
00333
00335 std::vector<std::string> mFileName;
00336
00337
00339 std::vector<int> mLine;
00340
00341 bool mThrow;
00342
00344 bool mCommandLine;
00345
00346
00350 std::vector<std::string> mFileNameHistory;
00351
00353 bbtk::InterpreterUser* mUser;
00354
00356 CommandDictType mCommandDict;
00357
00358
00359 private:
00360
00361
00362
00365 void LoadScript( std::string fullPathScriptName,std::string includeScriptName);
00366
00367
00368
00369
00370
00371
00372
00374 std::vector<std::istream*> mFile;
00375
00376
00378 std::vector<std::string> mIncludeFileName;
00379
00380
00382 bool mInsideComment;
00383
00385 ExitStatus mStatus;
00386
00388 std::deque< char* > mHistory;
00389
00390 int bufferNb;
00391
00392
00394
00395
00396 protected:
00397 ~InterpreterVirtual();
00398
00400 virtual void CatchInterpreterException( const InterpreterException& e );
00401 virtual void CatchBbtkException( const bbtk::Exception& e );
00402 virtual void CatchStdException( const std::exception& e );
00403 virtual void CatchUnknownException();
00404
00405
00406 };
00407
00408
00409
00410
00411 }
00412 #endif