OgreGLSLESPreprocessor.h
Go to the documentation of this file.
1 /*
2 -----------------------------------------------------------------------------
3 This source file is part of OGRE
4  (Object-oriented Graphics Rendering Engine)
5 For the latest info, see http://www.ogre3d.org/
6 
7 Copyright (c) 2000-2013 Torus Knot Software Ltd
8 
9 Permission is hereby granted, free of charge, to any person obtaining a copy
10 of this software and associated documentation files (the "Software"), to deal
11 in the Software without restriction, including without limitation the rights
12 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 copies of the Software, and to permit persons to whom the Software is
14 furnished to do so, subject to the following conditions:
15 
16 The above copyright notice and this permission notice shall be included in
17 all copies or substantial portions of the Software.
18 
19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 THE SOFTWARE.
26 -----------------------------------------------------------------------------
27 */
28 
29 #ifndef __OGRE_CPREPROCESSOR_H__
30 #define __OGRE_CPREPROCESSOR_H__
31 
32 #include <string.h>
33 #include <stdlib.h>
34 
35 namespace Ogre {
36 
61 class CPreprocessor
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 
97  Kind Type;
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:
176  Token Name;
178  int NumArgs;
180  Token *Args;
182  Token Value;
184  Token Body;
186  Macro *Next;
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  //{ OGRE_DELETE [] Args; OGRE_DELETE Next; }
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;
218  Macro *MacroList;
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 Ogre
524 
525 #endif // __OGRE_CPREPROCESSOR_H__
Ogre::CPreprocessor::Token::Length
size_t Length
Token length in bytes.
Definition: Plus/src/GLSL/include/OgreGLSLPreprocessor.h:108
Ogre::CPreprocessor::Token::TK_STRING
@ TK_STRING
Definition: Plus/src/GLSL/include/OgreGLSLPreprocessor.h:90
Ogre::CPreprocessor::Define
void Define(const char *iMacroName, size_t iMacroNameLen, const char *iMacroValue, size_t iMacroValueLen)
Define a macro without parameters.
Ogre
Definition: OgreAndroidLogListener.h:34
Ogre::CPreprocessor::Macro::Name
Token Name
Macro name.
Definition: Plus/src/GLSL/include/OgreGLSLPreprocessor.h:176
Ogre::CPreprocessor::ErrorHandler
static ErrorHandlerFunc ErrorHandler
A pointer to the preprocessor's error handler.
Definition: Plus/src/GLSL/include/OgreGLSLPreprocessor.h:516
Ogre::CPreprocessor::~CPreprocessor
virtual ~CPreprocessor()
Destroy the preprocessor object.
Ogre::CPreprocessor::IsDefined
Macro * IsDefined(const Token &iToken)
Check if a macro is defined, and if so, return it.
Ogre::CPreprocessor::Macro::Macro
Macro(const Token &iName)
Definition: Plus/src/GLSL/include/OgreGLSLPreprocessor.h:192
Ogre::CPreprocessor::Token
A input token.
Definition: Plus/src/GLSL/include/OgreGLSLPreprocessor.h:76
Ogre::CPreprocessor::Token::TK_EOS
@ TK_EOS
Definition: Plus/src/GLSL/include/OgreGLSLPreprocessor.h:81
Ogre::CPreprocessor::Macro::Next
Macro * Next
Next macro in chained list.
Definition: Plus/src/GLSL/include/OgreGLSLPreprocessor.h:186
Ogre::CPreprocessor::GetValue
bool GetValue(const Token &iToken, long &oValue, int iLine)
Get the numeric value of a token.
Ogre::CPreprocessor
This is a simplistic C/C++-like preprocessor.
Definition: Plus/src/GLSL/include/OgreGLSLPreprocessor.h:61
Ogre::CPreprocessor::HandleIf
bool HandleIf(Token &iBody, int iLine)
Handle an #if directive.
Ogre::CPreprocessor::HandleDirective
Token HandleDirective(Token &iToken, int iLine)
Handle a preprocessor directive.
Ogre::CPreprocessor::Token::~Token
~Token()
Definition: OgreGLSLESPreprocessor.h:129
Ogre::CPreprocessor::Token::Token
Token()
Definition: OgreGLSLESPreprocessor.h:110
Ogre::CPreprocessor::Token::TK_LINECONT
@ TK_LINECONT
Definition: Plus/src/GLSL/include/OgreGLSLPreprocessor.h:85
Ogre::CPreprocessor::Token::Token
Token(Kind iType, const char *iString, size_t iLength)
Definition: OgreGLSLESPreprocessor.h:116
Ogre::CPreprocessor::Line
int Line
Current line number.
Definition: Plus/src/GLSL/include/OgreGLSLPreprocessor.h:211
Ogre::CPreprocessor::MacroList
Macro * MacroList
The list of macros defined so far.
Definition: Plus/src/GLSL/include/OgreGLSLPreprocessor.h:217
Ogre::String
_StringBase String
Definition: OgrePrerequisites.h:439
Ogre::CPreprocessor::Token::Buffer
char * Buffer
A memory-allocated string.
Definition: Plus/src/GLSL/include/OgreGLSLPreprocessor.h:105
Ogre::CPreprocessor::Token::TK_LINECOMMENT
@ TK_LINECOMMENT
Definition: Plus/src/GLSL/include/OgreGLSLPreprocessor.h:92
Ogre::CPreprocessor::HandleDefine
bool HandleDefine(Token &iBody, int iLine)
Handle a #define directive.
Ogre::CPreprocessor::Token::TK_TEXT
@ TK_TEXT
Definition: Plus/src/GLSL/include/OgreGLSLPreprocessor.h:93
Ogre::CPreprocessor::GetToken
Token GetToken(bool iExpand)
Stateless tokenizer: Parse the input text and return the next token.
Ogre::CPreprocessor::ExpandDefined
static Token ExpandDefined(CPreprocessor *iParent, int iNumArgs, Token *iArgs)
The implementation of the defined() preprocessor function.
Ogre::CPreprocessor::Token::AppendNL
void AppendNL(int iCount)
Append given number of newlines to this token.
Ogre::CPreprocessor::Token::TK_NEWLINE
@ TK_NEWLINE
Definition: Plus/src/GLSL/include/OgreGLSLPreprocessor.h:84
Ogre::CPreprocessor::Macro::Args
Token * Args
The names of the arguments.
Definition: Plus/src/GLSL/include/OgreGLSLPreprocessor.h:180
Ogre::CPreprocessor::Parse
Token Parse(const Token &iSource)
Parse the input string and return a token containing the whole output.
Ogre::CPreprocessor::GetExpression
Token GetExpression(Token &oResult, int iLine, int iOpPriority=0)
Parse an expression, compute it and return the result.
Ogre::CPreprocessor::Token::TK_PUNCTUATION
@ TK_PUNCTUATION
Definition: Plus/src/GLSL/include/OgreGLSLPreprocessor.h:88
Ogre::CPreprocessor::Token::TK_DIRECTIVE
@ TK_DIRECTIVE
Definition: Plus/src/GLSL/include/OgreGLSLPreprocessor.h:89
Ogre::CPreprocessor::Token::Kind
Kind
Definition: Plus/src/GLSL/include/OgreGLSLPreprocessor.h:79
Ogre::CPreprocessor::Macro::Expanding
bool Expanding
true if macro expansion is in progress
Definition: Plus/src/GLSL/include/OgreGLSLPreprocessor.h:190
Ogre::CPreprocessor::Macro::Value
Token Value
The macro value.
Definition: Plus/src/GLSL/include/OgreGLSLPreprocessor.h:182
Ogre::CPreprocessor::Token::TK_NUMBER
@ TK_NUMBER
Definition: Plus/src/GLSL/include/OgreGLSLPreprocessor.h:86
Ogre::CPreprocessor::Token::Token
Token(Kind iType)
Definition: OgreGLSLESPreprocessor.h:113
Ogre::CPreprocessor::Macro::ExpandFunc
Token(* ExpandFunc)(CPreprocessor *iParent, int iNumArgs, Token *iArgs)
A pointer to function implementation (if macro is really a func)
Definition: Plus/src/GLSL/include/OgreGLSLPreprocessor.h:188
Ogre::CPreprocessor::ErrorHandlerFunc
void(* ErrorHandlerFunc)(void *iData, int iLine, const char *iError, const char *iToken, size_t iTokenLen)
An error handler function type.
Definition: Plus/src/GLSL/include/OgreGLSLPreprocessor.h:507
Ogre::CPreprocessor::Token::SetValue
void SetValue(long iValue)
Set the numeric value of the token.
Ogre::CPreprocessor::Macro::Expand
Token Expand(int iNumArgs, Token *iArgs, Macro *iMacros)
Expand the macro value (will not work for functions)
Ogre::CPreprocessor::Error
void Error(int iLine, const char *iError, const Token *iToken=NULL)
Call the error handler.
Ogre::CPreprocessor::Token::TK_KEYWORD
@ TK_KEYWORD
Definition: Plus/src/GLSL/include/OgreGLSLPreprocessor.h:87
Ogre::CPreprocessor::GetArguments
Token GetArguments(int &oNumArgs, Token *&oArgs, bool iExpand)
Get all the arguments of a macro: '(' arg1 { ',' arg2 { ',' ...
Ogre::CPreprocessor::Token::Token
Token(const Token &iOther)
Definition: OgreGLSLESPreprocessor.h:120
Ogre::CPreprocessor::ExpandMacro
Token ExpandMacro(const Token &iToken)
Expand the given macro, if it exists.
Ogre::CPreprocessor::Token::TK_WHITESPACE
@ TK_WHITESPACE
Definition: Plus/src/GLSL/include/OgreGLSLPreprocessor.h:83
Ogre::CPreprocessor::Token::Allocated
size_t Allocated
True if string was allocated (and must be freed)
Definition: Plus/src/GLSL/include/OgreGLSLPreprocessor.h:99
Ogre::CPreprocessor::BOL
bool BOL
True if we are at beginning of line.
Definition: Plus/src/GLSL/include/OgreGLSLPreprocessor.h:213
Ogre::CPreprocessor::Token::operator=
Token & operator=(const Token &iOther)
Assignment operator.
Definition: Plus/src/GLSL/include/OgreGLSLPreprocessor.h:133
Ogre::CPreprocessor::Token::Append
void Append(const char *iString, size_t iLength)
Append a string to this token.
Ogre::CPreprocessor::Token::CountNL
int CountNL()
Count number of newlines in this token.
Ogre::CPreprocessor::HandleElse
bool HandleElse(Token &iBody, int iLine)
Handle an #else directive.
Ogre::CPreprocessor::Token::GetValue
bool GetValue(long &oValue) const
Get the numeric value of the token.
Ogre::CPreprocessor::HandleUnDef
bool HandleUnDef(Token &iBody, int iLine)
Undefine a previously defined macro.
Ogre::CPreprocessor::GetArgument
Token GetArgument(Token &oArg, bool iExpand)
Get a single function argument until next ',' or ')'.
Ogre::CPreprocessor::Token::TK_ERROR
@ TK_ERROR
Definition: Plus/src/GLSL/include/OgreGLSLPreprocessor.h:82
Ogre::CPreprocessor::Macro::~Macro
~Macro()
Definition: OgreGLSLESPreprocessor.h:197
Ogre::CPreprocessor::CPreprocessor
CPreprocessor()
Create an empty preprocessor object.
Definition: Plus/src/GLSL/include/OgreGLSLPreprocessor.h:426
Ogre::CPreprocessor::Source
const char * Source
The current source text input.
Definition: Plus/src/GLSL/include/OgreGLSLPreprocessor.h:207
Ogre::CPreprocessor::Macro::NumArgs
int NumArgs
Number of arguments.
Definition: Plus/src/GLSL/include/OgreGLSLPreprocessor.h:178
Ogre::CPreprocessor::Token::String
const char * String
A pointer somewhere into the input buffer.
Definition: Plus/src/GLSL/include/OgreGLSLPreprocessor.h:103
Ogre::CPreprocessor::Token::operator==
bool operator==(const Token &iOther)
Test two tokens for equality.
Definition: Plus/src/GLSL/include/OgreGLSLPreprocessor.h:163
Ogre::CPreprocessor::HandleEndIf
bool HandleEndIf(Token &iBody, int iLine)
Handle an #endif directive.
Ogre::CPreprocessor::EnableOutput
unsigned EnableOutput
A stack of 32 booleans packed into one value :)
Definition: Plus/src/GLSL/include/OgreGLSLPreprocessor.h:215
Ogre::CPreprocessor::Undef
bool Undef(const char *iMacroName, size_t iMacroNameLen)
Undefine a macro.
Ogre::CPreprocessor::SourceEnd
const char * SourceEnd
The end of the source text.
Definition: Plus/src/GLSL/include/OgreGLSLPreprocessor.h:209
Ogre::CPreprocessor::Macro
A macro definition.
Definition: Plus/src/GLSL/include/OgreGLSLPreprocessor.h:172
Ogre::CPreprocessor::Token::TK_COMMENT
@ TK_COMMENT
Definition: Plus/src/GLSL/include/OgreGLSLPreprocessor.h:91
Ogre::CPreprocessor::ErrorData
void * ErrorData
User-specific storage, passed to Error()
Definition: Plus/src/GLSL/include/OgreGLSLPreprocessor.h:519
Ogre::CPreprocessor::Token::Type
Kind Type
Token type.
Definition: Plus/src/GLSL/include/OgreGLSLPreprocessor.h:97
Ogre::CPreprocessor::HandleIfDef
bool HandleIfDef(Token &iBody, int iLine)
Handle an #ifdef directive.
Ogre::CPreprocessor::Macro::Body
Token Body
Unparsed macro body (keeps the whole raw unparsed macro body)
Definition: Plus/src/GLSL/include/OgreGLSLPreprocessor.h:184

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