src/GLSL/include/OgreGLSLPreprocessor.h
Go to the documentation of this file.
1 
29 #ifndef __OGRE_CPREPROCESSOR_H__
30 #define __OGRE_CPREPROCESSOR_H__
31 
32 #include <string.h>
33 #include <stdlib.h>
34 
35 namespace Ogre {
36  namespace GLSL {
37 
63 {
77  class Token
78  {
79  public:
80  enum Kind
81  {
82  TK_EOS, // End of input stream
83  TK_ERROR, // An error has been encountered
84  TK_WHITESPACE, // A whitespace span (but not newline)
85  TK_NEWLINE, // A single newline (CR & LF)
86  TK_LINECONT, // Line continuation ('\' followed by LF)
87  TK_NUMBER, // A number
88  TK_KEYWORD, // A keyword
89  TK_PUNCTUATION, // A punctuation character
90  TK_DIRECTIVE, // A preprocessor directive
91  TK_STRING, // A string
92  TK_COMMENT, // A block comment
93  TK_LINECOMMENT, // A line comment
94  TK_TEXT // An unparsed text (cannot be returned from GetToken())
95  };
96 
100  mutable size_t Allocated;
101  union
102  {
104  const char *String;
106  char *Buffer;
107  };
109  size_t Length;
110 
111  Token () : Allocated (0), String (NULL)
112  { }
113 
114  Token (Kind iType) : Type (iType), Allocated (0), String (NULL)
115  { }
116 
117  Token (Kind iType, const char *iString, size_t iLength) :
118  Type (iType), Allocated (0), String (iString), Length (iLength)
119  { }
120 
121  Token (const Token &iOther)
122  {
123  Type = iOther.Type;
124  Allocated = iOther.Allocated;
125  iOther.Allocated = 0; // !!! not quite correct but effective
126  String = iOther.String;
127  Length = iOther.Length;
128  }
129 
131  { if (Allocated) free (Buffer); }
132 
134  Token &operator = (const Token &iOther)
135  {
136  if (Allocated) free (Buffer);
137  Type = iOther.Type;
138  Allocated = iOther.Allocated;
139  iOther.Allocated = 0; // !!! not quite correct but effective
140  String = iOther.String;
141  Length = iOther.Length;
142  return *this;
143  }
144 
146  void Append (const char *iString, size_t iLength);
147 
149  void Append (const Token &iOther);
150 
152  void AppendNL (int iCount);
153 
155  int CountNL ();
156 
158  bool GetValue (long &oValue) const;
159 
161  void SetValue (long iValue);
162 
164  bool operator == (const Token &iOther)
165  {
166  if (iOther.Length != Length)
167  return false;
168  return (memcmp (String, iOther.String, Length) == 0);
169  }
170  };
171 
173  class Macro
174  {
175  public:
179  int NumArgs;
189  Token (*ExpandFunc) (CPreprocessor *iParent, int iNumArgs, Token *iArgs);
191  bool Expanding;
192 
193  Macro (const Token &iName) :
194  Name (iName), NumArgs (0), Args (NULL), Next (NULL),
195  ExpandFunc (NULL), Expanding (false)
196  { }
197 
199  { delete [] Args; delete Next; }
200 
202  Token Expand (int iNumArgs, Token *iArgs, Macro *iMacros);
203  };
204 
205  friend class CPreprocessor::Macro;
206 
208  const char *Source;
210  const char *SourceEnd;
212  int Line;
214  bool BOL;
216  unsigned EnableOutput;
219 
223  CPreprocessor (const Token &iToken, int iLine);
224 
232  Token GetToken (bool iExpand);
233 
243  Token HandleDirective (Token &iToken, int iLine);
244 
255  bool HandleDefine (Token &iBody, int iLine);
256 
267  bool HandleUnDef (Token &iBody, int iLine);
268 
279  bool HandleIfDef (Token &iBody, int iLine);
280 
291  bool HandleIf (Token &iBody, int iLine);
292 
303  bool HandleElse (Token &iBody, int iLine);
304 
315  bool HandleEndIf (Token &iBody, int iLine);
316 
327  Token GetArgument (Token &oArg, bool iExpand);
328 
339  Token GetArguments (int &oNumArgs, Token *&oArgs, bool iExpand);
340 
354  Token GetExpression (Token &oResult, int iLine, int iOpPriority = 0);
355 
371  bool GetValue (const Token &iToken, long &oValue, int iLine);
372 
381  Token ExpandMacro (const Token &iToken);
382 
390  Macro *IsDefined (const Token &iToken);
391 
403  static Token ExpandDefined (CPreprocessor *iParent, int iNumArgs, Token *iArgs);
404 
412  Token Parse (const Token &iSource);
413 
423  void Error (int iLine, const char *iError, const Token *iToken = NULL);
424 
425 public:
428  { }
429 
431  virtual ~CPreprocessor ();
432 
444  void Define (const char *iMacroName, size_t iMacroNameLen,
445  const char *iMacroValue, size_t iMacroValueLen);
446 
456  void Define (const char *iMacroName, size_t iMacroNameLen, long iMacroValue);
457 
467  bool Undef (const char *iMacroName, size_t iMacroNameLen);
468 
491  char *Parse (const char *iSource, size_t iLength, size_t &oLength);
492 
508  typedef void (*ErrorHandlerFunc) (
509  void *iData, int iLine, const char *iError,
510  const char *iToken, size_t iTokenLen);
511 
518 
520  void *ErrorData;
521 };
522 
523 } // namespace GLSL
524 } // namespace Ogre
525 
526 #endif // __OGRE_CPREPROCESSOR_H__
Ogre::GLSL::CPreprocessor::Token::TK_EOS
@ TK_EOS
Definition: src/GLSL/include/OgreGLSLPreprocessor.h:82
Ogre::GLSL::CPreprocessor::Token::TK_PUNCTUATION
@ TK_PUNCTUATION
Definition: src/GLSL/include/OgreGLSLPreprocessor.h:89
Ogre::GLSL::CPreprocessor::Error
void Error(int iLine, const char *iError, const Token *iToken=NULL)
Call the error handler.
Ogre::GLSL::CPreprocessor::CPreprocessor
CPreprocessor()
Create an empty preprocessor object.
Definition: src/GLSL/include/OgreGLSLPreprocessor.h:427
Ogre
Definition: OgreAndroidLogListener.h:34
Ogre::GLSL::CPreprocessor
This is a simplistic C/C++-like preprocessor.
Definition: src/GLSL/include/OgreGLSLPreprocessor.h:62
Ogre::GLSL::CPreprocessor::Token::TK_COMMENT
@ TK_COMMENT
Definition: src/GLSL/include/OgreGLSLPreprocessor.h:92
Ogre::GLSL::CPreprocessor::GetArgument
Token GetArgument(Token &oArg, bool iExpand)
Get a single function argument until next ',' or ')'.
Ogre::GLSL::CPreprocessor::Token::Length
size_t Length
Token length in bytes.
Definition: src/GLSL/include/OgreGLSLPreprocessor.h:109
Ogre::GLSL::CPreprocessor::Token::CountNL
int CountNL()
Count number of newlines in this token.
Ogre::GLSL::CPreprocessor::IsDefined
Macro * IsDefined(const Token &iToken)
Check if a macro is defined, and if so, return it.
Ogre::GLSL::CPreprocessor::Token::TK_WHITESPACE
@ TK_WHITESPACE
Definition: src/GLSL/include/OgreGLSLPreprocessor.h:84
Ogre::GLSL::CPreprocessor::Token::TK_LINECONT
@ TK_LINECONT
Definition: src/GLSL/include/OgreGLSLPreprocessor.h:86
Ogre::GLSL::CPreprocessor::Token::AppendNL
void AppendNL(int iCount)
Append given number of newlines to this token.
Ogre::GLSL::CPreprocessor::Token::Allocated
size_t Allocated
True if string was allocated (and must be freed)
Definition: src/GLSL/include/OgreGLSLPreprocessor.h:100
Ogre::GLSL::CPreprocessor::Token::TK_LINECOMMENT
@ TK_LINECOMMENT
Definition: src/GLSL/include/OgreGLSLPreprocessor.h:93
Ogre::GLSL::CPreprocessor::MacroList
Macro * MacroList
The list of macros defined so far.
Definition: src/GLSL/include/OgreGLSLPreprocessor.h:218
Ogre::GLSL::CPreprocessor::Define
void Define(const char *iMacroName, size_t iMacroNameLen, const char *iMacroValue, size_t iMacroValueLen)
Define a macro without parameters.
Ogre::GLSL::CPreprocessor::EnableOutput
unsigned EnableOutput
A stack of 32 booleans packed into one value :)
Definition: src/GLSL/include/OgreGLSLPreprocessor.h:216
Ogre::GLSL::CPreprocessor::Token::Append
void Append(const char *iString, size_t iLength)
Append a string to this token.
Ogre::GLSL::CPreprocessor::ExpandMacro
Token ExpandMacro(const Token &iToken)
Expand the given macro, if it exists.
Ogre::GLSL::CPreprocessor::HandleIfDef
bool HandleIfDef(Token &iBody, int iLine)
Handle an #ifdef directive.
Ogre::GLSL::CPreprocessor::Token::TK_ERROR
@ TK_ERROR
Definition: src/GLSL/include/OgreGLSLPreprocessor.h:83
Ogre::String
_StringBase String
Definition: OgrePrerequisites.h:439
Ogre::GLSL::CPreprocessor::Undef
bool Undef(const char *iMacroName, size_t iMacroNameLen)
Undefine a macro.
Ogre::GLSL::CPreprocessor::Token::Token
Token(Kind iType)
Definition: src/GLSL/include/OgreGLSLPreprocessor.h:114
Ogre::GLSL::CPreprocessor::ErrorHandler
static ErrorHandlerFunc ErrorHandler
A pointer to the preprocessor's error handler.
Definition: src/GLSL/include/OgreGLSLPreprocessor.h:517
Ogre::GLSL::CPreprocessor::Token::TK_NEWLINE
@ TK_NEWLINE
Definition: src/GLSL/include/OgreGLSLPreprocessor.h:85
Ogre::GLSL::CPreprocessor::HandleDefine
bool HandleDefine(Token &iBody, int iLine)
Handle a #define directive.
Ogre::GLSL::CPreprocessor::Macro
A macro definition.
Definition: src/GLSL/include/OgreGLSLPreprocessor.h:173
Ogre::GLSL::CPreprocessor::Macro::Name
Token Name
Macro name.
Definition: src/GLSL/include/OgreGLSLPreprocessor.h:177
Ogre::GLSL::CPreprocessor::Macro::Macro
Macro(const Token &iName)
Definition: src/GLSL/include/OgreGLSLPreprocessor.h:193
Ogre::GLSL::CPreprocessor::Token::TK_KEYWORD
@ TK_KEYWORD
Definition: src/GLSL/include/OgreGLSLPreprocessor.h:88
Ogre::GLSL::CPreprocessor::Token::Token
Token()
Definition: src/GLSL/include/OgreGLSLPreprocessor.h:111
Ogre::GLSL::CPreprocessor::Token::Type
Kind Type
Token type.
Definition: src/GLSL/include/OgreGLSLPreprocessor.h:98
Ogre::GLSL::CPreprocessor::~CPreprocessor
virtual ~CPreprocessor()
Destroy the preprocessor object.
Ogre::GLSL::CPreprocessor::Token::Token
Token(Kind iType, const char *iString, size_t iLength)
Definition: src/GLSL/include/OgreGLSLPreprocessor.h:117
Ogre::GLSL::CPreprocessor::GetValue
bool GetValue(const Token &iToken, long &oValue, int iLine)
Get the numeric value of a token.
Ogre::GLSL::CPreprocessor::Token::TK_NUMBER
@ TK_NUMBER
Definition: src/GLSL/include/OgreGLSLPreprocessor.h:87
Ogre::GLSL::CPreprocessor::BOL
bool BOL
True if we are at beginning of line.
Definition: src/GLSL/include/OgreGLSLPreprocessor.h:214
Ogre::GLSL::CPreprocessor::Token::operator=
Token & operator=(const Token &iOther)
Assignment operator.
Definition: src/GLSL/include/OgreGLSLPreprocessor.h:134
Ogre::GLSL::CPreprocessor::Source
const char * Source
The current source text input.
Definition: src/GLSL/include/OgreGLSLPreprocessor.h:208
Ogre::GLSL::CPreprocessor::Token::TK_DIRECTIVE
@ TK_DIRECTIVE
Definition: src/GLSL/include/OgreGLSLPreprocessor.h:90
Ogre::GLSL::CPreprocessor::GetToken
Token GetToken(bool iExpand)
Stateless tokenizer: Parse the input text and return the next token.
Ogre::GLSL::CPreprocessor::HandleDirective
Token HandleDirective(Token &iToken, int iLine)
Handle a preprocessor directive.
Ogre::GLSL::CPreprocessor::HandleElse
bool HandleElse(Token &iBody, int iLine)
Handle an #else directive.
Ogre::GLSL::CPreprocessor::ErrorData
void * ErrorData
User-specific storage, passed to Error()
Definition: src/GLSL/include/OgreGLSLPreprocessor.h:520
Ogre::GLSL::CPreprocessor::GetExpression
Token GetExpression(Token &oResult, int iLine, int iOpPriority=0)
Parse an expression, compute it and return the result.
Ogre::GLSL::CPreprocessor::HandleIf
bool HandleIf(Token &iBody, int iLine)
Handle an #if directive.
Ogre::GLSL::CPreprocessor::ErrorHandlerFunc
void(* ErrorHandlerFunc)(void *iData, int iLine, const char *iError, const char *iToken, size_t iTokenLen)
An error handler function type.
Definition: src/GLSL/include/OgreGLSLPreprocessor.h:508
Ogre::GLSL::CPreprocessor::Token::Kind
Kind
Definition: src/GLSL/include/OgreGLSLPreprocessor.h:80
Ogre::GLSL::CPreprocessor::Macro::ExpandFunc
Token(* ExpandFunc)(CPreprocessor *iParent, int iNumArgs, Token *iArgs)
A pointer to function implementation (if macro is really a func)
Definition: src/GLSL/include/OgreGLSLPreprocessor.h:189
Ogre::GLSL::CPreprocessor::Token::~Token
~Token()
Definition: src/GLSL/include/OgreGLSLPreprocessor.h:130
Ogre::GLSL::CPreprocessor::Macro::Value
Token Value
The macro value.
Definition: src/GLSL/include/OgreGLSLPreprocessor.h:183
Ogre::GLSL::CPreprocessor::Macro::Expanding
bool Expanding
true if macro expansion is in progress
Definition: src/GLSL/include/OgreGLSLPreprocessor.h:191
Ogre::GLSL::CPreprocessor::SourceEnd
const char * SourceEnd
The end of the source text.
Definition: src/GLSL/include/OgreGLSLPreprocessor.h:210
Ogre::GLSL::CPreprocessor::Macro::Args
Token * Args
The names of the arguments.
Definition: src/GLSL/include/OgreGLSLPreprocessor.h:181
Ogre::GLSL::CPreprocessor::Token
A input token.
Definition: src/GLSL/include/OgreGLSLPreprocessor.h:77
Ogre::GLSL::CPreprocessor::Parse
Token Parse(const Token &iSource)
Parse the input string and return a token containing the whole output.
Ogre::GLSL::CPreprocessor::Token::TK_TEXT
@ TK_TEXT
Definition: src/GLSL/include/OgreGLSLPreprocessor.h:94
Ogre::GLSL::CPreprocessor::ExpandDefined
static Token ExpandDefined(CPreprocessor *iParent, int iNumArgs, Token *iArgs)
The implementation of the defined() preprocessor function.
Ogre::GLSL::CPreprocessor::Line
int Line
Current line number.
Definition: src/GLSL/include/OgreGLSLPreprocessor.h:212
Ogre::GLSL::CPreprocessor::Macro::Next
Macro * Next
Next macro in chained list.
Definition: src/GLSL/include/OgreGLSLPreprocessor.h:187
Ogre::GLSL::CPreprocessor::Token::TK_STRING
@ TK_STRING
Definition: src/GLSL/include/OgreGLSLPreprocessor.h:91
Ogre::GLSL::CPreprocessor::Token::String
const char * String
A pointer somewhere into the input buffer.
Definition: src/GLSL/include/OgreGLSLPreprocessor.h:104
Ogre::GLSL::CPreprocessor::Macro::Expand
Token Expand(int iNumArgs, Token *iArgs, Macro *iMacros)
Expand the macro value (will not work for functions)
Ogre::GLSL::CPreprocessor::Token::Buffer
char * Buffer
A memory-allocated string.
Definition: src/GLSL/include/OgreGLSLPreprocessor.h:106
Ogre::GLSL::CPreprocessor::Macro::~Macro
~Macro()
Definition: src/GLSL/include/OgreGLSLPreprocessor.h:198
Ogre::GLSL::CPreprocessor::Token::SetValue
void SetValue(long iValue)
Set the numeric value of the token.
Ogre::GLSL::CPreprocessor::Macro::Body
Token Body
Unparsed macro body (keeps the whole raw unparsed macro body)
Definition: src/GLSL/include/OgreGLSLPreprocessor.h:185
Ogre::GLSL::CPreprocessor::Token::operator==
bool operator==(const Token &iOther)
Test two tokens for equality.
Definition: src/GLSL/include/OgreGLSLPreprocessor.h:164
Ogre::GLSL::CPreprocessor::HandleUnDef
bool HandleUnDef(Token &iBody, int iLine)
Undefine a previously defined macro.
Ogre::GLSL::CPreprocessor::Token::Token
Token(const Token &iOther)
Definition: src/GLSL/include/OgreGLSLPreprocessor.h:121
Ogre::GLSL::CPreprocessor::GetArguments
Token GetArguments(int &oNumArgs, Token *&oArgs, bool iExpand)
Get all the arguments of a macro: '(' arg1 { ',' arg2 { ',' ...
Ogre::GLSL::CPreprocessor::Macro::NumArgs
int NumArgs
Number of arguments.
Definition: src/GLSL/include/OgreGLSLPreprocessor.h:179
Ogre::GLSL::CPreprocessor::HandleEndIf
bool HandleEndIf(Token &iBody, int iLine)
Handle an #endif directive.
Ogre::GLSL::CPreprocessor::Token::GetValue
bool GetValue(long &oValue) const
Get the numeric value of the token.

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 Tue Apr 13 2021 08:53:15