00001 00029 #ifndef __OGRE_CPREPROCESSOR_H__ 00030 #define __OGRE_CPREPROCESSOR_H__ 00031 00032 #include <string.h> 00033 #include <stdlib.h> 00034 00035 namespace Ogre { 00036 00061 class CPreprocessor 00062 { 00076 class Token 00077 { 00078 public: 00079 enum Kind 00080 { 00081 TK_EOS, // End of input stream 00082 TK_ERROR, // An error has been encountered 00083 TK_WHITESPACE, // A whitespace span (but not newline) 00084 TK_NEWLINE, // A single newline (CR & LF) 00085 TK_LINECONT, // Line continuation ('\' followed by LF) 00086 TK_NUMBER, // A number 00087 TK_KEYWORD, // A keyword 00088 TK_PUNCTUATION, // A punctuation character 00089 TK_DIRECTIVE, // A preprocessor directive 00090 TK_STRING, // A string 00091 TK_COMMENT, // A block comment 00092 TK_LINECOMMENT, // A line comment 00093 TK_TEXT // An unparsed text (cannot be returned from GetToken()) 00094 }; 00095 00097 Kind Type; 00099 mutable size_t Allocated; 00100 union 00101 { 00103 const char *String; 00105 char *Buffer; 00106 }; 00108 size_t Length; 00109 00110 Token () : Allocated (0), String (NULL) 00111 { } 00112 00113 Token (Kind iType) : Type (iType), Allocated (0), String (NULL) 00114 { } 00115 00116 Token (Kind iType, const char *iString, size_t iLength) : 00117 Type (iType), Allocated (0), String (iString), Length (iLength) 00118 { } 00119 00120 Token (const Token &iOther) 00121 { 00122 Type = iOther.Type; 00123 Allocated = iOther.Allocated; 00124 iOther.Allocated = 0; // !!! not quite correct but effective 00125 String = iOther.String; 00126 Length = iOther.Length; 00127 } 00128 00129 ~Token () 00130 { if (Allocated) free (Buffer); } 00131 00133 Token &operator = (const Token &iOther) 00134 { 00135 if (Allocated) free (Buffer); 00136 Type = iOther.Type; 00137 Allocated = iOther.Allocated; 00138 iOther.Allocated = 0; // !!! not quite correct but effective 00139 String = iOther.String; 00140 Length = iOther.Length; 00141 return *this; 00142 } 00143 00145 void Append (const char *iString, size_t iLength); 00146 00148 void Append (const Token &iOther); 00149 00151 void AppendNL (int iCount); 00152 00154 int CountNL (); 00155 00157 bool GetValue (long &oValue) const; 00158 00160 void SetValue (long iValue); 00161 00163 bool operator == (const Token &iOther) 00164 { 00165 if (iOther.Length != Length) 00166 return false; 00167 return (memcmp (String, iOther.String, Length) == 0); 00168 } 00169 }; 00170 00172 class Macro 00173 { 00174 public: 00176 Token Name; 00178 int NumArgs; 00180 Token *Args; 00182 Token Value; 00184 Token Body; 00186 Macro *Next; 00188 Token (*ExpandFunc) (CPreprocessor *iParent, int iNumArgs, Token *iArgs); 00190 bool Expanding; 00191 00192 Macro (const Token &iName) : 00193 Name (iName), NumArgs (0), Args (NULL), Next (NULL), 00194 ExpandFunc (NULL), Expanding (false) 00195 { } 00196 00197 ~Macro () 00198 //{ OGRE_DELETE [] Args; OGRE_DELETE Next; } 00199 { delete [] Args; delete Next; } 00200 00202 Token Expand (int iNumArgs, Token *iArgs, Macro *iMacros); 00203 }; 00204 00205 friend class CPreprocessor::Macro; 00206 00208 const char *Source; 00210 const char *SourceEnd; 00212 int Line; 00214 bool BOL; 00216 unsigned EnableOutput; 00218 Macro *MacroList; 00219 00223 CPreprocessor (const Token &iToken, int iLine); 00224 00232 Token GetToken (bool iExpand); 00233 00243 Token HandleDirective (Token &iToken, int iLine); 00244 00255 bool HandleDefine (Token &iBody, int iLine); 00256 00267 bool HandleUnDef (Token &iBody, int iLine); 00268 00279 bool HandleIfDef (Token &iBody, int iLine); 00280 00291 bool HandleIf (Token &iBody, int iLine); 00292 00303 bool HandleElse (Token &iBody, int iLine); 00304 00315 bool HandleEndIf (Token &iBody, int iLine); 00316 00327 Token GetArgument (Token &oArg, bool iExpand); 00328 00339 Token GetArguments (int &oNumArgs, Token *&oArgs, bool iExpand); 00340 00354 Token GetExpression (Token &oResult, int iLine, int iOpPriority = 0); 00355 00371 bool GetValue (const Token &iToken, long &oValue, int iLine); 00372 00381 Token ExpandMacro (const Token &iToken); 00382 00390 Macro *IsDefined (const Token &iToken); 00391 00403 static Token ExpandDefined (CPreprocessor *iParent, int iNumArgs, Token *iArgs); 00404 00412 Token Parse (const Token &iSource); 00413 00423 void Error (int iLine, const char *iError, const Token *iToken = NULL); 00424 00425 public: 00427 CPreprocessor () : MacroList (NULL) 00428 { } 00429 00431 virtual ~CPreprocessor (); 00432 00444 void Define (const char *iMacroName, size_t iMacroNameLen, 00445 const char *iMacroValue, size_t iMacroValueLen); 00446 00456 void Define (const char *iMacroName, size_t iMacroNameLen, long iMacroValue); 00457 00467 bool Undef (const char *iMacroName, size_t iMacroNameLen); 00468 00491 char *Parse (const char *iSource, size_t iLength, size_t &oLength); 00492 00508 typedef void (*ErrorHandlerFunc) ( 00509 void *iData, int iLine, const char *iError, 00510 const char *iToken, size_t iTokenLen); 00511 00517 static ErrorHandlerFunc ErrorHandler; 00518 00520 void *ErrorData; 00521 }; 00522 00523 } // namespace Ogre 00524 00525 #endif // __OGRE_CPREPROCESSOR_H__
Copyright © 2012 Torus Knot Software Ltd
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
Last modified Sun Sep 2 2012 07:27:21