Index: Preprocessor.cpp
===================================================================
--- Preprocessor.cpp (revision 23013)
+++ Preprocessor.cpp (working copy)
@@ -1,54866 +1,538 @@65 @@
/*
- * This source file originally came from OGRE v1.7.2 - http://www.ogre3d.org/
- * with some tweaks as part of 0 A.D.
- * All changes are released under the original license, as follows:
- */
+ -----------------------------------------------------------------------------
+ This source file is part of OGRE
+ (Object-oriented Graphics Rendering Engine)
+ For the latest info, see http://www.ogre3d.org/
-/*
------------------------------------------------------------------------------
-This source file is part of OGRE
-(Object-oriented Graphics Rendering Engine)
-For the latest info, see http://www.ogre3d.org/
+ Copyright (c) 2000-2014 Torus Knot Software Ltd
-Copyright (c) 2000-2009 Torus Knot Software Ltd
+ Permission is hereby granted, free of charge, to any person obtaining a copy
+ of this software and associated documentation files (the "Software"), to deal
+ in the Software without restriction, including without limitation the rights
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ copies of the Software, and to permit persons to whom the Software is
+ furnished to do so, subject to the following conditions:
-Permission is hereby granted, free of charge, to any person obtaining a copy
-of this software and associated documentation files (the "Software"), to deal
-in the Software without restriction, including without limitation the rights
-to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-copies of the Software, and to permit persons to whom the Software is
-furnished to do so, subject to the following conditions:
+ The above copyright notice and this permission notice shall be included in
+ all copies or substantial portions of the Software.
-The above copyright notice and this permission notice shall be included in
-all copies or substantial portions of the Software.
-
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
-THE SOFTWARE.
------------------------------------------------------------------------------
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ -----------------------------------------------------------------------------
*/
-#include "precompiled.h"
+#include "OgreGLSLPreprocessor.h"
+#include "OgreLogManager.h"
-#include "Preprocessor.h"
+#include <ctype.h>
+#include <stdio.h>
+#include <assert.h>
-#include "ps/CLogger.h"
+namespace Ogre {
-// Limit max number of macro arguments to this
+ // Limit max number of macro arguments to this
#define MAX_MACRO_ARGS 16
-//---------------------------------------------------------------------------//
+#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32 && !defined( __MINGW32__ )
+#define snprintf _snprintf
+#endif
-/// Return closest power of two not smaller than given number
-static size_t ClosestPow2 (size_t x)
-{
- if (!(x & (x - 1)))
- return x;
- while (x & (x + 1))
- x |= (x + 1);
- return x + 1;
-}
+ /// Return closest power of two not smaller than given number
+ static size_t ClosestPow2 (size_t x)
+ {
+ if (!(x & (x - 1)))
+ return x;
+ while (x & (x + 1))
x |= (x + 1);
+ x |= (return x + 1);;
+ return x + 1;-}
+ }
-void CPreprocessor::Token::Append (const char *iString, size_t iLength)
-{
- Token t (Token::TK_TEXT, iString, iLength);
- Append (t);
-}
-
-void CPreprocessor::Token::Append (const Token &iOther)
-{
- if (!iOther.String)
- return;
-
- if (!String)
+ void CPreprocessor::Token::Append (const char *iString, size_t iLength)
{
- String = iOther.String;
- Length = iOther.Length;
- Allocated = iOther.Allocated;
- iOther.Allocated = 0; // !!! not quite correct but effective
- return;
+ Token t (Token::TK_TEXT, iString, iLength);
+ Append (t);+ {
}
- if (Allocated)
+ void CPreprocessor::Token::Append (const Token &iOther)Token t (Token::TK_TEXT, iString, iLength);
{Append (t);
- size_t new_alloc = ClosestPow2 (Length + iOther.Length);
- if (new_alloc < 64)-}
- new_alloc = 64;+ }
- if (new_alloc != Allocated
-void CPreprocessor::Token::Append (const Token &iOther)
+ if (!iOther.String)-{
+ return;void CPreprocessor::Token::Append (const Token &iOther)
+ {
+ if (!if (!iOther.String)
{
- Allocated = new_alloc;
- Buffer = (char *)realloc (Buffer, Allocated);
+ String = iOther.String;
+ Length = iOther.Lengthreturn;
+ Allocated = iOther.Allocated;
+ iOther.Allocated = 0; // !!! not quite correct but effective
@@ -97,10 +96,10 @@
+ return;if (Allocated)
}
- }
- else if (Stringmemcpy (Buffer + Length !=, iOther.String)
- {
- Allocated = ClosestPow2 (Length +ng, iOther.Length);
- if (Allocated < 64)
- Allocated = 64;
- char *newstr = (char *)malloc (Allocated);
- memcpy (newstr, String, Length);
- Buffer = newstr;
- }
- if (Allocated)
- memcpy (Buffer + Length, iOther.String, iOther.Length);
- Length += iOther.Length;
-}
+ if (Allocated)
+ {
+ size_t new_alloc = ClosestPow2 (Length + iOther.Length);
+ if (new_alloc < 64)
+ new_alloc = 64;
+ if (new_alloc != Allocated)
+ {
+ Allocated = new_alloc;
+ Buffer = (char *)realloc (Buffer, Allocated);
+ }
+ }
+ else if (String + Length != iOther.String)
+ {
+ Allocated = ClosestPow2 (Length + iOther.Length);
+ if (Allocated < 64)
+ Allocated = 64;
+ char *newstr = (char *)malloc (Allocated);
+ memcpy (newstr, String, Length);
+ Buffer = newstr;
+ }
-bool CPreprocessor::Token::GetValue (long &oValue) const
-{
- long val = 0;
- size_t i = 0;
-
- while (isspace (String [i]))
- i++;
-
- long base = 10;
- if (String [i] == '0')
- {
- if (Length > i + 1 && String [i + 1] == 'x')
- base = 16, i += 2;
- else
- base = 8;
+ if (Allocated)+ bool CPreprocessor::Token::GetValue (long &oValue) const
+ memcpy (Buffer + Length, iOther.String, iOther.Length);{
+ Length += iOther.Length;long val = 0;
}size_t i = 0;
- for (; i < Length; i++)@@ -118,7 +117,7 @@
+ bool CPreprocessor::Token::GetValue (long &oValue) const
for (; i < Length; i++)
{
- long c = long (String [i]);
- if (isspace (c))
- // Possible end of number
- break;
+ long val = 0;
+ size_t i = 0;
- if (c >= 'a' && c <= 'z')
- c -= ('a' - 'A');
+ while (isspace (String [i]))
+ i++;
- c -= '0';
- if (c < 0)
- return false;
+ long base = 10;
+ if (String [i] == '0')
+ {
+ if (Length > i + 1 && String [i + 1] == 'x')
+ base = 16, i += 2;
+ else
+ base = 8;
+ }
- if (c > 9)
- c -= ('A' - '9' - 1);
+ for (; i < Length; i++)
+ {
+ int c = int (String [i]);
+ if (isspace (c))
+ // Possible end of number
+ break;
- if (c >= base)
- return false;
+ if (c >= 'a' && c <= 'z')
+ c -= ('a' - 'A');
- val = (val * base) + c;
- }
+ c -= '0';
+ if (c < 0)
+ return false;
- // Check that all other characters are just spaces
- for (; i < Length; i++)
- if (!isspace (String [i] if (isspace (c))
- return false;// Possible end of number
+ if (c > 9) break;
+ c -= ('A' - '9' - 1);@@ -146,19 +145,21 @@
- oValue = val;
- return true;
-}
+ if (c >= base)
+ return false;}
-void CPreprocessor::Token::SetValue (long iValue)
-{
- char tmp [21];
- int len = snprintf (tmp, sizeof (tmp), "%ld", iValue);
- Length = 0;
- Append (tmp, len);
- Type = TK_NUMBER;
-}
+ val = (val * base) + c;
+ }
-void CPreprocessor::Token::AppendNL (int iCount)
-{
- static const char newlines [8] =
- { '\n', '\n', '\n', '\n', '\n', '\n', '\n', '\n' };
+ // Check that all other characters are just spaces
+ for (; i < Length; i++)
+ if (!isspace (String [i]))
+ return false;
- while (iCount > 8)
- {
- Append (newlines, 8);
- iCount -= 8;+
+ ovoid CPreprocessor::Token::SetValue = val;(long iValue)
+ return true;{
}
- if (iCount > 0)
- Append (newlines, iCount);
-}
-int CPreprocessor::Token::CountNL ()
-{
- if (Type == TK_EOS || Type == TK_ERROR)
- return 0;
- const char *s = String;
- int l = Length;
- int c = 0;
- while (l > 0)
+ void CPreprocessor::Token::SetValue (long iValue)char tmp [21];
{
- const char *int len = (const char *)memchr (ssnprintf (tmp, '\n'sizeof (tmp), l);
- if (!n)
- return c;
- c++;
- l -= (n - s + 1);
- s = n + 1;"%ld", iValue);
+ char tmp [21];
+ int len = snprintf (tmp, sizeof (tmp), "%ld", iValue);
+ Length = 0;
+ Append (tmp, len);
+ Type = TK_NUMBER Length = 0;
}Append (tmp, len);
- return c;Type = TK_NUMBER;
-}
-//---------------------------------------------------------------------------//+ }
-CPreprocessor::Token-void CPreprocessor::Macro::Expand (
- Token::AppendNL (int iNumArgs, CPreprocessor::Token *iArgs, Macro *iMacrosCount)
-{
- Expanding = true;+
+ void CPreprocessor::Token::AppendNL (int iCount)
+ {
+ static const char newlines [8] =
+ { '\n', '\n', '\n', '\n', '\n', '\n', '\n', '\n' };
- CPreprocessor cpp;
- cpp.MacroList = iMacros;@@ -169,15 +170,16 @@
+ while (iCount > 8)
+ {
+ Append (newlines, 8);
+ iCount -= 8; }
+ }if (iCount > 0)
+ if (Append (newlines, iCount > 0));
+ Append (newlines, iCount);-}
+ }
- // Define a new macro for every argument
- int i;
- for (i = 0; i < iNumArgs; i++)
- cpp.Define (Args [i].String, Args [i].Length,
- iArgs [i].String, iArgs [i].Length);
- // The rest arguments are empty
- for (; i < NumArgs; i++)
- cpp.Define (Args [i].String, Args [i].Length, "", 0);int CPreprocessor::Token::CountNL ()
- // Now run the macro expansion through the supplimentary preprocessor-{
- Token xt = cpp.Parse (Value);+
+ int CPreprocessor::Token::CountNL ()
+ {
+ if (Type == TK_EOS || Type == TK_ERROR)
+ return 0;
- Expanding = false;const char *s = String;
+ - const char *s = String;int l = Length;
+ size_t l = Length;
+ int c = 0;
+ while (l > 0)
+ {
+ const char *n = (const char *)memchr (s, '\n', l);0;
+ if (!nwhile (l > 0)
+ return c; {
+ c++;@@ -189,13 +191,12 @@
+ l -= (n - ss = n + 1);;
+ s = n + 1; }
+ }return c;
+ return c;-}
+ }
- // Remove the extra macros we have defined
- for (int j = NumArgs - 1; j >= 0; j--)
- cpp.Undef (Args [j].String, Args [j].Length);//---------------------------------------------------------------------------//
- cpp.-CPreprocessor::Token CPreprocessor::MacroList = NULL;::Expand (
+ CPreprocessor::Token CPreprocessor::Macro::Expand (
+ int iNumArgs, CPreprocessor::Token *iArgs, Macro *iMacros)
-{
+ {
+ Expanding = true;
- return xt;
-}CPreprocessor cpp;
+ CPreprocessor cpp;@@ -222,49 +223,35 @@
+ cpp.MacroList = iMacros;NULL;
-//---------------------------------------------------------------------------//
+ // Define a new macro for every argument
+ int i;
+ for (i = 0; i < iNumArgs; i++)
+ cpp.Define (Args [i].String, Args [i].Length, return xt;
+ iArgs [i].String, iArgs [i].Length);-}
+ // The rest arguments are empty}
+ for (; i < NumArgs; i++)
-//---------------------------------------------------------------------------//
+ cpp.Define (Args [i].String, Args [i].Length, "", 0);-
-static void DefaultError (void *iData, int iLine, const char *iError,
- const char *iToken, size_t iTokenLen)
-{
- (void)iData;
-+ void CPreprocessor::Error(int iLine, const char *iError, const Token *iToken)
+ {
+ char line [1000];
if (iToken)
- LOGERROR("Preprocessor error: line %d: %s: '%s'\n",
- iLine, iError, std::string (iToken, iTokenLen));
-+ snprintf (line, sizeof (line), "line %d: %s: `%.*s'\n",
+ iLine, iError, int (iToken->Length), iToken->String);
else
- LOGERROR("Preprocessor error: line %d: %s\n", iLine, iError);
-}
+ // Now run the macro expansion through the supplimentary preprocessor snprintf (line, sizeof (line), "line %d: %s\n", iLine, iError);
+ LogManager::getSingleton ().logMessage (line, LML_CRITICAL);
+ Token xt = cpp.Parse (Value);}
-//---------------------------------------------------------------------------//
+ Expanding = false;-
-CPreprocessor::ErrorHandlerFunc CPreprocessor::ErrorHandler = DefaultError;
+ // Remove the extra macros we have defined
+ for (int j = NumArgs - 1; j >= 0; j--)
+ cpp.Undef (Args [j].String, Args [j].Length);-
-CPreprocessor::CPreprocessor (const Token &iToken, int iLine) : MacroList (NULL)
-{
-+ Source = CPreprocessor::CPreprocessor (const Token &iToken.String;, int iLine) : MacroList (NULL)
- SourceEnd = iToken.String + iToken.Length;+ {
- EnableOutput = 1;Source = iToken.String;
- Line = iLine;SourceEnd = iToken.String + iToken.Length;
- EnableOutput = 1;
Line = iLine;
BOL = true;
-}
+ cpp.MacroList = NULL;}
-CPreprocessor::~CPreprocessor ()
-{
-+ delete MacroList;CPreprocessor::~CPreprocessor ()
-}+ {
+ return xt;delete MacroList;
-}
+ }
-void CPreprocessor::Error (int iLine, const char *iError, const Token *iToken)
-{
- if (iToken)
- ErrorHandler (ErrorData, iLine, iError, iToken->String, iToken->Length);
- else
- ErrorHandler (ErrorData, iLine, iError, NULL, 0);
-}
+ void CPreprocessor::Error(int iLine, const char *iError, const Token *iToken)
+ {
+ char line [1000];
+ if (iToken)
+ snprintf (line, sizeof (line), "line %d: %s: `%.*s'\n",
+ iLine, iError, int (iToken->Length), iToken->String);
+ else
+ snprintf (line, sizeof (line), "line %d: %s\n", iLine, iError);
+ LogManager::getSingleton ().logMessage (line, LML_CRITICAL);
+ }-
-CPreprocessor::Token CPreprocessor::GetToken (bool iExpand)
-{
- if (Source >= SourceEnd)
- return Token (Token::TK_EOS);
-
- const char *begin = Source;
- char c = *Source++;
-
-
- if (c == '\n' || (c == '\r' && *Source == '\n')+ CPreprocessor::Token CPreprocessor::GetToken (bool iExpand)
+ CPreprocessor::CPreprocessor (const Token &iToken, int iLine) : MacroList (NULL){
{
- Line++;
+ Source = iToken.String;
+ SourceEnd = iToken.String + iToken.Length;
+ EnableOutput = 1;
+ Line = iLine;if (Source >= SourceEnd)
BOL = true;
- if (c == '\r')
- Source++;
- return Token (Token::TK_NEWLINE, begin, Source - begin);
}
- else if (isspace (c))
+
+ CPreprocessor::~CPreprocessor ()
{
- while (Source < SourceEnd &&
- *Source != '\r' &&
- *Source != '\n' &&
- isspace (*Source))
- Source++;
+ delete MacroList;
+ }return Token (Token::TK_EOS);
- return Token (Token::TK_WHITESPACE, begin, Source - begin);
- }
- else if (isdigit (c))
+ CPreprocessor::Token CPreprocessor::GetToken (bool iExpand)
{
- BOL = false;
- if (c == '0' && Source < SourceEnd && Source [0] == 'x') // hex numbers
+ if (Source >= SourceEnd)
+ return Token (Token::TK_EOS);
+
+ const char *begin = Source;
+ char c = *Source++;
+
+
+ if (c == '\n' || (c == '\r' && *Source == '\n'))
{
- Source++;
- while (Source < SourceEnd && isxdigit (*Source))
+ Line++;
+ BOL = true;
+ if (c == '\r')
Source++;
+ return Token (Token::TK_NEWLINE, begin, Source - begin);
}
- else
- while (Source < SourceEnd && isdigit (*Source))
+ else if (isspace (c))
+ {
+ while (Source < SourceEnd &&
+ *Source != '\r' &&
+ *Source != '\n' &&
+ isspace (*Source))
Source++;
- return Token (Token::TK_NUMBER, begin, Source - begin);
- }
- else if (c == '_' || isalnum (c))
- {
- BOL = false;
- while (Source < SourceEnd && (*Source == '_' || isalnum (*Source)))
- Source++;
- Token t (Token::TK_KEYWORD, begin, Source - begin);
- if (iExpand)
- t = ExpandMacro (t);
- return t;
- }
- else if (c == '"' || c == '\'')
- {
- BOL = false;
- while (Source < SourceEnd && *Source != c)
+
+ return Token (Token::TK_WHITESPACE, begin, Source - begin);
+ }
+ else if (isdigit (c))
{
- if (*Source == '\\')
+ BOL = false;
+ if (c == '0' && Source < SourceEnd && Source [0] == 'x') // hex numbers
{
Source++;
- if (Source >= SourceEnd)
- break;
+ while (Source < SourceEnd && isxdigit (*Source))
+ Source++;
}
- if (*Source == '\n')
- Line++;
+ else
+ while (Source < SourceEnd && isdigit (*Source))
+ Source++;
+ return Token (Token::TK_NUMBER, begin, Source - begin);
+ }
+ else if (c == '_' || isalnum (c))
+ {
+ BOL = false;
+ while (Source < SourceEnd && (*Source == '_' || isalnum (*Source)))
+ Source++;
+ Token t (Token::TK_KEYWORD, begin, Source - begin);
+ if (iExpand)
+ t = ExpandMacro (t);
+ return t;
+ }
+ else if (c == '"' || c == '\'')
+ {
+ BOL = false;
+ while (Source < SourceEnd && *Source != c)
+ {
+ if (*Source == '\\')
+ {
+ Source++;
+ if (Source >= SourceEnd)
+ break;
+ }
+ if (*Source == '\n')
+ Line++;
+ Source++;
+ }
+ if (Source < SourceEnd)
+ Source++;
+ return Token (Token::TK_STRING, begin, Source - begin);
+ }
+ else if (c == '/' && *Source == '/')
+ {
+ BOL = false;@@ -393,19 +380,21 @@
Source++;
+ while (Source < SourceEnd && *Source != '\r' && *Source != '\n')
+ Source++;
+ return Token (Token::TK_LINECOMMENT, begin, Source - begin);
}
- if (Source < SourceEnd)
+ else if (c == '/' && *Source == '*')
+ {
+ BOL = false;
Source++;
- return Token (Token::TK_STRING, begin, Source - begin);
- }
- else if (c == '/' && *Source == '/')
- {
- BOL = false;
- Source++;
- while (Source < SourceEnd && *Source != '\r' && *Source != '\n')
- Source++;
- return Token (Token::TK_LINECOMMENT, begin, Source - begin);
- }
- else if (c == '/' && *Source == '*')
- {
- BOL = false;
- Source++;
- while (Source < SourceEnd && (Source [0] != '*' || Source [1] != '/'))
+ while (Source < SourceEnd && (Source [0] != '*' || Source [1] != '/'))
+ {
+ if (*Source == '\n')
+ Line++;
+ Source++;
+ }
+ if (Source < SourceEnd && *Source == '*')
+ Source++;
+ if (Source < SourceEnd && *Source == '/')
+ Source++;
+ return Token (Token::TK_COMMENT, begin, Source - begin);
+ }
+ else if (c == '#' && BOL)
{
+ // Skip all whitespaces after '#'
+ while (Source < SourceEnd && isspace (*Source))
+ Source++return Token (Token::TK_PUNCTUATION, begin, Source - begin);
+ while (Source < SourceEnd && !isspace (*Source))
+ Source++;
+ return Token (Token::TK_DIRECTIVE, begin, Source - begin);
+ }
+ else if (c == '\\' && Source < SourceEnd && (*Source == '\r' || *Source == '\n'))
+ {
+ // Treat backslash-newline as a whole token
+ if (*Source == '\r')
+ Source++;
if (*Source == '\n')
- Line++;
- Source++;
+ Source++;
+ Line++;
+ BOL = true;
+ return Token (Token::TK_LINECONT, begin, Source - begin);
}
- if (Source < SourceEnd && *Source == '*')
- Source++;
- if (Source < SourceEnd && *Source == '/')
- Source++;
- return Token (Token::TK_COMMENT, begin, Source - begin);
+ else
+ {
+ BOL = false;
+ // Handle double-char operators here
+ if (c == '>' && (*Source == '>' || *Source == '='))
+ Source++;
+ else if (c == '<' && (*Source == '<' || *Source == '='))
+ Source++;
+ else if (c == '!' && *Source == '=')
+ Source++;
+ else if (c == '=' && *Source == '=')
+ Source++;
+ else if ((c == '|' || c == '&' || c == '^') && *Source == c)
+ Source++;
+ return Token (Token::TK_PUNCTUATION, begin, Source - begin);
+ }-}
+ }
- else if (c == '#' && BOL
-CPreprocessor::Macro *CPreprocessor::IsDefined (const Token &iToken)
+-{
+
+ CPreprocessor::Macro *CPreprocessor::IsDefined (const Token &iToken)
{
- // Skip all whitespaces after '#'
- while (Source < SourceEnd && isspace (*Source))
- Source++;
- while (Source < SourceEnd && !isspace (*Source))
- Source++;
- return Token (Token::TK_DIRECTIVE, begin, Source - begin);
+ for (Macro *cur = MacroList; cur; cur = cur->Next)
+ if (cur->Name == iToken)
+ return cur;
+
+ return NULL;+ {
}
- else if (c == '\\' && Source < SourceEnd && (*Source == '\r' || *Source == '\n'))
- {
- // Treat backslash-newline as a whole token
- if (*Source == '\r')
- Source++;
- if (*Source == '\n')
- Source++;
- Line++;
- BOL = true;
- return Token (Token::TK_LINECONT, begin, Source - begin);
- }
- else
- {
- BOL = false;
- // Handle double-char operators here
- if (c == '>' && (*Source == '>' || *Source == '='))
- Source++;
- else if (c == '<' && (*Source == '<' || *Source == '='))
- Source++;
- else if (c == '!' && *Source == '=')
- Source++;
- else if (c == '=' && *Source == '=')
- Source++;
- else if ((c == '|' || c == '&' || c == '^') && *Source == c)
- Source++;
- return Token (Token::TK_PUNCTUATION,for (Macro *cur = MacroList; begin,cur; Source - begin);
- }
-}
-CPreprocessor::Macro *CPreprocessor::IsDefined (const Token &iTokencur = cur->Next)
-{
- for (Macro *cur = MacroList; cur; cur = cur->Next)
- if (cur->Name == iToken)
- return cur;
- return NULL;
-}
-+ }
-CPreprocessor::Token CPreprocessor::ExpandMacro (const Token &iToken)
-{
- Macro *cur = IsDefined (iToken);
- if (cur && !cur->Expanding)+
+ CPreprocessor::Token CPreprocessor::ExpandMacro (const Token &iToken)
{
- Token *args = NULL;
- int nargs = 0;
- int old_line = Line;
+ Macro *cur = IsDefined (iToken);
+ if (cur && !cur->Expanding)+ {
+ {Macro *cur = IsDefined (iToken);
+ Token *args = NULL;if (cur && !cur->Expanding)
+ int nargs = 0; {
+ int old_line = Line;@@ -415,7 +404,7 @@
- if (cur->NumArgs != 0)
- {
- Token t = GetArguments (nargs, args, cur->ExpandFunc ? false : true);
-+ if (t.Type == Token::TK_ERROR)Token t = GetArguments (nargs, args, cur->ExpandFunc ? false : true, false);
+ if (cur->NumArgs != 0t.Type == Token::TK_ERROR)
{
- delete [] args;
- return t;
+ Token t = GetArguments (nargs, args, cur->ExpandFunc ? false : true, false);
+ if (t.Type == Token::TK_ERROR)
+ {
+ delete [] args;
+ return t;
+ }
+
+ // Put the token back into the source pool; we'll handle it later
+ if (t.String)
+ {
+ // Returned token should never be allocated on heap
+ assert (t.Allocated == 0);
+ Source = t.String;
+ Line -= t.CountNL () delete [] args;
+ }@@ -426,7 +415,7 @@
}
- // Put the token back into the source pool; we'll handle it later
- if (t.String)
+ if (nargs > cur->NumArgsif (t.String)
{
- // Returned token should never be allocated on heap
- ENSURE (t.Allocated == 0);
- Source = t.String;
- Line -= t.CountNL ();
+ char tmp [60];
+ snprintf (tmp, sizeof (tmp), "Macro `%.*s' passed %d arguments, but takes just %d",
+ int (cur->Name.Length), cur->Name.String,
+ nargs, cur->NumArgs+ assert (t.Allocated == 0);
+ Error (old_line, tmp);Source = t.String;
+ return Token (Token::TK_ERROR); Line -= t.CountNL ();
}
- }
- if (nargs > cur->NumArgs)
- {
- char tmp [60];
- snprintf (tmp, sizeof (tmp), "Macro `%.*s' passed %d arguments, but takes just %d",
- int (cur->Name.Length), cur->Name.String,
- nargs, cur->NumArgs);
- Error (old_line, tmp);
- return Token (Token::TK_ERROR);
- }
+ Token t = cur->ExpandFunc ?
+ cur->ExpandFunc (this, nargs, args) :
+ cur->Expand (nargs, args, MacroList);
+ t.AppendNL (Line - old_line);
- Token t = cur->ExpandFunc ?
- cur->ExpandFunc (this, nargs, args) :
- cur->Expand (nargs, args, MacroList);
- t.AppendNL (Line - old_line);
+ delete [] args;
- delete [] args;
+ return t;
+ }
- return t;
+ return iToken;@@ -453,9 +442,10 @@
}
- return iToken;
-}
+ }
-/**
- * Operator priority:
- * 0: Whole expression
- * 1: '(' ')'
- * 2: ||
- * 3: &&
- * 4: |
- * 5: ^
- * 6: &
- * 7: '==' '!='
- * 8: '<' '<=' '>' '>='
- * 9: '<<' '>>'
- * 10: '+' '-'
- * 11: '*' '/' '%'
- * 12: unary '+' '-' '!' '~'
- */
-CPreprocessor::Token CPreprocessor::GetExpression (
- Token &oResult, int iLine, int iOpPriority)
-{
- char tmp [40];
-
- do+
+ /**
+ * Operator priority:
+ * 0: Whole expression
+ * 1: '(' ')'
+ * 2: ||
+ * 3: &&
+ * 4: |
+ * 5: ^
+ * 6: & * Operator priority:
+ * 7: '==' '!=' * 0: Whole expression
+ * 8: '<' '<=' '>' '>='1: '(' ')'
+ * 9: '<<' '>>'@@ -471,9 +461,9 @@
+ * 10: '+' '-' * 11: '*' '/' '%'
+ * 11: '*' '/' '%' * 12: unary '+' '-' '!' '~'
+ * 12: unary '+' '-' '!' '~' */
+ */-CPreprocessor::Token CPreprocessor::GetExpression (
+ CPreprocessor::Token CPreprocessor::GetExpression (
+ Token &oResult, int iLine, int iOpPriority)
{
- oResult = GetToken (true);
- } while (oResult.Type == Token::TK_WHITESPACE ||
- oResult.Type == Token::TK_NEWLINE ||
- oResult.Type == Token::TK_COMMENT ||
- oResult.Type == Token::TK_LINECOMMENT ||
- oResult.Type == Token::TK_LINECONT);
+ char tmp [40];
- Token op (Token::TK_WHITESPACEToken &oResult, ""int iLine, 0);
+ do
+ {
+ oResult = GetToken (true);int iOpPriority)
+ } while (oResult.Type == Token::TK_WHITESPACE ||
+ oResult.Type == Token::TK_NEWLINE ||
+ oResult.Type == Token::TK_COMMENT ||-{
+ oResult.Type == Token::TK_LINECOMMENT ||{
+ oResult.Type == Token::TK_LINECONT);char tmp [40];
- // Handle unary operators here
- if (oResult.Type == Token::TK_PUNCTUATION && oResult.Length == 1)
- {
- if (strchr ("+-!~", oResult.String [0]))
+ Token op (Token::TK_WHITESPACE, "", 0);
+
+ // Handle unary operators here
+ if (oResult.Type == Token::TK_PUNCTUATION && oResult.Length == 1)
{
- char uop = oResult.String [0];
- op = GetExpression (oResult, iLine, 12);
- long val;
- if (!GetValue (oResult, val, iLine))
+ if (strchr ("+-!~", oResult.String [0]))
{
- snprintf (tmp, sizeof (tmp), "Unary '%c' not applicable", uop);
- Error (iLine, tmp, &oResult);
- return Token (Token::TK_ERROR);
+ char uop = oResult.String [0];
+ op = GetExpression (oResult, iLine, 12);
+ long val;
+ if (!GetValue (oResult, val, iLine))
+ {
+ snprintf (tmp, sizeof (tmp), "Unary '%c' not applicable", uop);
+ Error (iLine, tmp, &oResult);
+ return Token (Token::TK_ERROR);
+ }
+
+ if (uop == '-')
+ oResult.SetValue (-val);
+ else if (uop == '!')
+ oResult.SetValue (!val); do
+ else if (uop == '~')@@ -520,7 +510,7 @@
+ oResult.SetValue (~val);return Token (Token::TK_ERROR);
}
+ else if (oResult.String [0] == '(')
+ {
+ op = GetExpression (oResult, iLine, 1);
+ if (op.Type == Token::TK_ERROR)
+ return op;
+ if (op.Type == Token::TK_EOS)
+ {
+ Error (iLine, "Unclosed parenthesis in #if expression");
+ return Token (Token::TK_ERROR);
+ }
- if (uop == '-')
- oResult.SetValue (-val);
- else if (uop == '!')
- oResult.SetValue (!val);
- else if (uop == '~')
- oResult.SetValue (~val);
+ assert (op.Type == Token::TK_PUNCTUATION &&
+ op.Length == 1 &&
+ op.String [0] == ')');
+ op = GetToken (true);
+ }
}
- else if (oResult.String [0] == '(')
- {
- op = GetExpression (oResult, iLine, 1);
- if (op.Type == Token::TK_ERROR)
- return op;
- if (op.Type == Token::TK_EOS)
- {
- Error (iLine, "Unclosed parenthesis in #if expression");
- return Token (Token::TK_ERROR);
- }
- ENSURE (op.Type == Token::TK_PUNCTUATION &&
- op.Length == 1 &&
- op.String [0] == ')');
+ while (op.Type == Token::TK_WHITESPACE ||
+ op.Type == Token::TK_NEWLINE ||
+ op.Type == Token::TK_COMMENT ||+ assert (op.Type == Token::TK_PUNCTUATION &&
+ op.Type == Token::TK_LINECOMMENT || op.Length == 1 &&
+ op.Type == Token::TK_LINECONT) op.String [0] == ')');
op = GetToken (true);
- }
- }
- while (op.Type == Token::TK_WHITESPACE ||
- op.Type == Token::TK_NEWLINE ||
- op.Type == Token::TK_COMMENT ||
- op.Type == Token::TK_LINECOMMENT ||
- op.Type == Token::TK_LINECONT)
- op = GetToken (true);
+ while (true)
+ {
+ if (op.Type != Token::TK_PUNCTUATION)
+ return op;
- while (true)
- {
- if (op.Type != Token::TK_PUNCTUATION)
- return op;
-
- int prio = 0;
- if (op.Length == 1)
- switch (op.String [0])
- {
+ int prio = 0;
+ if (op.Length == 1)
+ switch (op.String [0])
+ {
case ')': return op;
case '|': prio = 4; break;
case '^': prio = 5; break;
@@ -554,10 +544,10 @@
case '*':
case '/':
case '%': prio = 11; break;
- }
- else if (op.Length == 2)
- switch (op.String [0])
- {
+ }
+ else if (op.Length == 2)
+ switch (op.String [0])
+ {
case '|': if (op.String [1] == '|') prio = 2; break;
case '&': if (op.String [1] == '&') prio = 3; break;
case '=': if (op.String [1] == '=') prio = 7; break;
@@ -574,37 +564,37 @@
else if (op.String [1] == '>')
prio = 9;
break;
+ }
+
+ if (!prio)
+ {
+ Error (iLine, "Expecting operator, got", &op);
+ return Token (Token::TK_ERROR);
}@@ -655,10 +645,11 @@
- if (!prio)
- {
- Error (iLine, "Expecting operator, got", &op);
- return Token (Token::TK_ERROR);
- }
+ if (iOpPriority >= prio)
+ return op;
- if (iOpPriority >= prio)
- return op;
+ Token rop;
+ Token nextop = GetExpression (rop, iLine, prio);
+ long vlop, vrop;
+ if (!GetValue (oResult, vlop, iLine))
+ {
+ snprintf (tmp, sizeof (tmp), "Left operand of '%.*s' is not a number",
+ int (op.Length), op.String);
+ Error (iLine, tmp, &oResult);
+ return Token (Token::TK_ERROR);
+ }
+ if (!GetValue (rop, vrop, iLine))
+ {
+ snprintf (tmp, sizeof (tmp), "Right operand of '%.*s' is not a number",
+ int (op.Length), op.String);
+ Error (iLine, tmp, &rop);
+ return Token (Token::TK_ERROR);
+ }
- Token rop;
- Token nextop = GetExpression (rop, iLine, prio);
- long vlop, vrop;
- if (!GetValue (oResult, vlop, iLine))
- {
- snprintf (tmp, sizeof (tmp), "Left operand of '%.*s' is not a number",
- int (op.Length), op.String);
- Error (iLine, tmp, &oResult);
- return Token (Token::TK_ERROR);
- }
- if (!GetValue (rop, vrop, iLine))
- {
- snprintf (tmp, sizeof (tmp), "Right operand of '%.*s' is not a number",
- int (op.Length), op.String);
- Error (iLine, tmp, &rop);
- return Token (Token::TK_ERROR);
- }
-
- switch (op.String [0])
- {
+ switch (op.String [0])
+ {
case '|':
if (prio == 2)
oResult.SetValue (vlop || vrop);
@@ -651,59 +641,59 @@
else
oResult.SetValue (vlop % vrop);
break;
+ }
+
+ op = nextop;
}
-
- op = nextop;
}
-}
+ }
-bool CPreprocessor::GetValue (const Token &iToken, long &oValue, int iLine)
-{
- Token r;
- const Token *vt = &iToken;
- if ((vt->Type == Token::TK_KEYWORD ||
- vt->Type == Token::TK_TEXT ||
- vt->Type == Token::TK_NUMBER) &&
- !vt->String)+
+ bool CPreprocessor::GetValue (const Token &iToken, long &oValue, int iLine)
{
- Error (iLine, "Trying to evaluate an empty expression");
- return false;
- }
+ Token r;
+ const Token *vt = &iToken;
- if (vt->Type == Token::TK_TEXT)
- {
- CPreprocessor cpp (iToken, iLine);
- cpp.MacroList = MacroList;
+ if ((vt->Type == Token::TK_KEYWORD ||
+ vt->Type == Token::TK_TEXT ||
+ vt->Type == Token::TK_NUMBER) &&
+ !vt->String)
+ {
+ Error (iLine, "Trying to evaluate an empty expression");
+ return false;
+ }
- Token t;
- t = cpp.GetExpression (r, iLine);
+ if (vt->Type == Token::TK_TEXT)
+ {
+ CPreprocessor cpp (iToken, iLine);
+ cpp.MacroList = MacroList;
- cpp.MacroList = NULL;
+ Token t;
+ t = cpp.GetExpression (r, iLine);
- if (t.Type == Token::TK_ERROR)+ {
- return false;Token r;
+ cpp.MacroLisconst Token *vt = NULL;&iToken;
- if (t.Type != Token::TK_EOS)
- {
- Error (iLine, "Garbage after expression", &t);
- return false;
+ if (t.Type == Token::TK_ERROR)
+ return false;
+
+ if (t.Type != Token::TK_EOS)
+ {
+ Error (iLine, "Garbage after expression", &t);
+ return false;
+ }
+
+ vt = &r;@@ -693,6 +684,7 @@
vt = &r;
}
- vt = &r;
- }
-
- switch (vt->Type)
- {
+ Macro *m;
+ switch (vt->Type)
+ {
case Token::TK_EOS:
case Token::TK_ERROR:@@ -700,10 +692,8 @@
return false;
case Token::TK_KEYWORD:
- {
// Try to expand the macro
- Macro *m = IsDefined (*vt);
- if (m != NULL && !m->Expanding)
+ if ((m = IsDefined (*vt)) && !m->Expanding)
{
Token x = ExpandMacro (*vt);
m->Expanding = true;
@@ -715,7 +705,7 @@
// Undefined macro, "expand" to 0 (mimic cpp behaviour)
oValue = 0;
break;
- }
+
case Token::TK_TEXT:
case Token::TK_NUMBER:
if (!vt->GetValue (oValue))
@@ -728,60 +718,91 @@
default:
Error (iLine, "Unexpected token", vt);
return false;
+ }
+
+ return true;31,10 +721,12 @@
}
- return true;
-}
+ }
-CPreprocessor::Token CPreprocessor::GetArgument (Token &oArg, bool iExpand)
-{
- do+
+ CPreprocessor::Token CPreprocessor::GetArgument (Token &oArg, bool iExpand,
+ bool shouldAppendArg)
{
- oArg = GetToken (iExpand);
- } while (oArg.Type == Token::TK_WHITESPACE ||
- oArg.Type == Token::TK_NEWLINE ||
- oArg.Type == Token::TK_COMMENT ||
- oArg.Type == Token::TK_LINECOMMENT ||
- oArg.Type == Token::TK_LINECONT);
+ do
+ {
+ oArg = GetToken (iExpand);
+ } while (oArg.Type == Token::TK_WHITESPACE ||
+ oArg.Type == Token::TK_NEWLINE ||
+ oArg.Type == Token::TK_COMMENT ||
+ oArg.Type == Token::TK_LINECOMMENT ||
+ oArg.Type == Token::TK_LINECONT);
- if (!iExpand)
- {
- if (oArg.Type == Token::TK_EOS)
- return oArg;
- else if (oArg.Type == Token::TK_PUNCTUATION &&
- (oArg.String [0] == ',' ||
- oArg.String [0] == ')'))+ {
+ if (!iExpand) do
{
- Token t = oArg;
- oArg = Token (Token::TK_TEXT, "", 0);
- return t;
+ if (oArg.Type == Token::TK_EOS)
+ return oArg;
+ else if (oArg.Type == Token::TK_PUNCTUATION &&
+ (oArg.String [0] == ',' ||
+ oArg.String [0] == ')'))
+ {
+ Token t = oArg;
+ oArg = Token (Token::TK_TEXT, "", 0);
+ return t;
+ }
+ else if (oArg.Type != Token::TK_KEYWORD)
+ {
+ Error (Line, "Unexpected token", &oArg);
+ return Token (Token::TK_ERROR oArg = GetToken (iExpand);
+ }@@ -763,7 +755,12 @@
}
- else if (oArg.Type != Token::TK_KEYWORD)
- {
- Error (Line, "Unexpected token", &oArg);
- return Token (Token::TK_ERROR);
- }
- }
- unsigned int len = oArg.Length;
- while (true)
- {
- Token t = GetToken (iExpand);
- switch (t.Type)
+ size_t braceCount = 0;
+
+ if( oArg.Type == Token::TK_PUNCTUATION && oArg.String[0] == '(' )
+ ++braceCount;
+
+ size_t len = oArg.Length;
+ while (true)
{
+ Token t = GetToken (iExpand);
+ switch (t.Type)@@ -771,17 +768,41 @@
+ {
case Token::TK_EOS:
Error (Line, "Unfinished list of arguments");
- FALLTHROUGH;
+ OGRE_FALLTHROUGH;
case Token::TK_ERROR:
return Token (Token::TK_ERROR);
case Token::TK_PUNCTUATION:
- if (t.String [0] == ',' ||
- t.String [0] == ')')
+ if( t.String [0] == '(' )
{
- // Trim whitespaces at the end
- oArg.Length = len;
- return t;+ {
+ ++braceCount;
+ }
+ else if( !braceCount )
+ {
+ if (t.String [0] == ',' ||
+ t.String [0] == ')')
+ {
+ // Trim whitespaces at the end
+ oArg.Length = len;
+
+ //Append "__arg_" to all macro arguments, otherwise if user does:
+ // #define mad( a, b, c ) fma( a, b, c )
+ // mad( x.s, y, a );
+ //It will be translated to:
+ // fma( x.s, y, x.s );
+ //instead of:
+ // fma( x.s, y, a );
+ //This does not fix the problem by the root, but
+ //typing "__arg_" by the user is extremely rare.
+ if( shouldAppendArg )
+ oArg.Append( "__arg_", 6 );
+ return t;
+ }
+ }
+ else
+ {
+ if( t.String [0] == ')' )
+ --braceCount;
+ }
break;
case Token::TK_LINECONT:
case Token::TK_COMMENT:
@@ -791,6804,11 +812,725,12 @@
continue;if (t.Type != Token::TK_WHITESPACE)
default:
break;
- }
+ }
- if (!iExpand && t.Type != Token::TK_WHITESPACE)
- {
- Error (Line, "Unexpected token", &oArg);
- return Token (Token::TK_ERROR);
- }
+ if (!iExpand && t.Type != Token::TK_WHITESPACE)
+ {
+ Error (Line, "Unexpected token", &oArg);
+ return Token (Token::TK_ERROR);
+ }
- oArg.Append (t);
+ oArg.Append (t);
- if (t.Type != Token::TK_WHITESPACE)
- len = oArg.Length;
+ if (t.Type != Token::TK_WHITESPACE)
+ len = oArg.Length;
+ }
}
-}
+ }
-CPreprocessor::Token CPreprocessor::GetArguments (int &oNumArgs, Token *&oArgs,
- bool iExpand)
-{
- Token args [MAX_MACRO_ARGS];
- int nargs = 0;
- // Suppose we'll leave by the wrong path
- oNumArgs = 0;
- oArgs = NULL;
-
- Token t;
- do+
+ CPreprocessor::Token CPreprocessor::GetArguments (int &oNumArgs, Token *&oArgs,
+ bool iExpand, bool shouldAppendArg)
{
- t = GetToken (iExpand);
- } while (t.Type == Token::TK_WHITESPACE ||
- t.Type == Token::TK_COMMENT ||
- t.Type == Token::TK_LINECOMMENT);+ {
+ Token args [MAX_MACRO_ARGS];
+ int nargs = 0;
- if (t.Type != Token::TK_PUNCTUATION || t.String [0] != '(')
- {
+ // Suppose we'll leave by the wrong path@@ -816,15 +838,25 @@
oNumArgs = 0;
oArgs = NULL;
- return t;
- }
- while (true)
- {
- if (nargs == MAX_MACRO_ARGS)
+ bool isFirstTokenParsed = false;
+ bool isFirstTokenNotAnOpenBrace = false;
+
+ Token t;
+ do
{
- Error (Line, "Too many arguments to macro");
- return Token (Token::TK_ERROR);
+ t = GetToken (iExpand);
+
+ if( !isFirstTokenParsed &&
+ (t.Type != Token::TK_PUNCTUATION || t.String [0] != '(') )
+ {
+ isFirstTokenNotAnOpenBrace = true;
+ }
+ isFirstTokenParsed = true;
+ } while (t.Type == Token::TK_WHITESPACE ||
+ t.Type == Token::TK_COMMENT ||
+ t.Type == Token::TK_LINECOMMENT);
+
- if (t.Type != Token::TK_PUNCTUATION || t.String [0] != '(')
+ if( isFirstTokenNotAnOpenBrace )
+ {
{oNumArgs = 0;
+ oNumArgs = 0;NULL;
+ oArgs = NULL;@@ -839,13 +871,13 @@
+ return t;n Token (Token::TK_ERROR);
}
- t = GetArgument (args [nargs++], iExpand);
+ while (true)
+ {
+ if (nargs == MAX_MACRO_ARGS)
+ {
+ Error (Line, "Too many arguments to macro");
+ return Token (Token::TK_ERROR);
+ }
- switch (t.Type)
- {
+ t = GetArgument (args [nargs++], iExpand, shouldAppendArg);
+
+
switch (t.Type)
+ {
case Token::TK_EOS:
Error (Line, "Unfinished list of arguments");
- FALLTHROUGH;
+ OGRE_FALLTHROUGH;
case Token::TK_ERROR:
return Token (Token::TK_ERROR);
@@ -860,41 +892,38 @@
default:
Error (Line, "Unexpected token", &t);
break;
+ }3,16 +895,17 @@
}
+
+ Done:
+ oNumArgs = nargs;
+ oArgs = new Token [nargs];
+ for (int i = 0; i < nargs; i++)
+ oArgs [i] = args [i];
+ return t;
}
-Done:
- oNumArgs = nargs;+ Done:
- oNumArgs = new Token [nargs];;
- for (int i = 0; i < oArgs = new Token [nargs; i++)];
- for (int i = 0; i < nargs; i++)
oArgs [i] = args [i];
- return t;
-}
+ }
-bool CPreprocessor::HandleDefine (Token &iBody, int iLine)
-{
- // Create an additional preprocessor to process macro body
- CPreprocessor cpp (iBody, iLine);
-
- Token t = cpp.GetToken (false);
- if (t.Type != Token::TK_KEYWORD)+
+ bool CPreprocessor::HandleDefine (Token &iBody, int iLine)
+ {
- Error (iLine, "Macro name expected after #define");// Create an additional preprocessor to process macro body
- return false;CPreprocessor cpp (iBody, iLine);
- }
@@ -883,13 +916,9 @@
+ // Create an additional preprocessor to process macro body return false;
+ CPreprocessor cpp (iBody, iLine); }
- bool output_enabled = ((EnableOutput & (EnableOutput + 1)) == 0);
- if (!output_enabled)
- return true;
+ Token t = cpp.GetToken (false);
+ if (t.Type != Token::TK_KEYWORD)
+ {
+ Error (iLine, "Macro name expected after #define");
+ return false;
+ }-
- Macro *m = new Macro (t);
- m->Body = iBody;
- t = cpp.GetArguments (m->NumArgs, m->Args, false);
- while (t.Type == Token::TK_WHITESPACE)
- t = cpp.GetToken (false);
+ Macro *m = new Macro (t);
+ m->Body = iBody;
+ t = cpp.GetArguments (m->NumArgs, m->Args, false, true);
+ while (t.Type == Token::TK_WHITESPACE)
+ t = cpp.GetToken (false);
- switch (t.Type)
- {
+ switch (t.Type)
+ {
case Token::TK_NEWLINE: while (t.Type == Token::TK_WHITESPACE)
case t = cpp.GetToken::TK_EOS: (false);
// Assign "" to token
@@ -907,18319 +936,243 @@39 @@
default:
t.Type = Token::TK_TEXT;
- ENSURE (t.String + t.Length == cpp.Source);
+ assert (t.String + t.Length == cpp.Source);
t.Length = cpp.SourceEnd - t.String;
break;
- }
+ }
- m->Value = t;
- m->Next = MacroList;
- MacroList = m;
- return true;
-}
+ if( m->NumArgs > 0 )
+ {
+ CPreprocessor cpp2;
-bool CPreprocessor::HandleUnDef (Token &iBody, int iLine)
-{
- CPreprocessor cpp (iBody, iLine);+
+ //We need to convert:
+ // #define mad( a__arg_, b__arg_, c__arg_ ) fma( a, b, c )
+ //into:
+ // #define mad( a__arg_, b__arg_, c__arg_ ) fma( a__arg_, b__arg_, c__arg_ )
+ for( int i = 0; i < m->NumArgs; ++i )
+ {
+ cpp2.Define( m->Args[i].String, m->Args[i].Length - 6,
+ m->Args[i].String, m->Args[i].Length );
+ }
- Token t = cpp.GetToken (false);+
+ // Now run the macro expansion through the supplimentary preprocessor
+ Token xt = cpp2.Parse( t );
+ t = xt;
+ }
- if (t.Type != Token::TK_KEYWORD)
- {
- Error (iLine, "Expecting a macro name after #undef, got", &t);
- return false;
+ m->Value = t;+
+ m->Next = MacroList;Value = t;
+ m->Next = MacroList = m;;
+ return true;MacroList = m;
}return true;
- // Don't barf if macro does not exist - standard C behaviour-}
- Undef (t.String, t.Length);+ }
- do
+ bool CPreprocessor::HandleUnDef (Token &iBody, int iLine)
{
- t = cpp.GetToken (false);-{
- } while (t.Type == Token::TK_WHITESPACE ||+
-+ t.Type == bool CPreprocessor::HandleUnDef (Token::TK_COMMENT || &iBody, int iLine)
- t.Type == Token::TK_LINECOMMENT);+ {
+ CPreprocessor cpp (iBody, iLine);
- if (t.Type != Token t = cpp.GetToken::TK_EOS) (false);
- Error (iLine, "Warning: Ignoring garbage after directive", &t);@@ -944,10 +993,10 @@
+ Token t = cpp.GetToken (false);Error (iLine, "Warning: Ignoring garbage after directive", &t);
- return true;
-}
+ if (t.Type != Token::TK_KEYWORD)
+ {
+ Error (iLine, "Expecting a macro name after #undef, got", &t);
+ return false;
+ }
-bool CPreprocessor::HandleIfDef (Token &iBody, int iLine)
-{
- if (EnableOutput & (1 << 31))
- {
- Error (iLine, "Too many embedded #if directives");
- return false;
+ // Don't barf if macro does not exist - standard C behaviour
+ Undef (t.String, t.Length);
+
+ do
+ {
+ t = cpp.GetToken (false);
+ } while (t.Type == Token::TK_WHITESPACE ||
+ t.Type == Token::TK_COMMENT ||
+ t.Type == Token::TK_LINECOMMENT);
+
+ if (t.Type != Token::TK_EOS)
+ Error (iLine, "Warning: Ignoring garbage after directive", &t);
+
+ return true;
}
- CPreprocessor cpp (iBody, iLine);
+ bool CPreprocessor::HandleIfDef (Token &iBody, int iLine)
+ {
+ if (EnableOutput & (1 << 31))
+ {
+ Error (iLine, "Too many embedded #if directives");
+ return false;
+ }
- Token t = cpp.GetToken (false);
+ CPreprocessor cpp (iBody, iLine);
- if (t.Type != Token::TK_KEYWORD)
- {
- Error (iLine, "Expecting a macro name after #ifdef, got", &t);
- return false;
+ Token t = cpp.GetToken (false);
+
+ if (t.Type != Token::TK_KEYWORD)
+ {
+ Error (iLine, "Expecting a macro name after #ifdef, got", &t);
+ return false;
+ }
+
+ EnableOutput <<= 1;
+ if (IsDefined (t))
+ EnableOutput |= 1;
+
+ do
+ {
+ t = cpp.GetToken (false);
+ } while (t.Type == Token::TK_WHITESPACE ||
+ t.Type == Token::TK_COMMENT ||
+ t.Type == Token::TK_LINECOMMENT);
+
+ if (t.Type != Token::TK_EOS)
+ Error (iLine, "Warning: Ignoring garbage after directive", &t);
+
+ return true;
}
- EnableOutput <<= 1;
- if (IsDefined (t))
- EnableOutput |= 1;
- do
+ CPreprocessor::Token CPreprocessor::ExpandDefined (CPreprocessor *iParent, int iNumArgs, Token *iArgsif (EnableOutput & (1 << 31))
{
- t = cpp.GetToken (false);
- } while (t.Type == Token::TK_WHITESPACE ||
- t.Type == Token::TK_COMMENT ||
- t.Type == Token::TK_LINECOMMENT);
+ if (iNumArgs != 1)
+ {
+ iParent->Error (iParent-> Error (iLine, "The defined() function takes exactly one argument");"Too many embedded #if directives");
+ return Token (Token::TK_ERROR);@@ -979,10 +1028,11 @@
+ } Error (iLine, "Warning: Ignoring garbage after directive", &t);
- if (t.Type != Token::TK_EOS)
- Error (iLine, "Warning: Ignoring garbage after directive", &t);
+ const char *v = iParent->IsDefined (iArgs [0]) ? "1" : "0" return true;
+ return Token (Token::TK_NUMBER, v, 1);-}
+ }
- return true;
-}
-CPreprocessor::Token CPreprocessor::ExpandDefined (CPreprocessor *iParent, int iNumArgs, Token *iArgs)
-{
- if (iNumArgs != 1)
+ bool CPreprocessor::HandleIf (Token &iBody, int iLine)
{
- iParent->Error (iParent->Line, "The defined() function takes exactly one argument");+
-+ return Token (Token::TK_ERROR);CPreprocessor::Token CPreprocessor::ExpandDefined (CPreprocessor *iParent, int iNumArgs, Token *iArgs)
-+ }{
+ Macro defined (Token (Token::TK_KEYWORD, "defined", 7)); if (iNumArgs != 1)
+ defined.Next = MacroList; {
+ defined.ExpandFunc = ExpandDefined;iParent->Error (iParent->Line, "The defined() function takes exactly one argument");
+ defined.NumArgs = 1;@@ -991,10 +1041,11 @@
- const char *v = iParent->IsDefined (iArgs [0]) ? "1" : "0";
- return Token (Token::TK_NUMBER, v, 1);
-}
+ // Temporary add the defined() function to the macro list
+ MacroList = &defined;}
-bool CPreprocessor::HandleIf (Token &iBody, int iLine)
-{
- Macro defined (Token (Token::TK_KEYWORD, "defined", 7));
- defined.Next = MacroList;
- defined.ExpandFunc = ExpandDefined;
- defined.NumArgs = 1;
+ long val;
+ bool rc = GetValue (iBody, val, iLine);
- // Temporary add the defined() function to the macro list
- MacroList = &defined;
+ // Restore the macro list
+ MacroList = defined.Next;
+ defined.Next = NULL;
- long val;
- bool rc = GetValue (iBody, val, iLine);
+ if (!rc)+
+ return false;bool CPreprocessor::HandleIf (Token &iBody, int iLine)
- // Restore the macro list+ {
- MacroList = defined (Token (Token::TK_KEYWORD, "defined.Next;", 7));
- defined.Next = NULL;MacroList;
+ EnableOutput <<= 1;defined.ExpandFunc = ExpandDefined;
+ if (val)@@ -1018,12 +1069,48 @@
+ EnableOutput |= 1;
- if (!rc)
- return false;true;
+ return true;-}
+ }
- EnableOutput <<= 1;
- if (val)
- EnableOutput |= 1;
- return true;
-}
-
-bool CPreprocessor::HandleElse (Token &iBody, int iLine)
-{
- if (EnableOutput == 1)+
+ bool CPreprocessor::HandleElif (Token &iBody, int iLine)
+ {
- Error (iLine, "#else without #if");
- return false;
- }
+ if (EnableOutput == 1)
+ {
+ Error (iLine, "#elif without #if");
+ return false;
+ }
- // Negate the result of last #if
- EnableOutput ^= 1;+
+ Macro defined (Token (Token::TK_KEYWORD, "defined", 7));
+ defined.Next = MacroList;
+ defined.ExpandFunc = ExpandDefined;
+ defined.NumArgs = 1;
- if (iBody.Length)
- Error (iLine, "Warning: Ignoring garbage after #else", &iBody);+
+ // Temporary add the defined() function to the macro list
+ MacroList = &defined;
- return true;
-}+
+ long val;
+ bool rc = GetValue (iBody, val, iLine);
-bool CPreprocessor::HandleEndIf (Token &iBody, int iLine)
-{
- EnableOutput >>= 1;
- if (EnableOutput == 0)
- {
- Error (iLine, "#endif without #if");
- return false;+
+ // Restore the macro list
+ MacroList = defined.Next;
+ defined.Next = NULL;
+
+ if (!rc)
+ return false;
+
+ if (val)
+ EnableOutput |= 1;
+ else
+ EnableOutput &= ~1;
+
+ return true;
+ }
- if (iBody.Length)
- Error (iLine, "Warning: Ignoring garbage after #endif", &iBody);
- return true;+
-}+
+ bool CPreprocessor::HandleElse (Token &iBody, int iLine)
+ {
+ if (EnableOutput == 1)
+ {
+ Error (iLine, "#else without #if");
+ return false;
+ }
-CPreprocessor::Token CPreprocessor::HandleDirective (Token &iToken Error (iLine, int iLine)
-{
- // Analyze preprocessor directive
- const char *directive = iToken.String + 1;
- int dirlen = iToken.Length - 1"#else without #if");
- while (dirlen && isspace (*directive)) return false;
- dirlen--, directive++; }
+ // Negate the result of last #if@@ -1035,10 +1122,11 @@
+ EnableOutput ^= 1;rror (iLine, "Warning: Ignoring garbage after #else", &iBody);
- int old_line = Line; return true;
+ if (iBody.Length)-}
+ Error (iLine, "Warning: Ignoring garbage after #else", &iBody);}
- // Collect the remaining part of the directive until EOL
- -bool CPreprocessor::HandleEndIf (Token t&iBody, last;
- do
+ return true;
+ }int iLine)
+-{
+
+ bool CPreprocessor::HandleEndIf (Token &iBody, int iLine)
+ {
- t = GetToken (false);
- if (t.Type == Token::TK_NEWLINE)
+ EnableOutput >>= 1;
+ if (EnableOutput == 0)
{
- // No directive arguments
- last = t;
- t.Length = 0;
- goto Done;
+ Error (iLine, "#endif without #if");
+ return false;@@ -1050,13 +1138,14 @@
}
- } while (t.Type == Token::TK_WHITESPACE ||
- t.Type == Token::TK_LINECONT ||
- t.Type == Token::TK_COMMENT ||
- t.Type == Token::TK_LINECOMMENTError (iLine, "Warning: Ignoring garbage after #endif", &iBody);
- for (;;)
+ if (iBody.Length)
+ Error (iLine, "Warning: Ignoring garbage after #endif", &iBody);
+return true;
+ return true;-}
+ }
+
-CPreprocessor::Token CPreprocessor::HandleDirective (Token &iToken, int iLine)
-{
+
+ CPreprocessor::Token CPreprocessor::HandleDirective (Token &iToken, int iLine)
+ {
- last = GetToken (false);
- switch (last.Type)// Analyze preprocessor directive
+ // Analyze preprocessor directiveconst char *directive = iToken.String + 1;
+ - const char *directiveint dirlen = iToken.String + 1;Length - 1;
+ size_t dirlen = iToken.Length - 1;
+ while (dirlen && isspace (*directive))
+ dirlen--, directive++;
+
+ int old_line = Line;
+
+ // Collect the remaining part of the directive until EOL
+ Token t, last;
+ do
{
+ t = GetToken (false);
+ if (t.Type == Token::TK_NEWLINE)
+ {
+ // No directive arguments
+ last = t;
+ t.Length = 0;
+ goto Done;
+ }
+ } while (t.Type == Token::TK_WHITESPACE ||
+ t.Type == Token::TK_LINECONT ||
+ t.Type == Token::TK_COMMENT ||
+ t.Type == Token::TK_LINECOMMENT);
+
+ for (;;)
+ {
+ last = GetToken (false);
+ switch (last.Type)
+ {
case Token::TK_EOS:
// Can happen and is not an error
goto Done;
@@ -1104,134 +1193,127 @@dirlen--, directive++;
default:@@ -1109,15 +1198,17 @@
break;
+ }
+
+ t.Append (last);
+ t.Type = Token::TK_TEXT;
t.Type = Token::TK_TEXT;
}
-Done:
+ Done:
- t.Append (last);
- t.Type = Token::TK_TEXT;
- }
-Done: #define IS_DIRECTIVE(s) \
+#define IS_DIRECTIVE- ((dirlen == sizeof (s) \- 1) && (strncmp (directive, s, sizeof (s) - 1) == 0))
+ (dirlen == strlen(s) && (strncmp (directive, s, dirlen) == 0))
-#define IS_DIRECTIVE(s) \
- ((dirlen == sizeof (s) - 1) && (strncmp (directive, s, sizeof (s) - 1) == 0))
+ bool outputEnabled = ((EnableOutput & (EnableOutput + 1)) == 0);
+ bool rc;
- bool rc;
- if (IS_DIRECTIVE ("define"))
- rc = HandleDefine (t, iLine);
- else if (IS_DIRECTIVE ("undef"))
- rc = HandleUnDef (t, iLine);
- else if (IS_DIRECTIVE ("ifdef"))
- rc = HandleIfDef (t, iLine);
- else if (IS_DIRECTIVE ("ifndef"))
- {
- rc = HandleIfDef (t, iLine);
- if (rc)
- EnableOutput ^= 1;
- }
- else if (IS_DIRECTIVE ("if"))
- rc = HandleIf (t, iLine);
- else if (IS_DIRECTIVE ("else"))+
-+ rc = HandleElse (t, iLine);if (IS_DIRECTIVE ("define") && outputEnabled)
- else if (IS_DIRECTIVE ("endif")) rc = HandleDefine (t, iLine);
- rc = HandleEndIf (t, iLine);else if (IS_DIRECTIVE ("undef"))
+ else if (IS_DIRECTIVE ("defineundef") && outputEnabled)
+ rc = HandleDefine (tUnDef (t, iLine);
+ else if (IS_DIRECTIVE ("unifdef") && outputEnabled)
+ rc = HandleUnIfDef (t, iLine);
+ else if (IS_DIRECTIVE ("ifdef"))@@ -1129,18 +1220,15 @@
+ rc = HandleIfDef (t, iLine); }
+ else if (IS_DIRECTIVE ("ifndef"f"))
+ {rc = HandleIf (t, iLine);
+ rc = HandleIfDef (t, iLine);else if (IS_DIRECTIVE ("elif"))
+ rc = HandleElif (rc)t, iLine);
+ EnableOutput ^= 1;
+ }else if (IS_DIRECTIVE ("else"))
+ else if (IS_DIRECTIVE ("if")) rc = HandleElse (t, iLine);
+ rc = HandleIf (t, iLine);else if (IS_DIRECTIVE ("endif"))
+ else if (IS_DIRECTIVE ("elif")) rc = HandleEndIf (t, iLine);
+ rc = HandleElif (t, iLine);-
- else
- {
- // elif is tricky to support because the EnableOutput stack doesn't
- // contain enough data to tell whether any previous branch matched
- if (IS_DIRECTIVE ("elif"))
- Error (iLine, "Unsupported preprocessor directive #elif");
+ else if (IS_DIRECTIVE ("else"))
+ rc = HandleElse (t, iLine);
+ else if (IS_DIRECTIVE ("endif"))
+ rc = HandleEndIf (t, iLine);
+ else
+ {-
+ //Error (iLine, "Unknown preprocessor directive", &iToken);
+ //return Token (Token::TK_ERROR);
- //Error (iLine, "Unknown preprocessor directive", &iToken);@@ -1156,38 +1244,31 @@
- //return Token (Token::TK_ERROR);
+ // Unknown preprocessor directive, roll back and pass through
+ Line = old_line; if (!rc)
+ Source = ireturn Token.String + in (Token.Length;::TK_ERROR);
+ iToken.Type = Token::TK_TEXT;return last;
+ return iToken;-}
+ }
- // Unknown preprocessor directive-void CPreprocessor::Define (const char *iMacroName, roll back and pass through
- Line = old_line;
- Source = iToken.String + iToken.Length;
- iToken.Type = Token::TK_TEXT;
- return iToken;
+#undef IS_DIRECTIVEsize_t iMacroNameLen,
+
+ if (!rc)void CPreprocessor::Define (const char *iMacroName, size_t iMacroNameLen,
+ return Token (Token::TK_ERROR); const char *iMacroValue, size_t iMacroValueLen)
+ return last;-{
+ }{
-#undef IS_DIRECTIVE Macro *m = new Macro (Token (Token::TK_KEYWORD, iMacroName, iMacroNameLen));
- if (!rc) m->Value = Token (Token::TK_TEXT, iMacroValue, iMacroValueLen);
- return Token (Token::TK_ERROR);m->Next = MacroList;
- return last;MacroList = m;
-}
+ void CPreprocessor::Define (const char *iMacroName, size_t iMacroNameLen,
+ const char *iMacroValue, size_t iMacroValueLen)
+ {
+ Macro *m = new Macro (Token (Token::TK_KEYWORD, iMacroName, iMacroNameLen));
+ m->Value = Token (Token::TK_TEXT, iMacroValue, iMacroValueLen);
+ m->Next = MacroList;
+ MacroList = m;
+ }
-void CPreprocessor::Define (const char *iMacroName, size_t iMacroNameLen,
- const char *iMacroValue, size_t iMacroValueLen)
-{
- Macro *m = new Macro (Token (Token::TK_KEYWORD, iMacroName, iMacroNameLen));
- m->Value = Token (Token::TK_TEXT, iMacroValue, iMacroValueLen);
- m->Next = MacroList;
- MacroList = m;
-}+
-+ void CPreprocessor::Define (const char *iMacroName, size_t iMacroNameLen,
- long iMacroValue)
-{
- Macro *m = new Macro (Token (Token::TK_KEYWORD, iMacroName, iMacroNameLen));
- m->Value.SetValue (iMacroValue);
- m->Next = MacroList;
- MacroList = m;
-}
+ void CPreprocessor::Define (const char *iMacroName, size_t iMacroNameLen,
+ long iMacroValue)
+ {
+ Macro *m = new Macro (Token (Token::TK_KEYWORD, iMacroName, iMacroNameLen));
m->Value.SetValue (iMacroValue);
+ m->Value.SetValue (iNext = MacroValue);List;
+ m->Next = MacroList; = m;
+ MacroList = m;-}
+ }
-void CPreprocessor::Define (const char *iMacroName, const char *iMacroValue)
-{
- Define (iMacroName, strlen(iMacroName), iMacroValue, strlen(iMacroValue));
-}
-void CPreprocessor::Define (const char *iMacroName, long iMacroValue)
-{
- Define (iMacroName, strlen(iMacroName), iMacroValue);
-}
-
-bool CPreprocessor::Undef (const char *iMacroName, size_t iMacroNameLen)
-{
- Macro **cur = &MacroList;
- Token name (Token::TK_KEYWORD, iMacroName, iMacroNameLen);
- while (*cur)
+ bool CPreprocessor::Undef (const char *iMacroName, size_t iMacroNameLen)
{
- if ((*cur)->Name == name)
+ Macro **cur = &MacroList;
+ Token name (Token::TK_KEYWORD, iMacroName, iMacroNameLen);
+ while (*cur)
{
- Macro *next = (*cur)->Next;
- (*cur)->Next = NULL;
- delete (*cur);
- *cur = next;
- return true;
+ if ((*cur)->Name == name)
+ {
+ Macro *next = (*cur)->Next;
+ (*cur)->Next = NULL;
+ delete (*cur);
+ *cur = next;
+ return true;
+ }
++ {
+ Macro **cur = &(*cur)->Next;MacroList;
}Token name (Token::TK_KEYWORD, iMacroName, iMacroNameLen);
- cur = &while (*cur)->Next;
+ return false;@@ -1205,10 +1286,11 @@
}
- return false;
-}
+ }
-CPreprocessor::Token CPreprocessor::Parse (const Token &iSource)
-{
- Source = iSource.String;
- SourceEnd = Source + iSource.Length;
- Line = 1;
- BOL = true;
- EnableOutput = 1;+
+ CPreprocessor::Token CPreprocessor::Parse (const Token &iSource)
+ {
+ Source = iSource.String;
+ SourceEnd = Source + iSource.Length;
+ Line = 1;
+ BOL = true;
+ EnableOutput = 1;
- // Accumulate output into this token
- Token output (Token::TK_TEXT);
- int empty_lines = 0;
+ // Accumulate output into this token
+ Token output (Token::TK_TEXT);
+ int empty_lines = 0;
- // Enable output only if all embedded #if's were true
- bool old_output_enabled = true;
- bool output_enabled = true;
- int output_disabled_line = 0;
+ // Enable output only if all embedded #if's were true
+ bool old_output_enabled = true;
+ bool output_enabled = true;
+ int output_disabled_line = 0;
- while (Source < SourceEnd)
- {
- int old_line = Line;
- Token t = GetToken (true);
+ while (Source < SourceEnd)
+ {
+ int old_line = Line;
+ Token t = GetToken (true);
- NextToken:
- switch (t.Type)
- {
+ NextToken:
+ switch (t.Type)
+ { Source = iSource.String;
case Token::TK_ERROR:SourceEnd = Source + iSource.Length;
return t;Line = 1;
@@ -1283,8 +1365,7 @@
output.AppendNL (empty_lines);
empty_lines = 0;
}
- // Fallthrough to default
- FALLTHROUGH;
+ OGRE_FALLTHROUGH; // to default
case Token::TK_WHITESPACE:
// Fallthrough to default
default:
@@ -1292,25 +1373,28 @@
if (output_enabled)
output.Append (t);
break;
+ }
}
+
+ if (EnableOutput != 1)
+ {
+ Error (Line, "Unclosed #if at end of source");
+ return Token (Token::TK_ERROR);
+ }
+
+ return output;302,10 +1383,11 @@
}
- if (EnableOutput != 1)
+
+ char *CPreprocessor::Parse (const char *iSource, size_t iLength, size_t &oLength)
{
- Error (Line, "Unclosed #if at end of source");return output;
- return Token (Token::TK_ERROR);-}
+ Token retval = Parse (Token (Token::TK_TEXT, iSource, iLength));}
+ if (retval.Type == Token::TK_ERROR
-char *CPreprocessor::Parse (const char *iSource, size_t iLength, size_t &oLength)
+ return NULL;-{
+
+ oLength = retval.Length;
+ retval.Allocated = 0;char *CPreprocessor::Parse (const char *iSource, size_t iLength, size_t &oLength)
+ return retval.Buffer;{
}
- return output;Token retval = Parse (Token (Token::TK_TEXT, iSource, iLength));
-}
-
-char *CPreprocessor::Parse (const char *iSource, size_t iLength, size_t &oLength)
-{ if (retval.Type == Token::TK_ERROR)
- Token retval = Parse (Token (Token::TK_TEXT, iSource, iLength)); return NULL;
- if (retval.Type == Token::TK_ERROR)@@ -1313,4 +1395,6 @@
- oLength = return NULL;val.Length;
- retval.Allocated = 0;
- oLength = return retval.Length;Buffer;
- retval.Allocated = 0;-}
- return retval.Buffer;+ }
-}+
+} // namespace Ogre
Index: Preprocessor.h
===================================================================
--- Preprocessor.h (revision 23013)
+++ Preprocessor.h (working copy)
@@ -1,16 +1,10 @@
-/*
- * This source file originally came from OGRE v1.7.2 - http://www.ogre3d.org/
- * with some tweaks as part of 0 A.D.
- * All changes are released under the original license, as follows:
- */
-
-/*
+/**
-----------------------------------------------------------------------------
This source file is part of OGRE
(Object-oriented Graphics Rendering Engine)
For the latest info, see http://www.ogre3d.org/
-Copyright (c) 2000-2009 Torus Knot Software Ltd
+Copyright (c) 2000-2011 Torus Knot Software Ltd
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
@@ -32,9 +26,14 @@
-----------------------------------------------------------------------------
*/
-#ifndef INCLUDED_CPREPROCESSOR
-#define INCLUDED_CPREPROCESSOR
+#ifndef __OGRE_CPREPROCESSOR_H__
+#define __OGRE_CPREPROCESSOR_H__
+#include <string.h>
+#include <stdlib.h>
+
+namespace Ogre {
+
/**
* This is a simplistic C/C++-like preprocessor.
* It takes an non-zero-terminated string on input and outputs a
@@ -46,18 +45,18 @@
*
* Here's a list of supported features:
* <ul>
- * <li>Fast memory allocation-less operation (mostly).</li>
- * <li>Line continuation (backslash-newline) is swallowed.</li>
+ * <li>Fast memory allocation-less operation (mostly).
+ * <li>Line continuation (backslash-newline) is swallowed.
* <li>Line numeration is fully preserved by inserting empty lines where
* required. This is crucial if, say, GLSL compiler reports you an error
- * with a line number.</li>
- * <li>\#define: Parametrized and non-parametrized macros. Invoking a macro with
- * less arguments than it takes assignes empty values to missing arguments.</li>
- * <li>\#undef: Forget defined macros</li>
- * <li>\#ifdef/\#ifndef/\#else/\#endif: Conditional suppression of parts of code.</li>
- * <li>\#if: Supports numeric expression of any complexity, also supports the
- * defined() pseudo-function.</li>
- * </ul>
+ * with a line number.
+ * <li>#define: Parametrized and non-parametrized macros. Invoking a macro with
+ * less arguments than it takes assignes empty values to missing arguments.
+ * <li>#undef: Forget defined macros
+ * <li>#ifdef/#ifndef/#else/#endif: Conditional suppression of parts of code.
+ * <li>#if: Supports numeric expression of any complexity, also supports the
+ * defined() pseudo-function.
+ * <ul>
*/
class CPreprocessor
{
@@ -108,10 +107,10 @@
/// Token length in bytes
size_t Length;
- Token () : Type (TK_ERROR), Allocated (0), String (NULL), Length (0)
+ Token () : Allocated (0), String (NULL)
{ }
- Token (Kind iType) : Type (iType), Allocated (0), String (NULL), Length (0)
+ Token (Kind iType) : Type (iType), Allocated (0), String (NULL)
{ }
Token (Kind iType, const char *iString, size_t iLength) :
@@ -243,7 +242,7 @@
Token HandleDirective (Token &iToken, int iLine);
/**
- * Handle a \#define directive.
+ * Handle a #define directive.
* @param iBody
* The body of the directive (everything after the directive
* until end of line).
@@ -267,7 +266,7 @@
bool HandleUnDef (Token &iBody, int iLine);
/**
- * Handle an \#ifdef directive.
+ * Handle an #ifdef directive.
* @param iBody
* The body of the directive (everything after the directive
* until end of line).
@@ -279,7 +278,7 @@
bool HandleIfDef (Token &iBody, int iLine);
/**
- * Handle an \#if directive.
+ * Handle an #if directive.
* @param iBody
* The body of the directive (everything after the directive
* until end of line).
@@ -291,7 +290,7 @@
bool HandleIf (Token &iBody, int iLine);
/**
- * Handle an \#else directive.
+ * Handle an #else directive.
* @param iBody
* The body of the directive (everything after the directive
* until end of line).
@@ -303,7 +302,7 @@
bool HandleElse (Token &iBody, int iLine);
/**
- * Handle an \#endif directive.
+ * Handle an #endif directive.
* @param iBody
* The body of the directive (everything after the directive
* until end of line).
@@ -456,24 +455,6 @@
void Define (const char *iMacroName, size_t iMacroNameLen, long iMacroValue);
/**
- * Define a macro without parameters.
- * @param iMacroName
- * The name of the defined macro
- * @param iMacroValue
- * The value of the defined macro
- */
- void Define (const char *iMacroName, const char *iMacroValue);
-
- /**
- * Define a numerical macro.
- * @param iMacroName
- * The name of the defined macro
- * @param iMacroValue
- * The value of the defined macro
- */
- void Define (const char *iMacroName, long iMacroValue);
-
- /**
* Undefine a macro.
* @param iMacroName
* The name of the macro to undefine
@@ -538,4 +519,6 @@
void *ErrorData;
};
-#endif // INCLUDED_CPREPROCESSOR
+} // namespace Ogre
+
+#endif // __OGRE_CPREPROCESSOR_H__