00001 /* 00002 * PROPRIETARY INFORMATION. This software is proprietary to 00003 * Side Effects Software Inc., and is not to be reproduced, 00004 * transmitted, or disclosed in any way without written permission. 00005 * 00006 * Produced by: 00007 * George ElKoura 00008 * Side Effects 00009 * 477 Richmond Street West 00010 * Toronto, Ontario 00011 * Canada M5V 3E7 00012 * 416-504-9876 00013 * 00014 * 00015 * COMMENTS: This is a general purpose skeleton of a parser. 00016 * It has facilities to provide failry straightforward parsing 00017 * of a "language". 00018 * 00019 * To parse a string, just instantiate the derived parser and 00020 * call the doIt() method. 00021 * 00022 */ 00023 00024 #ifndef __GOP_Parser_h__ 00025 #define __GOP_Parser_h__ 00026 00027 #include "GOP_API.h" 00028 #include <UT/UT_String.h> 00029 00030 00031 // These are the state constants that are common to all parsers. 00032 // Most parsers will probably want to add their own, must be 00033 // careful to number them uniquely. 00034 #define ST_START 0 00035 #define ST_ERROR 99 00036 00037 00038 class GOP_API GOP_Parser 00039 { 00040 public: 00041 GOP_Parser(const UT_String &str); 00042 virtual ~GOP_Parser() {} 00043 00044 // This is the method that the user would call to start the 00045 // parsing. The return value can be parser-depenedent. 00046 // (e.g. the number of items parsed, an error value, etc...) 00047 // The return value should be placed in myReturnVal 00048 virtual int doIt(); 00049 00050 protected: 00051 UT_String myString; 00052 00053 00054 00055 //////////// State Handlers 00056 virtual void handleStart() = 0; 00057 virtual void handleError() = 0; 00058 00059 //////////// Utility Methods 00060 void startToken(); 00061 void endToken(); 00062 00063 // Each parser should override this method and use it to 00064 // manage state transitions. Return false if you want the 00065 // dispatcher to stop (e.g. when an error occurs) 00066 virtual bool dispatch(char c) = 0; 00067 00068 // Method that loops on the characters of the string and calls dispatch. 00069 // There should be no need to override this method. 00070 virtual void parseDispatcher(); 00071 00072 // Called during the constructor to initialize the state machine 00073 // before any parsing is done. 00074 virtual void init(); 00075 00076 // Called after the parsing is done. 00077 virtual void finish(); 00078 00079 // Grabs the current token, goes to 'state' and returns true if 00080 // successful. Otherwise goes to state ST_ERROR and returns false 00081 bool getInt (int &retval, unsigned state); 00082 bool getFloat(float &retval, unsigned state); 00083 00084 // Checks if the current token is the character c, (stripping out 00085 // all leading and trailing spaces 00086 bool isChar(char c); 00087 00088 // We also need to count '\0' as a space 00089 bool isSpace(char c) { return (isspace(c) || c == '\0'); } 00090 00091 00092 00093 //////////// State Variables 00094 char *myTokBegin; 00095 char *myTokEnd; 00096 char *myTokLastGood; 00097 unsigned myCurrentState; 00098 int myReturnVal; 00099 00100 //////////// Utility Variables 00101 char myTempChar; 00102 }; 00103 00104 00105 #endif
1.5.9