35 #ifndef INCLUDED_CPREPROCESSOR 36 #define INCLUDED_CPREPROCESSOR 117 Token (
Kind iType,
const char *iString,
size_t iLength) :
118 Type (iType), Allocated (0),
String (iString), Length (iLength)
131 {
if (Allocated) free (
Buffer); }
136 if (Allocated) free (
Buffer);
146 void Append (
const char *iString,
size_t iLength);
166 if (iOther.
Length != Length)
194 Name (iName), NumArgs (0), Args (NULL), Next (NULL),
195 ExpandFunc (NULL), Expanding (false)
199 {
delete [] Args;
delete Next; }
371 bool GetValue (
const Token &iToken,
long &oValue,
int iLine);
423 void Error (
int iLine,
const char *iError,
const Token *iToken = NULL);
444 void Define (
const char *iMacroName,
size_t iMacroNameLen,
445 const char *iMacroValue,
size_t iMacroValueLen);
456 void Define (
const char *iMacroName,
size_t iMacroNameLen,
long iMacroValue);
465 void Define (
const char *iMacroName,
const char *iMacroValue);
474 void Define (
const char *iMacroName,
long iMacroValue);
485 bool Undef (
const char *iMacroName,
size_t iMacroNameLen);
509 char *
Parse (
const char *iSource,
size_t iLength,
size_t &oLength);
527 void *iData,
int iLine,
const char *iError,
528 const char *iToken,
size_t iTokenLen);
541 #endif // INCLUDED_CPREPROCESSOR char * Buffer
A memory-allocated string.
Definition: Preprocessor.h:106
const char * Source
The current source text input.
Definition: Preprocessor.h:208
void Define(const char *iMacroName, size_t iMacroNameLen, const char *iMacroValue, size_t iMacroValueLen)
Define a macro without parameters.
Definition: Preprocessor.cpp:1159
void Append(const char *iString, size_t iLength)
Append a string to this token.
Definition: Preprocessor.cpp:56
int NumArgs
Number of arguments.
Definition: Preprocessor.h:179
void(* ErrorHandlerFunc)(void *iData, int iLine, const char *iError, const char *iToken, size_t iTokenLen)
An error handler function type.
Definition: Preprocessor.h:526
void Error(int iLine, const char *iError, const Token *iToken=NULL)
Call the error handler.
Definition: Preprocessor.cpp:258
bool Undef(const char *iMacroName, size_t iMacroNameLen)
Undefine a macro.
Definition: Preprocessor.cpp:1187
CPreprocessor()
Create an empty preprocessor object.
Definition: Preprocessor.h:427
size_t Length
Token length in bytes.
Definition: Preprocessor.h:109
Definition: Preprocessor.h:92
Token()
Definition: Preprocessor.h:111
Definition: Preprocessor.h:87
bool HandleDefine(Token &iBody, int iLine)
Handle a #define directive.
Definition: Preprocessor.cpp:872
Token Parse(const Token &iSource)
Parse the input string and return a token containing the whole output.
Definition: Preprocessor.cpp:1208
const char * String
A pointer somewhere into the input buffer.
Definition: Preprocessor.h:104
bool HandleIf(Token &iBody, int iLine)
Handle an #if directive.
Definition: Preprocessor.cpp:994
bool HandleEndIf(Token &iBody, int iLine)
Handle an #endif directive.
Definition: Preprocessor.cpp:1038
Token * Args
The names of the arguments.
Definition: Preprocessor.h:181
Definition: Preprocessor.h:83
bool BOL
True if we are at beginning of line.
Definition: Preprocessor.h:214
~Macro()
Definition: Preprocessor.h:198
Definition: Preprocessor.h:91
Token GetToken(bool iExpand)
Stateless tokenizer: Parse the input text and return the next token.
Definition: Preprocessor.cpp:266
Token GetArguments(int &oNumArgs, Token *&oArgs, bool iExpand)
Get all the arguments of a macro: '(' arg1 { ',' arg2 { ',' ...
Definition: Preprocessor.cpp:808
virtual ~CPreprocessor()
Destroy the preprocessor object.
Definition: Preprocessor.cpp:253
Token & operator=(const Token &iOther)
Assignment operator.
Definition: Preprocessor.h:134
Token GetArgument(Token &oArg, bool iExpand)
Get a single function argument until next ',' or ')'.
Definition: Preprocessor.cpp:736
Definition: Preprocessor.h:89
Token HandleDirective(Token &iToken, int iLine)
Handle a preprocessor directive.
Definition: Preprocessor.cpp:1053
Macro(const Token &iName)
Definition: Preprocessor.h:193
Macro * Next
Next macro in chained list.
Definition: Preprocessor.h:187
size_t Allocated
True if string was allocated (and must be freed)
Definition: Preprocessor.h:100
Kind Type
Token type.
Definition: Preprocessor.h:98
A input token.
Definition: Preprocessor.h:77
Definition: Preprocessor.h:88
bool operator==(const Token &iOther)
Test two tokens for equality.
Definition: Preprocessor.h:164
int CountNL()
Count number of newlines in this token.
Definition: Preprocessor.cpp:174
bool HandleElse(Token &iBody, int iLine)
Handle an #else directive.
Definition: Preprocessor.cpp:1021
const char * SourceEnd
The end of the source text.
Definition: Preprocessor.h:210
static ErrorHandlerFunc ErrorHandler
A pointer to the preprocessor's error handler.
Definition: Preprocessor.h:535
Definition: Preprocessor.h:94
bool GetValue(long &oValue) const
Get the numeric value of the token.
Definition: Preprocessor.cpp:102
Macro * IsDefined(const Token &iToken)
Check if a macro is defined, and if so, return it.
Definition: Preprocessor.cpp:398
Token Value
The macro value.
Definition: Preprocessor.h:183
Definition: Preprocessor.h:93
Token Name
Macro name.
Definition: Preprocessor.h:177
Definition: Preprocessor.h:90
Token(Kind iType)
Definition: Preprocessor.h:114
A macro definition.
Definition: Preprocessor.h:173
Token ExpandMacro(const Token &iToken)
Expand the given macro, if it exists.
Definition: Preprocessor.cpp:407
~Token()
Definition: Preprocessor.h:130
bool HandleIfDef(Token &iBody, int iLine)
Handle an #ifdef directive.
Definition: Preprocessor.cpp:947
unsigned EnableOutput
A stack of 32 booleans packed into one value :)
Definition: Preprocessor.h:216
Definition: Preprocessor.h:85
Definition: Preprocessor.h:84
bool Expanding
true if macro expansion is in progress
Definition: Preprocessor.h:191
Definition: Preprocessor.h:86
Kind
Definition: Preprocessor.h:80
Token(const Token &iOther)
Definition: Preprocessor.h:121
This is a simplistic C/C++-like preprocessor.
Definition: Preprocessor.h:62
void AppendNL(int iCount)
Append given number of newlines to this token.
Definition: Preprocessor.cpp:160
Token GetExpression(Token &oResult, int iLine, int iOpPriority=0)
Parse an expression, compute it and return the result.
Definition: Preprocessor.cpp:474
Token(Kind iType, const char *iString, size_t iLength)
Definition: Preprocessor.h:117
Token Body
Unparsed macro body (keeps the whole raw unparsed macro body)
Definition: Preprocessor.h:185
Macro * MacroList
The list of macros defined so far.
Definition: Preprocessor.h:218
static Token ExpandDefined(CPreprocessor *iParent, int iNumArgs, Token *iArgs)
The implementation of the defined() preprocessor function.
Definition: Preprocessor.cpp:982
void * ErrorData
User-specific storage, passed to Error()
Definition: Preprocessor.h:538
Definition: Preprocessor.h:82
bool HandleUnDef(Token &iBody, int iLine)
Undefine a previously defined macro.
Definition: Preprocessor.cpp:919
int Line
Current line number.
Definition: Preprocessor.h:212
void SetValue(long iValue)
Set the numeric value of the token.
Definition: Preprocessor.cpp:151