This is a simplistic C/C++-like preprocessor. More...
#include <OgreGLSLPreprocessor.h>
Classes | |
class | Macro |
A macro definition. More... | |
class | Token |
A input token. More... | |
Public Types | |
typedef void(* | ErrorHandlerFunc )(void *iData, int iLine, const char *iError, const char *iToken, size_t iTokenLen) |
An error handler function type. | |
typedef void(* | ErrorHandlerFunc )(void *iData, int iLine, const char *iError, const char *iToken, size_t iTokenLen) |
An error handler function type. | |
Public Member Functions | |
CPreprocessor () | |
Create an empty preprocessor object. | |
virtual | ~CPreprocessor () |
Destroy the preprocessor object. | |
void | Define (const char *iMacroName, size_t iMacroNameLen, const char *iMacroValue, size_t iMacroValueLen) |
Define a macro without parameters. | |
void | Define (const char *iMacroName, size_t iMacroNameLen, long iMacroValue) |
Define a numerical macro. | |
bool | Undef (const char *iMacroName, size_t iMacroNameLen) |
Undefine a macro. | |
char * | Parse (const char *iSource, size_t iLength, size_t &oLength) |
Parse the input string and return a newly-allocated output string. | |
CPreprocessor () | |
Create an empty preprocessor object. | |
virtual | ~CPreprocessor () |
Destroy the preprocessor object. | |
void | Define (const char *iMacroName, size_t iMacroNameLen, const char *iMacroValue, size_t iMacroValueLen) |
Define a macro without parameters. | |
void | Define (const char *iMacroName, size_t iMacroNameLen, long iMacroValue) |
Define a numerical macro. | |
bool | Undef (const char *iMacroName, size_t iMacroNameLen) |
Undefine a macro. | |
char * | Parse (const char *iSource, size_t iLength, size_t &oLength) |
Parse the input string and return a newly-allocated output string. | |
Public Attributes | |
void * | ErrorData |
User-specific storage, passed to Error() | |
Static Public Attributes | |
static ErrorHandlerFunc | ErrorHandler |
A pointer to the preprocessor's error handler. | |
Private Member Functions | |
CPreprocessor (const Token &iToken, int iLine) | |
Private constructor to re-parse a single token. | |
Token | GetToken (bool iExpand) |
Stateless tokenizer: Parse the input text and return the next token. | |
Token | HandleDirective (Token &iToken, int iLine) |
Handle a preprocessor directive. | |
bool | HandleDefine (Token &iBody, int iLine) |
Handle a #define directive. | |
bool | HandleUnDef (Token &iBody, int iLine) |
Undefine a previously defined macro. | |
bool | HandleIfDef (Token &iBody, int iLine) |
Handle an #ifdef directive. | |
bool | HandleIf (Token &iBody, int iLine) |
Handle an #if directive. | |
bool | HandleElse (Token &iBody, int iLine) |
Handle an #else directive. | |
bool | HandleEndIf (Token &iBody, int iLine) |
Handle an #endif directive. | |
Token | GetArgument (Token &oArg, bool iExpand) |
Get a single function argument until next ',' or ')'. | |
Token | GetArguments (int &oNumArgs, Token *&oArgs, bool iExpand) |
Get all the arguments of a macro: '(' arg1 { ',' arg2 { ',' ... | |
Token | GetExpression (Token &oResult, int iLine, int iOpPriority=0) |
Parse an expression, compute it and return the result. | |
bool | GetValue (const Token &iToken, long &oValue, int iLine) |
Get the numeric value of a token. | |
Token | ExpandMacro (const Token &iToken) |
Expand the given macro, if it exists. | |
Macro * | IsDefined (const Token &iToken) |
Check if a macro is defined, and if so, return it. | |
Token | Parse (const Token &iSource) |
Parse the input string and return a token containing the whole output. | |
void | Error (int iLine, const char *iError, const Token *iToken=NULL) |
Call the error handler. | |
CPreprocessor (const Token &iToken, int iLine) | |
Private constructor to re-parse a single token. | |
Token | GetToken (bool iExpand) |
Stateless tokenizer: Parse the input text and return the next token. | |
Token | HandleDirective (Token &iToken, int iLine) |
Handle a preprocessor directive. | |
bool | HandleDefine (Token &iBody, int iLine) |
Handle a #define directive. | |
bool | HandleUnDef (Token &iBody, int iLine) |
Undefine a previously defined macro. | |
bool | HandleIfDef (Token &iBody, int iLine) |
Handle an #ifdef directive. | |
bool | HandleIf (Token &iBody, int iLine) |
Handle an #if directive. | |
bool | HandleElse (Token &iBody, int iLine) |
Handle an #else directive. | |
bool | HandleEndIf (Token &iBody, int iLine) |
Handle an #endif directive. | |
Token | GetArgument (Token &oArg, bool iExpand) |
Get a single function argument until next ',' or ')'. | |
Token | GetArguments (int &oNumArgs, Token *&oArgs, bool iExpand) |
Get all the arguments of a macro: '(' arg1 { ',' arg2 { ',' ... | |
Token | GetExpression (Token &oResult, int iLine, int iOpPriority=0) |
Parse an expression, compute it and return the result. | |
bool | GetValue (const Token &iToken, long &oValue, int iLine) |
Get the numeric value of a token. | |
Token | ExpandMacro (const Token &iToken) |
Expand the given macro, if it exists. | |
Macro * | IsDefined (const Token &iToken) |
Check if a macro is defined, and if so, return it. | |
Token | Parse (const Token &iSource) |
Parse the input string and return a token containing the whole output. | |
void | Error (int iLine, const char *iError, const Token *iToken=NULL) |
Call the error handler. | |
Static Private Member Functions | |
static Token | ExpandDefined (CPreprocessor *iParent, int iNumArgs, Token *iArgs) |
The implementation of the defined() preprocessor function. | |
static Token | ExpandDefined (CPreprocessor *iParent, int iNumArgs, Token *iArgs) |
The implementation of the defined() preprocessor function. | |
Private Attributes | |
const char * | Source |
The current source text input. | |
const char * | SourceEnd |
The end of the source text. | |
int | Line |
Current line number. | |
bool | BOL |
True if we are at beginning of line. | |
unsigned | EnableOutput |
A stack of 32 booleans packed into one value :) | |
Macro * | MacroList |
The list of macros defined so far. | |
Friends | |
class | CPreprocessor::Macro |
This is a simplistic C/C++-like preprocessor.
It takes an non-zero-terminated string on input and outputs a non-zero-terminated string buffer.
This preprocessor was designed specifically for GLSL shaders, so if you want to use it for other purposes you might want to check if the feature set it provides is enough for you.
Here's a list of supported features:
#if: Supports numeric expression of any complexity, also supports the defined() pseudo-function.
takes an non-zero-terminated string on input and outputs a non-zero-terminated string buffer.
This preprocessor was designed specifically for GLSL ES shaders, so if you want to use it for other purposes you might want to check if the feature set it provides is enough for you.
Here's a list of supported features:
Definition at line 61 of file OgreGLSLPreprocessor.h.
typedef void(* Ogre::CPreprocessor::ErrorHandlerFunc)(void *iData, int iLine, const char *iError, const char *iToken, size_t iTokenLen) |
An error handler function type.
The default implementation just drops a note to stderr and then the parser ends, returning NULL.
iData | User-specific pointer from the corresponding CPreprocessor object. |
iLine | The line at which the error happened. |
iError | The error string. |
iToken | If not NULL contains the erroneous token |
iTokenLen | The length of iToken. iToken is never zero-terminated! |
Definition at line 507 of file OgreGLSLPreprocessor.h.
typedef void(* Ogre::CPreprocessor::ErrorHandlerFunc)(void *iData, int iLine, const char *iError, const char *iToken, size_t iTokenLen) |
An error handler function type.
The default implementation just drops a note to stderr and then the parser ends, returning NULL.
iData | User-specific pointer from the corresponding CPreprocessor object. |
iLine | The line at which the error happened. |
iError | The error string. |
iToken | If not NULL contains the erroneous token |
iTokenLen | The length of iToken. iToken is never zero-terminated! |
Definition at line 508 of file OgreGLSLESPreprocessor.h.
Ogre::CPreprocessor::CPreprocessor | ( | const Token & | iToken, |
int | iLine | ||
) | [private] |
Private constructor to re-parse a single token.
Create an empty preprocessor object.
Definition at line 426 of file OgreGLSLPreprocessor.h.
virtual Ogre::CPreprocessor::~CPreprocessor | ( | ) | [virtual] |
Destroy the preprocessor object.
Ogre::CPreprocessor::CPreprocessor | ( | const Token & | iToken, |
int | iLine | ||
) | [private] |
Private constructor to re-parse a single token.
Create an empty preprocessor object.
Definition at line 427 of file OgreGLSLESPreprocessor.h.
virtual Ogre::CPreprocessor::~CPreprocessor | ( | ) | [virtual] |
Destroy the preprocessor object.
void Ogre::CPreprocessor::Define | ( | const char * | iMacroName, |
size_t | iMacroNameLen, | ||
const char * | iMacroValue, | ||
size_t | iMacroValueLen | ||
) |
Define a macro without parameters.
iMacroName | The name of the defined macro |
iMacroNameLen | The length of the name of the defined macro |
iMacroValue | The value of the defined macro |
iMacroValueLen | The length of the value of the defined macro |
void Ogre::CPreprocessor::Define | ( | const char * | iMacroName, |
size_t | iMacroNameLen, | ||
const char * | iMacroValue, | ||
size_t | iMacroValueLen | ||
) |
Define a macro without parameters.
iMacroName | The name of the defined macro |
iMacroNameLen | The length of the name of the defined macro |
iMacroValue | The value of the defined macro |
iMacroValueLen | The length of the value of the defined macro |
void Ogre::CPreprocessor::Define | ( | const char * | iMacroName, |
size_t | iMacroNameLen, | ||
long | iMacroValue | ||
) |
Define a numerical macro.
iMacroName | The name of the defined macro |
iMacroNameLen | The length of the name of the defined macro |
iMacroValue | The value of the defined macro |
void Ogre::CPreprocessor::Define | ( | const char * | iMacroName, |
size_t | iMacroNameLen, | ||
long | iMacroValue | ||
) |
Define a numerical macro.
iMacroName | The name of the defined macro |
iMacroNameLen | The length of the name of the defined macro |
iMacroValue | The value of the defined macro |
void Ogre::CPreprocessor::Error | ( | int | iLine, |
const char * | iError, | ||
const Token * | iToken = NULL |
||
) | [private] |
Call the error handler.
iLine | The line at which the error happened. |
iError | The error string. |
iToken | If not NULL contains the erroneous token |
void Ogre::CPreprocessor::Error | ( | int | iLine, |
const char * | iError, | ||
const Token * | iToken = NULL |
||
) | [private] |
Call the error handler.
iLine | The line at which the error happened. |
iError | The error string. |
iToken | If not NULL contains the erroneous token |
static Token Ogre::CPreprocessor::ExpandDefined | ( | CPreprocessor * | iParent, |
int | iNumArgs, | ||
Token * | iArgs | ||
) | [static, private] |
The implementation of the defined() preprocessor function.
iParent | The parent preprocessor object |
iNumArgs | Number of arguments |
iArgs | The arguments themselves |
static Token Ogre::CPreprocessor::ExpandDefined | ( | CPreprocessor * | iParent, |
int | iNumArgs, | ||
Token * | iArgs | ||
) | [static, private] |
The implementation of the defined() preprocessor function.
iParent | The parent preprocessor object |
iNumArgs | Number of arguments |
iArgs | The arguments themselves |
Token Ogre::CPreprocessor::ExpandMacro | ( | const Token & | iToken | ) | [private] |
Expand the given macro, if it exists.
If macro has arguments, they are collected from source stream.
iToken | A KEYWORD token containing the (possible) macro name. |
Token Ogre::CPreprocessor::ExpandMacro | ( | const Token & | iToken | ) | [private] |
Expand the given macro, if it exists.
If macro has arguments, they are collected from source stream.
iToken | A KEYWORD token containing the (possible) macro name. |
Token Ogre::CPreprocessor::GetArgument | ( | Token & | oArg, |
bool | iExpand | ||
) | [private] |
Get a single function argument until next ',' or ')'.
oArg | The argument is returned in this variable. |
iExpand | If false, parameters are not expanded and no expressions are allowed; only a single keyword is expected per argument. |
Token Ogre::CPreprocessor::GetArgument | ( | Token & | oArg, |
bool | iExpand | ||
) | [private] |
Get a single function argument until next ',' or ')'.
oArg | The argument is returned in this variable. |
iExpand | If false, parameters are not expanded and no expressions are allowed; only a single keyword is expected per argument. |
Token Ogre::CPreprocessor::GetArguments | ( | int & | oNumArgs, |
Token *& | oArgs, | ||
bool | iExpand | ||
) | [private] |
Get all the arguments of a macro: '(' arg1 { ',' arg2 { ',' ...
}} ')'
oNumArgs | Number of parsed arguments is stored into this variable. |
oArgs | This is set to a pointer to an array of parsed arguments. |
iExpand | If false, parameters are not expanded and no expressions are allowed; only a single keyword is expected per argument. |
Token Ogre::CPreprocessor::GetArguments | ( | int & | oNumArgs, |
Token *& | oArgs, | ||
bool | iExpand | ||
) | [private] |
Get all the arguments of a macro: '(' arg1 { ',' arg2 { ',' ...
}} ')'
oNumArgs | Number of parsed arguments is stored into this variable. |
oArgs | This is set to a pointer to an array of parsed arguments. |
iExpand | If false, parameters are not expanded and no expressions are allowed; only a single keyword is expected per argument. |
Token Ogre::CPreprocessor::GetExpression | ( | Token & | oResult, |
int | iLine, | ||
int | iOpPriority = 0 |
||
) | [private] |
Parse an expression, compute it and return the result.
oResult | A token containing the result of expression |
iLine | The line at which the expression starts (for error reports) |
iOpPriority | Operator priority (at which operator we will stop if proceeding recursively -- used internally. Parser stops when it encounters an operator with higher or equal priority). |
Token Ogre::CPreprocessor::GetExpression | ( | Token & | oResult, |
int | iLine, | ||
int | iOpPriority = 0 |
||
) | [private] |
Parse an expression, compute it and return the result.
oResult | A token containing the result of expression |
iLine | The line at which the expression starts (for error reports) |
iOpPriority | Operator priority (at which operator we will stop if proceeding recursively -- used internally. Parser stops when it encounters an operator with higher or equal priority). |
Token Ogre::CPreprocessor::GetToken | ( | bool | iExpand | ) | [private] |
Stateless tokenizer: Parse the input text and return the next token.
iExpand | If true, macros will be expanded to their values |
Token Ogre::CPreprocessor::GetToken | ( | bool | iExpand | ) | [private] |
Stateless tokenizer: Parse the input text and return the next token.
iExpand | If true, macros will be expanded to their values |
bool Ogre::CPreprocessor::GetValue | ( | const Token & | iToken, |
long & | oValue, | ||
int | iLine | ||
) | [private] |
Get the numeric value of a token.
If the token was produced by expanding a macro, we will get an TEXT token which can contain a whole expression; in this case we will call GetExpression to parse it. Otherwise we just call the token's GetValue() method.
iToken | The token to get the numeric value of |
oValue | The variable to put the value into |
iLine | The line where the directive begins (for error reports) |
bool Ogre::CPreprocessor::GetValue | ( | const Token & | iToken, |
long & | oValue, | ||
int | iLine | ||
) | [private] |
Get the numeric value of a token.
If the token was produced by expanding a macro, we will get an TEXT token which can contain a whole expression; in this case we will call GetExpression to parse it. Otherwise we just call the token's GetValue() method.
iToken | The token to get the numeric value of |
oValue | The variable to put the value into |
iLine | The line where the directive begins (for error reports) |
bool Ogre::CPreprocessor::HandleDefine | ( | Token & | iBody, |
int | iLine | ||
) | [private] |
Handle a #define directive.
iBody | The body of the directive (everything after the directive until end of line). |
iLine | The line where the directive begins (for error reports) |
bool Ogre::CPreprocessor::HandleDefine | ( | Token & | iBody, |
int | iLine | ||
) | [private] |
Handle a #define directive.
iBody | The body of the directive (everything after the directive until end of line). |
iLine | The line where the directive begins (for error reports) |
Token Ogre::CPreprocessor::HandleDirective | ( | Token & | iToken, |
int | iLine | ||
) | [private] |
Handle a preprocessor directive.
iToken | The whole preprocessor directive line (until EOL) |
iLine | The line where the directive begins (for error reports) |
Token Ogre::CPreprocessor::HandleDirective | ( | Token & | iToken, |
int | iLine | ||
) | [private] |
Handle a preprocessor directive.
iToken | The whole preprocessor directive line (until EOL) |
iLine | The line where the directive begins (for error reports) |
bool Ogre::CPreprocessor::HandleElse | ( | Token & | iBody, |
int | iLine | ||
) | [private] |
Handle an #else directive.
iBody | The body of the directive (everything after the directive until end of line). |
iLine | The line where the directive begins (for error reports) |
bool Ogre::CPreprocessor::HandleElse | ( | Token & | iBody, |
int | iLine | ||
) | [private] |
Handle an #else directive.
iBody | The body of the directive (everything after the directive until end of line). |
iLine | The line where the directive begins (for error reports) |
bool Ogre::CPreprocessor::HandleEndIf | ( | Token & | iBody, |
int | iLine | ||
) | [private] |
Handle an #endif directive.
iBody | The body of the directive (everything after the directive until end of line). |
iLine | The line where the directive begins (for error reports) |
bool Ogre::CPreprocessor::HandleEndIf | ( | Token & | iBody, |
int | iLine | ||
) | [private] |
Handle an #endif directive.
iBody | The body of the directive (everything after the directive until end of line). |
iLine | The line where the directive begins (for error reports) |
bool Ogre::CPreprocessor::HandleIf | ( | Token & | iBody, |
int | iLine | ||
) | [private] |
Handle an #if directive.
iBody | The body of the directive (everything after the directive until end of line). |
iLine | The line where the directive begins (for error reports) |
bool Ogre::CPreprocessor::HandleIf | ( | Token & | iBody, |
int | iLine | ||
) | [private] |
Handle an #if directive.
iBody | The body of the directive (everything after the directive until end of line). |
iLine | The line where the directive begins (for error reports) |
bool Ogre::CPreprocessor::HandleIfDef | ( | Token & | iBody, |
int | iLine | ||
) | [private] |
Handle an #ifdef directive.
iBody | The body of the directive (everything after the directive until end of line). |
iLine | The line where the directive begins (for error reports) |
bool Ogre::CPreprocessor::HandleIfDef | ( | Token & | iBody, |
int | iLine | ||
) | [private] |
Handle an #ifdef directive.
iBody | The body of the directive (everything after the directive until end of line). |
iLine | The line where the directive begins (for error reports) |
bool Ogre::CPreprocessor::HandleUnDef | ( | Token & | iBody, |
int | iLine | ||
) | [private] |
Undefine a previously defined macro.
iBody | The body of the directive (everything after the directive until end of line). |
iLine | The line where the directive begins (for error reports) |
bool Ogre::CPreprocessor::HandleUnDef | ( | Token & | iBody, |
int | iLine | ||
) | [private] |
Undefine a previously defined macro.
iBody | The body of the directive (everything after the directive until end of line). |
iLine | The line where the directive begins (for error reports) |
Macro* Ogre::CPreprocessor::IsDefined | ( | const Token & | iToken | ) | [private] |
Check if a macro is defined, and if so, return it.
iToken | Macro name |
Macro* Ogre::CPreprocessor::IsDefined | ( | const Token & | iToken | ) | [private] |
Check if a macro is defined, and if so, return it.
iToken | Macro name |
Token Ogre::CPreprocessor::Parse | ( | const Token & | iSource | ) | [private] |
Parse the input string and return a token containing the whole output.
iSource | The source text enclosed in a token |
Token Ogre::CPreprocessor::Parse | ( | const Token & | iSource | ) | [private] |
Parse the input string and return a token containing the whole output.
iSource | The source text enclosed in a token |
char* Ogre::CPreprocessor::Parse | ( | const char * | iSource, |
size_t | iLength, | ||
size_t & | oLength | ||
) |
Parse the input string and return a newly-allocated output string.
iSource | The source text |
iLength | The length of the source text in characters |
oLength | The length of the output string. |
char* Ogre::CPreprocessor::Parse | ( | const char * | iSource, |
size_t | iLength, | ||
size_t & | oLength | ||
) |
Parse the input string and return a newly-allocated output string.
iSource | The source text |
iLength | The length of the source text in characters |
oLength | The length of the output string. |
bool Ogre::CPreprocessor::Undef | ( | const char * | iMacroName, |
size_t | iMacroNameLen | ||
) |
Undefine a macro.
iMacroName | The name of the macro to undefine |
iMacroNameLen | The length of the name of the macro to undefine |
bool Ogre::CPreprocessor::Undef | ( | const char * | iMacroName, |
size_t | iMacroNameLen | ||
) |
Undefine a macro.
iMacroName | The name of the macro to undefine |
iMacroNameLen | The length of the name of the macro to undefine |
CPreprocessor::Macro [friend] |
Definition at line 204 of file OgreGLSLPreprocessor.h.
bool Ogre::CPreprocessor::BOL [private] |
True if we are at beginning of line.
Definition at line 213 of file OgreGLSLPreprocessor.h.
unsigned Ogre::CPreprocessor::EnableOutput [private] |
A stack of 32 booleans packed into one value :)
Definition at line 215 of file OgreGLSLPreprocessor.h.
User-specific storage, passed to Error()
Definition at line 519 of file OgreGLSLPreprocessor.h.
static ErrorHandlerFunc Ogre::CPreprocessor::ErrorHandler [static] |
A pointer to the preprocessor's error handler.
You can assign the address of your own function to this variable and implement your own error handling (e.g. throwing an exception etc).
Definition at line 516 of file OgreGLSLPreprocessor.h.
int Ogre::CPreprocessor::Line [private] |
Current line number.
Definition at line 211 of file OgreGLSLPreprocessor.h.
Macro * Ogre::CPreprocessor::MacroList [private] |
The list of macros defined so far.
Definition at line 217 of file OgreGLSLPreprocessor.h.
const char * Ogre::CPreprocessor::Source [private] |
The current source text input.
Definition at line 207 of file OgreGLSLPreprocessor.h.
const char * Ogre::CPreprocessor::SourceEnd [private] |
The end of the source text.
Definition at line 209 of file OgreGLSLPreprocessor.h.
Copyright © 2012 Torus Knot Software Ltd
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
Last modified Sun Sep 2 2012 07:27:55