Plus/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 
62 {
76  class Token
77  {
78  public:
79  enum Kind
80  {
81  TK_EOS, // End of input stream
82  TK_ERROR, // An error has been encountered
83  TK_WHITESPACE, // A whitespace span (but not newline)
84  TK_NEWLINE, // A single newline (CR & LF)
85  TK_LINECONT, // Line continuation ('\' followed by LF)
86  TK_NUMBER, // A number
87  TK_KEYWORD, // A keyword
88  TK_PUNCTUATION, // A punctuation character
89  TK_DIRECTIVE, // A preprocessor directive
90  TK_STRING, // A string
91  TK_COMMENT, // A block comment
92  TK_LINECOMMENT, // A line comment
93  TK_TEXT // An unparsed text (cannot be returned from GetToken())
94  };
95 
99  mutable size_t Allocated;
100  union
101  {
103  const char *String;
105  char *Buffer;
106  };
108  size_t Length;
109 
110  Token () : Allocated (0), String (NULL)
111  { }
112 
113  Token (Kind iType) : Type (iType), Allocated (0), String (NULL)
114  { }
115 
116  Token (Kind iType, const char *iString, size_t iLength) :
117  Type (iType), Allocated (0), String (iString), Length (iLength)
118  { }
119 
120  Token (const Token &iOther)
121  {
122  Type = iOther.Type;
123  Allocated = iOther.Allocated;
124  iOther.Allocated = 0; // !!! not quite correct but effective
125  String = iOther.String;
126  Length = iOther.Length;
127  }
128 
130  { if (Allocated) free (Buffer); }
131 
133  Token &operator = (const Token &iOther)
134  {
135  if (Allocated) free (Buffer);
136  Type = iOther.Type;
137  Allocated = iOther.Allocated;
138  iOther.Allocated = 0; // !!! not quite correct but effective
139  String = iOther.String;
140  Length = iOther.Length;
141  return *this;
142  }
143 
145  void Append (const char *iString, size_t iLength);
146 
148  void Append (const Token &iOther);
149 
151  void AppendNL (int iCount);
152 
154  int CountNL ();
155 
157  bool GetValue (long &oValue) const;
158 
160  void SetValue (long iValue);
161 
163  bool operator == (const Token &iOther)
164  {
165  if (iOther.Length != Length)
166  return false;
167  return (memcmp (String, iOther.String, Length) == 0);
168  }
169  };
170 
172  class Macro
173  {
174  public:
178  int NumArgs;
188  Token (*ExpandFunc) (CPreprocessor *iParent, int iNumArgs, Token *iArgs);
190  bool Expanding;
191 
192  Macro (const Token &iName) :
193  Name (iName), NumArgs (0), Args (NULL), Next (NULL),
194  ExpandFunc (NULL), Expanding (false)
195  { }
196 
198  { delete [] Args; delete Next; }
199 
201  Token Expand (int iNumArgs, Token *iArgs, Macro *iMacros);
202  };
203 
204  friend class CPreprocessor::Macro;
205 
207  const char *Source;
209  const char *SourceEnd;
211  int Line;
213  bool BOL;
215  unsigned EnableOutput;
218 
222  CPreprocessor (const Token &iToken, int iLine);
223 
231  Token GetToken (bool iExpand);
232 
242  Token HandleDirective (Token &iToken, int iLine);
243 
254  bool HandleDefine (Token &iBody, int iLine);
255 
266  bool HandleUnDef (Token &iBody, int iLine);
267 
278  bool HandleIfDef (Token &iBody, int iLine);
279 
290  bool HandleIf (Token &iBody, int iLine);
291 
302  bool HandleElse (Token &iBody, int iLine);
303 
314  bool HandleEndIf (Token &iBody, int iLine);
315 
326  Token GetArgument (Token &oArg, bool iExpand);
327 
338  Token GetArguments (int &oNumArgs, Token *&oArgs, bool iExpand);
339 
353  Token GetExpression (Token &oResult, int iLine, int iOpPriority = 0);
354 
370  bool GetValue (const Token &iToken, long &oValue, int iLine);
371 
380  Token ExpandMacro (const Token &iToken);
381 
389  Macro *IsDefined (const Token &iToken);
390 
402  static Token ExpandDefined (CPreprocessor *iParent, int iNumArgs, Token *iArgs);
403 
411  Token Parse (const Token &iSource);
412 
422  void Error (int iLine, const char *iError, const Token *iToken = NULL);
423 
424 public:
427  { }
428 
430  virtual ~CPreprocessor ();
431 
443  void Define (const char *iMacroName, size_t iMacroNameLen,
444  const char *iMacroValue, size_t iMacroValueLen);
445 
455  void Define (const char *iMacroName, size_t iMacroNameLen, long iMacroValue);
456 
466  bool Undef (const char *iMacroName, size_t iMacroNameLen);
467 
490  char *Parse (const char *iSource, size_t iLength, size_t &oLength);
491 
507  typedef void (*ErrorHandlerFunc) (
508  void *iData, int iLine, const char *iError,
509  const char *iToken, size_t iTokenLen);
510 
517 
519  void *ErrorData;
520 };
521 
522 } // namespace Ogre
523 
524 #endif // __OGRE_CPREPROCESSOR_H__
void Error(int iLine, const char *iError, const Token *iToken=NULL)
Call the error handler.
This is a simplistic C/C++-like preprocessor.
bool HandleIfDef(Token &iBody, int iLine)
Handle an #ifdef directive.
Token Parse(const Token &iSource)
Parse the input string and return a token containing the whole output.
static Token ExpandDefined(CPreprocessor *iParent, int iNumArgs, Token *iArgs)
The implementation of the defined() preprocessor function.
void SetValue(long iValue)
Set the numeric value of the token.
const char * Source
The current source text input.
Token HandleDirective(Token &iToken, int iLine)
Handle a preprocessor directive.
Token Body
Unparsed macro body (keeps the whole raw unparsed macro body)
Token GetArguments(int &oNumArgs, Token *&oArgs, bool iExpand)
Get all the arguments of a macro: '(' arg1 { ',' arg2 { ',' ...
Token(* ExpandFunc)(CPreprocessor *iParent, int iNumArgs, Token *iArgs)
A pointer to function implementation (if macro is really a func)
bool Undef(const char *iMacroName, size_t iMacroNameLen)
Undefine a macro.
bool GetValue(long &oValue) const
Get the numeric value of the token.
bool BOL
True if we are at beginning of line.
void AppendNL(int iCount)
Append given number of newlines to this token.
static ErrorHandlerFunc ErrorHandler
A pointer to the preprocessor's error handler.
void * ErrorData
User-specific storage, passed to Error()
Token GetExpression(Token &oResult, int iLine, int iOpPriority=0)
Parse an expression, compute it and return the result.
Macro * MacroList
The list of macros defined so far.
bool GetValue(const Token &iToken, long &oValue, int iLine)
Get the numeric value of a token.
const char * String
A pointer somewhere into the input buffer.
_StringBase String
CPreprocessor()
Create an empty preprocessor object.
bool HandleUnDef(Token &iBody, int iLine)
Undefine a previously defined macro.
int CountNL()
Count number of newlines in this token.
void Append(const char *iString, size_t iLength)
Append a string to this token.
bool Expanding
true if macro expansion is in progress
bool HandleElse(Token &iBody, int iLine)
Handle an #else directive.
Token Expand(int iNumArgs, Token *iArgs, Macro *iMacros)
Expand the macro value (will not work for functions)
size_t Allocated
True if string was allocated (and must be freed)
const char * SourceEnd
The end of the source text.
Token GetToken(bool iExpand)
Stateless tokenizer: Parse the input text and return the next token.
Token & operator=(const Token &iOther)
Assignment operator.
bool operator==(const Token &iOther)
Test two tokens for equality.
Token ExpandMacro(const Token &iToken)
Expand the given macro, if it exists.
void(* ErrorHandlerFunc)(void *iData, int iLine, const char *iError, const char *iToken, size_t iTokenLen)
An error handler function type.
virtual ~CPreprocessor()
Destroy the preprocessor object.
Macro * IsDefined(const Token &iToken)
Check if a macro is defined, and if so, return it.
bool HandleEndIf(Token &iBody, int iLine)
Handle an #endif directive.
void Define(const char *iMacroName, size_t iMacroNameLen, const char *iMacroValue, size_t iMacroValueLen)
Define a macro without parameters.
bool HandleIf(Token &iBody, int iLine)
Handle an #if directive.
bool HandleDefine(Token &iBody, int iLine)
Handle a #define directive.
Token GetArgument(Token &oArg, bool iExpand)
Get a single function argument until next ',' or ')'.
unsigned EnableOutput
A stack of 32 booleans packed into one value :)
Token(Kind iType, const char *iString, size_t iLength)

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 Mar 18 2014 19:15:25