home *** CD-ROM | disk | FTP | other *** search
/ GameStar 2006 March / Gamestar_82_2006-03_dvd.iso / DVDStar / Editace / quake4_sdkv10.exe / source / idlib / Parser.cpp < prev    next >
C/C++ Source or Header  |  2005-11-14  |  72KB  |  3,221 lines

  1.  
  2. #include "precompiled.h"
  3. #pragma hdrstop
  4.  
  5. //#define DEBUG_EVAL
  6. #define MAX_DEFINEPARMS                128
  7. #define DEFINEHASHSIZE                2048
  8.  
  9. #define TOKEN_FL_RECURSIVE_DEFINE    1
  10.  
  11. define_t * idParser::globaldefines;
  12.  
  13. /*
  14. ================
  15. idParser::SetBaseFolder
  16. ================
  17. */
  18. void idParser::SetBaseFolder( const char *path) {
  19. // RAVEN BEGIN
  20. // jsinger: changed to be Lexer instead of idLexer so that we have the ability to read binary files
  21.     Lexer::SetBaseFolder(path);
  22. // RAVEN END
  23. }
  24.  
  25. /*
  26. ================
  27. idParser::AddGlobalDefine
  28. ================
  29. */
  30. int idParser::AddGlobalDefine( const char *string ) {
  31.     define_t *define;
  32.  
  33.     define = idParser::DefineFromString(string);
  34.     if (!define) {
  35.         return false;
  36.     }
  37.     define->next = globaldefines;
  38.     globaldefines = define;
  39.     return true;
  40. }
  41.  
  42. /*
  43. ================
  44. idParser::RemoveGlobalDefine
  45. ================
  46. */
  47. int idParser::RemoveGlobalDefine( const char *name ) {
  48.     define_t *d, *prev;
  49.  
  50.     for ( prev = NULL, d = idParser::globaldefines; d; prev = d, d = d->next ) {
  51.         if ( !idStr::Cmp( d->name, name ) ) {
  52.             break;
  53.         }
  54.     }
  55.     if ( d ) {
  56.         if ( prev ) {
  57.             prev->next = d->next;
  58.         }
  59.         else {
  60.             idParser::globaldefines = d->next;
  61.         }
  62.         idParser::FreeDefine( d );
  63.         return true;
  64.     }
  65.     return false;
  66. }
  67.  
  68. /*
  69. ================
  70. idParser::RemoveAllGlobalDefines
  71. ================
  72. */
  73. void idParser::RemoveAllGlobalDefines( void ) {
  74.     define_t *define;
  75.  
  76.     for ( define = globaldefines; define; define = globaldefines ) {
  77.         globaldefines = globaldefines->next;
  78.         idParser::FreeDefine(define);
  79.     }
  80. }
  81.  
  82.  
  83. /*
  84. ===============================================================================
  85.  
  86. idParser
  87.  
  88. ===============================================================================
  89. */
  90.  
  91. /*
  92. ================
  93. idParser::PrintDefine
  94. ================
  95. */
  96. void idParser::PrintDefine( define_t *define ) {
  97.     idLib::common->Printf("define->name = %s\n", define->name);
  98.     idLib::common->Printf("define->flags = %d\n", define->flags);
  99.     idLib::common->Printf("define->builtin = %d\n", define->builtin);
  100.     idLib::common->Printf("define->numparms = %d\n", define->numparms);
  101. }
  102.  
  103. /*
  104. ================
  105. PC_PrintDefineHashTable
  106. ================
  107. * /
  108. static void PC_PrintDefineHashTable(define_t **definehash) {
  109.     int i;
  110.     define_t *d;
  111.  
  112.     for (i = 0; i < DEFINEHASHSIZE; i++) {
  113.         Log_Write("%4d:", i);
  114.         for (d = definehash[i]; d; d = d->hashnext) {
  115.             Log_Write(" %s", d->name);
  116.         }
  117.         Log_Write("\n");
  118.     }
  119. }
  120. */
  121.  
  122. /*
  123. ================
  124. PC_NameHash
  125. ================
  126. */
  127. ID_INLINE int PC_NameHash( const char *name ) {
  128.     int hash, i;
  129.  
  130.     hash = 0;
  131.     for ( i = 0; name[i] != '\0'; i++ ) {
  132.         hash += name[i] * (119 + i);
  133.     }
  134.     hash = (hash ^ (hash >> 10) ^ (hash >> 20)) & (DEFINEHASHSIZE-1);
  135.     return hash;
  136. }
  137.  
  138. /*
  139. ================
  140. idParser::AddDefineToHash
  141. ================
  142. */
  143. void idParser::AddDefineToHash( define_t *define, define_t **definehash ) {
  144.     int hash;
  145.  
  146.     hash = PC_NameHash(define->name);
  147.     define->hashnext = definehash[hash];
  148.     definehash[hash] = define;
  149. }
  150.  
  151. /*
  152. ================
  153. FindHashedDefine
  154. ================
  155. */
  156. define_t *idParser::FindHashedDefine( define_t **definehash, const char *name ) {
  157.     define_t *d;
  158.     int hash;
  159.  
  160.     hash = PC_NameHash(name);
  161.     for ( d = definehash[hash]; d; d = d->hashnext ) {
  162.         if ( !idStr::Cmp(d->name, name) ) {
  163.             return d;
  164.         }
  165.     }
  166.     return NULL;
  167. }
  168.  
  169. /*
  170. ================
  171. idParser::FindDefine
  172. ================
  173. */
  174. define_t *idParser::FindDefine( define_t *defines, const char *name ) {
  175.     define_t *d;
  176.  
  177.     for ( d = defines; d; d = d->next ) {
  178.         if ( !idStr::Cmp(d->name, name) ) {
  179.             return d;
  180.         }
  181.     }
  182.     return NULL;
  183. }
  184.  
  185. /*
  186. ================
  187. idParser::FindDefineParm
  188. ================
  189. */
  190. int idParser::FindDefineParm( define_t *define, const char *name ) {
  191.     idToken *p;
  192.     int i;
  193.  
  194.     i = 0;
  195.     for ( p = define->parms; p; p = p->next ) {
  196.         if ( (*p) == name ) {
  197.             return i;
  198.         }
  199.         i++;
  200.     }
  201.     return -1;
  202. }
  203.  
  204. /*
  205. ================
  206. idParser::CopyDefine
  207. ================
  208. */
  209. define_t *idParser::CopyDefine( define_t *define ) {
  210.     define_t *newdefine;
  211.     idToken *token, *newtoken, *lasttoken;
  212. //RAVEN BEGIN
  213. //amccarthy: Added memory allocation tag
  214.     newdefine = (define_t *) Mem_Alloc(sizeof(define_t) + strlen(define->name) + 1, MA_PARSER);
  215. //RAVEN END
  216.     //copy the define name
  217.     newdefine->name = (char *) newdefine + sizeof(define_t);
  218.     strcpy(newdefine->name, define->name);
  219.     newdefine->flags = define->flags;
  220.     newdefine->builtin = define->builtin;
  221.     newdefine->numparms = define->numparms;
  222.     //the define is not linked
  223.     newdefine->next = NULL;
  224.     newdefine->hashnext = NULL;
  225.     //copy the define tokens
  226.     newdefine->tokens = NULL;
  227.     for (lasttoken = NULL, token = define->tokens; token; token = token->next) {
  228.         newtoken = new idToken(token);
  229.         newtoken->next = NULL;
  230.         if (lasttoken) lasttoken->next = newtoken;
  231.         else newdefine->tokens = newtoken;
  232.         lasttoken = newtoken;
  233.     }
  234.     //copy the define parameters
  235.     newdefine->parms = NULL;
  236.     for (lasttoken = NULL, token = define->parms; token; token = token->next) {
  237.         newtoken = new idToken(token);
  238.         newtoken->next = NULL;
  239.         if (lasttoken) lasttoken->next = newtoken;
  240.         else newdefine->parms = newtoken;
  241.         lasttoken = newtoken;
  242.     }
  243.     return newdefine;
  244. }
  245.  
  246. /*
  247. ================
  248. idParser::FreeDefine
  249. ================
  250. */
  251. void idParser::FreeDefine( define_t *define ) {
  252.     idToken *t, *next;
  253.  
  254.     //free the define parameters
  255.     for (t = define->parms; t; t = next) {
  256.         next = t->next;
  257.         delete t;
  258.     }
  259.     //free the define tokens
  260.     for (t = define->tokens; t; t = next) {
  261.         next = t->next;
  262.         delete t;
  263.     }
  264.     //free the define
  265.     Mem_Free( define );
  266. }
  267.  
  268. /*
  269. ================
  270. idParser::DefineFromString
  271. ================
  272. */
  273. define_t *idParser::DefineFromString( const char *string ) {
  274.     idParser src;
  275.     define_t *def;
  276.  
  277.     if ( !src.LoadMemory(string, strlen(string), "*defineString") ) {
  278.         return NULL;
  279.     }
  280.     // create a define from the source
  281.     if ( !src.Directive_define() ) {
  282.         src.FreeSource();
  283.         return NULL;
  284.     }
  285.     def = src.CopyFirstDefine();
  286.     src.FreeSource();
  287.     //if the define was created succesfully
  288.     return def;
  289. }
  290.  
  291. /*
  292. ================
  293. idParser::Error
  294. ================
  295. */
  296. void idParser::Error( const char *str, ... ) const {
  297.     char text[MAX_STRING_CHARS];
  298.     va_list ap;
  299.  
  300.     va_start(ap, str);
  301.     vsprintf(text, str, ap);
  302.     va_end(ap);
  303.     if ( idParser::scriptstack ) {
  304.         idParser::scriptstack->Error( text );
  305.     }
  306. }
  307.  
  308. /*
  309. ================
  310. idParser::Warning
  311. ================
  312. */
  313. void idParser::Warning( const char *str, ... ) const {
  314.     char text[MAX_STRING_CHARS];
  315.     va_list ap;
  316.  
  317.     va_start(ap, str);
  318.     vsprintf(text, str, ap);
  319.     va_end(ap);
  320.     if ( idParser::scriptstack ) {
  321.         idParser::scriptstack->Warning( text );
  322.     }
  323. }
  324.  
  325. /*
  326. ================
  327. idParser::PushIndent
  328. ================
  329. */
  330. void idParser::PushIndent( int type, int skip ) {
  331.     indent_t *indent;
  332.  
  333. //RAVEN BEGIN
  334. //amccarthy: Added memory allocation tag
  335.     indent = (indent_t *) Mem_Alloc(sizeof(indent_t),MA_PARSER);
  336. //RAVEN END
  337.     indent->type = type;
  338.     indent->script = idParser::scriptstack;
  339.     indent->skip = (skip != 0);
  340.     idParser::skip += indent->skip;
  341.     indent->next = idParser::indentstack;
  342.     idParser::indentstack = indent;
  343. }
  344.  
  345. /*
  346. ================
  347. idParser::PopIndent
  348. ================
  349. */
  350. void idParser::PopIndent( int *type, int *skip ) {
  351.     indent_t *indent;
  352.  
  353.     *type = 0;
  354.     *skip = 0;
  355.  
  356.     indent = idParser::indentstack;
  357.     if (!indent) return;
  358.  
  359.     // must be an indent from the current script
  360.     if (idParser::indentstack->script != idParser::scriptstack) {
  361.         return;
  362.     }
  363.  
  364.     *type = indent->type;
  365.     *skip = indent->skip;
  366.     idParser::indentstack = idParser::indentstack->next;
  367.     idParser::skip -= indent->skip;
  368.     Mem_Free( indent );
  369. }
  370.  
  371. /*
  372. ================
  373. idParser::PushScript
  374. ================
  375. */
  376. void idParser::PushScript( idLexer *script ) {
  377.     idLexer *s;
  378.  
  379.     for ( s = idParser::scriptstack; s; s = s->next ) {
  380.         if ( !idStr::Icmp(s->GetFileName(), script->GetFileName()) ) {
  381.             idParser::Warning( "'%s' recursively included", script->GetFileName() );
  382.             return;
  383.         }
  384.     }
  385.     //push the script on the script stack
  386.     script->next = idParser::scriptstack;
  387.     idParser::scriptstack = script;
  388. }
  389.  
  390. /*
  391. ================
  392. idParser::ReadSourceToken
  393. ================
  394. */
  395. int idParser::ReadSourceToken( idToken *token ) {
  396.     idToken *t;
  397.     idLexer *script;
  398.     int type, skip, changedScript;
  399.  
  400.     if ( !idParser::scriptstack ) {
  401.         idLib::common->FatalError( "idParser::ReadSourceToken: not loaded" );
  402.         return false;
  403.     }
  404.     changedScript = 0;
  405.     // if there's no token already available
  406.     while( !idParser::tokens ) {
  407.         // if there's a token to read from the script
  408.         if ( idParser::scriptstack->ReadToken( token ) ) {
  409.             token->linesCrossed += changedScript;
  410.  
  411.             // set the marker based on the start of the token read in
  412.             if ( !marker_p ) {
  413.                 marker_p = token->whiteSpaceEnd_p;
  414.             }
  415.             return true;
  416.         }
  417.         // if at the end of the script
  418.         if ( idParser::scriptstack->EndOfFile() ) {
  419.             // remove all indents of the script
  420.             while( idParser::indentstack && idParser::indentstack->script == idParser::scriptstack ) {
  421.                 idParser::Warning( "missing #endif" );
  422.                 idParser::PopIndent( &type, &skip );
  423.             }
  424.             changedScript = 1;
  425.         }
  426.         // if this was the initial script
  427.         if ( !idParser::scriptstack->next ) {
  428.             return false;
  429.         }
  430.         // remove the script and return to the previous one
  431.         script = idParser::scriptstack;
  432.         idParser::scriptstack = idParser::scriptstack->next;
  433.         delete script;
  434.     }
  435.     // copy the already available token
  436.     *token = idParser::tokens;
  437.     // remove the token from the source
  438.     t = idParser::tokens;
  439.     idParser::tokens = idParser::tokens->next;
  440.     delete t;
  441.     return true;
  442. }
  443.  
  444. /*
  445. ================
  446. idParser::UnreadSourceToken
  447. ================
  448. */
  449. int idParser::UnreadSourceToken( idToken *token ) {
  450.     idToken *t;
  451.  
  452.     t = new idToken(token);
  453.     t->next = idParser::tokens;
  454.     idParser::tokens = t;
  455.     return true;
  456. }
  457.  
  458. /*
  459. ================
  460. idParser::ReadDefineParms
  461. ================
  462. */
  463. int idParser::ReadDefineParms( define_t *define, idToken **parms, int maxparms ) {
  464.     define_t *newdefine;
  465.     idToken token, *t, *last;
  466.     int i, done, lastcomma, numparms, indent;
  467.  
  468.     if ( !idParser::ReadSourceToken( &token ) ) {
  469.         idParser::Error( "define '%s' missing parameters", define->name );
  470.         return false;
  471.     }
  472.  
  473.     if ( define->numparms > maxparms ) {
  474.         idParser::Error( "define with more than %d parameters", maxparms );
  475.         return false;
  476.     }
  477.  
  478.     for ( i = 0; i < define->numparms; i++ ) {
  479.         parms[i] = NULL;
  480.     }
  481.     // if no leading "("
  482.     if ( token != "(" ) {
  483.         idParser::UnreadSourceToken( &token );
  484.         idParser::Error( "define '%s' missing parameters", define->name );
  485.         return false;
  486.     }
  487.     // read the define parameters
  488.     for ( done = 0, numparms = 0, indent = 1; !done; ) {
  489.         if ( numparms >= maxparms ) {
  490.             idParser::Error( "define '%s' with too many parameters", define->name );
  491.             return false;
  492.         }
  493.         parms[numparms] = NULL;
  494.         lastcomma = 1;
  495.         last = NULL;
  496.         while( !done ) {
  497.  
  498.             if ( !idParser::ReadSourceToken( &token ) ) {
  499.                 idParser::Error( "define '%s' incomplete", define->name );
  500.                 return false;
  501.             }
  502.  
  503.             if ( token == "," ) {
  504.                 if ( indent <= 1 ) {
  505.                     if ( lastcomma ) {
  506.                         idParser::Warning( "too many comma's" );
  507.                     }
  508.                     if ( numparms >= define->numparms ) {
  509.                         idParser::Warning( "too many define parameters" );
  510.                     }
  511.                     lastcomma = 1;
  512.                     break;
  513.                 }
  514.             }
  515.             else if ( token == "(" ) {
  516.                 indent++;
  517.             }
  518.             else if ( token == ")" ) {
  519.                 indent--;
  520.                 if ( indent <= 0 ) {
  521.                     if ( !parms[define->numparms-1] ) {
  522.                         idParser::Warning( "too few define parameters" );
  523.                     }
  524.                     done = 1;
  525.                     break;
  526.                 }
  527.             }
  528.             else if ( token.type == TT_NAME ) {
  529.                 newdefine = FindHashedDefine( idParser::definehash, token.c_str() );
  530.                 if ( newdefine ) {
  531.                     if ( !idParser::ExpandDefineIntoSource( &token, newdefine ) ) {
  532.                         return false;
  533.                     }
  534.                     continue;
  535.                 }
  536.             }
  537.  
  538.             lastcomma = 0;
  539.  
  540.             if ( numparms < define->numparms ) {
  541.  
  542.                 t = new idToken( token );
  543.                 t->next = NULL;
  544.                 if (last) last->next = t;
  545.                 else parms[numparms] = t;
  546.                 last = t;
  547.             }
  548.         }
  549.         numparms++;
  550.     }
  551.     return true;
  552. }
  553.  
  554. /*
  555. ================
  556. idParser::StringizeTokens
  557. ================
  558. */
  559. int idParser::StringizeTokens( idToken *tokens, idToken *token ) {
  560.     idToken *t;
  561.  
  562.     token->type = TT_STRING;
  563.     token->whiteSpaceStart_p = NULL;
  564.     token->whiteSpaceEnd_p = NULL;
  565.     (*token) = "";
  566.     for ( t = tokens; t; t = t->next ) {
  567.         token->Append( t->c_str() );
  568.     }
  569.     return true;
  570. }
  571.  
  572. /*
  573. ================
  574. idParser::MergeTokens
  575. ================
  576. */
  577. int idParser::MergeTokens( idToken *t1, idToken *t2 ) {
  578.     // merging of a name with a name or number
  579.     if ( t1->type == TT_NAME && (t2->type == TT_NAME || (t2->type == TT_NUMBER && !(t2->subtype & TT_FLOAT))) ) {
  580.         t1->Append( t2->c_str() );
  581.         return true;
  582.     }
  583.     // merging of two strings
  584.     if (t1->type == TT_STRING && t2->type == TT_STRING) {
  585.         t1->Append( t2->c_str() );
  586.         return true;
  587.     }
  588.     // merging of two numbers
  589.     if ( t1->type == TT_NUMBER && t2->type == TT_NUMBER &&
  590.             !(t1->subtype & (TT_HEX|TT_BINARY)) && !(t2->subtype & (TT_HEX|TT_BINARY)) &&
  591.             (!(t1->subtype & TT_FLOAT) || !(t2->subtype & TT_FLOAT)) ) {
  592.         t1->Append( t2->c_str() );
  593.         return true;
  594.     }
  595.  
  596.     return false;
  597. }
  598.  
  599. /*
  600. ================
  601. idParser::AddBuiltinDefines
  602. ================
  603. */
  604. void idParser::AddBuiltinDefines( void ) {
  605.     int i;
  606.     define_t *define;
  607.     struct builtin
  608.     {
  609.         char *string;
  610.         int id;
  611.     } builtin[] = {
  612.         { "__LINE__",    BUILTIN_LINE }, 
  613.         { "__FILE__",    BUILTIN_FILE },
  614.         { "__DATE__",    BUILTIN_DATE },
  615.         { "__TIME__",    BUILTIN_TIME },
  616.         { "__STDC__", BUILTIN_STDC },
  617.         { NULL, 0 }
  618.     };
  619.  
  620.     for (i = 0; builtin[i].string; i++) {
  621. //RAVEN BEGIN
  622. //amccarthy: Added memory allocation tag
  623.         define = (define_t *) Mem_Alloc(sizeof(define_t) + strlen(builtin[i].string) + 1,MA_PARSER);
  624. //RAVEN END
  625.         define->name = (char *) define + sizeof(define_t);
  626.         strcpy(define->name, builtin[i].string);
  627.         define->flags = DEFINE_FIXED;
  628.         define->builtin = builtin[i].id;
  629.         define->numparms = 0;
  630.         define->parms = NULL;
  631.         define->tokens = NULL;
  632.         // add the define to the source
  633.         AddDefineToHash(define, idParser::definehash);
  634.     }
  635. }
  636.  
  637. /*
  638. ================
  639. idParser::CopyFirstDefine
  640. ================
  641. */
  642. define_t *idParser::CopyFirstDefine( void ) {
  643.     int i;
  644.  
  645.     for ( i = 0; i < DEFINEHASHSIZE; i++ ) {
  646.         if ( idParser::definehash[i] ) {
  647.             return CopyDefine(idParser::definehash[i]);
  648.         }
  649.     }
  650.     return NULL;
  651. }
  652.  
  653. /*
  654. ================
  655. idParser::ExpandBuiltinDefine
  656. ================
  657. */
  658. int idParser::ExpandBuiltinDefine( idToken *deftoken, define_t *define, idToken **firsttoken, idToken **lasttoken ) {
  659.     idToken *token;
  660.     time_t t;
  661.     char *curtime;
  662.     char buf[MAX_STRING_CHARS];
  663.  
  664.     token = new idToken(deftoken);
  665.     switch( define->builtin ) {
  666.         case BUILTIN_LINE: {
  667.             sprintf( buf, "%d", deftoken->line );
  668.             (*token) = buf;
  669.             token->intvalue = deftoken->line;
  670.             token->floatvalue = deftoken->line;
  671.             token->type = TT_NUMBER;
  672.             token->subtype = TT_DECIMAL | TT_INTEGER | TT_VALUESVALID;
  673.             token->line = deftoken->line;
  674.             token->linesCrossed = deftoken->linesCrossed;
  675.             token->flags = 0;
  676.             *firsttoken = token;
  677.             *lasttoken = token;
  678.             break;
  679.         }
  680.         case BUILTIN_FILE: {
  681.             (*token) = idParser::scriptstack->GetFileName();
  682.             token->type = TT_NAME;
  683.             token->subtype = token->Length();
  684.             token->line = deftoken->line;
  685.             token->linesCrossed = deftoken->linesCrossed;
  686.             token->flags = 0;
  687.             *firsttoken = token;
  688.             *lasttoken = token;
  689.             break;
  690.         }
  691.         case BUILTIN_DATE: {
  692.             t = time(NULL);
  693.             curtime = ctime(&t);
  694.             (*token) = "\"";
  695.             token->Append( curtime+4 );
  696.             token[7] = '\0';
  697.             token->Append( curtime+20 );
  698.             token[10] = '\0';
  699.             token->Append( "\"" );
  700.             free(curtime);
  701.             token->type = TT_STRING;
  702.             token->subtype = token->Length();
  703.             token->line = deftoken->line;
  704.             token->linesCrossed = deftoken->linesCrossed;
  705.             token->flags = 0;
  706.             *firsttoken = token;
  707.             *lasttoken = token;
  708.             break;
  709.         }
  710.         case BUILTIN_TIME: {
  711.             t = time(NULL);
  712.             curtime = ctime(&t);
  713.             (*token) = "\"";
  714.             token->Append( curtime+11 );
  715.             token[8] = '\0';
  716.             token->Append( "\"" );
  717.             free(curtime);
  718.             token->type = TT_STRING;
  719.             token->subtype = token->Length();
  720.             token->line = deftoken->line;
  721.             token->linesCrossed = deftoken->linesCrossed;
  722.             token->flags = 0;
  723.             *firsttoken = token;
  724.             *lasttoken = token;
  725.             break;
  726.         }
  727.         case BUILTIN_STDC: {
  728.             idParser::Warning( "__STDC__ not supported\n" );
  729.             *firsttoken = NULL;
  730.             *lasttoken = NULL;
  731.             break;
  732.         }
  733.         default: {
  734.             *firsttoken = NULL;
  735.             *lasttoken = NULL;
  736.             break;
  737.         }
  738.     }
  739.     return true;
  740. }
  741.  
  742. /*
  743. ================
  744. idParser::ExpandDefine
  745. ================
  746. */
  747. int idParser::ExpandDefine( idToken *deftoken, define_t *define, idToken **firsttoken, idToken **lasttoken ) {
  748.     idToken *parms[MAX_DEFINEPARMS], *dt, *pt, *t;
  749.     idToken *t1, *t2, *first, *last, *nextpt, token;
  750.     int parmnum, i;
  751.  
  752.     // if it is a builtin define
  753.     if ( define->builtin ) {
  754.         return idParser::ExpandBuiltinDefine( deftoken, define, firsttoken, lasttoken );
  755.     }
  756.     // if the define has parameters
  757.     if ( define->numparms ) {
  758.         if ( !idParser::ReadDefineParms( define, parms, MAX_DEFINEPARMS ) ) {
  759.             return false;
  760.         }
  761. #ifdef DEBUG_EVAL
  762.         for ( i = 0; i < define->numparms; i++ ) {
  763.             Log_Write("define parms %d:", i);
  764.             for ( pt = parms[i]; pt; pt = pt->next ) {
  765.                 Log_Write( "%s", pt->c_str() );
  766.             }
  767.         }
  768. #endif //DEBUG_EVAL
  769.     }
  770.     // empty list at first
  771.     first = NULL;
  772.     last = NULL;
  773.     // create a list with tokens of the expanded define
  774.     for ( dt = define->tokens; dt; dt = dt->next ) {
  775.         parmnum = -1;
  776.         // if the token is a name, it could be a define parameter
  777.         if ( dt->type == TT_NAME ) {
  778.             parmnum = FindDefineParm( define, dt->c_str() );
  779.         }
  780.         // if it is a define parameter
  781.         if ( parmnum >= 0 ) {
  782.             for ( pt = parms[parmnum]; pt; pt = pt->next ) {
  783.                 t = new idToken(pt);
  784.                 //add the token to the list
  785.                 t->next = NULL;
  786.                 if (last) last->next = t;
  787.                 else first = t;
  788.                 last = t;
  789.             }
  790.         }
  791.         else {
  792.             // if stringizing operator
  793.             if ( (*dt) == "#" ) {
  794.                 // the stringizing operator must be followed by a define parameter
  795.                 if ( dt->next ) {
  796.                     parmnum = FindDefineParm( define, dt->next->c_str() );
  797.                 }
  798.                 else {
  799.                     parmnum = -1;
  800.                 }
  801.  
  802.                 if ( parmnum >= 0 ) {
  803.                     // step over the stringizing operator
  804.                     dt = dt->next;
  805.                     // stringize the define parameter tokens
  806.                     if ( !idParser::StringizeTokens( parms[parmnum], &token ) ) {
  807.                         idParser::Error( "can't stringize tokens" );
  808.                         return false;
  809.                     }
  810.                     t = new idToken(token);
  811.                     t->line = deftoken->line;
  812.                 }
  813.                 else {
  814.                     idParser::Warning( "stringizing operator without define parameter" );
  815.                     continue;
  816.                 }
  817.             }
  818.             else {
  819.                 t = new idToken(dt);
  820.                 t->line = deftoken->line;
  821.             }
  822.             // add the token to the list
  823.             t->next = NULL;
  824. // the token being read from the define list should use the line number of
  825. // the original file, not the header file            
  826.             t->line = deftoken->line;
  827.  
  828.             if ( last ) last->next = t;
  829.             else first = t;
  830.             last = t;
  831.         }
  832.     }
  833.     // check for the merging operator
  834.     for ( t = first; t; ) {
  835.         if ( t->next ) {
  836.             // if the merging operator
  837.             if ( (*t->next) == "##" ) {
  838.                 t1 = t;
  839.                 t2 = t->next->next;
  840.                 if ( t2 ) {
  841.                     if ( !idParser::MergeTokens( t1, t2 ) ) {
  842.                         idParser::Error( "can't merge '%s' with '%s'", t1->c_str(), t2->c_str() );
  843.                         return false;
  844.                     }
  845.                     delete t1->next;
  846.                     t1->next = t2->next;
  847.                     if ( t2 == last ) last = t1;
  848.                     delete t2;
  849.                     continue;
  850.                 }
  851.             }
  852.         }
  853.         t = t->next;
  854.     }
  855.     // store the first and last token of the list
  856.     *firsttoken = first;
  857.     *lasttoken = last;
  858.     // free all the parameter tokens
  859.     for ( i = 0; i < define->numparms; i++ ) {
  860.         for ( pt = parms[i]; pt; pt = nextpt ) {
  861.             nextpt = pt->next;
  862.             delete pt;
  863.         }
  864.     }
  865.  
  866.     return true;
  867. }
  868.  
  869. /*
  870. ================
  871. idParser::ExpandDefineIntoSource
  872. ================
  873. */
  874. int idParser::ExpandDefineIntoSource( idToken *deftoken, define_t *define ) {
  875.     idToken *firsttoken, *lasttoken;
  876.  
  877.     if ( !idParser::ExpandDefine( deftoken, define, &firsttoken, &lasttoken ) ) {
  878.         return false;
  879.     }
  880.     // if the define is not empty
  881.     if ( firsttoken && lasttoken ) {
  882.         firsttoken->linesCrossed += deftoken->linesCrossed;
  883.         lasttoken->next = idParser::tokens;
  884.         idParser::tokens = firsttoken;
  885.     }
  886.     return true;
  887. }
  888.  
  889. /*
  890. ================
  891. idParser::ReadLine
  892.  
  893. reads a token from the current line, continues reading on the next
  894. line only if a backslash '\' is found
  895. ================
  896. */
  897. int idParser::ReadLine( idToken *token ) {
  898.     int crossline;
  899.  
  900.     crossline = 0;
  901.     do {
  902.         if (!idParser::ReadSourceToken( token )) {
  903.             return false;
  904.         }
  905.         
  906.         if (token->linesCrossed > crossline) {
  907.             idParser::UnreadSourceToken( token );
  908.             return false;
  909.         }
  910.         crossline = 1;
  911.     } while( (*token) == "\\" );
  912.     return true;
  913. }
  914.  
  915. /*
  916. ================
  917. idParser::Directive_include
  918. ================
  919. */
  920. int idParser::Directive_include( void ) {
  921.     idLexer *script;
  922.     idToken token;
  923.     idStr path;
  924.  
  925.     if ( !idParser::ReadSourceToken( &token ) ) {
  926.         idParser::Error( "#include without file name" );
  927.         return false;
  928.     }
  929.     if ( token.linesCrossed > 0 ) {
  930.         idParser::Error( "#include without file name" );
  931.         return false;
  932.     }
  933.     if ( token.type == TT_STRING ) {
  934.         script = new idLexer;
  935.         // try relative to the current file
  936.         path = scriptstack->GetFileName();
  937.         path.StripFilename();
  938. // RAVEN BEGIN
  939. // jscott: avoid the leading slash
  940.         if( path.Length() )
  941.         {
  942.             path += "/";
  943.         }
  944. // RAVEN END
  945.         path += token;
  946.         if ( !script->LoadFile( path, OSPath ) ) {
  947.             // try absolute path
  948.             path = token;
  949.             if ( !script->LoadFile( path, OSPath ) ) {
  950.                 // try from the include path
  951.                 path = includepath + token;
  952.                 if ( !script->LoadFile( path, OSPath ) ) {
  953.                     delete script;
  954.                     script = NULL;
  955.                 }
  956.             }
  957.         }
  958.     }
  959.     else if ( token.type == TT_PUNCTUATION && token == "<" ) {
  960.         path = idParser::includepath;
  961.         while( idParser::ReadSourceToken( &token ) ) {
  962.             if ( token.linesCrossed > 0 ) {
  963.                 idParser::UnreadSourceToken( &token );
  964.                 break;
  965.             }
  966.             if ( token.type == TT_PUNCTUATION && token == ">" ) {
  967.                 break;
  968.             }
  969.             path += token;
  970.         }
  971.         if ( token != ">" ) {
  972.             idParser::Warning( "#include missing trailing >" );
  973.         }
  974.         if ( !path.Length() ) {
  975.             idParser::Error( "#include without file name between < >" );
  976.             return false;
  977.         }
  978.         if ( idParser::flags & LEXFL_NOBASEINCLUDES ) {
  979.             return true;
  980.         }
  981.         script = new idLexer;
  982.         if ( !script->LoadFile( includepath + path, OSPath ) ) {
  983.             delete script;
  984.             script = NULL;
  985.         }
  986.     }
  987.     else {
  988.         idParser::Error( "#include without file name" );
  989.         return false;
  990.     }
  991.     if (!script) {
  992.         idParser::Error( "file '%s' not found", path.c_str() );
  993.         return false;
  994.     }
  995.     script->SetFlags( idParser::flags );
  996.     script->SetPunctuations( idParser::punctuations );
  997.     idParser::PushScript( script );
  998.     return true;
  999. }
  1000.  
  1001. /*
  1002. ================
  1003. idParser::Directive_undef
  1004. ================
  1005. */
  1006. int idParser::Directive_undef( void ) {
  1007.     idToken token;
  1008.     define_t *define, *lastdefine;
  1009.     int hash;
  1010.  
  1011.     //
  1012.     if (!idParser::ReadLine( &token )) {
  1013.         idParser::Error( "undef without name" );
  1014.         return false;
  1015.     }
  1016.     if (token.type != TT_NAME) {
  1017.         idParser::UnreadSourceToken( &token );
  1018.         idParser::Error( "expected name but found '%s'", token.c_str() );
  1019.         return false;
  1020.     }
  1021.  
  1022.     hash = PC_NameHash( token.c_str() );
  1023.     for (lastdefine = NULL, define = idParser::definehash[hash]; define; define = define->hashnext) {
  1024.         if (!idStr::Cmp(define->name, token.c_str()))
  1025.         {
  1026.             if (define->flags & DEFINE_FIXED) {
  1027.                 idParser::Warning( "can't undef '%s'", token.c_str() );
  1028.             }
  1029.             else {
  1030.                 if (lastdefine) {
  1031.                     lastdefine->hashnext = define->hashnext;
  1032.                 }
  1033.                 else {
  1034.                     idParser::definehash[hash] = define->hashnext;
  1035.                 }
  1036.                 FreeDefine(define);
  1037.             }
  1038.             break;
  1039.         }
  1040.         lastdefine = define;
  1041.     }
  1042.     return true;
  1043. }
  1044.  
  1045. /*
  1046. ================
  1047. idParser::Directive_define
  1048. ================
  1049. */
  1050. int idParser::Directive_define( void ) {
  1051.     idToken token, *t, *last;
  1052.     define_t *define;
  1053.  
  1054.     if (!idParser::ReadLine( &token )) {
  1055.         idParser::Error( "#define without name" );
  1056.         return false;
  1057.     }
  1058.     if (token.type != TT_NAME) {
  1059.         idParser::UnreadSourceToken( &token );
  1060.         idParser::Error( "expected name after #define, found '%s'", token.c_str() );
  1061.         return false;
  1062.     }
  1063.     // check if the define already exists
  1064.     define = FindHashedDefine(idParser::definehash, token.c_str());
  1065.     if (define) {
  1066.         if (define->flags & DEFINE_FIXED) {
  1067.             idParser::Error( "can't redefine '%s'", token.c_str() );
  1068.             return false;
  1069.         }
  1070.         idParser::Warning( "redefinition of '%s'", token.c_str() );
  1071.         // unread the define name before executing the #undef directive
  1072.         idParser::UnreadSourceToken( &token );
  1073.         if (!idParser::Directive_undef())
  1074.             return false;
  1075.         // if the define was not removed (define->flags & DEFINE_FIXED)
  1076.         define = FindHashedDefine(idParser::definehash, token.c_str());
  1077.     }
  1078.     // allocate define
  1079. //RAVEN BEGIN
  1080. //amccarthy: Added memory allocation tag
  1081.     define = (define_t *) Mem_ClearedAlloc(sizeof(define_t) + token.Length() + 1, MA_PARSER);
  1082. //RAVEN END
  1083.     define->name = (char *) define + sizeof(define_t);
  1084.     strcpy(define->name, token.c_str());
  1085.     // add the define to the source
  1086.     AddDefineToHash(define, idParser::definehash);
  1087.     // if nothing is defined, just return
  1088.     if ( !idParser::ReadLine( &token ) ) {
  1089.         return true;
  1090.     }
  1091.     // if it is a define with parameters
  1092.     if ( token.WhiteSpaceBeforeToken() == 0 && token == "(" ) {
  1093.         // read the define parameters
  1094.         last = NULL;
  1095.         if ( !idParser::CheckTokenString(")") ) {
  1096.             while(1) {
  1097.                 if ( !idParser::ReadLine( &token ) ) {
  1098.                     idParser::Error( "expected define parameter" );
  1099.                     return false;
  1100.                 }
  1101.                 // if it isn't a name
  1102.                 if (token.type != TT_NAME) {
  1103.                     idParser::Error( "invalid define parameter" );
  1104.                     return false;
  1105.                 }
  1106.  
  1107.                 if (FindDefineParm(define, token.c_str()) >= 0) {
  1108.                     idParser::Error( "two the same define parameters" );
  1109.                     return false;
  1110.                 }
  1111.                 // add the define parm
  1112.                 t = new idToken(token);
  1113.                 t->ClearTokenWhiteSpace();
  1114.                 t->next = NULL;
  1115.                 if (last) last->next = t;
  1116.                 else define->parms = t;
  1117.                 last = t;
  1118.                 define->numparms++;
  1119.                 // read next token
  1120.                 if (!idParser::ReadLine( &token )) {
  1121.                     idParser::Error( "define parameters not terminated" );
  1122.                     return false;
  1123.                 }
  1124.  
  1125.                 if ( token == ")" ) {
  1126.                     break;
  1127.                 }
  1128.                 // then it must be a comma
  1129.                 if ( token != "," ) {
  1130.                     idParser::Error( "define not terminated" );
  1131.                     return false;
  1132.                 }
  1133.             }
  1134.         }
  1135.         if ( !idParser::ReadLine( &token ) ) {
  1136.             return true;
  1137.         }
  1138.     }
  1139.     // read the defined stuff
  1140.     last = NULL;
  1141.     do
  1142.     {
  1143.         t = new idToken(token);
  1144.         if ( t->type == TT_NAME && !idStr::Cmp( t->c_str(), define->name ) ) {
  1145.             t->flags |= TOKEN_FL_RECURSIVE_DEFINE;
  1146.             idParser::Warning( "recursive define (removed recursion)" );
  1147.         }
  1148.         t->ClearTokenWhiteSpace();
  1149.         t->next = NULL;
  1150.         if ( last ) last->next = t;
  1151.         else define->tokens = t;
  1152.         last = t;
  1153.     } while( idParser::ReadLine( &token ) );
  1154.  
  1155.     if ( last ) {
  1156.         // check for merge operators at the beginning or end
  1157.         if ( (*define->tokens) == "##" || (*last) == "##" ) {
  1158.             idParser::Error( "define with misplaced ##" );
  1159.             return false;
  1160.         }
  1161.     }
  1162.     return true;
  1163. }
  1164.  
  1165. /*
  1166. ================
  1167. idParser::AddDefine
  1168. ================
  1169. */
  1170. int idParser::AddDefine( const char *string ) {
  1171.     define_t *define;
  1172.  
  1173.     define = DefineFromString( string );
  1174.     if (!define) {
  1175.         return false;
  1176.     }
  1177.     AddDefineToHash(define, idParser::definehash);
  1178.     return true;
  1179. }
  1180.  
  1181. /*
  1182. ================
  1183. idParser::AddGlobalDefinesToSource
  1184. ================
  1185. */
  1186. void idParser::AddGlobalDefinesToSource( void ) {
  1187.     define_t *define, *newdefine;
  1188.  
  1189.     for (define = globaldefines; define; define = define->next) {
  1190.         newdefine = CopyDefine( define );
  1191.         AddDefineToHash(newdefine, idParser::definehash);
  1192.     }
  1193. }
  1194.  
  1195. /*
  1196. ================
  1197. idParser::Directive_if_def
  1198. ================
  1199. */
  1200. int idParser::Directive_if_def( int type ) {
  1201.     idToken token;
  1202.     define_t *d;
  1203.     int skip;
  1204.  
  1205.     if ( !idParser::ReadLine( &token ) ) {
  1206.         idParser::Error( "#ifdef without name" );
  1207.         return false;
  1208.     }
  1209.     if (token.type != TT_NAME) {
  1210.         idParser::UnreadSourceToken( &token );
  1211.         idParser::Error( "expected name after #ifdef, found '%s'", token.c_str() );
  1212.         return false;
  1213.     }
  1214.     d = FindHashedDefine(idParser::definehash, token.c_str());
  1215.     skip = (type == INDENT_IFDEF) == (d == NULL);
  1216.     idParser::PushIndent( type, skip );
  1217.     return true;
  1218. }
  1219.  
  1220. /*
  1221. ================
  1222. idParser::Directive_ifdef
  1223. ================
  1224. */
  1225. int idParser::Directive_ifdef( void ) {
  1226.     return idParser::Directive_if_def( INDENT_IFDEF );
  1227. }
  1228.  
  1229. /*
  1230. ================
  1231. idParser::Directive_ifndef
  1232. ================
  1233. */
  1234. int idParser::Directive_ifndef( void ) {
  1235.     return idParser::Directive_if_def( INDENT_IFNDEF );
  1236. }
  1237.  
  1238. /*
  1239. ================
  1240. idParser::Directive_else
  1241. ================
  1242. */
  1243. int idParser::Directive_else( void ) {
  1244.     int type, skip;
  1245.  
  1246.     idParser::PopIndent( &type, &skip );
  1247.     if (!type) {
  1248.         idParser::Error( "misplaced #else" );
  1249.         return false;
  1250.     }
  1251.     if (type == INDENT_ELSE) {
  1252.         idParser::Error( "#else after #else" );
  1253.         return false;
  1254.     }
  1255.     idParser::PushIndent( INDENT_ELSE, !skip );
  1256.     return true;
  1257. }
  1258.  
  1259. /*
  1260. ================
  1261. idParser::Directive_endif
  1262. ================
  1263. */
  1264. int idParser::Directive_endif( void ) {
  1265.     int type, skip;
  1266.  
  1267.     idParser::PopIndent( &type, &skip );
  1268.     if (!type) {
  1269.         idParser::Error( "misplaced #endif" );
  1270.         return false;
  1271.     }
  1272.     return true;
  1273. }
  1274.  
  1275. /*
  1276. ================
  1277. idParser::EvaluateTokens
  1278. ================
  1279. */
  1280. typedef struct operator_s
  1281. {
  1282.     int op;
  1283.     int priority;
  1284.     int parentheses;
  1285.     struct operator_s *prev, *next;
  1286. } operator_t;
  1287.  
  1288. typedef struct value_s
  1289. {
  1290.     signed long int intvalue;
  1291.     double floatvalue;
  1292.     int parentheses;
  1293.     struct value_s *prev, *next;
  1294. } value_t;
  1295.  
  1296. int PC_OperatorPriority(int op) {
  1297.     switch(op) {
  1298.         case P_MUL: return 15;
  1299.         case P_DIV: return 15;
  1300.         case P_MOD: return 15;
  1301.         case P_ADD: return 14;
  1302.         case P_SUB: return 14;
  1303.  
  1304.         case P_LOGIC_AND: return 7;
  1305.         case P_LOGIC_OR: return 6;
  1306.         case P_LOGIC_GEQ: return 12;
  1307.         case P_LOGIC_LEQ: return 12;
  1308.         case P_LOGIC_EQ: return 11;
  1309.         case P_LOGIC_UNEQ: return 11;
  1310.  
  1311.         case P_LOGIC_NOT: return 16;
  1312.         case P_LOGIC_GREATER: return 12;
  1313.         case P_LOGIC_LESS: return 12;
  1314.  
  1315.         case P_RSHIFT: return 13;
  1316.         case P_LSHIFT: return 13;
  1317.  
  1318.         case P_BIN_AND: return 10;
  1319.         case P_BIN_OR: return 8;
  1320.         case P_BIN_XOR: return 9;
  1321.         case P_BIN_NOT: return 16;
  1322.  
  1323.         case P_COLON: return 5;
  1324.         case P_QUESTIONMARK: return 5;
  1325.     }
  1326.     return false;
  1327. }
  1328.  
  1329. //#define AllocValue()            GetClearedMemory(sizeof(value_t));
  1330. //#define FreeValue(val)        FreeMemory(val)
  1331. //#define AllocOperator(op)        op = (operator_t *) GetClearedMemory(sizeof(operator_t));
  1332. //#define FreeOperator(op)        FreeMemory(op);
  1333.  
  1334. #define MAX_VALUES        64
  1335. #define MAX_OPERATORS    64
  1336.  
  1337. #define AllocValue(val)                                    \
  1338.     if ( numvalues >= MAX_VALUES ) {                    \
  1339.         idParser::Error( "out of value space\n" );        \
  1340.         error = 1;                                        \
  1341.         break;                                            \
  1342.     }                                                    \
  1343.     else {                                                \
  1344.         val = &value_heap[numvalues++];                    \
  1345.     }
  1346.  
  1347. #define FreeValue(val)
  1348.  
  1349. #define AllocOperator(op)                                \
  1350.     if ( numoperators >= MAX_OPERATORS ) {                \
  1351.         idParser::Error( "out of operator space\n" );    \
  1352.         error = 1;                                        \
  1353.         break;                                            \
  1354.     }                                                    \
  1355.     else {                                                \
  1356.         op = &operator_heap[numoperators++];            \
  1357.     }
  1358.  
  1359. #define FreeOperator(op)
  1360.  
  1361. int idParser::EvaluateTokens( idToken *tokens, signed long int *intvalue, double *floatvalue, int integer ) {
  1362.     operator_t *o, *firstoperator, *lastoperator;
  1363.     value_t *v, *firstvalue, *lastvalue, *v1, *v2;
  1364.     idToken *t;
  1365.     int brace = 0;
  1366.     int parentheses = 0;
  1367.     int error = 0;
  1368.     int lastwasvalue = 0;
  1369.     int negativevalue = 0;
  1370.     int questmarkintvalue = 0;
  1371.     double questmarkfloatvalue = 0;
  1372.     int gotquestmarkvalue = false;
  1373.     int lastoperatortype = 0;
  1374.     //
  1375.     operator_t operator_heap[MAX_OPERATORS];
  1376.     int numoperators = 0;
  1377.     value_t value_heap[MAX_VALUES];
  1378.     int numvalues = 0;
  1379.  
  1380.     firstoperator = lastoperator = NULL;
  1381.     firstvalue = lastvalue = NULL;
  1382.     if (intvalue) *intvalue = 0;
  1383.     if (floatvalue) *floatvalue = 0;
  1384.     for ( t = tokens; t; t = t->next ) {
  1385.         switch( t->type ) {
  1386.             case TT_NAME:
  1387.             {
  1388.                 if ( lastwasvalue || negativevalue ) {
  1389.                     idParser::Error( "syntax error in #if/#elif" );
  1390.                     error = 1;
  1391.                     break;
  1392.                 }
  1393.                 if ( (*t) != "defined" ) {
  1394.                     idParser::Error( "undefined name '%s' in #if/#elif", t->c_str() );
  1395.                     error = 1;
  1396.                     break;
  1397.                 }
  1398.                 t = t->next;
  1399.                 if ( (*t) == "(" ) {
  1400.                     brace = true;
  1401.                     t = t->next;
  1402.                 }
  1403.                 if (!t || t->type != TT_NAME) {
  1404.                     idParser::Error( "defined() without name in #if/#elif" );
  1405.                     error = 1;
  1406.                     break;
  1407.                 }
  1408.                 //v = (value_t *) GetClearedMemory(sizeof(value_t));
  1409.                 AllocValue(v);
  1410.                 if (FindHashedDefine(idParser::definehash, t->c_str())) {
  1411.                     v->intvalue = 1;
  1412.                     v->floatvalue = 1;
  1413.                 }
  1414.                 else {
  1415.                     v->intvalue = 0;
  1416.                     v->floatvalue = 0;
  1417.                 }
  1418.                 v->parentheses = parentheses;
  1419.                 v->next = NULL;
  1420.                 v->prev = lastvalue;
  1421.                 if (lastvalue) lastvalue->next = v;
  1422.                 else firstvalue = v;
  1423.                 lastvalue = v;
  1424.                 if (brace) {
  1425.                     t = t->next;
  1426.                     if (!t || (*t) != ")" ) {
  1427.                         idParser::Error( "defined missing ) in #if/#elif" );
  1428.                         error = 1;
  1429.                         break;
  1430.                     }
  1431.                 }
  1432.                 brace = false;
  1433.                 // defined() creates a value
  1434.                 lastwasvalue = 1;
  1435.                 break;
  1436.             }
  1437.             case TT_NUMBER:
  1438.             {
  1439.                 if (lastwasvalue) {
  1440.                     idParser::Error( "syntax error in #if/#elif" );
  1441.                     error = 1;
  1442.                     break;
  1443.                 }
  1444.                 //v = (value_t *) GetClearedMemory(sizeof(value_t));
  1445.                 AllocValue(v);
  1446.                 if (negativevalue) {
  1447.                     v->intvalue = - t->GetIntValue();
  1448.                     v->floatvalue = - t->GetFloatValue();
  1449.                 }
  1450.                 else {
  1451.                     v->intvalue = t->GetIntValue();
  1452.                     v->floatvalue = t->GetFloatValue();
  1453.                 }
  1454.                 v->parentheses = parentheses;
  1455.                 v->next = NULL;
  1456.                 v->prev = lastvalue;
  1457.                 if (lastvalue) lastvalue->next = v;
  1458.                 else firstvalue = v;
  1459.                 lastvalue = v;
  1460.                 //last token was a value
  1461.                 lastwasvalue = 1;
  1462.                 //
  1463.                 negativevalue = 0;
  1464.                 break;
  1465.             }
  1466.             case TT_PUNCTUATION:
  1467.             {
  1468.                 if (negativevalue) {
  1469.                     idParser::Error( "misplaced minus sign in #if/#elif" );
  1470.                     error = 1;
  1471.                     break;
  1472.                 }
  1473.                 if (t->subtype == P_PARENTHESESOPEN) {
  1474.                     parentheses++;
  1475.                     break;
  1476.                 }
  1477.                 else if (t->subtype == P_PARENTHESESCLOSE) {
  1478.                     parentheses--;
  1479.                     if (parentheses < 0) {
  1480.                         idParser::Error( "too many ) in #if/#elsif" );
  1481.                         error = 1;
  1482.                     }
  1483.                     break;
  1484.                 }
  1485.                 //check for invalid operators on floating point values
  1486.                 if ( !integer ) {
  1487.                     if (t->subtype == P_BIN_NOT || t->subtype == P_MOD ||
  1488.                         t->subtype == P_RSHIFT || t->subtype == P_LSHIFT ||
  1489.                         t->subtype == P_BIN_AND || t->subtype == P_BIN_OR ||
  1490.                         t->subtype == P_BIN_XOR) {
  1491.                         idParser::Error( "illigal operator '%s' on floating point operands\n", t->c_str() );
  1492.                         error = 1;
  1493.                         break;
  1494.                     }
  1495.                 }
  1496.                 switch( t->subtype ) {
  1497.                     case P_LOGIC_NOT:
  1498.                     case P_BIN_NOT:
  1499.                     {
  1500.                         if (lastwasvalue) {
  1501.                             idParser::Error( "! or ~ after value in #if/#elif" );
  1502.                             error = 1;
  1503.                             break;
  1504.                         }
  1505.                         break;
  1506.                     }
  1507.                     case P_INC:
  1508.                     case P_DEC:
  1509.                     {
  1510.                         idParser::Error( "++ or -- used in #if/#elif" );
  1511.                         break;
  1512.                     }
  1513.                     case P_SUB:
  1514.                     {
  1515.                         if (!lastwasvalue) {
  1516.                             negativevalue = 1;
  1517.                             break;
  1518.                         }
  1519.                     }
  1520.                     
  1521.                     case P_MUL:
  1522.                     case P_DIV:
  1523.                     case P_MOD:
  1524.                     case P_ADD:
  1525.  
  1526.                     case P_LOGIC_AND:
  1527.                     case P_LOGIC_OR:
  1528.                     case P_LOGIC_GEQ:
  1529.                     case P_LOGIC_LEQ:
  1530.                     case P_LOGIC_EQ:
  1531.                     case P_LOGIC_UNEQ:
  1532.  
  1533.                     case P_LOGIC_GREATER:
  1534.                     case P_LOGIC_LESS:
  1535.  
  1536.                     case P_RSHIFT:
  1537.                     case P_LSHIFT:
  1538.  
  1539.                     case P_BIN_AND:
  1540.                     case P_BIN_OR:
  1541.                     case P_BIN_XOR:
  1542.  
  1543.                     case P_COLON:
  1544.                     case P_QUESTIONMARK:
  1545.                     {
  1546.                         if (!lastwasvalue) {
  1547.                             idParser::Error( "operator '%s' after operator in #if/#elif", t->c_str() );
  1548.                             error = 1;
  1549.                             break;
  1550.                         }
  1551.                         break;
  1552.                     }
  1553.                     default:
  1554.                     {
  1555.                         idParser::Error( "invalid operator '%s' in #if/#elif", t->c_str() );
  1556.                         error = 1;
  1557.                         break;
  1558.                     }
  1559.                 }
  1560.                 if (!error && !negativevalue) {
  1561.                     //o = (operator_t *) GetClearedMemory(sizeof(operator_t));
  1562.                     AllocOperator(o);
  1563.                     o->op = t->subtype;
  1564.                     o->priority = PC_OperatorPriority(t->subtype);
  1565.                     o->parentheses = parentheses;
  1566.                     o->next = NULL;
  1567.                     o->prev = lastoperator;
  1568.                     if (lastoperator) lastoperator->next = o;
  1569.                     else firstoperator = o;
  1570.                     lastoperator = o;
  1571.                     lastwasvalue = 0;
  1572.                 }
  1573.                 break;
  1574.             }
  1575.             default:
  1576.             {
  1577.                 idParser::Error( "unknown '%s' in #if/#elif", t->c_str() );
  1578.                 error = 1;
  1579.                 break;
  1580.             }
  1581.         }
  1582.         if (error) {
  1583.             break;
  1584.         }
  1585.     }
  1586.     if (!error) {
  1587.         if (!lastwasvalue) {
  1588.             idParser::Error( "trailing operator in #if/#elif" );
  1589.             error = 1;
  1590.         }
  1591.         else if (parentheses) {
  1592.             idParser::Error( "too many ( in #if/#elif" );
  1593.             error = 1;
  1594.         }
  1595.     }
  1596.     //
  1597.     gotquestmarkvalue = false;
  1598.     questmarkintvalue = 0;
  1599.     questmarkfloatvalue = 0;
  1600.     //while there are operators
  1601.     while( !error && firstoperator ) {
  1602.         v = firstvalue;
  1603.         for (o = firstoperator; o->next; o = o->next) {
  1604.             //if the current operator is nested deeper in parentheses
  1605.             //than the next operator
  1606.             if (o->parentheses > o->next->parentheses) {
  1607.                 break;
  1608.             }
  1609.             //if the current and next operator are nested equally deep in parentheses
  1610.             if (o->parentheses == o->next->parentheses) {
  1611.                 //if the priority of the current operator is equal or higher
  1612.                 //than the priority of the next operator
  1613.                 if (o->priority >= o->next->priority) {
  1614.                     break;
  1615.                 }
  1616.             }
  1617.             //if the arity of the operator isn't equal to 1
  1618.             if (o->op != P_LOGIC_NOT && o->op != P_BIN_NOT) {
  1619.                 v = v->next;
  1620.             }
  1621.             //if there's no value or no next value
  1622.             if (!v) {
  1623.                 idParser::Error( "mising values in #if/#elif" );
  1624.                 error = 1;
  1625.                 break;
  1626.             }
  1627.         }
  1628.         if (error) {
  1629.             break;
  1630.         }
  1631.         v1 = v;
  1632.         v2 = v->next;
  1633. #ifdef DEBUG_EVAL
  1634.         if (integer) {
  1635.             Log_Write("operator %s, value1 = %d", idParser::scriptstack->getPunctuationFromId(o->op), v1->intvalue);
  1636.             if (v2) Log_Write("value2 = %d", v2->intvalue);
  1637.         }
  1638.         else {
  1639.             Log_Write("operator %s, value1 = %f", idParser::scriptstack->getPunctuationFromId(o->op), v1->floatvalue);
  1640.             if (v2) Log_Write("value2 = %f", v2->floatvalue);
  1641.         }
  1642. #endif //DEBUG_EVAL
  1643.         switch(o->op) {
  1644.             case P_LOGIC_NOT:        v1->intvalue = !v1->intvalue;
  1645.                                     v1->floatvalue = !v1->floatvalue; break;
  1646.             case P_BIN_NOT:            v1->intvalue = ~v1->intvalue;
  1647.                                     break;
  1648.             case P_MUL:                v1->intvalue *= v2->intvalue;
  1649.                                     v1->floatvalue *= v2->floatvalue; break;
  1650.             case P_DIV:                if (!v2->intvalue || !v2->floatvalue)
  1651.                                     {
  1652.                                         idParser::Error( "divide by zero in #if/#elif\n" );
  1653.                                         error = 1;
  1654.                                         break;
  1655.                                     }
  1656.                                     v1->intvalue /= v2->intvalue;
  1657.                                     v1->floatvalue /= v2->floatvalue; break;
  1658.             case P_MOD:                if (!v2->intvalue)
  1659.                                     {
  1660.                                         idParser::Error( "divide by zero in #if/#elif\n" );
  1661.                                         error = 1;
  1662.                                         break;
  1663.                                     }
  1664.                                     v1->intvalue %= v2->intvalue; break;
  1665.             case P_ADD:                v1->intvalue += v2->intvalue;
  1666.                                     v1->floatvalue += v2->floatvalue; break;
  1667.             case P_SUB:                v1->intvalue -= v2->intvalue;
  1668.                                     v1->floatvalue -= v2->floatvalue; break;
  1669.             case P_LOGIC_AND:        v1->intvalue = v1->intvalue && v2->intvalue;
  1670.                                     v1->floatvalue = v1->floatvalue && v2->floatvalue; break;
  1671.             case P_LOGIC_OR:        v1->intvalue = v1->intvalue || v2->intvalue;
  1672.                                     v1->floatvalue = v1->floatvalue || v2->floatvalue; break;
  1673.             case P_LOGIC_GEQ:        v1->intvalue = v1->intvalue >= v2->intvalue;
  1674.                                     v1->floatvalue = v1->floatvalue >= v2->floatvalue; break;
  1675.             case P_LOGIC_LEQ:        v1->intvalue = v1->intvalue <= v2->intvalue;
  1676.                                     v1->floatvalue = v1->floatvalue <= v2->floatvalue; break;
  1677.             case P_LOGIC_EQ:        v1->intvalue = v1->intvalue == v2->intvalue;
  1678.                                     v1->floatvalue = v1->floatvalue == v2->floatvalue; break;
  1679.             case P_LOGIC_UNEQ:        v1->intvalue = v1->intvalue != v2->intvalue;
  1680.                                     v1->floatvalue = v1->floatvalue != v2->floatvalue; break;
  1681.             case P_LOGIC_GREATER:    v1->intvalue = v1->intvalue > v2->intvalue;
  1682.                                     v1->floatvalue = v1->floatvalue > v2->floatvalue; break;
  1683.             case P_LOGIC_LESS:        v1->intvalue = v1->intvalue < v2->intvalue;
  1684.                                     v1->floatvalue = v1->floatvalue < v2->floatvalue; break;
  1685.             case P_RSHIFT:            v1->intvalue >>= v2->intvalue;
  1686.                                     break;
  1687.             case P_LSHIFT:            v1->intvalue <<= v2->intvalue;
  1688.                                     break;
  1689.             case P_BIN_AND:            v1->intvalue &= v2->intvalue;
  1690.                                     break;
  1691.             case P_BIN_OR:            v1->intvalue |= v2->intvalue;
  1692.                                     break;
  1693.             case P_BIN_XOR:            v1->intvalue ^= v2->intvalue;
  1694.                                     break;
  1695.             case P_COLON:
  1696.             {
  1697.                 if (!gotquestmarkvalue) {
  1698.                     idParser::Error( ": without ? in #if/#elif" );
  1699.                     error = 1;
  1700.                     break;
  1701.                 }
  1702.                 if (integer) {
  1703.                     if (!questmarkintvalue)
  1704.                         v1->intvalue = v2->intvalue;
  1705.                 }
  1706.                 else {
  1707.                     if (!questmarkfloatvalue)
  1708.                         v1->floatvalue = v2->floatvalue;
  1709.                 }
  1710.                 gotquestmarkvalue = false;
  1711.                 break;
  1712.             }
  1713.             case P_QUESTIONMARK:
  1714.             {
  1715.                 if (gotquestmarkvalue) {
  1716.                     idParser::Error( "? after ? in #if/#elif" );
  1717.                     error = 1;
  1718.                     break;
  1719.                 }
  1720.                 questmarkintvalue = v1->intvalue;
  1721.                 questmarkfloatvalue = v1->floatvalue;
  1722.                 gotquestmarkvalue = true;
  1723.                 break;
  1724.             }
  1725.         }
  1726. #ifdef DEBUG_EVAL
  1727.         if (integer) Log_Write("result value = %d", v1->intvalue);
  1728.         else Log_Write("result value = %f", v1->floatvalue);
  1729. #endif //DEBUG_EVAL
  1730.         if (error)
  1731.             break;
  1732.         lastoperatortype = o->op;
  1733.         //if not an operator with arity 1
  1734.         if (o->op != P_LOGIC_NOT && o->op != P_BIN_NOT) {
  1735.             //remove the second value if not question mark operator
  1736.             if (o->op != P_QUESTIONMARK) {
  1737.                 v = v->next;
  1738.             }
  1739.             //
  1740.             if (v->prev) v->prev->next = v->next;
  1741.             else firstvalue = v->next;
  1742.             if (v->next) v->next->prev = v->prev;
  1743.             else lastvalue = v->prev;
  1744.             //FreeMemory(v);
  1745.             FreeValue(v);
  1746.         }
  1747.         //remove the operator
  1748.         if (o->prev) o->prev->next = o->next;
  1749.         else firstoperator = o->next;
  1750.         if (o->next) o->next->prev = o->prev;
  1751.         else lastoperator = o->prev;
  1752.         //FreeMemory(o);
  1753.         FreeOperator(o);
  1754.     }
  1755.     if (firstvalue) {
  1756.         if (intvalue) *intvalue = firstvalue->intvalue;
  1757.         if (floatvalue) *floatvalue = firstvalue->floatvalue;
  1758.     }
  1759.     for (o = firstoperator; o; o = lastoperator) {
  1760.         lastoperator = o->next;
  1761.         //FreeMemory(o);
  1762.         FreeOperator(o);
  1763.     }
  1764.     for (v = firstvalue; v; v = lastvalue) {
  1765.         lastvalue = v->next;
  1766.         //FreeMemory(v);
  1767.         FreeValue(v);
  1768.     }
  1769.     if (!error) {
  1770.         return true;
  1771.     }
  1772.     if (intvalue) {
  1773.         *intvalue = 0;
  1774.     }
  1775.     if (floatvalue) {
  1776.         *floatvalue = 0;
  1777.     }
  1778.     return false;
  1779. }
  1780.  
  1781. /*
  1782. ================
  1783. idParser::Evaluate
  1784. ================
  1785. */
  1786. int idParser::Evaluate( signed long int *intvalue, double *floatvalue, int integer ) {
  1787.     idToken token, *firsttoken, *lasttoken;
  1788.     idToken *t, *nexttoken;
  1789.     define_t *define;
  1790.     int defined = false;
  1791.  
  1792.     if (intvalue) {
  1793.         *intvalue = 0;
  1794.     }
  1795.     if (floatvalue) {
  1796.         *floatvalue = 0;
  1797.     }
  1798.     //
  1799.     if ( !idParser::ReadLine( &token ) ) {
  1800.         idParser::Error( "no value after #if/#elif" );
  1801.         return false;
  1802.     }
  1803.     firsttoken = NULL;
  1804.     lasttoken = NULL;
  1805.     do {
  1806.         //if the token is a name
  1807.         if (token.type == TT_NAME) {
  1808.             if (defined) {
  1809.                 defined = false;
  1810.                 t = new idToken(token);
  1811.                 t->next = NULL;
  1812.                 if (lasttoken) lasttoken->next = t;
  1813.                 else firsttoken = t;
  1814.                 lasttoken = t;
  1815.             }
  1816.             else if ( token == "defined" ) {
  1817.                 defined = true;
  1818.                 t = new idToken(token);
  1819.                 t->next = NULL;
  1820.                 if (lasttoken) lasttoken->next = t;
  1821.                 else firsttoken = t;
  1822.                 lasttoken = t;
  1823.             }
  1824.             else {
  1825.                 //then it must be a define
  1826.                 define = FindHashedDefine(idParser::definehash, token.c_str());
  1827.                 if (!define) {
  1828.                     idParser::Error( "can't Evaluate '%s', not defined", token.c_str() );
  1829.                     return false;
  1830.                 }
  1831.                 if ( !idParser::ExpandDefineIntoSource( &token, define ) ) {
  1832.                     return false;
  1833.                 }
  1834.             }
  1835.         }
  1836.         //if the token is a number or a punctuation
  1837.         else if (token.type == TT_NUMBER || token.type == TT_PUNCTUATION) {
  1838.             t = new idToken(token);
  1839.             t->next = NULL;
  1840.             if (lasttoken) lasttoken->next = t;
  1841.             else firsttoken = t;
  1842.             lasttoken = t;
  1843.         }
  1844.         else {
  1845.             idParser::Error( "can't Evaluate '%s'", token.c_str() );
  1846.             return false;
  1847.         }
  1848.     } while(idParser::ReadLine( &token ));
  1849.     //
  1850.     if ( !idParser::EvaluateTokens( firsttoken, intvalue, floatvalue, integer ) ) {
  1851.         return false;
  1852.     }
  1853.     //
  1854. #ifdef DEBUG_EVAL
  1855.     Log_Write("eval:");
  1856. #endif //DEBUG_EVAL
  1857.     for (t = firsttoken; t; t = nexttoken) {
  1858. #ifdef DEBUG_EVAL
  1859.         Log_Write(" %s", t->c_str());
  1860. #endif //DEBUG_EVAL
  1861.         nexttoken = t->next;
  1862.         delete t;
  1863.     } //end for
  1864. #ifdef DEBUG_EVAL
  1865.     if (integer) Log_Write("eval result: %d", *intvalue);
  1866.     else Log_Write("eval result: %f", *floatvalue);
  1867. #endif //DEBUG_EVAL
  1868.     //
  1869.     return true;
  1870. }
  1871.  
  1872. /*
  1873. ================
  1874. idParser::DollarEvaluate
  1875. ================
  1876. */
  1877. int idParser::DollarEvaluate( signed long int *intvalue, double *floatvalue, int integer) {
  1878.     int indent, defined = false;
  1879.     idToken token, *firsttoken, *lasttoken;
  1880.     idToken *t, *nexttoken;
  1881.     define_t *define;
  1882.  
  1883.     if (intvalue) {
  1884.         *intvalue = 0;
  1885.     }
  1886.     if (floatvalue) {
  1887.         *floatvalue = 0;
  1888.     }
  1889.     //
  1890.     if ( !idParser::ReadSourceToken( &token ) ) {
  1891.         idParser::Error( "no leading ( after $evalint/$evalfloat" );
  1892.         return false;
  1893.     }
  1894.     if ( !idParser::ReadSourceToken( &token ) ) {
  1895.         idParser::Error( "nothing to Evaluate" );
  1896.         return false;
  1897.     }
  1898.     indent = 1;
  1899.     firsttoken = NULL;
  1900.     lasttoken = NULL;
  1901.     do {
  1902.         //if the token is a name
  1903.         if (token.type == TT_NAME) {
  1904.             if (defined) {
  1905.                 defined = false;
  1906.                 t = new idToken(token);
  1907.                 t->next = NULL;
  1908.                 if (lasttoken) lasttoken->next = t;
  1909.                 else firsttoken = t;
  1910.                 lasttoken = t;
  1911.             }
  1912.             else if ( token == "defined" ) {
  1913.                 defined = true;
  1914.                 t = new idToken(token);
  1915.                 t->next = NULL;
  1916.                 if (lasttoken) lasttoken->next = t;
  1917.                 else firsttoken = t;
  1918.                 lasttoken = t;
  1919.             }
  1920.             else {
  1921.                 //then it must be a define
  1922.                 define = FindHashedDefine(idParser::definehash, token.c_str());
  1923.                 if (!define) {
  1924.                     idParser::Warning( "can't Evaluate '%s', not defined", token.c_str() );
  1925.                     return false;
  1926.                 }
  1927.                 if ( !idParser::ExpandDefineIntoSource( &token, define ) ) {
  1928.                     return false;
  1929.                 }
  1930.             }
  1931.         }
  1932.         //if the token is a number or a punctuation
  1933.         else if (token.type == TT_NUMBER || token.type == TT_PUNCTUATION) {
  1934.             if ( token[0] == '(' ) indent++;
  1935.             else if ( token[0] == ')' ) indent--;
  1936.             if (indent <= 0) {
  1937.                 break;
  1938.             }
  1939.             t = new idToken(token);
  1940.             t->next = NULL;
  1941.             if (lasttoken) lasttoken->next = t;
  1942.             else firsttoken = t;
  1943.             lasttoken = t;
  1944.         }
  1945.         else {
  1946.             idParser::Error( "can't Evaluate '%s'", token.c_str() );
  1947.             return false;
  1948.         }
  1949.     } while(idParser::ReadSourceToken( &token ));
  1950.     //
  1951.     if (!idParser::EvaluateTokens( firsttoken, intvalue, floatvalue, integer)) {
  1952.         return false;
  1953.     }
  1954.     //
  1955. #ifdef DEBUG_EVAL
  1956.     Log_Write("$eval:");
  1957. #endif //DEBUG_EVAL
  1958.     for (t = firsttoken; t; t = nexttoken) {
  1959. #ifdef DEBUG_EVAL
  1960.         Log_Write(" %s", t->c_str());
  1961. #endif //DEBUG_EVAL
  1962.         nexttoken = t->next;
  1963.         delete t;
  1964.     } //end for
  1965. #ifdef DEBUG_EVAL
  1966.     if (integer) Log_Write("$eval result: %d", *intvalue);
  1967.     else Log_Write("$eval result: %f", *floatvalue);
  1968. #endif //DEBUG_EVAL
  1969.     //
  1970.     return true;
  1971. }
  1972.  
  1973. /*
  1974. ================
  1975. idParser::Directive_elif
  1976. ================
  1977. */
  1978. int idParser::Directive_elif( void ) {
  1979.     signed long int value;
  1980.     int type, skip;
  1981.  
  1982.     idParser::PopIndent( &type, &skip );
  1983.     if (!type || type == INDENT_ELSE) {
  1984.         idParser::Error( "misplaced #elif" );
  1985.         return false;
  1986.     }
  1987.     if ( !idParser::Evaluate( &value, NULL, true ) ) {
  1988.         return false;
  1989.     }
  1990.     skip = (value == 0);
  1991.     idParser::PushIndent( INDENT_ELIF, skip );
  1992.     return true;
  1993. }
  1994.  
  1995. /*
  1996. ================
  1997. idParser::Directive_if
  1998. ================
  1999. */
  2000. int idParser::Directive_if( void ) {
  2001.     signed long int value;
  2002.     int skip;
  2003.  
  2004.     if ( !idParser::Evaluate( &value, NULL, true ) ) {
  2005.         return false;
  2006.     }
  2007.     skip = (value == 0);
  2008.     idParser::PushIndent( INDENT_IF, skip );
  2009.     return true;
  2010. }
  2011.  
  2012. /*
  2013. ================
  2014. idParser::Directive_line
  2015. ================
  2016. */
  2017. int idParser::Directive_line( void ) {
  2018.     idToken token;
  2019.  
  2020.     idParser::Error( "#line directive not supported" );
  2021.     while( idParser::ReadLine( &token ) ) {
  2022.     }
  2023.     return true;
  2024. }
  2025.  
  2026. /*
  2027. ================
  2028. idParser::Directive_error
  2029. ================
  2030. */
  2031. int idParser::Directive_error( void ) {
  2032.     idToken token;
  2033.  
  2034.     if ( !idParser::ReadLine( &token) || token.type != TT_STRING ) {
  2035.         idParser::Error( "#error without string" );
  2036.         return false;
  2037.     }
  2038.     idParser::Error( "#error: %s", token.c_str() );
  2039.     return true;
  2040. }
  2041.  
  2042. /*
  2043. ================
  2044. idParser::Directive_warning
  2045. ================
  2046. */
  2047. int idParser::Directive_warning( void ) {
  2048.     idToken token;
  2049.  
  2050.     if ( !idParser::ReadLine( &token) || token.type != TT_STRING ) {
  2051.         idParser::Warning( "#warning without string" );
  2052.         return false;
  2053.     }
  2054.     idParser::Warning( "#warning: %s", token.c_str() );
  2055.     return true;
  2056. }
  2057.  
  2058. /*
  2059. ================
  2060. idParser::Directive_pragma
  2061. ================
  2062. */
  2063. int idParser::Directive_pragma( void ) {
  2064.     idToken token;
  2065.  
  2066.     idParser::Warning( "#pragma directive not supported" );
  2067.     while( idParser::ReadLine( &token ) ) {
  2068.     }
  2069.     return true;
  2070. }
  2071.  
  2072. /*
  2073. ================
  2074. idParser::UnreadSignToken
  2075. ================
  2076. */
  2077. void idParser::UnreadSignToken( void ) {
  2078.     idToken token;
  2079.  
  2080.     token.line = idParser::scriptstack->GetLineNum();
  2081.     token.whiteSpaceStart_p = NULL;
  2082.     token.whiteSpaceEnd_p = NULL;
  2083.     token.linesCrossed = 0;
  2084.     token.flags = 0;
  2085.     token = "-";
  2086.     token.type = TT_PUNCTUATION;
  2087.     token.subtype = P_SUB;
  2088.     idParser::UnreadSourceToken( &token );
  2089. }
  2090.  
  2091. /*
  2092. ================
  2093. idParser::Directive_eval
  2094. ================
  2095. */
  2096. int idParser::Directive_eval( void ) {
  2097.     signed long int value;
  2098.     idToken token;
  2099.     char buf[128];
  2100.  
  2101.     if ( !idParser::Evaluate( &value, NULL, true ) ) {
  2102.         return false;
  2103.     }
  2104.  
  2105.     token.line = idParser::scriptstack->GetLineNum();
  2106.     token.whiteSpaceStart_p = NULL;
  2107.     token.whiteSpaceEnd_p = NULL;
  2108.     token.linesCrossed = 0;
  2109.     token.flags = 0;
  2110.     sprintf(buf, "%d", abs(value));
  2111.     token = buf;
  2112.     token.type = TT_NUMBER;
  2113.     token.subtype = TT_INTEGER|TT_LONG|TT_DECIMAL;
  2114.     idParser::UnreadSourceToken( &token );
  2115.     if ( value < 0 ) {
  2116.         idParser::UnreadSignToken();
  2117.     }
  2118.     return true;
  2119. }
  2120.  
  2121. /*
  2122. ================
  2123. idParser::Directive_evalfloat
  2124. ================
  2125. */
  2126. int idParser::Directive_evalfloat( void ) {
  2127.     double value;
  2128.     idToken token;
  2129.     char buf[128];
  2130.  
  2131.     if ( !idParser::Evaluate( NULL, &value, false ) ) {
  2132.         return false;
  2133.     }
  2134.  
  2135.     token.line = idParser::scriptstack->GetLineNum();
  2136.     token.whiteSpaceStart_p = NULL;
  2137.     token.whiteSpaceEnd_p = NULL;
  2138.     token.linesCrossed = 0;
  2139.     token.flags = 0;
  2140.     sprintf(buf, "%1.2f", idMath::Fabs(value));
  2141.     token = buf;
  2142.     token.type = TT_NUMBER;
  2143.     token.subtype = TT_FLOAT|TT_LONG|TT_DECIMAL;
  2144.     idParser::UnreadSourceToken( &token );
  2145.     if (value < 0) {
  2146.         idParser::UnreadSignToken();
  2147.     }
  2148.     return true;
  2149. }
  2150.  
  2151. /*
  2152. ================
  2153. idParser::ReadDirective
  2154. ================
  2155. */
  2156. int idParser::ReadDirective( void ) {
  2157.     idToken token;
  2158.  
  2159.     //read the directive name
  2160.     if ( !idParser::ReadSourceToken( &token ) ) {
  2161.         idParser::Error( "found '#' without name" );
  2162.         return false;
  2163.     }
  2164.     //directive name must be on the same line
  2165.     if (token.linesCrossed > 0) {
  2166.         idParser::UnreadSourceToken( &token );
  2167.         idParser::Error( "found '#' at end of line" );
  2168.         return false;
  2169.     }
  2170.     //if if is a name
  2171.     if (token.type == TT_NAME) {
  2172.         if ( token == "if" ) {
  2173.             return idParser::Directive_if();
  2174.         }
  2175.         else if ( token == "ifdef" ) {
  2176.             return idParser::Directive_ifdef();
  2177.         }
  2178.         else if ( token == "ifndef" ) {
  2179.             return idParser::Directive_ifndef();
  2180.         }
  2181.         else if ( token == "elif" ) {
  2182.             return idParser::Directive_elif();
  2183.         }
  2184.         else if ( token == "else" ) {
  2185.             return idParser::Directive_else();
  2186.         }
  2187.         else if ( token == "endif" ) {
  2188.             return idParser::Directive_endif();
  2189.         }
  2190.         else if (idParser::skip > 0) {
  2191.             // skip the rest of the line
  2192.             while( idParser::ReadLine( &token ) ) {
  2193.             }
  2194.             return true;
  2195.         }
  2196.         else {
  2197.             if ( token == "include" ) {
  2198.                 return idParser::Directive_include();
  2199.             }
  2200.             else if ( token == "define" ) {
  2201.                 return idParser::Directive_define();
  2202.             }
  2203.             else if ( token == "undef" ) {
  2204.                 return idParser::Directive_undef();
  2205.             }
  2206.             else if ( token == "line" ) {
  2207.                 return idParser::Directive_line();
  2208.             }
  2209.             else if ( token == "error" ) {
  2210.                 return idParser::Directive_error();
  2211.             }
  2212.             else if ( token == "warning" ) {
  2213.                 return idParser::Directive_warning();
  2214.             }
  2215.             else if ( token == "pragma" ) {
  2216.                 return idParser::Directive_pragma();
  2217.             }
  2218.             else if ( token == "eval" ) {
  2219.                 return idParser::Directive_eval();
  2220.             }
  2221.             else if ( token == "evalfloat" ) {
  2222.                 return idParser::Directive_evalfloat();
  2223.             }
  2224.         }
  2225.     }
  2226.     idParser::Error( "unknown precompiler directive '%s'", token.c_str() );
  2227.     return false;
  2228. }
  2229.  
  2230. /*
  2231. ================
  2232. idParser::DollarDirective_evalint
  2233. ================
  2234. */
  2235. int idParser::DollarDirective_evalint( void ) {
  2236.     signed long int value;
  2237.     idToken token;
  2238.     char buf[128];
  2239.  
  2240.     if ( !idParser::DollarEvaluate( &value, NULL, true ) ) {
  2241.         return false;
  2242.     }
  2243.  
  2244.     token.line = idParser::scriptstack->GetLineNum();
  2245.     token.whiteSpaceStart_p = NULL;
  2246.     token.whiteSpaceEnd_p = NULL;
  2247.     token.linesCrossed = 0;
  2248.     token.flags = 0;
  2249.     sprintf( buf, "%d", abs( value ) );
  2250.     token = buf;
  2251.     token.type = TT_NUMBER;
  2252.     token.subtype = TT_INTEGER | TT_LONG | TT_DECIMAL | TT_VALUESVALID;
  2253.     token.intvalue = abs( value );
  2254.     token.floatvalue = abs( value );
  2255.     idParser::UnreadSourceToken( &token );
  2256.     if ( value < 0 ) {
  2257.         idParser::UnreadSignToken();
  2258.     }
  2259.     return true;
  2260. }
  2261.  
  2262. /*
  2263. ================
  2264. idParser::DollarDirective_evalfloat
  2265. ================
  2266. */
  2267. int idParser::DollarDirective_evalfloat( void ) {
  2268.     double value;
  2269.     idToken token;
  2270.     char buf[128];
  2271.  
  2272.     if ( !idParser::DollarEvaluate( NULL, &value, false ) ) {
  2273.         return false;
  2274.     }
  2275.  
  2276.     token.line = idParser::scriptstack->GetLineNum();
  2277.     token.whiteSpaceStart_p = NULL;
  2278.     token.whiteSpaceEnd_p = NULL;
  2279.     token.linesCrossed = 0;
  2280.     token.flags = 0;
  2281.     sprintf( buf, "%1.2f", fabs( value ) );
  2282.     token = buf;
  2283.     token.type = TT_NUMBER;
  2284.     token.subtype = TT_FLOAT | TT_LONG | TT_DECIMAL | TT_VALUESVALID;
  2285.     token.intvalue = (unsigned long) fabs( value );
  2286.     token.floatvalue = fabs( value );
  2287.     idParser::UnreadSourceToken( &token );
  2288.     if ( value < 0 ) {
  2289.         idParser::UnreadSignToken();
  2290.     }
  2291.     return true;
  2292. }
  2293.  
  2294. /*
  2295. ================
  2296. idParser::ReadDollarDirective
  2297. ================
  2298. */
  2299. int idParser::ReadDollarDirective( void ) {
  2300.     idToken token;
  2301.  
  2302.     // read the directive name
  2303.     if ( !idParser::ReadSourceToken( &token ) ) {
  2304.         idParser::Error( "found '$' without name" );
  2305.         return false;
  2306.     }
  2307.     // directive name must be on the same line
  2308.     if ( token.linesCrossed > 0 ) {
  2309.         idParser::UnreadSourceToken( &token );
  2310.         idParser::Error( "found '$' at end of line" );
  2311.         return false;
  2312.     }
  2313.     // if if is a name
  2314.     if (token.type == TT_NAME) {
  2315.         if ( token == "evalint" ) {
  2316.             return idParser::DollarDirective_evalint();
  2317.         }
  2318.         else if ( token == "evalfloat" ) {
  2319.             return idParser::DollarDirective_evalfloat();
  2320.         }
  2321.     }
  2322.     idParser::UnreadSourceToken( &token );
  2323.     return false;
  2324. }
  2325.  
  2326. /*
  2327. ================
  2328. idParser::ReadToken
  2329. ================
  2330. */
  2331. int idParser::ReadToken( idToken *token ) {
  2332.     define_t *define;
  2333.  
  2334.     while(1) {
  2335.         if ( !idParser::ReadSourceToken( token ) ) {
  2336.             return false;
  2337.         }
  2338.         // check for precompiler directives
  2339.         if ( token->type == TT_PUNCTUATION && (*token)[0] == '#' && (*token)[1] == '\0' ) {
  2340.             // read the precompiler directive
  2341.             if ( !idParser::ReadDirective() ) {
  2342.                 return false;
  2343.             }
  2344.             continue;
  2345.         }
  2346.         // if skipping source because of conditional compilation
  2347.         if ( idParser::skip ) {
  2348.             continue;
  2349.         }
  2350.         // recursively concatenate strings that are behind each other still resolving defines
  2351.         if ( token->type == TT_STRING && !(idParser::scriptstack->GetFlags() & LEXFL_NOSTRINGCONCAT) ) {
  2352.             idToken newtoken;
  2353.             if ( idParser::ReadToken( &newtoken ) ) {
  2354.                 if ( newtoken.type == TT_STRING ) {
  2355.                     token->Append( newtoken.c_str() );
  2356.                 }
  2357.                 else {
  2358.                     idParser::UnreadSourceToken( &newtoken );
  2359.                 }
  2360.             }
  2361.         }
  2362.         //
  2363.         if ( !(idParser::scriptstack->GetFlags() & LEXFL_NODOLLARPRECOMPILE) ) {
  2364.             // check for special precompiler directives
  2365.             if ( token->type == TT_PUNCTUATION && (*token)[0] == '$' && (*token)[1] == '\0' ) {
  2366.                 // read the precompiler directive
  2367.                 if ( idParser::ReadDollarDirective() ) {
  2368.                     continue;
  2369.                 }
  2370.             }
  2371.         }
  2372.         // if the token is a name
  2373.         if ( token->type == TT_NAME && !( token->flags & TOKEN_FL_RECURSIVE_DEFINE ) ) {
  2374.             // check if the name is a define macro
  2375.             define = FindHashedDefine( idParser::definehash, token->c_str() );
  2376.             // if it is a define macro
  2377.             if ( define ) {
  2378.                 // expand the defined macro
  2379.                 if ( !idParser::ExpandDefineIntoSource( token, define ) ) {
  2380.                     return false;
  2381.                 }
  2382.                 continue;
  2383.             }
  2384.         }
  2385.         // found a token
  2386.         return true;
  2387.     }
  2388. }
  2389.  
  2390. /*
  2391. ================
  2392. idParser::ExpectTokenString
  2393. ================
  2394. */
  2395. int idParser::ExpectTokenString( const char *string ) {
  2396.     idToken token;
  2397.  
  2398.     if ( !idParser::ReadToken( &token ) ) {
  2399.         idParser::Error( "couldn't find expected '%s'", string );
  2400.         return false;
  2401.     }
  2402.  
  2403.     if ( token != string ) {
  2404.         idParser::Error( "expected '%s' but found '%s'", string, token.c_str() );
  2405.         return false;
  2406.     }
  2407.     return true;
  2408. }
  2409.  
  2410. /*
  2411. ================
  2412. idParser::ExpectTokenType
  2413. ================
  2414. */
  2415. int idParser::ExpectTokenType( int type, int subtype, idToken *token ) {
  2416.     idStr str;
  2417.  
  2418.     if ( !idParser::ReadToken( token ) ) {
  2419.         idParser::Error( "couldn't read expected token" );
  2420.         return 0;
  2421.     }
  2422.  
  2423.     if ( token->type != type ) {
  2424.         switch( type ) {
  2425.             case TT_STRING: str = "string"; break;
  2426.             case TT_LITERAL: str = "literal"; break;
  2427.             case TT_NUMBER: str = "number"; break;
  2428.             case TT_NAME: str = "name"; break;
  2429.             case TT_PUNCTUATION: str = "punctuation"; break;
  2430.             default: str = "unknown type"; break;
  2431.         }
  2432.         idParser::Error( "expected a %s but found '%s'", str.c_str(), token->c_str() );
  2433.         return 0;
  2434.     }
  2435.     if ( token->type == TT_NUMBER ) {
  2436.         if ( (token->subtype & subtype) != subtype ) {
  2437.             str.Clear();
  2438.             if ( subtype & TT_DECIMAL ) str = "decimal ";
  2439.             if ( subtype & TT_HEX ) str = "hex ";
  2440.             if ( subtype & TT_OCTAL ) str = "octal ";
  2441.             if ( subtype & TT_BINARY ) str = "binary ";
  2442.             if ( subtype & TT_UNSIGNED ) str += "unsigned ";
  2443.             if ( subtype & TT_LONG ) str += "long ";
  2444.             if ( subtype & TT_FLOAT ) str += "float ";
  2445.             if ( subtype & TT_INTEGER ) str += "integer ";
  2446.             str.StripTrailing( ' ' );
  2447.             idParser::Error( "expected %s but found '%s'", str.c_str(), token->c_str() );
  2448.             return 0;
  2449.         }
  2450.     }
  2451.     else if ( token->type == TT_PUNCTUATION ) {
  2452.         if ( subtype < 0 ) {
  2453.             idParser::Error( "BUG: wrong punctuation subtype" );
  2454.             return 0;
  2455.         }
  2456.         if ( token->subtype != subtype ) {
  2457.             idParser::Error( "expected '%s' but found '%s'", scriptstack->GetPunctuationFromId( subtype ), token->c_str() );
  2458.             return 0;
  2459.         }
  2460.     }
  2461.     return 1;
  2462. }
  2463.  
  2464. /*
  2465. ================
  2466. idParser::ExpectAnyToken
  2467. ================
  2468. */
  2469. int idParser::ExpectAnyToken( idToken *token ) {
  2470.     if (!idParser::ReadToken( token )) {
  2471.         idParser::Error( "couldn't read expected token" );
  2472.         return false;
  2473.     }
  2474.     else {
  2475.         return true;
  2476.     }
  2477. }
  2478.  
  2479. /*
  2480. ================
  2481. idParser::CheckTokenString
  2482. ================
  2483. */
  2484. int idParser::CheckTokenString( const char *string ) {
  2485.     idToken tok;
  2486.  
  2487.     if (!idParser::ReadToken( &tok )) {
  2488.         return false;
  2489.     }
  2490.     //if the token is available
  2491.     if ( tok == string ) {
  2492.         return true;
  2493.     }
  2494.     //
  2495.     idParser::UnreadSourceToken( &tok );
  2496.     return false;
  2497. }
  2498.  
  2499. /*
  2500. ================
  2501. idParser::CheckTokenType
  2502. ================
  2503. */
  2504. int idParser::CheckTokenType( int type, int subtype, idToken *token ) {
  2505.     idToken tok;
  2506.  
  2507.     if (!idParser::ReadToken( &tok )) {
  2508.         return false;
  2509.     }
  2510.     //if the type matches
  2511.     if (tok.type == type && (tok.subtype & subtype) == subtype) {
  2512.         *token = tok;
  2513.         return true;
  2514.     }
  2515.     //
  2516.     idParser::UnreadSourceToken( &tok );
  2517.     return false;
  2518. }
  2519.  
  2520. /*
  2521. ================
  2522. idParser::SkipUntilString
  2523. ================
  2524. */
  2525. int idParser::SkipUntilString( const char *string ) {
  2526.     idToken token;
  2527.  
  2528.     while(idParser::ReadToken( &token )) {
  2529.         if ( token == string ) {
  2530.             return true;
  2531.         }
  2532.     }
  2533.     return false;
  2534. }
  2535.  
  2536. /*
  2537. ================
  2538. idParser::SkipRestOfLine
  2539. ================
  2540. */
  2541. int idParser::SkipRestOfLine( void ) {
  2542.     idToken token;
  2543.  
  2544.     while(idParser::ReadToken( &token )) {
  2545.         if ( token.linesCrossed ) {
  2546.             idParser::UnreadSourceToken( &token );
  2547.             return true;
  2548.         }
  2549.     }
  2550.     return false;
  2551. }
  2552.  
  2553. /*
  2554. =================
  2555. idParser::SkipBracedSection
  2556.  
  2557. Skips until a matching close brace is found.
  2558. Internal brace depths are properly skipped.
  2559. =================
  2560. */
  2561. int idParser::SkipBracedSection( bool parseFirstBrace ) {
  2562.     idToken token;
  2563.     int depth;
  2564.  
  2565.     depth = parseFirstBrace ? 0 : 1;
  2566.     do {
  2567.         if ( !ReadToken( &token ) ) {
  2568.             return false;
  2569.         }
  2570.         if( token.type == TT_PUNCTUATION ) {
  2571.             if( token == "{" ) {
  2572.                 depth++;
  2573.             } else if ( token == "}" ) {
  2574.                 depth--;
  2575.             }
  2576.         }
  2577.     } while( depth );
  2578.     return true;
  2579. }
  2580.  
  2581. /*
  2582. =================
  2583. idParser::ParseBracedSectionExact
  2584.  
  2585. The next token should be an open brace.
  2586. Parses until a matching close brace is found.
  2587. Maintains the exact formating of the braced section
  2588.  
  2589.   FIXME: what about precompilation ?
  2590. =================
  2591. */
  2592. const char *idParser::ParseBracedSectionExact( idStr &out, int tabs ) {
  2593.     return scriptstack->ParseBracedSectionExact( out, tabs );
  2594. }
  2595.  
  2596. /*
  2597. =================
  2598. idParser::ParseBracedSection
  2599.  
  2600. The next token should be an open brace.
  2601. Parses until a matching close brace is found.
  2602. Internal brace depths are properly skipped.
  2603. =================
  2604. */
  2605. const char *idParser::ParseBracedSection( idStr &out, int tabs ) {
  2606.     idToken token;
  2607.     int i, depth;
  2608.     bool doTabs = false;
  2609.     if (tabs >= 0) {
  2610.         doTabs = true;
  2611.     }
  2612.  
  2613.     out.Empty();
  2614.     if ( !idParser::ExpectTokenString( "{" ) ) {
  2615.         return out.c_str();
  2616.     }
  2617.     out = "{";
  2618.     depth = 1;
  2619.     do {
  2620.         if ( !idParser::ReadToken( &token ) ) {
  2621.             Error( "missing closing brace" );
  2622.             return out.c_str();
  2623.         }
  2624.  
  2625.         // if the token is on a new line
  2626.         for ( i = 0; i < token.linesCrossed; i++ ) {
  2627.             out += "\r\n";
  2628.         }
  2629.  
  2630.         if (doTabs && token.linesCrossed) {
  2631.             i = tabs;
  2632.             if (token[0] == '}' && i > 0) {
  2633.                 i--;
  2634.             }
  2635.             while (i-- > 0) {
  2636.                 out += "\t";
  2637.             }
  2638.         }
  2639.         if ( token.type == TT_PUNCTUATION ) {
  2640.             if ( token[0] == '{' ) {
  2641.                 depth++;
  2642.                 if (doTabs) {
  2643.                     tabs++;
  2644.                 }
  2645.             }
  2646.             else if ( token[0] == '}' ) {
  2647.                 depth--;
  2648.                 if (doTabs) {
  2649.                     tabs--;
  2650.                 }
  2651.             }
  2652.         }
  2653.  
  2654.         if ( token.type == TT_STRING ) {
  2655.             out += "\"" + token + "\"";
  2656.         }
  2657.         else {
  2658.             out += token;
  2659.         }
  2660.         out += " ";
  2661.     } while( depth );
  2662.  
  2663.     return out.c_str();
  2664. }
  2665.  
  2666. /*
  2667. =================
  2668. idParser::ParseRestOfLine
  2669.  
  2670.   parse the rest of the line
  2671. =================
  2672. */
  2673. const char *idParser::ParseRestOfLine( idStr &out ) {
  2674.     idToken token;
  2675.  
  2676.     out.Empty();
  2677.     while(idParser::ReadToken( &token )) {
  2678.         if ( token.linesCrossed ) {
  2679.             idParser::UnreadSourceToken( &token );
  2680.             break;
  2681.         }
  2682.         if ( out.Length() ) {
  2683.             out += " ";
  2684.         }
  2685.         out += token;
  2686.     }
  2687.     return out.c_str();
  2688. }
  2689.  
  2690. /*
  2691. ================
  2692. idParser::UnreadToken
  2693. ================
  2694. */
  2695. void idParser::UnreadToken( idToken *token ) {
  2696.     idParser::UnreadSourceToken( token );
  2697. }
  2698.  
  2699. /*
  2700. ================
  2701. idParser::ReadTokenOnLine
  2702. ================
  2703. */
  2704. int idParser::ReadTokenOnLine( idToken *token ) {
  2705.     idToken tok;
  2706.  
  2707.     if (!idParser::ReadToken( &tok )) {
  2708.         return false;
  2709.     }
  2710.     // if no lines were crossed before this token
  2711.     if ( !tok.linesCrossed ) {
  2712.         *token = tok;
  2713.         return true;
  2714.     }
  2715.     //
  2716.     idParser::UnreadSourceToken( &tok );
  2717.     return false;
  2718. }
  2719.  
  2720. /*
  2721. ================
  2722. idParser::ParseInt
  2723. ================
  2724. */
  2725. int idParser::ParseInt( void ) {
  2726.     idToken token;
  2727.  
  2728.     if ( !idParser::ReadToken( &token ) ) {
  2729.         idParser::Error( "couldn't read expected integer" );
  2730.         return 0;
  2731.     }
  2732.     if ( token.type == TT_PUNCTUATION && token == "-" ) {
  2733.         idParser::ExpectTokenType( TT_NUMBER, TT_INTEGER, &token );
  2734.         return -((signed int) token.GetIntValue());
  2735.     }
  2736.     else if ( token.type != TT_NUMBER || token.subtype == TT_FLOAT ) {
  2737.         idParser::Error( "expected integer value, found '%s'", token.c_str() );
  2738.     }
  2739.     return token.GetIntValue();
  2740. }
  2741.  
  2742. /*
  2743. ================
  2744. idParser::ParseBool
  2745. ================
  2746. */
  2747. bool idParser::ParseBool( void ) {
  2748.     idToken token;
  2749.  
  2750.     if ( !idParser::ExpectTokenType( TT_NUMBER, 0, &token ) ) {
  2751.         idParser::Error( "couldn't read expected boolean" );
  2752.         return false;
  2753.     }
  2754.     return ( token.GetIntValue() != 0 );
  2755. }
  2756.  
  2757. /*
  2758. ================
  2759. idParser::ParseFloat
  2760. ================
  2761. */
  2762. float idParser::ParseFloat( void ) {
  2763.     idToken token;
  2764.  
  2765.     if ( !idParser::ReadToken( &token ) ) {
  2766.         idParser::Error( "couldn't read expected floating point number" );
  2767.         return 0.0f;
  2768.     }
  2769.     if ( token.type == TT_PUNCTUATION && token == "-" ) {
  2770.         idParser::ExpectTokenType( TT_NUMBER, 0, &token );
  2771.         return -token.GetFloatValue();
  2772.     }
  2773.     else if ( token.type != TT_NUMBER ) {
  2774.         idParser::Error( "expected float value, found '%s'", token.c_str() );
  2775.     }
  2776.     return token.GetFloatValue();
  2777. }
  2778.  
  2779. /*
  2780. ================
  2781. idParser::Parse1DMatrix
  2782. ================
  2783. */
  2784. int idParser::Parse1DMatrix( int x, float *m ) {
  2785.     int i;
  2786.  
  2787.     if ( !idParser::ExpectTokenString( "(" ) ) {
  2788.         return false;
  2789.     }
  2790.  
  2791.     for ( i = 0; i < x; i++ ) {
  2792.         m[i] = idParser::ParseFloat();
  2793.     }
  2794.  
  2795.     if ( !idParser::ExpectTokenString( ")" ) ) {
  2796.         return false;
  2797.     }
  2798.     return true;
  2799. }
  2800.  
  2801. /*
  2802. ================
  2803. idParser::Parse2DMatrix
  2804. ================
  2805. */
  2806. int idParser::Parse2DMatrix( int y, int x, float *m ) {
  2807.     int i;
  2808.  
  2809.     if ( !idParser::ExpectTokenString( "(" ) ) {
  2810.         return false;
  2811.     }
  2812.  
  2813.     for ( i = 0; i < y; i++ ) {
  2814.         if ( !idParser::Parse1DMatrix( x, m + i * x ) ) {
  2815.             return false;
  2816.         }
  2817.     }
  2818.  
  2819.     if ( !idParser::ExpectTokenString( ")" ) ) {
  2820.         return false;
  2821.     }
  2822.     return true;
  2823. }
  2824.  
  2825. /*
  2826. ================
  2827. idParser::Parse3DMatrix
  2828. ================
  2829. */
  2830. int idParser::Parse3DMatrix( int z, int y, int x, float *m ) {
  2831.     int i;
  2832.  
  2833.     if ( !idParser::ExpectTokenString( "(" ) ) {
  2834.         return false;
  2835.     }
  2836.  
  2837.     for ( i = 0 ; i < z; i++ ) {
  2838.         if ( !idParser::Parse2DMatrix( y, x, m + i * x*y ) ) {
  2839.             return false;
  2840.         }
  2841.     }
  2842.  
  2843.     if ( !idParser::ExpectTokenString( ")" ) ) {
  2844.         return false;
  2845.     }
  2846.     return true;
  2847. }
  2848.  
  2849. /*
  2850. ================
  2851. idParser::GetLastWhiteSpace
  2852. ================
  2853. */
  2854. int idParser::GetLastWhiteSpace( idStr &whiteSpace ) const {
  2855.     if ( scriptstack ) {
  2856.         scriptstack->GetLastWhiteSpace( whiteSpace );
  2857.     } else {
  2858.         whiteSpace.Clear();
  2859.     }
  2860.     return whiteSpace.Length();
  2861. }
  2862.  
  2863. /*
  2864. ================
  2865. idParser::SetMarker
  2866. ================
  2867. */
  2868. void idParser::SetMarker( void ) {
  2869.     marker_p = NULL;
  2870. }
  2871.  
  2872. /*
  2873. ================
  2874. idParser::GetStringFromMarker
  2875.  
  2876.   FIXME: this is very bad code, the script isn't even garrenteed to still be around
  2877. ================
  2878. */
  2879. void idParser::GetStringFromMarker( idStr& out, bool clean ) {
  2880.     char*    p;
  2881.     char    save;
  2882.  
  2883.     if ( marker_p == NULL ) {
  2884.         marker_p = scriptstack->buffer;
  2885.     }
  2886.         
  2887.     if ( tokens ) {
  2888.         p = (char*)tokens->whiteSpaceStart_p;
  2889.     } else {
  2890.         p = (char*)scriptstack->script_p;
  2891.     }
  2892.     
  2893.     // Set the end character to NULL to give us a complete string
  2894.     save = *p;
  2895.     *p = 0;
  2896.     
  2897.     // If cleaning then reparse
  2898.     if ( clean ) {    
  2899.         idParser temp( marker_p, strlen( marker_p ), "temp", flags );
  2900.         idToken token;
  2901.         while ( temp.ReadToken ( &token ) ) {
  2902.             out += token;
  2903.         }
  2904.     } else {
  2905.         out = marker_p;
  2906.     }
  2907.     
  2908.     // restore the character we set to NULL
  2909.     *p = save;        
  2910. }
  2911.  
  2912. /*
  2913. ================
  2914. idParser::SetIncludePath
  2915. ================
  2916. */
  2917. void idParser::SetIncludePath( const char *path ) {
  2918.     idParser::includepath = path;
  2919.     // add trailing path seperator
  2920.     if (idParser::includepath[idParser::includepath.Length()-1] != '\\' &&
  2921.         idParser::includepath[idParser::includepath.Length()-1] != '/') {
  2922. // RAVEN BEGIN
  2923.         idParser::includepath += "/";
  2924. // RAVEN END
  2925.     }
  2926. }
  2927.  
  2928. /*
  2929. ================
  2930. idParser::SetPunctuations
  2931. ================
  2932. */
  2933. void idParser::SetPunctuations( const punctuation_t *p ) {
  2934.     idParser::punctuations = p;
  2935. }
  2936.  
  2937. /*
  2938. ================
  2939. idParser::SetFlags
  2940. ================
  2941. */
  2942. void idParser::SetFlags( int flags ) {
  2943.     idLexer *s;
  2944.  
  2945.     idParser::flags = flags;
  2946.     for ( s = idParser::scriptstack; s; s = s->next ) {
  2947.         s->SetFlags( flags );
  2948.     }
  2949. }
  2950.  
  2951. /*
  2952. ================
  2953. idParser::GetFlags
  2954. ================
  2955. */
  2956. int idParser::GetFlags( void ) const {
  2957.     return idParser::flags;
  2958. }
  2959.  
  2960. /*
  2961. ================
  2962. idParser::LoadFile
  2963. ================
  2964. */
  2965. int idParser::LoadFile( const char *filename, bool OSPath ) {
  2966.     idLexer *script;
  2967.  
  2968.     if ( idParser::loaded ) {
  2969.         idLib::common->FatalError("idParser::loadFile: another source already loaded");
  2970.         return false;
  2971.     }
  2972.     script = new idLexer( filename, 0, OSPath );
  2973.     if ( !script->IsLoaded() ) {
  2974.         delete script;
  2975.         return false;
  2976.     }
  2977.     script->SetFlags( idParser::flags );
  2978.     script->SetPunctuations( idParser::punctuations );
  2979.     script->next = NULL;
  2980.     idParser::OSPath = OSPath;
  2981.     idParser::filename = filename;
  2982.     idParser::scriptstack = script;
  2983.     idParser::tokens = NULL;
  2984.     idParser::indentstack = NULL;
  2985.     idParser::skip = 0;
  2986.     idParser::loaded = true;
  2987.  
  2988.     if ( !idParser::definehash ) {
  2989.         idParser::defines = NULL;
  2990. //RAVEN BEGIN
  2991. //amccarthy: Added memory allocation tag
  2992.         idParser::definehash = (define_t **) Mem_ClearedAlloc( DEFINEHASHSIZE * sizeof(define_t *), MA_PARSER );
  2993. //RAVEN END
  2994.         idParser::AddGlobalDefinesToSource();
  2995.     }
  2996.     return true;
  2997. }
  2998.  
  2999. /*
  3000. ================
  3001. idParser::LoadMemory
  3002. ================
  3003. */
  3004. int idParser::LoadMemory(const char *ptr, int length, const char *name ) {
  3005.     idLexer *script;
  3006.  
  3007.     if ( idParser::loaded ) {
  3008.         idLib::common->FatalError("idParser::loadMemory: another source already loaded");
  3009.         return false;
  3010.     }
  3011.     script = new idLexer( ptr, length, name );
  3012.     if ( !script->IsLoaded() ) {
  3013.         delete script;
  3014.         return false;
  3015.     }
  3016.     script->SetFlags( idParser::flags );
  3017.     script->SetPunctuations( idParser::punctuations );
  3018.     script->next = NULL;
  3019.     idParser::filename = name;
  3020.     idParser::scriptstack = script;
  3021.     idParser::tokens = NULL;
  3022.     idParser::indentstack = NULL;
  3023.     idParser::skip = 0;
  3024.     idParser::loaded = true;
  3025.  
  3026.     if ( !idParser::definehash ) {
  3027.         idParser::defines = NULL;
  3028. //RAVEN BEGIN
  3029. //amccarthy: Added memory allocation tag
  3030.         idParser::definehash = (define_t **) Mem_ClearedAlloc( DEFINEHASHSIZE * sizeof(define_t *), MA_PARSER );
  3031. //RAVEN END
  3032.         idParser::AddGlobalDefinesToSource();
  3033.     }
  3034.     return true;
  3035. }
  3036.  
  3037. /*
  3038. ================
  3039. idParser::FreeSource
  3040. ================
  3041. */
  3042. void idParser::FreeSource( bool keepDefines ) {
  3043.     idLexer *script;
  3044.     idToken *token;
  3045.     define_t *define;
  3046.     indent_t *indent;
  3047.     int i;
  3048.  
  3049.     // free all the scripts
  3050.     while(idParser::scriptstack) {
  3051.         script = idParser::scriptstack;
  3052.         idParser::scriptstack = idParser::scriptstack->next;
  3053.         delete script;
  3054.     }
  3055.     // free all the tokens
  3056.     while(idParser::tokens) {
  3057.         token = idParser::tokens;
  3058.         idParser::tokens = idParser::tokens->next;
  3059.         delete token;
  3060.     }
  3061.     // free all indents
  3062.     while(idParser::indentstack) {
  3063.         indent = idParser::indentstack;
  3064.         idParser::indentstack = idParser::indentstack->next;
  3065.         Mem_Free( indent );
  3066.     }
  3067.     if ( !keepDefines ) {
  3068.         // free hash table
  3069.         if ( idParser::definehash ) {
  3070.             // free defines
  3071.             for ( i = 0; i < DEFINEHASHSIZE; i++ ) {
  3072.                 while( idParser::definehash[i] ) {
  3073.                     define = idParser::definehash[i];
  3074.                     idParser::definehash[i] = idParser::definehash[i]->hashnext;
  3075.                     FreeDefine(define);
  3076.                 }
  3077.             }
  3078.             idParser::defines = NULL;
  3079.             Mem_Free( idParser::definehash );
  3080.             idParser::definehash = NULL;
  3081.         }
  3082.     }
  3083.     idParser::loaded = false;
  3084. }
  3085.  
  3086. /*
  3087. ================
  3088. idParser::GetPunctuationFromId
  3089. ================
  3090. */
  3091. const char *idParser::GetPunctuationFromId( int id ) {
  3092.     int i;
  3093.  
  3094.     if ( !idParser::punctuations ) {
  3095.         idLexer lex;
  3096.         return lex.GetPunctuationFromId( id );
  3097.     }
  3098.  
  3099.     for (i = 0; idParser::punctuations[i].p; i++) {
  3100.         if ( idParser::punctuations[i].n == id ) {
  3101.             return idParser::punctuations[i].p;
  3102.         }
  3103.     }
  3104.     return "unkown punctuation";
  3105. }
  3106.  
  3107. /*
  3108. ================
  3109. idParser::GetPunctuationId
  3110. ================
  3111. */
  3112. int idParser::GetPunctuationId( const char *p ) {
  3113.     int i;
  3114.  
  3115.     if ( !idParser::punctuations ) {
  3116.         idLexer lex;
  3117.         return lex.GetPunctuationId( p );
  3118.     }
  3119.  
  3120.     for (i = 0; idParser::punctuations[i].p; i++) {
  3121.         if ( !idStr::Cmp(idParser::punctuations[i].p, p) ) {
  3122.             return idParser::punctuations[i].n;
  3123.         }
  3124.     }
  3125.     return 0;
  3126. }
  3127.  
  3128. /*
  3129. ================
  3130. idParser::idParser
  3131. ================
  3132. */
  3133. idParser::idParser() {
  3134.     this->loaded = false;
  3135.     this->OSPath = false;
  3136.     this->punctuations = 0;
  3137.     this->flags = 0;
  3138.     this->scriptstack = NULL;
  3139.     this->indentstack = NULL;
  3140.     this->definehash = NULL;
  3141.     this->defines = NULL;
  3142.     this->tokens = NULL;
  3143.     this->marker_p = NULL;
  3144.  
  3145. // RAVEN BEGIN
  3146. // bdube: added members    
  3147.     marker_p = NULL;
  3148. // RAVEN END
  3149. }
  3150.  
  3151. /*
  3152. ================
  3153. idParser::idParser
  3154. ================
  3155. */
  3156. idParser::idParser( int flags ) {
  3157.     this->loaded = false;
  3158.     this->OSPath = false;
  3159.     this->punctuations = 0;
  3160.     this->flags = flags;
  3161.     this->scriptstack = NULL;
  3162.     this->indentstack = NULL;
  3163.     this->definehash = NULL;
  3164.     this->defines = NULL;
  3165.     this->tokens = NULL;
  3166.     this->marker_p = NULL;
  3167.  
  3168. // RAVEN BEGIN
  3169. // bdube: added members    
  3170.     marker_p = NULL;
  3171. // RAVEN END
  3172. }
  3173.  
  3174. /*
  3175. ================
  3176. idParser::idParser
  3177. ================
  3178. */
  3179. idParser::idParser( const char *filename, int flags, bool OSPath ) {
  3180.     this->loaded = false;
  3181.     this->OSPath = true;
  3182.     this->punctuations = 0;
  3183.     this->flags = flags;
  3184.     this->scriptstack = NULL;
  3185.     this->indentstack = NULL;
  3186.     this->definehash = NULL;
  3187.     this->defines = NULL;
  3188.     this->tokens = NULL;
  3189.     this->marker_p = NULL;
  3190.     LoadFile( filename, OSPath );
  3191. }
  3192.  
  3193. /*
  3194. ================
  3195. idParser::idParser
  3196. ================
  3197. */
  3198. idParser::idParser( const char *ptr, int length, const char *name, int flags ) {
  3199.     this->loaded = false;
  3200.     this->OSPath = false;
  3201.     this->punctuations = 0;
  3202.     this->flags = flags;
  3203.     this->scriptstack = NULL;
  3204.     this->indentstack = NULL;
  3205.     this->definehash = NULL;
  3206.     this->defines = NULL;
  3207.     this->tokens = NULL;
  3208.     this->marker_p = NULL;
  3209.     LoadMemory( ptr, length, name );
  3210. }
  3211.  
  3212. /*
  3213. ================
  3214. idParser::~idParser
  3215. ================
  3216. */
  3217. idParser::~idParser( void ) {
  3218.     idParser::FreeSource( false );
  3219. }
  3220.  
  3221.