HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GOP_Parser.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * COMMENTS: This is a general purpose skeleton of a parser.
7  * It has facilities to provide failry straightforward parsing
8  * of a "language".
9  *
10  * To parse a string, just instantiate the derived parser and
11  * call the doIt() method.
12  *
13  */
14 
15 #ifndef __GOP_Parser_h__
16 #define __GOP_Parser_h__
17 
18 #include "GOP_API.h"
19 #include <GA/GA_Types.h>
20 #include <UT/UT_String.h>
21 
22 
23 // These are the state constants that are common to all parsers.
24 // Most parsers will probably want to add their own, must be
25 // careful to number them uniquely.
26 #define ST_START 0
27 #define ST_ERROR 99
28 
29 
31 {
32 public:
33  GOP_Parser(const UT_String &str);
34  GOP_Parser(const UT_StringHolder &str);
35  virtual ~GOP_Parser() {}
36 
37  // This is the method that the user would call to start the
38  // parsing. The return value can be parser-depenedent.
39  // (e.g. the number of items parsed, an error value, etc...)
40  // The return value should be placed in myReturnVal
41  virtual int doIt();
42 
43 protected:
45 
46 
47 
48  //////////// State Handlers
49  virtual void handleStart() = 0;
50  virtual void handleError() = 0;
51 
52  //////////// Utility Methods
53  void startToken();
54  void endToken();
55 
56  // Each parser should override this method and use it to
57  // manage state transitions. Return false if you want the
58  // dispatcher to stop (e.g. when an error occurs)
59  virtual bool dispatch(char c) = 0;
60 
61  // Method that loops on the characters of the string and calls dispatch.
62  // There should be no need to override this method.
63  virtual void parseDispatcher();
64 
65  // Called during the constructor to initialize the state machine
66  // before any parsing is done.
67  virtual void init();
68 
69  // Called after the parsing is done.
70  virtual void finish();
71 
72  // Grabs the current token, goes to 'state' and returns true if
73  // successful. Otherwise goes to state ST_ERROR and returns false
74  bool getInt (GA_Index &retval, unsigned state);
75  bool getInt (int &retval, unsigned state);
76  bool getFloat(float &retval, unsigned state);
77  bool getFloat(double &retval, unsigned state);
78 
79  // Checks if the current token is the character c, (stripping out
80  // all leading and trailing spaces
81  bool isChar(char c);
82 
83  // We also need to count '\0' as a space
84  bool isSpace(char c) { return (isspace(c) || c == '\0'); }
85 
86 
87 
88  //////////// State Variables
89  char *myTokBegin;
90  char *myTokEnd;
92  unsigned myCurrentState;
94 
95  //////////// Utility Variables
96  char myTempChar;
97 };
98 
99 
100 #endif
char * myTokEnd
Definition: GOP_Parser.h:90
char * myTokBegin
Definition: GOP_Parser.h:89
#define GOP_API
Definition: GOP_API.h:10
unsigned myCurrentState
Definition: GOP_Parser.h:92
virtual ~GOP_Parser()
Definition: GOP_Parser.h:35
int myReturnVal
Definition: GOP_Parser.h:93
UT_String myString
Definition: GOP_Parser.h:44
GA_Size GA_Index
Define the strictness of GA_Offset/GA_Index.
Definition: GA_Types.h:640
bool isspace(char c)
Definition: strutil.h:799
char * myTokLastGood
Definition: GOP_Parser.h:91
bool isSpace(char c)
Definition: GOP_Parser.h:84
char myTempChar
Definition: GOP_Parser.h:96