00001 /* 00002 ----------------------------------------------------------------------------- 00003 This source file is part of OGRE 00004 (Object-oriented Graphics Rendering Engine) 00005 For the latest info, see http://www.ogre3d.org/ 00006 00007 Copyright (c) 2000-2013 Torus Knot Software Ltd 00008 00009 Permission is hereby granted, free of charge, to any person obtaining a copy 00010 of this software and associated documentation files (the "Software"), to deal 00011 in the Software without restriction, including without limitation the rights 00012 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 00013 copies of the Software, and to permit persons to whom the Software is 00014 furnished to do so, subject to the following conditions: 00015 00016 The above copyright notice and this permission notice shall be included in 00017 all copies or substantial portions of the Software. 00018 00019 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00020 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00021 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 00022 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00023 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00024 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 00025 THE SOFTWARE. 00026 ----------------------------------------------------------------------------- 00027 */ 00028 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 Mon Jul 27 2020 13:40:42