33 #define MAX_DEFINEPARMS 128
34 #define DEFINEHASHSIZE 2048
36 #define TOKEN_FL_RECURSIVE_DEFINE 1
74 for ( prev =
NULL, d = idParser::globaldefines; d; prev = d, d = d->
next ) {
75 if ( !
strcmp( d->name, name ) ) {
84 idParser::globaldefines = d->
next;
155 for ( i = 0; name[
i] !=
'\0'; i++ ) {
156 hash += name[
i] * (119 +
i);
171 define->
hashnext = definehash[hash];
172 definehash[hash] = define;
185 for ( d = definehash[hash]; d; d = d->
hashnext ) {
201 for ( d = defines; d; d = d->
next ) {
219 for ( p = define->
parms; p; p = p->
next ) {
220 if ( (*p) == name ) {
235 idToken *token, *newtoken, *lasttoken;
239 newdefine->
name = (
char *) newdefine +
sizeof(
define_t);
240 strcpy(newdefine->
name, define->
name);
249 for (lasttoken =
NULL, token = define->
tokens; token; token = token->
next) {
252 if (lasttoken) lasttoken->
next = newtoken;
253 else newdefine->
tokens = newtoken;
254 lasttoken = newtoken;
258 for (lasttoken =
NULL, token = define->
parms; token; token = token->
next) {
261 if (lasttoken) lasttoken->
next = newtoken;
262 else newdefine->
parms = newtoken;
263 lasttoken = newtoken;
277 for (t = define->
parms; t; t = next) {
282 for (t = define->
tokens; t; t = next) {
299 if ( !src.
LoadMemory(
string, strlen(
string),
"*defineString") ) {
358 indent->
skip = (skip != 0);
383 *type = indent->
type;
384 *skip = indent->
skip;
485 int i, done, lastcomma, numparms, indent;
492 if ( define->
numparms > maxparms ) {
497 for ( i = 0; i < define->
numparms; i++ ) {
501 if ( token !=
"(" ) {
507 for ( done = 0, numparms = 0, indent = 1; !done; ) {
508 if ( numparms >= maxparms ) {
512 parms[numparms] =
NULL;
522 if ( token ==
"," ) {
527 if ( numparms >= define->
numparms ) {
534 else if ( token ==
"(" ) {
537 else if ( token ==
")" ) {
559 if ( numparms < define->numparms ) {
563 if (last) last->
next =
t;
564 else parms[numparms] =
t;
585 for ( t = tokens;
t; t = t->
next ) {
639 for (i = 0; builtin[
i].string; i++) {
642 strcpy(define->
name, builtin[i].string);
711 token->
Append( curtime+4 );
713 token->
Append( curtime+20 );
730 token->
Append( curtime+11 );
778 for ( i = 0; i < define->
numparms; i++ ) {
779 Log_Write(
"define parms %d:", i);
780 for ( pt = parms[i]; pt; pt = pt->
next ) {
781 Log_Write(
"%s", pt->
c_str() );
790 for ( dt = define->
tokens; dt; dt = dt->
next ) {
797 if ( parmnum >= 0 ) {
798 for ( pt = parms[parmnum]; pt; pt = pt->
next ) {
802 if (last) last->
next =
t;
809 if ( (*dt) ==
"#" ) {
818 if ( parmnum >= 0 ) {
844 if ( last ) last->
next =
t;
850 for ( t = first;
t; ) {
853 if ( (*t->
next) ==
"##" ) {
863 if ( t2 == last ) last = t1;
875 for ( i = 0; i < define->
numparms; i++ ) {
876 for ( pt = parms[i]; pt; pt = nextpt ) {
891 idToken *firsttoken, *lasttoken;
897 if ( firsttoken && lasttoken ) {
927 }
while( (*token) ==
"\\" );
981 if ( token !=
">" ) {
1050 lastdefine = define;
1122 if (last) last->
next =
t;
1132 if ( token ==
")" ) {
1136 if ( token !=
"," ) {
1157 if ( last ) last->
next =
t;
1164 if ( (*define->
tokens) ==
"##" || (*last) ==
"##" ) {
1305 case P_MUL:
return 15;
1306 case P_DIV:
return 15;
1307 case P_MOD:
return 15;
1308 case P_ADD:
return 14;
1309 case P_SUB:
return 14;
1341 #define MAX_VALUES 64
1342 #define MAX_OPERATORS 64
1344 #define AllocValue(val) \
1345 if ( numvalues >= MAX_VALUES ) { \
1346 idParser::Error( "out of value space\n" ); \
1351 val = &value_heap[numvalues++]; \
1354 #define FreeValue(val)
1356 #define AllocOperator(op) \
1357 if ( numoperators >= MAX_OPERATORS ) { \
1358 idParser::Error( "out of operator space\n" ); \
1363 op = &operator_heap[numoperators++]; \
1366 #define FreeOperator(op)
1369 operator_t *o, *firstoperator, *lastoperator;
1375 int lastwasvalue = 0;
1376 int negativevalue = 0;
1377 int questmarkintvalue = 0;
1378 double questmarkfloatvalue = 0;
1379 int gotquestmarkvalue =
false;
1380 int lastoperatortype = 0;
1383 int numoperators = 0;
1387 firstoperator = lastoperator =
NULL;
1388 firstvalue = lastvalue =
NULL;
1389 if (intvalue) *intvalue = 0;
1390 if (floatvalue) *floatvalue = 0;
1391 for ( t = tokens;
t; t = t->
next ) {
1395 if ( lastwasvalue || negativevalue ) {
1400 if ( (*t) !=
"defined" ) {
1406 if ( (*t) ==
"(" ) {
1427 v->
prev = lastvalue;
1428 if (lastvalue) lastvalue->
next =
v;
1429 else firstvalue =
v;
1433 if (!t || (*t) !=
")" ) {
1453 if (negativevalue) {
1463 v->
prev = lastvalue;
1464 if (lastvalue) lastvalue->
next =
v;
1465 else firstvalue =
v;
1475 if (negativevalue) {
1486 if (parentheses < 0) {
1522 if (!lastwasvalue) {
1553 if (!lastwasvalue) {
1567 if (!error && !negativevalue) {
1574 o->
prev = lastoperator;
1575 if (lastoperator) lastoperator->
next = o;
1576 else firstoperator = o;
1594 if (!lastwasvalue) {
1598 else if (parentheses) {
1604 gotquestmarkvalue =
false;
1605 questmarkintvalue = 0;
1606 questmarkfloatvalue = 0;
1608 while( !error && firstoperator ) {
1610 for (o = firstoperator; o->
next; o = o->
next) {
1643 if (v2) Log_Write(
"value2 = %d", v2->
intvalue);
1647 if (v2) Log_Write(
"value2 = %f", v2->
floatvalue);
1704 if (!gotquestmarkvalue) {
1710 if (!questmarkintvalue)
1714 if (!questmarkfloatvalue)
1717 gotquestmarkvalue =
false;
1722 if (gotquestmarkvalue) {
1729 gotquestmarkvalue =
true;
1734 if (integer) Log_Write(
"result value = %d", v1->
intvalue);
1735 else Log_Write(
"result value = %f", v1->
floatvalue);
1739 lastoperatortype = o->
op;
1748 else firstvalue = v->
next;
1750 else lastvalue = v->
prev;
1756 else firstoperator = o->
next;
1758 else lastoperator = o->
prev;
1763 if (intvalue) *intvalue = firstvalue->
intvalue;
1764 if (floatvalue) *floatvalue = firstvalue->
floatvalue;
1766 for (o = firstoperator; o; o = lastoperator) {
1767 lastoperator = o->
next;
1771 for (v = firstvalue;
v; v = lastvalue) {
1772 lastvalue = v->
next;
1794 idToken token, *firsttoken, *lasttoken;
1797 int defined =
false;
1819 if (lasttoken) lasttoken->
next =
t;
1820 else firsttoken =
t;
1823 else if ( token ==
"defined" ) {
1827 if (lasttoken) lasttoken->
next =
t;
1828 else firsttoken =
t;
1847 if (lasttoken) lasttoken->
next =
t;
1848 else firsttoken =
t;
1864 for (t = firsttoken;
t; t = nexttoken) {
1866 Log_Write(
" %s", t->
c_str());
1868 nexttoken = t->
next;
1872 if (integer) Log_Write(
"eval result: %d", *intvalue);
1873 else Log_Write(
"eval result: %f", *floatvalue);
1885 int indent, defined =
false;
1886 idToken token, *firsttoken, *lasttoken;
1915 if (lasttoken) lasttoken->
next =
t;
1916 else firsttoken =
t;
1919 else if ( token ==
"defined" ) {
1923 if (lasttoken) lasttoken->
next =
t;
1924 else firsttoken =
t;
1941 if ( token[0] ==
'(' ) indent++;
1942 else if ( token[0] ==
')' ) indent--;
1948 if (lasttoken) lasttoken->
next =
t;
1949 else firsttoken =
t;
1963 Log_Write(
"$eval:");
1965 for (t = firsttoken;
t; t = nexttoken) {
1967 Log_Write(
" %s", t->
c_str());
1969 nexttoken = t->
next;
1973 if (integer) Log_Write(
"$eval result: %d", *intvalue);
1974 else Log_Write(
"$eval result: %f", *floatvalue);
1986 signed long int value;
1997 skip = (value == 0);
2008 signed long int value;
2014 skip = (value == 0);
2104 signed long int value;
2117 sprintf(buf,
"%d", abs(value));
2179 if ( token ==
"if" ) {
2182 else if ( token ==
"ifdef" ) {
2185 else if ( token ==
"ifndef" ) {
2188 else if ( token ==
"elif" ) {
2191 else if ( token ==
"else" ) {
2194 else if ( token ==
"endif" ) {
2204 if ( token ==
"include" ) {
2207 else if ( token ==
"define" ) {
2210 else if ( token ==
"undef" ) {
2213 else if ( token ==
"line" ) {
2216 else if ( token ==
"error" ) {
2219 else if ( token ==
"warning" ) {
2222 else if ( token ==
"pragma" ) {
2225 else if ( token ==
"eval" ) {
2228 else if ( token ==
"evalfloat" ) {
2243 signed long int value;
2256 sprintf( buf,
"%d", abs( value ) );
2288 sprintf( buf,
"%1.2f", fabs( value ) );
2292 token.
intvalue = (
unsigned long) fabs( value );
2322 if ( token ==
"evalint" ) {
2325 else if ( token ==
"evalfloat" ) {
2410 if ( token !=
string ) {
2430 if ( token->
type != type ) {
2435 case TT_NAME: str =
"name";
break;
2437 default: str =
"unknown type";
break;
2443 if ( (token->
subtype & subtype) != subtype ) {
2445 if ( subtype &
TT_DECIMAL ) str =
"decimal ";
2446 if ( subtype &
TT_HEX ) str =
"hex ";
2447 if ( subtype &
TT_OCTAL ) str =
"octal ";
2448 if ( subtype &
TT_BINARY ) str =
"binary ";
2450 if ( subtype &
TT_LONG ) str +=
"long ";
2451 if ( subtype &
TT_FLOAT ) str +=
"float ";
2452 if ( subtype &
TT_INTEGER ) str +=
"integer ";
2459 if ( subtype < 0 ) {
2463 if ( token->
subtype != subtype ) {
2498 if ( tok ==
string ) {
2518 if (tok.
type == type && (tok.
subtype & subtype) == subtype) {
2542 if ( tok ==
string ) {
2563 if ( tok.
type == type && ( tok.
subtype & subtype ) == subtype ) {
2579 if ( token ==
string ) {
2615 depth = parseFirstBrace ? 0 : 1;
2621 if( token ==
"{" ) {
2623 }
else if ( token ==
"}" ) {
2658 bool doTabs =
false;
2671 Error(
"missing closing brace" );
2682 if (token[0] ==
'}' && i > 0) {
2690 if ( token[0] ==
'{' ) {
2696 else if ( token[0] ==
'}' ) {
2705 out +=
"\"" + token +
"\"";
2841 for ( i = 0; i <
x; i++ ) {
2863 for ( i = 0; i <
y; i++ ) {
2887 for ( i = 0 ; i <
z; i++ ) {
2910 return whiteSpace.
Length();
3020 script =
new idLexer( filename, 0, OSPath );
3056 script =
new idLexer( ptr, length, name );
3109 if ( !keepDefines ) {
3146 return "unkown punctuation";
3197 this->flags =
flags;
3213 this->OSPath =
true;
3215 this->flags =
flags;
3234 this->flags =
flags;
void Error(const char *str,...) const id_attribute((format(printf
const char * whiteSpaceStart_p
static define_t * DefineFromString(const char *string)
GLsizei const GLfloat * value
ID_INLINE int PC_NameHash(const char *name)
int AddDefine(const char *string)
void void static int AddGlobalDefine(const char *string)
void UnreadToken(idToken *token)
int GetPunctuationId(const char *p)
int ExpandDefineIntoSource(idToken *deftoken, define_t *define)
const char * GetFileName(void)
int MergeTokens(idToken *t1, idToken *t2)
int Parse3DMatrix(int z, int y, int x, float *m)
int DollarDirective_evalfloat(void)
int SkipUntilString(const char *string)
int EvaluateTokens(idToken *tokens, signed long int *intvalue, double *floatvalue, int integer)
int UnreadSourceToken(idToken *token)
void StripTrailing(const char c)
int ReadDefineParms(define_t *define, idToken **parms, int maxparms)
define_t * CopyFirstDefine(void)
int ExpandDefine(idToken *deftoken, define_t *define, idToken **firsttoken, idToken **lasttoken)
float GetFloatValue(void)
GLint GLint GLsizei GLsizei GLsizei depth
GLenum GLsizei const GLvoid * string
int FindDefineParm(define_t *define, const char *name)
int LoadMemory(const char *ptr, int length, const char *name)
int DollarEvaluate(signed long int *intvalue, double *floatvalue, int integer)
void FreeSource(bool keepDefines=false)
GLuint GLuint GLsizei GLenum type
int ExpectTokenString(const char *string)
void SetIncludePath(const char *path)
#define AllocOperator(op)
void ClearTokenWhiteSpace(void)
const char * ParseBracedSection(idStr &out, int tabs=-1)
int Icmp(const char *text) const
#define P_PARENTHESESCLOSE
define_t * FindHashedDefine(define_t **definehash, const char *name)
int Directive_include(void)
struct operator_s operator_t
int ReadToken(idToken *token)
const int GetLineNum(void)
GLfloat GLfloat GLfloat v2
void Error(const char *str,...) id_attribute((format(printf
void GetStringFromMarker(idStr &out, bool clean=false)
int Directive_warning(void)
int Directive_pragma(void)
int PeekTokenString(const char *string)
int Directive_error(void)
int Parse1DMatrix(int x, float *m)
int Directive_ifndef(void)
define_t * CopyDefine(define_t *define)
static float Fabs(float f)
void SetPunctuations(const punctuation_t *p)
static int RemoveGlobalDefine(const char *name)
virtual void virtual void FatalError(const char *fmt,...) id_attribute((format(printf
void AddGlobalDefinesToSource(void)
const char * whiteSpaceEnd_p
static void PrintDefine(define_t *define)
void PopIndent(int *type, int *skip)
int GetPunctuationId(const char *p)
void void Warning(const char *str,...) const id_attribute((format(printf
const char * GetPunctuationFromId(int id)
static void FreeDefine(define_t *define)
const punctuation_t * punctuations
int Evaluate(signed long int *intvalue, double *floatvalue, int integer)
int Directive_undef(void)
virtual void Printf(const char *fmt,...) id_attribute((format(printf
int ExpectAnyToken(idToken *token)
const char * GetPunctuationFromId(int id)
const char * ParseBracedSectionExact(idStr &out, int tabs=-1)
void AddDefineToHash(define_t *define, define_t **definehash)
void SetPunctuations(const punctuation_t *p)
int WhiteSpaceBeforeToken(void) const
int ReadDollarDirective(void)
int Directive_if_def(int type)
int ReadSourceToken(idToken *token)
int ExpandBuiltinDefine(idToken *deftoken, define_t *define, idToken **firsttoken, idToken **lasttoken)
int DollarDirective_evalint(void)
static define_t * FindDefine(define_t *defines, const char *name)
struct define_s * hashnext
void void Warning(const char *str,...) id_attribute((format(printf
int Directive_define(void)
static void SetBaseFolder(const char *path)
void UnreadSignToken(void)
void PushIndent(int type, int skip)
void Append(const char a)
static void SetBaseFolder(const char *path)
int LoadFile(const char *filename, bool OSPath=false)
int GetLastWhiteSpace(idStr &whiteSpace) const
const char * ParseRestOfLine(idStr &out)
void PushScript(idLexer *script)
int PeekTokenType(int type, int subtype, idToken *token)
int CheckTokenString(const char *string)
GLsizei const GLcharARB const GLint * length
int SkipBracedSection(bool parseFirstBrace=true)
static define_t * globaldefines
int Directive_ifdef(void)
int vsprintf(idStr &string, const char *fmt, va_list argptr)
void * Mem_ClearedAlloc(const int size)
int ReadTokenOnLine(idToken *token)
const char * c_str(void) const
static void RemoveAllGlobalDefines(void)
void * Mem_Alloc(const int size)
#define P_PARENTHESESOPEN
void AddBuiltinDefines(void)
const char * ParseBracedSectionExact(idStr &out, int tabs=-1)
int sprintf(idStr &string, const char *fmt,...)
int StringizeTokens(idToken *tokens, idToken *token)
int Parse2DMatrix(int y, int x, float *m)
idStr & StripFilename(void)
int PC_OperatorPriority(int op)
int ReadLine(idToken *token)
#define TOKEN_FL_RECURSIVE_DEFINE
int CheckTokenType(int type, int subtype, idToken *token)
int LoadFile(const char *filename, bool OSPath=false)
static class idCommon * common
int ExpectTokenType(int type, int subtype, idToken *token)
int Directive_endif(void)
int Directive_evalfloat(void)
int GetLastWhiteSpace(idStr &whiteSpace) const