OgreGLSLPreprocessor.h
Go to the documentation of this file.
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     namespace GLSL {
00037 
00062 class CPreprocessor
00063 {
00077     class Token
00078     {
00079     public:
00080         enum Kind
00081         {
00082             TK_EOS,          // End of input stream
00083             TK_ERROR,        // An error has been encountered
00084             TK_WHITESPACE,   // A whitespace span (but not newline)
00085             TK_NEWLINE,      // A single newline (CR & LF)
00086             TK_LINECONT,     // Line continuation ('\' followed by LF)
00087             TK_NUMBER,       // A number
00088             TK_KEYWORD,      // A keyword
00089             TK_PUNCTUATION,  // A punctuation character
00090             TK_DIRECTIVE,    // A preprocessor directive
00091             TK_STRING,       // A string
00092             TK_COMMENT,      // A block comment
00093             TK_LINECOMMENT,  // A line comment
00094             TK_TEXT          // An unparsed text (cannot be returned from GetToken())
00095         };
00096 
00098         Kind Type;
00100         mutable size_t Allocated;
00101         union
00102         {
00104             const char *String;
00106             char *Buffer;
00107         };
00109         size_t Length;
00110 
00111         Token () : Allocated (0), String (NULL)
00112         { }
00113 
00114         Token (Kind iType) : Type (iType), Allocated (0), String (NULL)
00115         { }
00116 
00117         Token (Kind iType, const char *iString, size_t iLength) :
00118             Type (iType), Allocated (0), String (iString), Length (iLength)
00119         { }
00120 
00121         Token (const Token &iOther)
00122         {
00123             Type = iOther.Type;
00124             Allocated = iOther.Allocated;
00125             iOther.Allocated = 0; // !!! not quite correct but effective
00126             String = iOther.String;
00127             Length = iOther.Length;
00128         }
00129 
00130         ~Token ()
00131         { if (Allocated) free (Buffer); }
00132 
00134         Token &operator = (const Token &iOther)
00135         {
00136             if (Allocated) free (Buffer);
00137             Type = iOther.Type;
00138             Allocated = iOther.Allocated;
00139             iOther.Allocated = 0; // !!! not quite correct but effective
00140             String = iOther.String;
00141             Length = iOther.Length;
00142             return *this;
00143         }
00144 
00146         void Append (const char *iString, size_t iLength);
00147 
00149         void Append (const Token &iOther);
00150 
00152         void AppendNL (int iCount);
00153 
00155         int CountNL ();
00156 
00158         bool GetValue (long &oValue) const;
00159 
00161         void SetValue (long iValue);
00162 
00164         bool operator == (const Token &iOther)
00165         {
00166             if (iOther.Length != Length)
00167                 return false;
00168             return (memcmp (String, iOther.String, Length) == 0);
00169         }
00170     };
00171 
00173     class Macro
00174     {
00175     public:
00177         Token Name;
00179         int NumArgs;
00181         Token *Args;
00183         Token Value;
00185         Token Body;
00187         Macro *Next;
00189         Token (*ExpandFunc) (CPreprocessor *iParent, int iNumArgs, Token *iArgs);
00191         bool Expanding;
00192 
00193         Macro (const Token &iName) :
00194             Name (iName), NumArgs (0), Args (NULL), Next (NULL),
00195             ExpandFunc (NULL), Expanding (false)
00196         { }
00197 
00198         ~Macro ()
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 GLSL
00524 } // namespace Ogre
00525 
00526 #endif // __OGRE_CPREPROCESSOR_H__

Copyright © 2012 Torus Knot Software Ltd
Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
Last modified Mon Jul 27 2020 13:40:43