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

  1.  
  2. #include "precompiled.h"
  3. #pragma hdrstop
  4.  
  5. #ifdef LEXER_READ_AHEAD
  6.  
  7. namespace {
  8.  
  9.     #define IO_BUFFER_CHUNK_SIZE (1024 * 64)
  10.  
  11.     enum {
  12.         IOJobPending = 0,
  13.         IOJobQuit,
  14.         IOJobComplete,
  15.         IOJobCount
  16.     };
  17.     static HANDLE mIOJobs[IOJobCount];
  18.  
  19.     static DWORD mThreadId = 0;
  20.     static HANDLE mThreadHandle = 0;
  21.  
  22.  
  23.     struct IOJobData {
  24.         idFile *mFile;
  25.         int mAmtToRead;
  26.         byte mBuffer[IO_BUFFER_CHUNK_SIZE];
  27.     };
  28.  
  29.     IOJobData gIOJobData;
  30.  
  31.     void SetPendingJob(idFile *f, int size) {
  32.         gIOJobData.mFile = f;
  33.         gIOJobData.mAmtToRead = size;
  34.         
  35.         if ( mThreadHandle ) {
  36.             SetEvent( mIOJobs[IOJobPending] );
  37.         }
  38.     }
  39.  
  40.     IOJobData *GetPendingJob() {
  41.         return &gIOJobData;
  42.     }
  43.  
  44.     void GetPendingJobResults(byte *dest, int &size) {
  45.         if ( mThreadHandle ) {        
  46.             if ( WAIT_OBJECT_0 == WaitForSingleObject( mIOJobs[IOJobComplete], INFINITE ) ) {
  47.                 //ResetEvent( mIOJobs[IOJobComplete] );
  48.                 size = gIOJobData.mAmtToRead;
  49.                 memcpy(dest, gIOJobData.mBuffer, size );
  50.             } else {
  51.                 assert( 0 );
  52.             }
  53.         } else {
  54.             gIOJobData.mFile->Read( dest, gIOJobData.mAmtToRead );
  55.             size = gIOJobData.mAmtToRead;
  56.         }
  57.     }
  58.  
  59.     static LONG IOThread( void * ) {
  60.         while ( 1 ) {
  61.             DWORD res = WaitForMultipleObjects( 2, mIOJobs, FALSE, INFINITE );        
  62.             if ( res == WAIT_OBJECT_0 ) {
  63.                 //ResetEvent( mIOJobs[IOJobPending] );
  64.                 IOJobData *j = GetPendingJob();
  65.                 j->mFile->Read( j->mBuffer, j->mAmtToRead );
  66.                 SetEvent( mIOJobs[IOJobComplete] );
  67.             } else if ( res == (WAIT_OBJECT_0 + 1) ) {
  68.                 ExitThread(0);
  69.             } else { 
  70.                 assert( 0 );
  71.             }
  72.         }
  73.         ExitThread(0);
  74.     }
  75.  
  76.     void InitIO() {
  77.         for ( int i = 0; i < IOJobCount; ++i ) {
  78.             //mIOJobs[i] = CreateEvent( 0, TRUE, FALSE, 0 );
  79.             mIOJobs[i] = CreateEvent( 0, FALSE, FALSE, 0 );
  80.         }
  81.         mThreadHandle = CreateThread( NULL, 1024 * 512, ( LPTHREAD_START_ROUTINE )IOThread, 0, CREATE_SUSPENDED, &mThreadId );
  82.         ResumeThread( mThreadHandle );
  83.     }
  84.  
  85.     void ShutdownIO() {
  86.         
  87.         SetEvent( mIOJobs[IOJobQuit] );
  88.         
  89.         DWORD ecode = 0;
  90.         while ( !GetExitCodeThread( mThreadHandle, &ecode ) ) {
  91.             Sleep( 1 );
  92.         }
  93.         CloseHandle( mThreadHandle );
  94.  
  95.         for ( int i = 0; i < IOJobCount; ++i ) {
  96.             CloseHandle( mIOJobs[i] );
  97.         }
  98.         
  99.         mThreadHandle = 0;
  100.     }
  101.  
  102. }
  103.  
  104. namespace ChunkBuffer {
  105.  
  106.     idFile *mFile;
  107.     int mFileSize;
  108.     int mBytesRead;
  109.     
  110.     int mFileBytesRead;
  111.     
  112.     byte mChunkMem[IO_BUFFER_CHUNK_SIZE];
  113.     int mCurrentChunkBytes;
  114.     int mCurrentChunkBytesRead = 0;
  115.     
  116.     /*
  117.     ================
  118.     ChunkBuffer::Length
  119.     ================
  120.     */
  121.     int Length() {
  122.         return mFileSize;
  123.     }
  124.     
  125.     /*
  126.     ================
  127.     ChunkBuffer::BytesRead
  128.     ================
  129.     */
  130.     int BytesRead() {
  131.         return mBytesRead;
  132.     }
  133.     
  134.     /*
  135.     ================
  136.     ChunkBuffer::Begin
  137.     ================
  138.     */
  139.     void Begin( idFile *f ) {
  140.         mFile = f;
  141.         mFileSize = f->Length();
  142.         mBytesRead = 0;
  143.         
  144.         int initialRead = (mFileSize < IO_BUFFER_CHUNK_SIZE) ? mFileSize : IO_BUFFER_CHUNK_SIZE;
  145.         mFile->Read( mChunkMem, initialRead );
  146.         mFileBytesRead = initialRead;
  147.         mCurrentChunkBytesRead = 0;
  148.         mCurrentChunkBytes = initialRead;
  149.         
  150.         int bytesRemaining = mFileSize - mFileBytesRead;
  151.         if ( bytesRemaining > 0 ) {
  152.             SetPendingJob( mFile, (bytesRemaining > IO_BUFFER_CHUNK_SIZE) ? IO_BUFFER_CHUNK_SIZE : bytesRemaining );
  153.         }
  154.     }
  155.     
  156.     /*
  157.     ================
  158.     ChunkBuffer::End
  159.     ================
  160.     */
  161.     void End() { 
  162.     }
  163.     
  164.     /*
  165.     ================
  166.     ChunkBuffer::Advance
  167.     ================
  168.     */
  169.     int Advance( byte *chunkMem ) {
  170.         int readAmt = 0;
  171.         GetPendingJobResults( chunkMem, readAmt );
  172.         mFileBytesRead+=readAmt;
  173.         
  174.         int bytesRemaining = mFileSize - mFileBytesRead;
  175.         if ( bytesRemaining > 0 ) {
  176.             SetPendingJob( mFile, (bytesRemaining > IO_BUFFER_CHUNK_SIZE) ? IO_BUFFER_CHUNK_SIZE : bytesRemaining );
  177.         }
  178.         
  179.         return readAmt;
  180.     }
  181.     
  182.     /*
  183.     ================
  184.     ChunkBuffer::Read
  185.     ================
  186.     */
  187.     int Read( byte *dest, int size ) {
  188.         int bytesRem = mFileSize - mBytesRead;
  189.         
  190.         if ( size > bytesRem ) {
  191.             size = bytesRem;
  192.         }
  193.         
  194.         if ( !size ) {
  195.             return 0;
  196.         }
  197.  
  198.         byte *writePos = dest;
  199.         int cnt = size;
  200.         
  201.         while ( cnt ) {
  202.             if ( mCurrentChunkBytesRead >= mCurrentChunkBytes ) {
  203.                 mCurrentChunkBytes = Advance( mChunkMem );
  204.                 mCurrentChunkBytesRead = 0;
  205.             }
  206.             
  207.             int chunkBytesRemaining = mCurrentChunkBytes - mCurrentChunkBytesRead;
  208.             int toRead = (cnt > chunkBytesRemaining) ? chunkBytesRemaining : cnt;
  209.             
  210.             byte *readPos = &mChunkMem[mCurrentChunkBytesRead];
  211.             
  212.             memcpy( writePos, readPos, toRead );
  213.             
  214.             cnt-=toRead;
  215.             mCurrentChunkBytesRead+=toRead;
  216.             writePos+=toRead;
  217.         }
  218.         
  219.         mBytesRead+=size;        
  220.         return size;
  221.     }
  222. }
  223.  
  224. /*
  225. ================
  226. LexerIOWrapper::LexerIOWrapper
  227. ================
  228. */
  229. LexerIOWrapper::LexerIOWrapper() {
  230.     file = 0;    
  231.     offset = 0;
  232.     size = 0;
  233.     memory = 0;
  234. }
  235.  
  236. /*
  237. ================
  238. LexerIOWrapper::InitFromMemory
  239. ================
  240. */
  241. void LexerIOWrapper::InitFromMemory(char const * const ptr, int length) {
  242.     file = 0;    
  243.     offset = 0;
  244.     size = length;
  245.     memory = (char *)ptr;
  246. }
  247.  
  248. /*
  249. ================
  250. LexerIOWrapper::OpenFile
  251. ================
  252. */
  253. bool LexerIOWrapper::OpenFile(char const *filename, bool OSPath) {
  254.     if ( OSPath ) {
  255.         file = idLib::fileSystem->OpenExplicitFileRead( filename );
  256.     } else {
  257.         file = idLib::fileSystem->OpenFileRead( filename );
  258.     }
  259.     
  260.     if ( !file ) {
  261.         return false;
  262.     }
  263.     
  264.     ChunkBuffer::Begin( file );
  265.     
  266.     return true;
  267. }
  268.  
  269. /*
  270. ================
  271. LexerIOWrapper::Length
  272. ================
  273. */
  274. int LexerIOWrapper::Length() {
  275.     if ( file ) {
  276.         return ChunkBuffer::Length();
  277.     }
  278.     return size;
  279. }
  280.  
  281. /*
  282. ================
  283. LexerIOWrapper::Tell
  284. ================
  285. */
  286. int LexerIOWrapper::Tell() {
  287.     if ( file ) {
  288.         return ChunkBuffer::BytesRead();
  289.     }
  290.     return offset;
  291. }
  292.  
  293. /*
  294. ================
  295. LexerIOWrapper::Seek
  296. ================
  297. */
  298. void LexerIOWrapper::Seek( int loc, int mode ) { 
  299.     if ( file ) {
  300.         if ( loc != 0 || mode != FS_SEEK_SET ) {
  301.             common->Error( "lol\n" );
  302.         }
  303.         file->Seek( 0, FS_SEEK_SET );
  304.         ChunkBuffer::Begin( file );
  305.         return;
  306.     }
  307.     if ( mode == FS_SEEK_SET ) {
  308.         offset = loc;
  309.     } else {
  310.         common->Error( "lol" );
  311.     }
  312. }
  313.  
  314. /*
  315. ================
  316. LexerIOWrapper::Read
  317. ================
  318. */
  319. void LexerIOWrapper::Read( void *dest, int s ) {
  320.     if ( file ) {
  321.         ChunkBuffer::Read( (byte*)dest, s );
  322.         return;
  323.     }
  324.     char *d = (char *)dest;
  325.     char *dEnd = d + (((s + offset) > size) ? (size - offset) : (s));
  326.     while ( d < dEnd ) {
  327.         *d = memory[offset];
  328.         ++d;
  329.         ++offset;
  330.     }
  331. }
  332.  
  333. /*
  334. ================
  335. LexerIOWrapper::Close
  336. ================
  337. */
  338. void LexerIOWrapper::Close() {
  339.     if ( file ) {
  340.         ChunkBuffer::End();
  341.         idLib::fileSystem->CloseFile(file);
  342.         return;
  343.     }
  344. }
  345.  
  346. /*
  347. ================
  348. LexerIOWrapper::IsLoaded
  349. ================
  350. */
  351. int LexerIOWrapper::IsLoaded() {
  352.     if ( file || memory ) {
  353.         return 1;
  354.     }
  355.     return 0;
  356. }
  357.  
  358. /*
  359. ================
  360. Lexer::BeginLevelLoad
  361. ================
  362. */
  363. void Lexer::BeginLevelLoad() {
  364.     InitIO();
  365. }
  366.  
  367. /*
  368. ================
  369. Lexer::EndLevelLoad
  370. ================
  371. */
  372. void Lexer::EndLevelLoad() {
  373.     ShutdownIO();
  374. }
  375.  
  376. /*
  377. ================
  378. ReadValue
  379. ================
  380. */
  381. template <typename type> inline type ReadValue(LexerIOWrapper *in) 
  382.     type ret; 
  383.     
  384.     in->Read(&ret, sizeof(type));
  385.     return ret;
  386. }
  387.  
  388. /*
  389. ================
  390. ReadValue
  391. specialization read for idStr's
  392. ================
  393. */
  394. template <> inline idStr ReadValue(LexerIOWrapper *in) 
  395.     char c;
  396.     idStr str;
  397.  
  398.     in->Read(&c, 1);
  399.     while(c != '\0')
  400.     {
  401.         str.Append(c);
  402.         in->Read(&c, 1);
  403.     }
  404.  
  405.     str.Append(c);
  406.  
  407.     return str;
  408. }
  409.  
  410. #endif
  411.  
  412. #define PUNCTABLE
  413.  
  414. //longer punctuations first
  415. punctuation_t default_punctuations[] = {
  416.     //binary operators
  417.     {">>=",P_RSHIFT_ASSIGN},
  418.     {"<<=",P_LSHIFT_ASSIGN},
  419.     //
  420.     {"...",P_PARMS},
  421.     //define merge operator
  422.     {"##",P_PRECOMPMERGE},                // pre-compiler
  423.     //logic operators
  424.     {"&&",P_LOGIC_AND},                    // pre-compiler
  425.     {"||",P_LOGIC_OR},                    // pre-compiler
  426.     {">=",P_LOGIC_GEQ},                    // pre-compiler
  427.     {"<=",P_LOGIC_LEQ},                    // pre-compiler
  428.     {"==",P_LOGIC_EQ},                    // pre-compiler
  429.     {"!=",P_LOGIC_UNEQ},                // pre-compiler
  430.     //arithmatic operators
  431.     {"*=",P_MUL_ASSIGN},
  432.     {"/=",P_DIV_ASSIGN},
  433.     {"%=",P_MOD_ASSIGN},
  434.     {"+=",P_ADD_ASSIGN},
  435.     {"-=",P_SUB_ASSIGN},
  436.     {"++",P_INC},
  437.     {"--",P_DEC},
  438.     //binary operators
  439.     {"&=",P_BIN_AND_ASSIGN},
  440.     {"|=",P_BIN_OR_ASSIGN},
  441.     {"^=",P_BIN_XOR_ASSIGN},
  442.     {">>",P_RSHIFT},                    // pre-compiler
  443.     {"<<",P_LSHIFT},                    // pre-compiler
  444.     //reference operators
  445.     {"->",P_POINTERREF},
  446.     //C++
  447.     {"::",P_CPP1},
  448.     {".*",P_CPP2},
  449.     //arithmatic operators
  450.     {"*",P_MUL},                        // pre-compiler
  451.     {"/",P_DIV},                        // pre-compiler
  452.     {"%",P_MOD},                        // pre-compiler
  453.     {"+",P_ADD},                        // pre-compiler
  454.     {"-",P_SUB},                        // pre-compiler
  455.     {"=",P_ASSIGN},
  456.     //binary operators
  457.     {"&",P_BIN_AND},                    // pre-compiler
  458.     {"|",P_BIN_OR},                        // pre-compiler
  459.     {"^",P_BIN_XOR},                    // pre-compiler
  460.     {"~",P_BIN_NOT},                    // pre-compiler
  461.     //logic operators
  462.     {"!",P_LOGIC_NOT},                    // pre-compiler
  463.     {">",P_LOGIC_GREATER},                // pre-compiler
  464.     {"<",P_LOGIC_LESS},                    // pre-compiler
  465.     //reference operator
  466.     {".",P_REF},
  467.     //seperators
  468.     {",",P_COMMA},                        // pre-compiler
  469.     {";",P_SEMICOLON},
  470.     //label indication
  471.     {":",P_COLON},                        // pre-compiler
  472.     //if statement
  473.     {"?",P_QUESTIONMARK},                // pre-compiler
  474.     //embracements
  475.     {"(",P_PARENTHESESOPEN},            // pre-compiler
  476.     {")",P_PARENTHESESCLOSE},            // pre-compiler
  477.     {"{",P_BRACEOPEN},                    // pre-compiler
  478.     {"}",P_BRACECLOSE},                    // pre-compiler
  479.     {"[",P_SQBRACKETOPEN},
  480.     {"]",P_SQBRACKETCLOSE},
  481.     //
  482.     {"\\",P_BACKSLASH},
  483.     //precompiler operator
  484.     {"#",P_PRECOMP},                    // pre-compiler
  485.     {"$",P_DOLLAR},
  486. // RAVEN BEGIN
  487.     {"í",P_INVERTED_PLING},
  488.     {"┐",P_INVERTED_QUERY},
  489. // RAVEN END
  490.     {NULL, 0}
  491. };
  492.  
  493. int default_punctuationtable[256];
  494. int default_nextpunctuation[sizeof(default_punctuations) / sizeof(punctuation_t)];
  495. int default_setup;
  496.  
  497. // RAVEN BEGIN
  498. // jsinger: changed to be Lexer instead of idLexer so that we have the ability to read binary files
  499. char Lexer::baseFolder[ 256 ];
  500.  
  501. // Added this to allow easy changing of the suffix that signifies a binary file
  502. idStr const        Lexer::sCompiledFileSuffix("c");
  503. // RAVEN END
  504.  
  505. /*
  506. ================
  507. idLexer::CreatePunctuationTable
  508. ================
  509. */
  510. void idLexer::CreatePunctuationTable( const punctuation_t *punctuations ) {
  511.     int i, n, lastp;
  512.     const punctuation_t *p, *newp;
  513.  
  514.     //get memory for the table
  515.     if ( punctuations == default_punctuations ) {
  516.         idLexer::punctuationtable = default_punctuationtable;
  517.         idLexer::nextpunctuation = default_nextpunctuation;
  518.         if ( default_setup ) {
  519.             return;
  520.         }
  521.         default_setup = true;
  522.         i = sizeof(default_punctuations) / sizeof(punctuation_t);
  523.     }
  524.     else {
  525.         if ( !idLexer::punctuationtable || idLexer::punctuationtable == default_punctuationtable ) {
  526. //RAVEN BEGIN
  527. //amccarthy: Added memory allocation tag
  528.             idLexer::punctuationtable = (int *) Mem_Alloc(256 * sizeof(int),MA_LEXER);
  529. //RAVEN END
  530.         }
  531.         if ( idLexer::nextpunctuation && idLexer::nextpunctuation != default_nextpunctuation ) {
  532.             Mem_Free( idLexer::nextpunctuation );
  533.         }
  534.         for (i = 0; punctuations[i].p; i++) {
  535.         }
  536. //RAVEN BEGIN
  537. //amccarthy: Added memory allocation tag
  538.         idLexer::nextpunctuation = (int *) Mem_Alloc(i * sizeof(int),MA_LEXER);
  539. //RAVEN END
  540.     }
  541.     memset(idLexer::punctuationtable, 0xFF, 256 * sizeof(int));
  542.     memset(idLexer::nextpunctuation, 0xFF, i * sizeof(int));
  543.     //add the punctuations in the list to the punctuation table
  544.     for (i = 0; punctuations[i].p; i++) {
  545.         newp = &punctuations[i];
  546.         lastp = -1;
  547.         //sort the punctuations in this table entry on length (longer punctuations first)
  548.         for (n = idLexer::punctuationtable[(unsigned int) newp->p[0]]; n >= 0; n = idLexer::nextpunctuation[n] ) {
  549.             p = &punctuations[n];
  550.             if (strlen(p->p) < strlen(newp->p)) {
  551.                 idLexer::nextpunctuation[i] = n;
  552.                 if (lastp >= 0) {
  553.                     idLexer::nextpunctuation[lastp] = i;
  554.                 }
  555.                 else {
  556.                     idLexer::punctuationtable[(unsigned int) newp->p[0]] = i;
  557.                 }
  558.                 break;
  559.             }
  560.             lastp = n;
  561.         }
  562.         if (n < 0) {
  563.             idLexer::nextpunctuation[i] = -1;
  564.             if (lastp >= 0) {
  565.                 idLexer::nextpunctuation[lastp] = i;
  566.             }
  567.             else {
  568.                 idLexer::punctuationtable[(unsigned int) newp->p[0]] = i;
  569.             }
  570.         }
  571.     }
  572. }
  573.  
  574. /*
  575. ================
  576. idLexer::GetPunctuationFromId
  577. ================
  578. */
  579. const char *idLexer::GetPunctuationFromId( int id ) {
  580.     int i;
  581.  
  582.     for (i = 0; idLexer::punctuations[i].p; i++) {
  583.         if ( idLexer::punctuations[i].n == id ) {
  584.             return idLexer::punctuations[i].p;
  585.         }
  586.     }
  587.     return "unkown punctuation";
  588. }
  589.  
  590. /*
  591. ================
  592. idLexer::GetPunctuationId
  593. ================
  594. */
  595. int idLexer::GetPunctuationId( const char *p ) {
  596.     int i;
  597.  
  598.     for (i = 0; idLexer::punctuations[i].p; i++) {
  599.         if ( !idStr::Cmp(idLexer::punctuations[i].p, p) ) {
  600.             return idLexer::punctuations[i].n;
  601.         }
  602.     }
  603.     return 0;
  604. }
  605.  
  606. /*
  607. ================
  608. idLexer::Error
  609. ================
  610. */
  611.  
  612. void idLexer::Error( const char *str, ... ) {
  613.     char text[MAX_STRING_CHARS];
  614.     va_list ap;
  615.  
  616.     hadError = true;
  617.  
  618.     if ( idLexer::flags & LEXFL_NOERRORS ) {
  619.         return;
  620.     }
  621.  
  622.     va_start(ap, str);
  623.     vsprintf(text, str, ap);
  624.     va_end(ap);
  625.  
  626.     if ( idLexer::flags & LEXFL_NOFATALERRORS ) {
  627.         idLib::common->Warning( "file %s, line %d: %s", idLexer::filename.c_str(), idLexer::line, text );
  628.     } else {
  629.         idLib::common->Error( "file %s, line %d: %s", idLexer::filename.c_str(), idLexer::line, text );
  630.     }
  631. }
  632.  
  633. /*
  634. ================
  635. idLexer::Warning
  636. ================
  637. */
  638. void idLexer::Warning( const char *str, ... ) {
  639.     char text[MAX_STRING_CHARS];
  640.     va_list ap;
  641.  
  642.     if ( idLexer::flags & LEXFL_NOWARNINGS ) {
  643.         return;
  644.     }
  645.  
  646.     va_start( ap, str );
  647.     vsprintf( text, str, ap );
  648.     va_end( ap );
  649.     idLib::common->Warning( "file %s, line %d: %s", idLexer::filename.c_str(), idLexer::line, text );
  650. }
  651.  
  652. /*
  653. ================
  654. idLexer::SetPunctuations
  655. ================
  656. */
  657. void idLexer::SetPunctuations( const punctuation_t *p ) {
  658. #ifdef PUNCTABLE
  659.     if (p) {
  660.         idLexer::CreatePunctuationTable( p );
  661.     }
  662.     else {
  663.         idLexer::CreatePunctuationTable( default_punctuations );
  664.     }
  665. #endif //PUNCTABLE
  666.     if (p) {
  667.         idLexer::punctuations = p;
  668.     }
  669.     else {
  670.         idLexer::punctuations = default_punctuations;
  671.     }
  672. }
  673.  
  674. /*
  675. ================
  676. idLexer::ReadWhiteSpace
  677.  
  678. Reads spaces, tabs, C-like comments etc.
  679. When a newline character is found the scripts line counter is increased.
  680. ================
  681. */
  682. int idLexer::ReadWhiteSpace( void ) {
  683.     while(1) {
  684.         // skip white space
  685. // RAVEN BEGIN
  686.         while(( byte )*idLexer::script_p <= ' ') {
  687. // RAVEN END
  688.             if (!*idLexer::script_p) {
  689.                 return 0;
  690.             }
  691.             if (*idLexer::script_p == '\n') {
  692.                 idLexer::line++;
  693.             }
  694.             idLexer::script_p++;
  695.         }
  696.         // skip comments
  697.         if (*idLexer::script_p == '/') {
  698.             // comments //
  699.             if (*(idLexer::script_p+1) == '/') {
  700.                 idLexer::script_p++;
  701.                 do {
  702.                     idLexer::script_p++;
  703.                     if ( !*idLexer::script_p ) {
  704.                         return 0;
  705.                     }
  706.                 }
  707.                 while( *idLexer::script_p != '\n' );
  708.                 idLexer::line++;
  709.                 idLexer::script_p++;
  710.                 if ( !*idLexer::script_p ) {
  711.                     return 0;
  712.                 }
  713.                 continue;
  714.             }
  715.             // comments /* */
  716.             else if (*(idLexer::script_p+1) == '*') {
  717.                 idLexer::script_p++;
  718.                 while( 1 ) {
  719.                     idLexer::script_p++;
  720.                     if ( !*idLexer::script_p ) {
  721.                         return 0;
  722.                     }
  723.                     if ( *idLexer::script_p == '\n' ) {
  724.                         idLexer::line++;
  725.                     }
  726.                     else if ( *idLexer::script_p == '/' ) {
  727.                         if ( *(idLexer::script_p-1) == '*' ) {
  728.                             break;
  729.                         }
  730.                         if ( *(idLexer::script_p+1) == '*' ) {
  731.                             idLexer::Warning( "nested comment" );
  732.                         }
  733.                     }
  734.                 }
  735.                 idLexer::script_p++;
  736.                 if ( !*idLexer::script_p ) {
  737.                     return 0;
  738.                 }
  739.                 idLexer::script_p++;
  740.                 if ( !*idLexer::script_p ) {
  741.                     return 0;
  742.                 }
  743.                 continue;
  744.             }
  745.         }
  746.         break;
  747.     }
  748.     return 1;
  749. }
  750.  
  751. /*
  752. ================
  753. idLexer::ReadEscapeCharacter
  754. ================
  755. */
  756. int idLexer::ReadEscapeCharacter( char *ch ) {
  757.     int c, val, i;
  758.  
  759.     // step over the leading '\\'
  760.     idLexer::script_p++;
  761.     // determine the escape character
  762.     switch(*idLexer::script_p) {
  763.         case '\\': c = '\\'; break;
  764.         case 'n': c = '\n'; break;
  765.         case 'r': c = '\r'; break;
  766.         case 't': c = '\t'; break;
  767.         case 'v': c = '\v'; break;
  768.         case 'b': c = '\b'; break;
  769.         case 'f': c = '\f'; break;
  770.         case 'a': c = '\a'; break;
  771.         case '\'': c = '\''; break;
  772.         case '\"': c = '\"'; break;
  773.         case '\?': c = '\?'; break;
  774.         case 'x':
  775.         {
  776.             idLexer::script_p++;
  777.             for (i = 0, val = 0; ; i++, idLexer::script_p++) {
  778.                 c = *idLexer::script_p;
  779.                 if (c >= '0' && c <= '9')
  780.                     c = c - '0';
  781.                 else if (c >= 'A' && c <= 'Z')
  782.                     c = c - 'A' + 10;
  783.                 else if (c >= 'a' && c <= 'z')
  784.                     c = c - 'a' + 10;
  785.                 else
  786.                     break;
  787.                 val = (val << 4) + c;
  788.             }
  789.             idLexer::script_p--;
  790.             if (val > 0xFF) {
  791.                 idLexer::Warning( "too large value in escape character" );
  792.                 val = 0xFF;
  793.             }
  794.             c = val;
  795.             break;
  796.         }
  797.         default: //NOTE: decimal ASCII code, NOT octal
  798.         {
  799.             if (*idLexer::script_p < '0' || *idLexer::script_p > '9') {
  800.                 idLexer::Error("unknown escape char");
  801.             }
  802.             for (i = 0, val = 0; ; i++, idLexer::script_p++) {
  803.                 c = *idLexer::script_p;
  804.                 if (c >= '0' && c <= '9')
  805.                     c = c - '0';
  806.                 else
  807.                     break;
  808.                 val = val * 10 + c;
  809.             }
  810.             idLexer::script_p--;
  811.             if (val > 0xFF) {
  812.                 idLexer::Warning( "too large value in escape character" );
  813.                 val = 0xFF;
  814.             }
  815.             c = val;
  816.             break;
  817.         }
  818.     }
  819.     // step over the escape character or the last digit of the number
  820.     idLexer::script_p++;
  821.     // store the escape character
  822.     *ch = c;
  823.     // succesfully read escape character
  824.     return 1;
  825. }
  826.  
  827. /*
  828. ================
  829. idLexer::ReadString
  830.  
  831. Escape characters are interpretted.
  832. Reads two strings with only a white space between them as one string.
  833. ================
  834. */
  835. int idLexer::ReadString( idToken *token, int quote ) {
  836.     int tmpline;
  837.     const char *tmpscript_p;
  838.     char ch;
  839.  
  840.     if ( quote == '\"' ) {
  841.         token->type = TT_STRING;
  842.     } else {
  843.         token->type = TT_LITERAL;
  844.     }
  845.  
  846.     // leading quote
  847.     idLexer::script_p++;
  848.  
  849.     while(1) {
  850.         // if there is an escape character and escape characters are allowed
  851.         if (*idLexer::script_p == '\\' && !(idLexer::flags & LEXFL_NOSTRINGESCAPECHARS)) {
  852.             if ( !idLexer::ReadEscapeCharacter( &ch ) ) {
  853.                 return 0;
  854.             }
  855.             token->AppendDirty( ch );
  856.         }
  857.         // if a trailing quote
  858.         else if (*idLexer::script_p == quote) {
  859.             // step over the quote
  860.             idLexer::script_p++;
  861.             // if consecutive strings should not be concatenated
  862.             if ( (idLexer::flags & LEXFL_NOSTRINGCONCAT) &&
  863.                     (!(idLexer::flags & LEXFL_ALLOWBACKSLASHSTRINGCONCAT) || (quote != '\"')) ) {
  864.                 break;
  865.             }
  866.  
  867.             tmpscript_p = idLexer::script_p;
  868.             tmpline = idLexer::line;
  869.             // read white space between possible two consecutive strings
  870.             if ( !idLexer::ReadWhiteSpace() ) {
  871.                 idLexer::script_p = tmpscript_p;
  872.                 idLexer::line = tmpline;
  873.                 break;
  874.             }
  875.  
  876.             if ( idLexer::flags & LEXFL_NOSTRINGCONCAT ) {
  877.                 if ( *idLexer::script_p != '\\' ) {
  878.                     idLexer::script_p = tmpscript_p;
  879.                     idLexer::line = tmpline;
  880.                     break;
  881.                 }
  882.                 // step over the '\\'
  883.                 idLexer::script_p++;
  884.                 if ( !idLexer::ReadWhiteSpace() || ( *idLexer::script_p != quote ) ) {
  885.                     idLexer::Error( "expecting string after '\' terminated line" );
  886.                     return 0;
  887.                 }
  888.             }
  889.  
  890.             // if there's no leading qoute
  891.             if ( *idLexer::script_p != quote ) {
  892.                 idLexer::script_p = tmpscript_p;
  893.                 idLexer::line = tmpline;
  894.                 break;
  895.             }
  896.             // step over the new leading quote
  897.             idLexer::script_p++;
  898.         }
  899.         else {
  900.             if (*idLexer::script_p == '\0') {
  901.                 idLexer::Error( "missing trailing quote" );
  902.                 return 0;
  903.             }
  904.             if (*idLexer::script_p == '\n') {
  905.                 idLexer::Error( "newline inside string" );
  906.                 return 0;
  907.             }
  908.             token->AppendDirty( *idLexer::script_p++ );
  909.         }
  910.     }
  911.     token->data[token->len] = '\0';
  912.  
  913.     if ( token->type == TT_LITERAL ) {
  914.         if ( !(idLexer::flags & LEXFL_ALLOWMULTICHARLITERALS) ) {
  915.             if ( token->Length() != 1 ) {
  916.                 idLexer::Warning( "literal is not one character long" );
  917.             }
  918.         }
  919.         token->subtype = (*token)[0];
  920.     }
  921.     else {
  922.         // the sub type is the length of the string
  923.         token->subtype = token->Length();
  924.     }
  925.     return 1;
  926. }
  927.  
  928. /*
  929. ================
  930. idLexer::ReadName
  931. ================
  932. */
  933. int idLexer::ReadName( idToken *token ) {
  934.     char c;
  935.  
  936.     token->type = TT_NAME;
  937.     do {
  938.         token->AppendDirty( *idLexer::script_p++ );
  939.         c = *idLexer::script_p;
  940. // RAVEN BEGIN
  941.     } while ( idStr::CharIsAlpha( c ) || idStr::CharIsNumeric( c ) || c == '_' ||
  942. // RAVEN EBD
  943.                 // if treating all tokens as strings, don't parse '-' as a seperate token
  944.                 ((idLexer::flags & LEXFL_ONLYSTRINGS) && (c == '-')) ||
  945.                 // if special path name characters are allowed
  946.                 ((idLexer::flags & LEXFL_ALLOWPATHNAMES) && (c == '/' || c == '\\' || c == ':' || c == '.')) );
  947.     token->data[token->len] = '\0';
  948.     //the sub type is the length of the name
  949.     token->subtype = token->Length();
  950.     return 1;
  951. }
  952.  
  953. /*
  954. ================
  955. idLexer::CheckString
  956. ================
  957. */
  958. ID_INLINE int idLexer::CheckString( const char *str ) const {
  959.     int i;
  960.  
  961.     for ( i = 0; str[i]; i++ ) {
  962.         if ( idLexer::script_p[i] != str[i] ) {
  963.             return false;
  964.         }
  965.     }
  966.     return true;
  967. }
  968.  
  969. /*
  970. ================
  971. idLexer::ReadNumber
  972. ================
  973. */
  974. int idLexer::ReadNumber( idToken *token ) {
  975.     int i;
  976.     int dot;
  977.     char c, c2;
  978.  
  979.     token->type = TT_NUMBER;
  980.     token->subtype = 0;
  981.     token->intvalue = 0;
  982.     token->floatvalue = 0;
  983.  
  984.     c = *idLexer::script_p;
  985.     c2 = *(idLexer::script_p + 1);
  986.  
  987.     if ( c == '0' && c2 != '.' ) {
  988.         // check for a hexadecimal number
  989.         if ( c2 == 'x' || c2 == 'X' ) {
  990.             token->AppendDirty( *idLexer::script_p++ );
  991.             token->AppendDirty( *idLexer::script_p++ );
  992.             c = *idLexer::script_p;
  993.             while((c >= '0' && c <= '9') ||
  994.                         (c >= 'a' && c <= 'f') ||
  995.                         (c >= 'A' && c <= 'F')) {
  996.                 token->AppendDirty( c );
  997.                 c = *(++idLexer::script_p);
  998.             }
  999.             token->subtype = TT_HEX | TT_INTEGER;
  1000.         }
  1001.         // check for a binary number
  1002.         else if ( c2 == 'b' || c2 == 'B' ) {
  1003.             token->AppendDirty( *idLexer::script_p++ );
  1004.             token->AppendDirty( *idLexer::script_p++ );
  1005.             c = *idLexer::script_p;
  1006.             while( c == '0' || c == '1' ) {
  1007.                 token->AppendDirty( c );
  1008.                 c = *(++idLexer::script_p);
  1009.             }
  1010.             token->subtype = TT_BINARY | TT_INTEGER;
  1011.         }
  1012.         // its an octal number
  1013.         else {
  1014.             token->AppendDirty( *idLexer::script_p++ );
  1015.             c = *idLexer::script_p;
  1016.             while( c >= '0' && c <= '7' ) {
  1017.                 token->AppendDirty( c );
  1018.                 c = *(++idLexer::script_p);
  1019.             }
  1020.             token->subtype = TT_OCTAL | TT_INTEGER;
  1021.         }
  1022.     }
  1023.     else {
  1024.         // decimal integer or floating point number or ip address
  1025.         dot = 0;
  1026.         while( 1 ) {
  1027.             if ( c >= '0' && c <= '9' ) {
  1028.             }
  1029.             else if ( c == '.' ) {
  1030.                 dot++;
  1031.             }
  1032.             else {
  1033.                 break;
  1034.             }
  1035.             token->AppendDirty( c );
  1036.             c = *(++idLexer::script_p);
  1037.         }
  1038.         if( c == 'e' && dot == 0) {
  1039.             //We have scientific notation without a decimal point
  1040.             dot++;
  1041.         }
  1042.         // if a floating point number
  1043.         if ( dot == 1 ) {
  1044.             token->subtype = TT_DECIMAL | TT_FLOAT;
  1045.             // check for floating point exponent
  1046.             if ( c == 'e' ) {
  1047.                 //Append the e so that GetFloatValue code works
  1048.                 token->AppendDirty( c );
  1049.                 c = *(++idLexer::script_p);
  1050.                 if ( c == '-' ) {
  1051.                     token->AppendDirty( c );
  1052.                     c = *(++idLexer::script_p);
  1053.                 }
  1054.                 else if ( c == '+' ) {
  1055.                     token->AppendDirty( c );
  1056.                     c = *(++idLexer::script_p);
  1057.                 }
  1058.                 while( c >= '0' && c <= '9' ) {
  1059.                     token->AppendDirty( c );
  1060.                     c = *(++idLexer::script_p);
  1061.                 }
  1062.             }
  1063.             // check for floating point exception infinite 1.#INF or indefinite 1.#IND or NaN
  1064.             else if ( c == '#' ) {
  1065.                 c2 = 4;
  1066.                 if ( CheckString( "INF" ) ) {
  1067.                     token->subtype |= TT_INFINITE;
  1068.                 }
  1069.                 else if ( CheckString( "IND" ) ) {
  1070.                     token->subtype |= TT_INDEFINITE;
  1071.                 }
  1072.                 else if ( CheckString( "NAN" ) ) {
  1073.                     token->subtype |= TT_NAN;
  1074.                 }
  1075.                 else if ( CheckString( "QNAN" ) ) {
  1076.                     token->subtype |= TT_NAN;
  1077.                     c2++;
  1078.                 }
  1079.                 else if ( CheckString( "SNAN" ) ) {
  1080.                     token->subtype |= TT_NAN;
  1081.                     c2++;
  1082.                 }
  1083.                 for ( i = 0; i < c2; i++ ) {
  1084.                     token->AppendDirty( c );
  1085.                     c = *(++idLexer::script_p);
  1086.                 }
  1087.                 while( c >= '0' && c <= '9' ) {
  1088.                     token->AppendDirty( c );
  1089.                     c = *(++idLexer::script_p);
  1090.                 }
  1091.                 if ( !(idLexer::flags & LEXFL_ALLOWFLOATEXCEPTIONS) ) {
  1092.                     token->AppendDirty( 0 );    // zero terminate for c_str
  1093.                     idLexer::Error( "parsed %s", token->c_str() );
  1094.                 }
  1095.             }
  1096.         }
  1097.         else if ( dot > 1 ) {
  1098.             if ( !( idLexer::flags & LEXFL_ALLOWIPADDRESSES ) ) {
  1099.                 idLexer::Error( "more than one dot in number" );
  1100.                 return 0;
  1101.             }
  1102.             if ( dot != 3 ) {
  1103.                 idLexer::Error( "ip address should have three dots" );
  1104.                 return 0;
  1105.             }
  1106.             token->subtype = TT_IPADDRESS;
  1107.         }
  1108.         else {
  1109.             token->subtype = TT_DECIMAL | TT_INTEGER;
  1110.         }
  1111.     }
  1112.  
  1113.     if ( token->subtype & TT_FLOAT ) {
  1114.         if ( c > ' ' ) {
  1115.             // single-precision: float
  1116.             if ( c == 'f' || c == 'F' ) {
  1117.                 token->subtype |= TT_SINGLE_PRECISION;
  1118.                 idLexer::script_p++;
  1119.             }
  1120.             // extended-precision: long double
  1121.             else if ( c == 'l' || c == 'L' ) {
  1122.                 token->subtype |= TT_EXTENDED_PRECISION;
  1123.                 idLexer::script_p++;
  1124.             }
  1125.             // default is double-precision: double
  1126.             else {
  1127.                 token->subtype |= TT_DOUBLE_PRECISION;
  1128.             }
  1129.         }
  1130.         else {
  1131.             token->subtype |= TT_DOUBLE_PRECISION;
  1132.         }
  1133.     }
  1134.     else if ( token->subtype & TT_INTEGER ) {
  1135.         if ( c > ' ' ) {
  1136.             // default: signed long
  1137.             for ( i = 0; i < 2; i++ ) {
  1138.                 // long integer
  1139.                 if ( c == 'l' || c == 'L' ) {
  1140.                     token->subtype |= TT_LONG;
  1141.                 }
  1142.                 // unsigned integer
  1143.                 else if ( c == 'u' || c == 'U' ) {
  1144.                     token->subtype |= TT_UNSIGNED;
  1145.                 }
  1146.                 else {
  1147.                     break;
  1148.                 }
  1149.                 c = *(++idLexer::script_p);
  1150.             }
  1151.         }
  1152.     }
  1153.     else if ( token->subtype & TT_IPADDRESS ) {
  1154.         if ( c == ':' ) {
  1155.             token->AppendDirty( c );
  1156.             c = *(++idLexer::script_p);
  1157.             while( c >= '0' && c <= '9' ) {
  1158.                 token->AppendDirty( c );
  1159.                 c = *(++idLexer::script_p);
  1160.             }
  1161.             token->subtype |= TT_IPPORT;
  1162.         }
  1163.     }
  1164.     token->data[token->len] = '\0';
  1165.     return 1;
  1166. }
  1167.  
  1168. /*
  1169. ================
  1170. idLexer::ReadPunctuation
  1171. ================
  1172. */
  1173. int idLexer::ReadPunctuation( idToken *token ) {
  1174.     int l, n, i;
  1175.     char *p;
  1176.     const punctuation_t *punc;
  1177.  
  1178. #ifdef PUNCTABLE
  1179.     for (n = idLexer::punctuationtable[(unsigned int)*(idLexer::script_p)]; n >= 0; n = idLexer::nextpunctuation[n])
  1180.     {
  1181.         punc = &(idLexer::punctuations[n]);
  1182. #else
  1183.     int i;
  1184.  
  1185.     for (i = 0; idLexer::punctuations[i].p; i++) {
  1186.         punc = &idLexer::punctuations[i];
  1187. #endif
  1188.         p = punc->p;
  1189.         // check for this punctuation in the script
  1190.         for ( l = 0; p[l] && idLexer::script_p[l]; l++ ) {
  1191.             if ( idLexer::script_p[l] != p[l] ) {
  1192.                 break;
  1193.             }
  1194.         }
  1195.         if ( !p[l] ) {
  1196.             //
  1197.             token->EnsureAlloced( l+1, false );
  1198.             for ( i = 0; i <= l; i++ ) {
  1199.                 token->data[i] = p[i];
  1200.             }
  1201.             token->len = l;
  1202.             //
  1203.             idLexer::script_p += l;
  1204.             token->type = TT_PUNCTUATION;
  1205.             // sub type is the punctuation id
  1206.             token->subtype = punc->n;
  1207.             return 1;
  1208.         }
  1209.     }
  1210.     return 0;
  1211. }
  1212.  
  1213. /*
  1214. ================
  1215. idLexer::ReadToken
  1216. ================
  1217. */
  1218. int idLexer::ReadToken( idToken *token ) {
  1219.     int c;
  1220.  
  1221.     if ( !loaded ) {
  1222. // RAVEN BEGIN
  1223. #ifndef _XENON
  1224. // nrausch: should not be an error on xenon since it prevents the precache stuff from working
  1225.         idLib::common->Error( "idLexer::ReadToken: no file loaded" );
  1226. #else
  1227.         idLib::common->Warning( "idLexer::ReadToken: no file loaded" );
  1228. #endif
  1229. // RAVEN END
  1230.         return 0;
  1231.     }
  1232.  
  1233.     // if there is a token available (from unreadToken)
  1234.     if ( tokenavailable ) {
  1235.         tokenavailable = 0;
  1236.         *token = idLexer::token;
  1237.         return 1;
  1238.     }
  1239.     // save script pointer
  1240.     lastScript_p = script_p;
  1241.     // save line counter
  1242.     lastline = line;
  1243.     // clear the token stuff
  1244.     token->data[0] = '\0';
  1245.     token->len = 0;
  1246.     // start of the white space
  1247.     whiteSpaceStart_p = script_p;
  1248.     token->whiteSpaceStart_p = script_p;
  1249.     // read white space before token
  1250.     if ( !ReadWhiteSpace() ) {
  1251.         return 0;
  1252.     }
  1253.     // end of the white space
  1254.     idLexer::whiteSpaceEnd_p = script_p;
  1255.     token->whiteSpaceEnd_p = script_p;
  1256.     // line the token is on
  1257.     token->line = line;
  1258.     // number of lines crossed before token
  1259.     token->linesCrossed = line - lastline;
  1260.     // clear token flags
  1261.     token->flags = 0;
  1262.  
  1263.     c = *idLexer::script_p;
  1264.  
  1265.     // if we're keeping everything as whitespace deliminated strings
  1266.     if ( idLexer::flags & LEXFL_ONLYSTRINGS ) {
  1267.         // if there is a leading quote
  1268.         if ( c == '\"' || c == '\'' ) {
  1269.             if (!idLexer::ReadString( token, c )) {
  1270.                 return 0;
  1271.             }
  1272.         } else if ( !idLexer::ReadName( token ) ) {
  1273.             return 0;
  1274.         }
  1275.     }
  1276.     // if there is a number
  1277.     else if ( (c >= '0' && c <= '9') ||
  1278.             (c == '.' && (*(idLexer::script_p + 1) >= '0' && *(idLexer::script_p + 1) <= '9')) ) {
  1279.         if ( !idLexer::ReadNumber( token ) ) {
  1280.             return 0;
  1281.         }
  1282.         // if names are allowed to start with a number
  1283.         if ( idLexer::flags & LEXFL_ALLOWNUMBERNAMES ) {
  1284.             c = *idLexer::script_p;
  1285.             if ( idStr::CharIsAlpha( c ) || idStr::CharIsNumeric( c ) || c == '_' ) {
  1286.                 if ( !idLexer::ReadName( token ) ) {
  1287.                     return 0;
  1288.                 }
  1289.             }
  1290.         }
  1291.     }
  1292.     // if there is a leading quote
  1293.     else if ( c == '\"' || c == '\'' ) {
  1294.         if (!idLexer::ReadString( token, c )) {
  1295.             return 0;
  1296.         }
  1297.     }
  1298.     // if there is a name
  1299. // RAVEN BEGIN
  1300.     else if ( idStr::CharIsAlpha( c ) || idStr::CharIsNumeric( c ) || c == '_' ) {
  1301. // RAVEN END
  1302.         if ( !idLexer::ReadName( token ) ) {
  1303.             return 0;
  1304.         }
  1305.     }
  1306.     // names may also start with a slash when pathnames are allowed
  1307.     else if ( ( idLexer::flags & LEXFL_ALLOWPATHNAMES ) && ( (c == '/' || c == '\\') || c == '.' ) ) {
  1308.         if ( !idLexer::ReadName( token ) ) {
  1309.             return 0;
  1310.         }
  1311.     }
  1312.     // check for punctuations
  1313.     else if ( !idLexer::ReadPunctuation( token ) ) {
  1314.         idLexer::Error( "unknown punctuation %c", c );
  1315.         return 0;
  1316.     }
  1317. // RAVEN BEGIN
  1318. // jsinger: added to write out the binary version of this token when binary writes have been turned on
  1319.     WriteBinaryToken(token);
  1320.  
  1321. // RAVEN END
  1322.     // succesfully read a token
  1323.     return 1;
  1324. }
  1325.  
  1326. /*
  1327. ================
  1328. idLexer::ExpectTokenString
  1329. ================
  1330. */
  1331. int idLexer::ExpectTokenString( const char *string ) {
  1332.     idToken token;
  1333.  
  1334.     if (!idLexer::ReadToken( &token )) {
  1335.         idLexer::Error( "couldn't find expected '%s'", string );
  1336.         return 0;
  1337.     }
  1338.     if ( token != string ) {
  1339.         idLexer::Error( "expected '%s' but found '%s'", string, token.c_str() );
  1340.         return 0;
  1341.     }
  1342.     return 1;
  1343. }
  1344.  
  1345. /*
  1346. ================
  1347. idLexer::ExpectTokenType
  1348. ================
  1349. */
  1350. int idLexer::ExpectTokenType( int type, int subtype, idToken *token ) {
  1351.     idStr str;
  1352.  
  1353.     if ( !idLexer::ReadToken( token ) ) {
  1354.         idLexer::Error( "couldn't read expected token" );
  1355.         return 0;
  1356.     }
  1357.  
  1358.     if ( token->type != type ) {
  1359.         switch( type ) {
  1360.             case TT_STRING: str = "string"; break;
  1361.             case TT_LITERAL: str = "literal"; break;
  1362.             case TT_NUMBER: str = "number"; break;
  1363.             case TT_NAME: str = "name"; break;
  1364.             case TT_PUNCTUATION: str = "punctuation"; break;
  1365.             default: str = "unknown type"; break;
  1366.         }
  1367.         idLexer::Error( "expected a %s but found '%s'", str.c_str(), token->c_str() );
  1368.         return 0;
  1369.     }
  1370.     if ( token->type == TT_NUMBER ) {
  1371.         if ( (token->subtype & subtype) != subtype ) {
  1372.             str.Clear();
  1373.             if ( subtype & TT_DECIMAL ) str = "decimal ";
  1374.             if ( subtype & TT_HEX ) str = "hex ";
  1375.             if ( subtype & TT_OCTAL ) str = "octal ";
  1376.             if ( subtype & TT_BINARY ) str = "binary ";
  1377.             if ( subtype & TT_UNSIGNED ) str += "unsigned ";
  1378.             if ( subtype & TT_LONG ) str += "long ";
  1379.             if ( subtype & TT_FLOAT ) str += "float ";
  1380.             if ( subtype & TT_INTEGER ) str += "integer ";
  1381.             str.StripTrailing( ' ' );
  1382.             idLexer::Error( "expected %s but found '%s'", str.c_str(), token->c_str() );
  1383.             return 0;
  1384.         }
  1385.     }
  1386.     else if ( token->type == TT_PUNCTUATION ) {
  1387.         if ( subtype < 0 ) {
  1388.             idLexer::Error( "BUG: wrong punctuation subtype" );
  1389.             return 0;
  1390.         }
  1391.         if ( token->subtype != subtype ) {
  1392.             idLexer::Error( "expected '%s' but found '%s'", GetPunctuationFromId( subtype ), token->c_str() );
  1393.             return 0;
  1394.         }
  1395.     }
  1396.     return 1;
  1397. }
  1398.  
  1399. /*
  1400. ================
  1401. idLexer::ExpectAnyToken
  1402. ================
  1403. */
  1404. int idLexer::ExpectAnyToken( idToken *token ) {
  1405.     if (!idLexer::ReadToken( token )) {
  1406.         idLexer::Error( "couldn't read expected token" );
  1407.         return 0;
  1408.     }
  1409.     else {
  1410.         return 1;
  1411.     }
  1412. }
  1413.  
  1414. /*
  1415. ================
  1416. idLexer::CheckTokenString
  1417. ================
  1418. */
  1419. int idLexer::CheckTokenString( const char *string ) {
  1420.     idToken tok;
  1421.  
  1422.     if (!idLexer::ReadToken( &tok )) {
  1423.         return 0;
  1424.     }
  1425.  
  1426.     // if the token is available
  1427.     if ( tok == string ) {
  1428.         return 1;
  1429.     }
  1430.     // token not available
  1431.     UnreadToken( &tok );
  1432.     return 0;
  1433. }
  1434.  
  1435. /*
  1436. ================
  1437. idLexer::CheckTokenType
  1438. ================
  1439. */
  1440. int idLexer::CheckTokenType( int type, int subtype, idToken *token ) {
  1441.     idToken tok;
  1442.  
  1443.     if (!idLexer::ReadToken( &tok )) {
  1444.         return 0;
  1445.     }
  1446.     // if the type matches
  1447.     if (tok.type == type && (tok.subtype & subtype) == subtype) {
  1448.         *token = tok;
  1449.         return 1;
  1450.     }
  1451.     // token is not available
  1452.     idLexer::script_p = lastScript_p;
  1453.     idLexer::line = lastline;
  1454.     return 0;
  1455. }
  1456.  
  1457. /*
  1458. ================
  1459. idLexer::SkipUntilString
  1460. ================
  1461. */
  1462. int idLexer::SkipUntilString( const char *string ) {
  1463.     idToken token;
  1464.  
  1465.     while(idLexer::ReadToken( &token )) {
  1466.         if ( token == string ) {
  1467.             return 1;
  1468.         }
  1469.     }
  1470.     return 0;
  1471. }
  1472.  
  1473. /*
  1474. ================
  1475. idLexer::SkipRestOfLine
  1476. ================
  1477. */
  1478. int idLexer::SkipRestOfLine( void ) {
  1479.     idToken token;
  1480.  
  1481.     while(idLexer::ReadToken( &token )) {
  1482.         if ( token.linesCrossed ) {
  1483.             idLexer::script_p = lastScript_p;
  1484.             idLexer::line = lastline;
  1485.             return 1;
  1486.         }
  1487.     }
  1488.     return 0;
  1489. }
  1490.  
  1491. /*
  1492. =================
  1493. idLexer::SkipBracedSection
  1494.  
  1495. Skips until a matching close brace is found.
  1496. Internal brace depths are properly skipped.
  1497. =================
  1498. */
  1499. int idLexer::SkipBracedSection( bool parseFirstBrace ) {
  1500.     idToken token;
  1501.     int depth;
  1502.  
  1503.     depth = parseFirstBrace ? 0 : 1;
  1504.     do {
  1505.         if ( !ReadToken( &token ) ) {
  1506.             return false;
  1507.         }
  1508.         if ( token.type == TT_PUNCTUATION ) {
  1509.             if ( token == "{" ) {
  1510.                 depth++;
  1511.             } else if ( token == "}" ) {
  1512.                 depth--;
  1513.             }
  1514.         }
  1515.     } while( depth );
  1516.     return true;
  1517. }
  1518.  
  1519. /*
  1520. ================
  1521. idLexer::UnreadToken
  1522. ================
  1523. */
  1524. void idLexer::UnreadToken( const idToken *token ) {
  1525.     if ( idLexer::tokenavailable ) {
  1526.         idLib::common->FatalError( "idLexer::unreadToken, unread token twice\n" );
  1527.     }
  1528.     idLexer::token = *token;
  1529.     idLexer::tokenavailable = 1;
  1530. }
  1531.  
  1532. /*
  1533. ================
  1534. idLexer::ReadTokenOnLine
  1535. ================
  1536. */
  1537. int idLexer::ReadTokenOnLine( idToken *token ) {
  1538.     idToken tok;
  1539.  
  1540.     if (!idLexer::ReadToken( &tok )) {
  1541.         idLexer::script_p = lastScript_p;
  1542.         idLexer::line = lastline;
  1543.         return false;
  1544.     }
  1545.     // if no lines were crossed before this token
  1546.     if ( !tok.linesCrossed ) {
  1547.         *token = tok;
  1548.         return true;
  1549.     }
  1550.     // restore our position
  1551.     idLexer::script_p = lastScript_p;
  1552.     idLexer::line = lastline;
  1553.     token->Clear();
  1554.     return false;
  1555. }
  1556.  
  1557. /*
  1558. ================
  1559. idLexer::ReadRestOfLine
  1560. ================
  1561. */
  1562. const char*    idLexer::ReadRestOfLine(idStr& out) {
  1563.     while(1) {
  1564.  
  1565.         if(*idLexer::script_p == '\n') {
  1566.             idLexer::line++;
  1567.             break;
  1568.         }
  1569.  
  1570.         if(!*idLexer::script_p) {
  1571.             break;
  1572.         }
  1573.  
  1574.         if(*idLexer::script_p <= ' ') {
  1575.             out += " ";
  1576.         } else {
  1577.             out += *idLexer::script_p;
  1578.         }
  1579.         idLexer::script_p++;
  1580.  
  1581.     }
  1582.  
  1583.     out.Strip(' ');
  1584.     return out.c_str();
  1585. }
  1586.  
  1587. /*
  1588. ================
  1589. idLexer::ParseInt
  1590. ================
  1591. */
  1592. int idLexer::ParseInt( void ) {
  1593.     idToken token;
  1594.  
  1595.     if ( !idLexer::ReadToken( &token ) ) {
  1596.         idLexer::Error( "couldn't read expected integer" );
  1597.         return 0;
  1598.     }
  1599.     if ( token.type == TT_PUNCTUATION && token == "-" ) {
  1600.         idLexer::ExpectTokenType( TT_NUMBER, TT_INTEGER, &token );
  1601.         return -((signed int) token.GetIntValue());
  1602.     }
  1603.     else if ( token.type != TT_NUMBER || token.subtype == TT_FLOAT ) {
  1604.         idLexer::Error( "expected integer value, found '%s'", token.c_str() );
  1605.     }
  1606.     return token.GetIntValue();
  1607. }
  1608.  
  1609. /*
  1610. ================
  1611. idLexer::ParseBool
  1612. ================
  1613. */
  1614. bool idLexer::ParseBool( void ) {
  1615.     idToken token;
  1616.  
  1617.     if ( !idLexer::ExpectTokenType( TT_NUMBER, 0, &token ) ) {
  1618.         idLexer::Error( "couldn't read expected boolean" );
  1619.         return false;
  1620.     }
  1621.     return ( token.GetIntValue() != 0 );
  1622. }
  1623.  
  1624. /*
  1625. ================
  1626. idLexer::ParseFloat
  1627. ================
  1628. */
  1629. float idLexer::ParseFloat( bool *errorFlag ) {
  1630.     idToken token;
  1631.  
  1632.     if ( errorFlag ) {
  1633.         *errorFlag = false;
  1634.     }
  1635.  
  1636.     if ( !idLexer::ReadToken( &token ) ) {
  1637.         if ( errorFlag ) {
  1638.             idLexer::Warning( "couldn't read expected floating point number" );
  1639.             *errorFlag = true;
  1640.         } else {
  1641.             idLexer::Error( "couldn't read expected floating point number" );
  1642.         }
  1643.         return 0;
  1644.     }
  1645.     if ( token.type == TT_PUNCTUATION && token == "-" ) {
  1646.         idLexer::ExpectTokenType( TT_NUMBER, 0, &token );
  1647.         return -token.GetFloatValue();
  1648.     }
  1649.     else if ( token.type != TT_NUMBER ) {
  1650.         if ( errorFlag ) {
  1651.             idLexer::Warning( "expected float value, found '%s'", token.c_str() );
  1652.             *errorFlag = true;
  1653.         } else {
  1654.             idLexer::Error( "expected float value, found '%s'", token.c_str() );
  1655.         }
  1656.     }
  1657.     return token.GetFloatValue();
  1658. }
  1659.  
  1660. /*
  1661. ================
  1662. idLexer::Parse1DMatrix
  1663. ================
  1664. */
  1665. int idLexer::Parse1DMatrix( int x, float *m ) {
  1666.     int i;
  1667.  
  1668.     if ( !idLexer::ExpectTokenString( "(" ) ) {
  1669.         return false;
  1670.     }
  1671.  
  1672.     for ( i = 0; i < x; i++ ) {
  1673.         m[i] = idLexer::ParseFloat();
  1674.     }
  1675.  
  1676.     if ( !idLexer::ExpectTokenString( ")" ) ) {
  1677.         return false;
  1678.     }
  1679.     return true;
  1680. }
  1681.  
  1682. // RAVEN BEGIN
  1683. // rjohnson: added vertex color support to proc files.  assume a default RGBA of 0x000000ff
  1684. /*
  1685. ================
  1686. idLexer::Parse1DMatrixOpenEnded
  1687. ================
  1688. */
  1689. int idLexer::Parse1DMatrixOpenEnded( int MaxCount, float *m ) {
  1690.     int i;
  1691.  
  1692.     if ( !idLexer::ExpectTokenString( "(" ) ) {
  1693.         return 0;
  1694.     }
  1695.  
  1696.     for ( i = 0; i < MaxCount; i++ ) {
  1697.         idToken tok;
  1698.  
  1699.         if (!idLexer::ReadToken( &tok )) {
  1700.             return 0;
  1701.         }
  1702.  
  1703.         if ( tok == ")" ) {
  1704.             return i;
  1705.         }
  1706.  
  1707.         idLexer::UnreadToken( &tok );
  1708.  
  1709.         m[i] = idLexer::ParseFloat();
  1710.     }
  1711.  
  1712.     if ( !idLexer::ExpectTokenString( ")" ) ) {
  1713.         return 0;
  1714.     }
  1715.  
  1716.     return i;
  1717. }
  1718. // RAVEN END
  1719.  
  1720. /*
  1721. ================
  1722. idLexer::Parse2DMatrix
  1723. ================
  1724. */
  1725. int idLexer::Parse2DMatrix( int y, int x, float *m ) {
  1726.     int i;
  1727.  
  1728.     if ( !idLexer::ExpectTokenString( "(" ) ) {
  1729.         return false;
  1730.     }
  1731.  
  1732.     for ( i = 0; i < y; i++ ) {
  1733.         if ( !idLexer::Parse1DMatrix( x, m + i * x ) ) {
  1734.             return false;
  1735.         }
  1736.     }
  1737.  
  1738.     if ( !idLexer::ExpectTokenString( ")" ) ) {
  1739.         return false;
  1740.     }
  1741.     return true;
  1742. }
  1743.  
  1744. /*
  1745. ================
  1746. idLexer::Parse3DMatrix
  1747. ================
  1748. */
  1749. int idLexer::Parse3DMatrix( int z, int y, int x, float *m ) {
  1750.     int i;
  1751.  
  1752.     if ( !idLexer::ExpectTokenString( "(" ) ) {
  1753.         return false;
  1754.     }
  1755.  
  1756.     for ( i = 0 ; i < z; i++ ) {
  1757.         if ( !idLexer::Parse2DMatrix( y, x, m + i * x*y ) ) {
  1758.             return false;
  1759.         }
  1760.     }
  1761.  
  1762.     if ( !idLexer::ExpectTokenString( ")" ) ) {
  1763.         return false;
  1764.     }
  1765.     return true;
  1766. }
  1767.  
  1768. /*
  1769. ================
  1770. idLexer::ParseNumericStructArray
  1771. ================
  1772. */
  1773. void idLexer::ParseNumericStructArray( int numStructElements, int tokenSubTypeStructElements[], int arrayCount, byte *arrayStorage )
  1774. {
  1775.     int arrayOffset, curElement;
  1776.  
  1777.     for ( arrayOffset = 0; arrayOffset < arrayCount; arrayOffset++ )
  1778.     {
  1779.         for ( curElement = 0; curElement < numStructElements; curElement++ )
  1780.         {
  1781.             if ( tokenSubTypeStructElements[curElement] & TT_FLOAT )
  1782.             {
  1783.                 *(float*)arrayStorage = idLexer::ParseFloat();
  1784.                 arrayStorage += sizeof(float);
  1785.             }
  1786.             else
  1787.             {
  1788.                 *(int*)arrayStorage = idLexer::ParseInt();
  1789.                 arrayStorage += sizeof(int);
  1790.             }
  1791.         }
  1792.     }
  1793. }
  1794.  
  1795. /*
  1796. =================
  1797. idParser::ParseBracedSection
  1798.  
  1799. The next token should be an open brace.
  1800. Parses until a matching close brace is found.
  1801. Maintains exact characters between braces.
  1802.  
  1803.   FIXME: this should use ReadToken and replace the token white space with correct indents and newlines
  1804. =================
  1805. */
  1806. const char *idLexer::ParseBracedSectionExact( idStr &out, int tabs ) {
  1807.     int        depth;
  1808.     bool    doTabs;
  1809.     bool    skipWhite;
  1810.  
  1811.     out.Empty();
  1812.  
  1813.     if ( !idLexer::ExpectTokenString( "{" ) ) {
  1814.         return out.c_str( );
  1815.     }
  1816.  
  1817.     out = "{";
  1818.     depth = 1;    
  1819.     skipWhite = false;
  1820.     doTabs = tabs >= 0;
  1821.  
  1822.     while( depth && *idLexer::script_p ) {
  1823.         char c = *(idLexer::script_p++);
  1824.  
  1825.         switch ( c ) {
  1826.             case '\t':
  1827.             case ' ': {
  1828.                 if ( skipWhite ) {
  1829.                     continue;
  1830.                 }
  1831.                 break;
  1832.             }
  1833.             case '\n': {
  1834. // RAVEN BEGIN
  1835. // jscott: now gives correct line number in error reports
  1836.                 line++;
  1837. // RAVEN END
  1838.                 if ( doTabs ) {
  1839.                     skipWhite = true;
  1840.                     out += c;
  1841.                     continue;
  1842.                 }
  1843.                 break;
  1844.             }
  1845.             case '{': {
  1846.                 depth++;
  1847.                 tabs++;
  1848.                 break;
  1849.             }
  1850.             case '}': {
  1851.                 depth--;
  1852.                 tabs--;
  1853.                 break;                
  1854.             }
  1855.         }
  1856.  
  1857.         if ( skipWhite ) {
  1858.             int i = tabs;
  1859.             if ( c == '{' ) {
  1860.                 i--;
  1861.             }
  1862.             skipWhite = false;
  1863.             for ( ; i > 0; i-- ) {
  1864.                 out += '\t';
  1865.             }
  1866.         }
  1867.         out += c;
  1868.     }
  1869.     return out.c_str();
  1870. }
  1871.  
  1872. /*
  1873. =================
  1874. idLexer::ParseBracedSection
  1875.  
  1876. The next token should be an open brace.
  1877. Parses until a matching close brace is found.
  1878. Internal brace depths are properly skipped.
  1879. =================
  1880. */
  1881. const char *idLexer::ParseBracedSection( idStr &out ) {
  1882.     idToken token;
  1883.     int i, depth;
  1884.  
  1885.     out.Empty();
  1886.     if ( !idLexer::ExpectTokenString( "{" ) ) {
  1887.         return out.c_str();
  1888.     }
  1889.     out = "{";
  1890.     depth = 1;
  1891.     do {
  1892.         if ( !idLexer::ReadToken( &token ) ) {
  1893.             Error( "missing closing brace" );
  1894.             return out.c_str();
  1895.         }
  1896.  
  1897.         // if the token is on a new line
  1898.         for ( i = 0; i < token.linesCrossed; i++ ) {
  1899.             out += "\r\n";
  1900.         }
  1901.  
  1902.         if ( token.type == TT_PUNCTUATION ) {
  1903.             if ( token[0] == '{' ) {
  1904.                 depth++;
  1905.             }
  1906.             else if ( token[0] == '}' ) {
  1907.                 depth--;
  1908.             }
  1909.         }
  1910.  
  1911.         if ( token.type == TT_STRING ) {
  1912.             out += "\"" + token + "\"";
  1913.         }
  1914.         else {
  1915.             out += token;
  1916.         }
  1917.         out += " ";
  1918.     } while( depth );
  1919.  
  1920.     return out.c_str();
  1921. }
  1922.  
  1923. /*
  1924. =================
  1925. idLexer::ParseRestOfLine
  1926.  
  1927.   parse the rest of the line
  1928. =================
  1929. */
  1930. const char *idLexer::ParseRestOfLine( idStr &out ) {
  1931.     idToken token;
  1932.  
  1933.     out.Empty();
  1934.     while(idLexer::ReadToken( &token )) {
  1935.         if ( token.linesCrossed ) {
  1936.             idLexer::script_p = lastScript_p;
  1937.             idLexer::line = lastline;
  1938.             break;
  1939.         }
  1940.         if ( out.Length() ) {
  1941.             out += " ";
  1942.         }
  1943.         out += token;
  1944.     }
  1945.     return out.c_str();
  1946. }
  1947.  
  1948. /*
  1949. ================
  1950. idLexer::GetLastWhiteSpace
  1951. ================
  1952. */
  1953. int idLexer::GetLastWhiteSpace( idStr &whiteSpace ) const {
  1954.     whiteSpace.Clear();
  1955.     for ( const char *p = whiteSpaceStart_p; p < whiteSpaceEnd_p; p++ ) {
  1956.         whiteSpace.Append( *p );
  1957.     }
  1958.     return whiteSpace.Length();
  1959. }
  1960.  
  1961. /*
  1962. ================
  1963. idLexer::GetLastWhiteSpaceStart
  1964. ================
  1965. */
  1966. int idLexer::GetLastWhiteSpaceStart( void ) const {
  1967.     return whiteSpaceStart_p - buffer;
  1968. }
  1969.  
  1970. /*
  1971. ================
  1972. idLexer::GetLastWhiteSpaceEnd
  1973. ================
  1974. */
  1975. int idLexer::GetLastWhiteSpaceEnd( void ) const {
  1976.     return whiteSpaceEnd_p - buffer;
  1977. }
  1978.  
  1979. /*
  1980. ================
  1981. idLexer::Reset
  1982. ================
  1983. */
  1984. void idLexer::Reset( void ) {
  1985.     // pointer in script buffer
  1986.     idLexer::script_p = idLexer::buffer;
  1987.     // pointer in script buffer before reading token
  1988.     idLexer::lastScript_p = idLexer::buffer;
  1989.     // begin of white space
  1990.     idLexer::whiteSpaceStart_p = NULL;
  1991.     // end of white space
  1992.     idLexer::whiteSpaceEnd_p = NULL;
  1993.     // set if there's a token available in idLexer::token
  1994.     idLexer::tokenavailable = 0;
  1995.  
  1996.     idLexer::line = 1;
  1997.     idLexer::lastline = 1;
  1998.     // clear the saved token
  1999.     idLexer::token = "";
  2000. }
  2001.  
  2002. /*
  2003. ================
  2004. idLexer::EndOfFile
  2005. ================
  2006. */
  2007. int idLexer::EndOfFile( void ) {
  2008.     return idLexer::script_p >= idLexer::end_p;
  2009. }
  2010.  
  2011. /*
  2012. ================
  2013. idLexer::NumLinesCrossed
  2014. ================
  2015. */
  2016. int idLexer::NumLinesCrossed( void ) {
  2017.     return idLexer::line - idLexer::lastline;
  2018. }
  2019.  
  2020. /*
  2021. ================
  2022. idLexer::LoadFile
  2023. ================
  2024. */
  2025. int idLexer::LoadFile( const char *filename, bool OSPath ) {
  2026.     idFile *fp;
  2027.     idStr pathname;
  2028.     int length;
  2029.     char *buf;
  2030.  
  2031.     if ( idLexer::loaded ) {
  2032.         idLib::common->Error("idLexer::LoadFile: another script already loaded");
  2033.         return false;
  2034.     }
  2035.     
  2036.     if ( !OSPath && ( baseFolder[0] != '\0' ) ) {
  2037.         pathname = va( "%s/%s", baseFolder, filename );
  2038.     } else {
  2039.         pathname = filename;
  2040.     }
  2041.     if ( OSPath ) {
  2042.         fp = idLib::fileSystem->OpenExplicitFileRead( pathname );
  2043.     } else {
  2044.         fp = idLib::fileSystem->OpenFileRead( pathname );
  2045.     }
  2046.     if ( !fp ) {
  2047.         return false;
  2048.     }
  2049.     length = fp->Length();
  2050. // RAVEN BEGIN
  2051. // amccarthy: Added memory allocation tag
  2052.     buf = (char *) Mem_Alloc( length + 1, MA_LEXER );
  2053.     if( !buf ) {
  2054.         common->FatalError( "Memory system failure : out of memory" );
  2055.     }
  2056. // RAVEN END
  2057.     buf[length] = '\0';
  2058.     fp->Read( buf, length );
  2059.     idLexer::fileTime = fp->Timestamp();
  2060.     idLexer::filename = fp->GetFullPath();
  2061.     idLib::fileSystem->CloseFile( fp );
  2062.  
  2063.     idLexer::buffer = buf;
  2064.     idLexer::length = length;
  2065.     // pointer in script buffer
  2066.     idLexer::script_p = idLexer::buffer;
  2067.     // pointer in script buffer before reading token
  2068.     idLexer::lastScript_p = idLexer::buffer;
  2069.     // pointer to end of script buffer
  2070.     idLexer::end_p = &(idLexer::buffer[length]);
  2071.  
  2072.     idLexer::tokenavailable = 0;
  2073.     idLexer::line = 1;
  2074.     idLexer::lastline = 1;
  2075.     idLexer::allocated = true;
  2076.     idLexer::loaded = true;
  2077.  
  2078. // RAVEN BEGIN
  2079. // jsinger: initialize compiled file
  2080.     if(flags & LEXFL_WRITEBINARY)
  2081.     {
  2082.         pathname.Append(Lexer::sCompiledFileSuffix);
  2083.         if ( OSPath ) {
  2084.             mBinaryFile = idLib::fileSystem->OpenExplicitFileWrite( pathname );
  2085.         } else {
  2086.             mBinaryFile = idLib::fileSystem->OpenFileWrite( pathname );
  2087.         }
  2088.     }
  2089. // RAVEN END
  2090.  
  2091.     return true;
  2092. }
  2093.  
  2094. /*
  2095. ================
  2096. idLexer::LoadMemory
  2097. ================
  2098. */
  2099. int idLexer::LoadMemory( const char *ptr, int length, const char *name, int startLine ) {
  2100.     if ( idLexer::loaded ) {
  2101.         idLib::common->Error("idLexer::LoadMemory: another script already loaded");
  2102.         return false;
  2103.     }
  2104.     idLexer::filename = name;
  2105.     idLexer::buffer = ptr;
  2106.     idLexer::fileTime = 0;
  2107.     idLexer::length = length;
  2108.     // pointer in script buffer
  2109.     idLexer::script_p = idLexer::buffer;
  2110.     // pointer in script buffer before reading token
  2111.     idLexer::lastScript_p = idLexer::buffer;
  2112.     // pointer to end of script buffer
  2113.     idLexer::end_p = &(idLexer::buffer[length]);
  2114.  
  2115.     idLexer::tokenavailable = 0;
  2116.     idLexer::line = startLine;
  2117.     idLexer::lastline = startLine;
  2118.     idLexer::allocated = false;
  2119.     idLexer::loaded = true;
  2120.  
  2121.     return true;
  2122. }
  2123.  
  2124. /*
  2125. ================
  2126. idLexer::FreeSource
  2127. ================
  2128. */
  2129. void idLexer::FreeSource( void ) {
  2130. #ifdef PUNCTABLE
  2131.     if ( idLexer::punctuationtable && idLexer::punctuationtable != default_punctuationtable ) {
  2132.         Mem_Free( (void *) idLexer::punctuationtable );
  2133.         idLexer::punctuationtable = NULL;
  2134.     }
  2135.     if ( idLexer::nextpunctuation && idLexer::nextpunctuation != default_nextpunctuation ) {
  2136.         Mem_Free( (void *) idLexer::nextpunctuation );
  2137.         idLexer::nextpunctuation = NULL;
  2138.     }
  2139. #endif //PUNCTABLE
  2140.     if ( idLexer::allocated ) {
  2141.         Mem_Free( (void *) idLexer::buffer );
  2142.         idLexer::buffer = NULL;
  2143.         idLexer::allocated = false;
  2144.     }
  2145.     idLexer::tokenavailable = 0;
  2146.     idLexer::token = "";
  2147.     idLexer::loaded = false;
  2148. // RAVEN BEGIN
  2149. // jsinger: close compile file if it exists
  2150.     if(flags & LEXFL_WRITEBINARY)
  2151.     {
  2152.         if(mBinaryFile)
  2153.         {
  2154.             idLib::fileSystem->CloseFile(mBinaryFile);
  2155.             mBinaryFile = NULL;
  2156.         }
  2157.     }
  2158. // RAVEN END
  2159. }
  2160.  
  2161. /*
  2162. ================
  2163. idLexer::idLexer
  2164. ================
  2165. */
  2166. idLexer::idLexer( void ) {
  2167.     idLexer::loaded = false;
  2168.     idLexer::filename = "";
  2169.     idLexer::flags = 0;
  2170.     idLexer::SetPunctuations( NULL );
  2171.     idLexer::allocated = false;
  2172.     idLexer::fileTime = 0;
  2173.     idLexer::length = 0;
  2174.     idLexer::line = 0;
  2175.     idLexer::lastline = 0;
  2176.     idLexer::tokenavailable = 0;
  2177.     idLexer::token = "";
  2178.     idLexer::next = NULL;
  2179.     idLexer::hadError = false;
  2180. // RAVEN BEGIN
  2181. // jsinger: initialize compiled file
  2182.     idLexer::mBinaryFile = NULL;
  2183. // RAVEN END
  2184. }
  2185.  
  2186. /*
  2187. ================
  2188. idLexer::idLexer
  2189. ================
  2190. */
  2191. idLexer::idLexer( int flags ) {
  2192.     idLexer::loaded = false;
  2193.     idLexer::filename = "";
  2194.     idLexer::flags = flags;
  2195.     idLexer::SetPunctuations( NULL );
  2196.     idLexer::allocated = false;
  2197.     idLexer::fileTime = 0;
  2198.     idLexer::length = 0;
  2199.     idLexer::line = 0;
  2200.     idLexer::lastline = 0;
  2201.     idLexer::tokenavailable = 0;
  2202.     idLexer::token = "";
  2203.     idLexer::next = NULL;
  2204.     idLexer::hadError = false;
  2205. // RAVEN BEGIN
  2206. // jsinger: initialize compiled file
  2207.     idLexer::mBinaryFile = NULL;
  2208. // RAVEN END
  2209. }
  2210.  
  2211. /*
  2212. ================
  2213. idLexer::idLexer
  2214. ================
  2215. */
  2216. idLexer::idLexer( const char *filename, int flags, bool OSPath ) {
  2217.     idLexer::loaded = false;
  2218.     idLexer::flags = flags;
  2219.     idLexer::SetPunctuations( NULL );
  2220.     idLexer::allocated = false;
  2221.     idLexer::token = "";
  2222.     idLexer::next = NULL;
  2223.     idLexer::hadError = false;
  2224. // RAVEN BEGIN
  2225. // jsinger: initialize compiled file
  2226.     idLexer::mBinaryFile = NULL;
  2227. // RAVEN END
  2228.     idLexer::LoadFile( filename, OSPath );
  2229. }
  2230.  
  2231. /*
  2232. ================
  2233. idLexer::idLexer
  2234. ================
  2235. */
  2236. idLexer::idLexer( const char *ptr, int length, const char *name, int flags ) {
  2237.     idLexer::loaded = false;
  2238.     idLexer::flags = flags;
  2239.     idLexer::SetPunctuations( NULL );
  2240.     idLexer::allocated = false;
  2241.     idLexer::token = "";
  2242.     idLexer::next = NULL;
  2243.     idLexer::hadError = false;
  2244. // RAVEN BEGIN
  2245. // jsinger: initialize compiled file
  2246.     idLexer::mBinaryFile = NULL;
  2247. // RAVEN END
  2248.     idLexer::LoadMemory( ptr, length, name );
  2249. }
  2250.  
  2251. /*
  2252. ================
  2253. idLexer::~idLexer
  2254. ================
  2255. */
  2256. idLexer::~idLexer( void ) {
  2257.     idLexer::FreeSource();
  2258. }
  2259.  
  2260. // RAVEN BEGIN
  2261. // jsinger: SetBaseFolder was moved to the Lexer base class to unify its functionality across all
  2262. //            derived classes
  2263. /*
  2264. ================
  2265. idLexer::SetBaseFolder
  2266. ================
  2267.  
  2268. void idLexer::SetBaseFolder( const char *path ) {
  2269.     idStr::Copynz( baseFolder, path, sizeof( baseFolder ) );
  2270. }
  2271. */
  2272. // RAVEN END
  2273. /*
  2274. ================
  2275. idLexer::HadError
  2276. ================
  2277. */
  2278. bool idLexer::HadError( void ) const {
  2279.     return hadError;
  2280. }
  2281.  
  2282.  
  2283. // RAVEN BEGIN
  2284. // jsinger: This method can write out a binary representation of a token in a format
  2285. //          suitable to be read by the Lexer
  2286. /*
  2287. ================
  2288. idLexer::WriteBinaryToken
  2289.     Writes a binary representation of the token value to the compiled file
  2290.     when compiling is turned on
  2291. ================
  2292. */
  2293. void idLexer::WriteBinaryToken(idToken *tok)
  2294. {
  2295.     bool swapBytes = (flags & LEXFL_BYTESWAP) == LEXFL_BYTESWAP;
  2296.  
  2297.     if(flags & LEXFL_WRITEBINARY)
  2298.     {
  2299.         unsigned char prefix;
  2300.  
  2301.         switch(tok->type)
  2302.         {
  2303.         case TT_NUMBER:
  2304.             if(tok->subtype & TT_INTEGER)
  2305.             {
  2306.                 if(tok->subtype & TT_UNSIGNED)
  2307.                 {
  2308.                     unsigned long val = tok->GetUnsignedLongValue();
  2309.                     unsigned char byteVal = (unsigned char)val;
  2310.                     unsigned short shortVal = (unsigned short)val;
  2311.                 
  2312.                     if(byteVal == val)
  2313.                     {
  2314.                         prefix = BTT_MAKENUMBER_PREFIX(BTT_NUMBER, BTT_SUBTYPE_UNSIGNEDINT, BTT_STORED_1BYTE);
  2315.                         TextCompiler::WriteValue<unsigned char>(prefix, mBinaryFile, swapBytes);
  2316.                         TextCompiler::WriteValue<unsigned char>(byteVal, mBinaryFile, swapBytes);
  2317.                     }
  2318.                     else if(shortVal == val)
  2319.                     {
  2320.                         prefix = BTT_MAKENUMBER_PREFIX(BTT_NUMBER, BTT_SUBTYPE_UNSIGNEDINT, BTT_STORED_2BYTE);
  2321.                         TextCompiler::WriteValue<unsigned char>(prefix, mBinaryFile, swapBytes);
  2322.                         TextCompiler::WriteValue<unsigned short>(shortVal, mBinaryFile, swapBytes);
  2323.                     }
  2324.                     else
  2325.                     {
  2326.                         prefix = BTT_MAKENUMBER_PREFIX(BTT_NUMBER, BTT_SUBTYPE_UNSIGNEDINT, BTT_STORED_4BYTE);
  2327.                         TextCompiler::WriteValue<unsigned char>(prefix, mBinaryFile, swapBytes);
  2328.                         TextCompiler::WriteValue<unsigned int>(val, mBinaryFile, swapBytes);
  2329.                     }
  2330.                 }
  2331.                 else
  2332.                 {
  2333.                     long val = tok->GetIntValue();
  2334.                     char byteVal = (char)val;
  2335.                     short shortVal = (short)val;
  2336.                 
  2337.                     if(byteVal == val)
  2338.                     {
  2339.                         prefix = BTT_MAKENUMBER_PREFIX(BTT_NUMBER, BTT_SUBTYPE_INT, BTT_STORED_1BYTE);
  2340.                         TextCompiler::WriteValue<unsigned char>(prefix, mBinaryFile, swapBytes);
  2341.                         TextCompiler::WriteValue<char>(byteVal, mBinaryFile, swapBytes);
  2342.                     }
  2343.                     else if(shortVal == val)
  2344.                     {
  2345.                         prefix = BTT_MAKENUMBER_PREFIX(BTT_NUMBER, BTT_SUBTYPE_INT, BTT_STORED_2BYTE);
  2346.                         TextCompiler::WriteValue<unsigned char>(prefix, mBinaryFile, swapBytes);
  2347.                         TextCompiler::WriteValue<short>(shortVal, mBinaryFile, swapBytes);
  2348.                     }
  2349.                     else
  2350.                     {
  2351.                         prefix = BTT_MAKENUMBER_PREFIX(BTT_NUMBER, BTT_SUBTYPE_INT, BTT_STORED_4BYTE);
  2352.                         TextCompiler::WriteValue<unsigned char>(prefix, mBinaryFile, swapBytes);
  2353.                         TextCompiler::WriteValue<int>(val, mBinaryFile, swapBytes);
  2354.                     }
  2355.                 }
  2356.             }
  2357.             else if(tok->subtype & TT_FLOAT)
  2358.             {
  2359.                 if(tok->subtype & TT_SINGLE_PRECISION)
  2360.                 {                
  2361.                     float val = tok->GetFloatValue();
  2362.                     int intval = tok->GetIntValue();
  2363.                     if(((float)intval) == val)        // integral check
  2364.                     {
  2365.                         char byteVal = (char)intval;
  2366.                         short shortVal = (short)intval;
  2367.                     
  2368.                         if(byteVal == intval)
  2369.                         {
  2370.                             prefix = BTT_MAKENUMBER_PREFIX(BTT_NUMBER, BTT_SUBTYPE_FLOAT, BTT_STORED_1BYTE);
  2371.                             TextCompiler::WriteValue<unsigned char>(prefix, mBinaryFile, swapBytes);
  2372.                             TextCompiler::WriteValue<char>(byteVal, mBinaryFile, swapBytes);
  2373.                         }
  2374.                         else if(shortVal == intval)
  2375.                         {
  2376.                             prefix = BTT_MAKENUMBER_PREFIX(BTT_NUMBER, BTT_SUBTYPE_FLOAT, BTT_STORED_2BYTE);
  2377.                             TextCompiler::WriteValue<unsigned char>(prefix, mBinaryFile, swapBytes);
  2378.                             TextCompiler::WriteValue<short>(shortVal, mBinaryFile, swapBytes);
  2379.                         }
  2380.                         else
  2381.                         {
  2382.                             prefix = BTT_MAKENUMBER_PREFIX(BTT_NUMBER, BTT_SUBTYPE_FLOAT, BTT_STORED_4BYTE);
  2383.                             TextCompiler::WriteValue<unsigned char>(prefix, mBinaryFile, swapBytes);
  2384.                             TextCompiler::WriteValue<float>(val, mBinaryFile, swapBytes);
  2385.                         }
  2386.                     }
  2387.                     else
  2388.                     {
  2389.                         prefix = BTT_MAKENUMBER_PREFIX(BTT_NUMBER, BTT_SUBTYPE_FLOAT, BTT_STORED_4BYTE);
  2390.                         TextCompiler::WriteValue<unsigned char>(prefix, mBinaryFile, swapBytes);
  2391.                         TextCompiler::WriteValue<float>(val, mBinaryFile, swapBytes);
  2392.                     }
  2393.                 }
  2394.                 else if(tok->subtype & TT_DOUBLE_PRECISION)
  2395.                 {
  2396.                     // we can write out doubles as floats because the text file doesn't have double precision anyway
  2397.                     float val = tok->GetDoubleValue();
  2398.                     int intval = tok->GetIntValue();
  2399.                     if(((float)intval) == val)        // integral check
  2400.                     {
  2401.                         char byteVal = (char)intval;
  2402.                         short shortVal = (short)intval;
  2403.                     
  2404.                         if(byteVal == intval)
  2405.                         {
  2406.                             prefix = BTT_MAKENUMBER_PREFIX(BTT_NUMBER, BTT_SUBTYPE_FLOAT, BTT_STORED_1BYTE);
  2407.                             TextCompiler::WriteValue<unsigned char>(prefix, mBinaryFile, swapBytes);
  2408.                             TextCompiler::WriteValue<char>(byteVal, mBinaryFile, swapBytes);
  2409.                         }
  2410.                         else if(shortVal == intval)
  2411.                         {
  2412.                             prefix = BTT_MAKENUMBER_PREFIX(BTT_NUMBER, BTT_SUBTYPE_FLOAT, BTT_STORED_2BYTE);
  2413.                             TextCompiler::WriteValue<unsigned char>(prefix, mBinaryFile, swapBytes);
  2414.                             TextCompiler::WriteValue<short>(shortVal, mBinaryFile, swapBytes);
  2415.                         }
  2416.                         else
  2417.                         {
  2418.                             prefix = BTT_MAKENUMBER_PREFIX(BTT_NUMBER, BTT_SUBTYPE_DOUBLE, BTT_STORED_4BYTE);
  2419.                             TextCompiler::WriteValue<unsigned char>(prefix, mBinaryFile, swapBytes);
  2420.                             TextCompiler::WriteValue<float>(val, mBinaryFile, swapBytes);
  2421.                         }
  2422.                     }
  2423.                     else
  2424.                     {
  2425.                         prefix = BTT_MAKENUMBER_PREFIX(BTT_NUMBER, BTT_SUBTYPE_DOUBLE, BTT_STORED_4BYTE);
  2426.                         TextCompiler::WriteValue<unsigned char>(prefix, mBinaryFile, swapBytes);
  2427.                         TextCompiler::WriteValue<float>(val, mBinaryFile, swapBytes);
  2428.                     }
  2429.                 }
  2430.             }
  2431.             break;
  2432.         case TT_STRING:
  2433.             TextCompiler::WriteValue<unsigned char>(BTT_MAKESTRING_PREFIX(BTT_STRING, tok->Length()), mBinaryFile, swapBytes);
  2434.             TextCompiler::WriteValue<idStr>(tok, mBinaryFile, swapBytes);
  2435.             break;
  2436.         case TT_LITERAL:
  2437.             TextCompiler::WriteValue<unsigned char>(BTT_MAKESTRING_PREFIX(BTT_LITERAL, tok->Length()), mBinaryFile, swapBytes);
  2438.             TextCompiler::WriteValue<idStr>(tok, mBinaryFile, swapBytes);
  2439.             break;
  2440.         case TT_NAME:
  2441.             TextCompiler::WriteValue<unsigned char>(BTT_MAKESTRING_PREFIX(BTT_NAME, tok->Length()), mBinaryFile, swapBytes);
  2442.             TextCompiler::WriteValue<idStr>(tok, mBinaryFile, swapBytes);
  2443.             break;
  2444.         case TT_PUNCTUATION:
  2445.             if(*tok == "&&")
  2446.             {
  2447.                 TextCompiler::WriteValue<unsigned char>(BTT_MAKEPUNCTUATION_PREFIX(BTT_PUNC_LOGICALAND), mBinaryFile, swapBytes);
  2448.             }
  2449.             else if (*tok == "&")
  2450.             {
  2451.                 TextCompiler::WriteValue<unsigned char>(BTT_MAKEPUNCTUATION_PREFIX(BTT_PUNC_AMPERSAND), mBinaryFile, swapBytes);
  2452.             }
  2453.             else if(*tok == "=")
  2454.             {
  2455.                 TextCompiler::WriteValue<unsigned char>(BTT_MAKEPUNCTUATION_PREFIX(BTT_PUNC_EQUAL), mBinaryFile, swapBytes);
  2456.             }
  2457.             else if(*tok == "==")
  2458.             {
  2459.                 TextCompiler::WriteValue<unsigned char>(BTT_MAKEPUNCTUATION_PREFIX(BTT_PUNC_EQUALEQUAL), mBinaryFile, swapBytes);
  2460.             }
  2461.             else if(*tok == "!=")
  2462.             {
  2463.                 TextCompiler::WriteValue<unsigned char>(BTT_MAKEPUNCTUATION_PREFIX(BTT_PUNC_NOTEQUAL), mBinaryFile, swapBytes);
  2464.             }
  2465.             else if(*tok == "!")
  2466.             {
  2467.                 TextCompiler::WriteValue<unsigned char>(BTT_MAKEPUNCTUATION_PREFIX(BTT_PUNC_EXCLAMATION), mBinaryFile, swapBytes);
  2468.             }
  2469.             else if(*tok == "<")
  2470.             {
  2471.                 TextCompiler::WriteValue<unsigned char>(BTT_MAKEPUNCTUATION_PREFIX(BTT_PUNC_LESSTHAN), mBinaryFile, swapBytes);
  2472.             }
  2473.             else if(*tok == "<=")
  2474.             {
  2475.                 TextCompiler::WriteValue<unsigned char>(BTT_MAKEPUNCTUATION_PREFIX(BTT_PUNC_LESSOREQUAL), mBinaryFile, swapBytes);
  2476.             }
  2477.             else if(*tok == "<<")
  2478.             {
  2479.                 TextCompiler::WriteValue<unsigned char>(BTT_MAKEPUNCTUATION_PREFIX(BTT_PUNC_SHIFTLEFT), mBinaryFile, swapBytes);
  2480.             }
  2481.             else if(*tok == ">")
  2482.             {
  2483.                 TextCompiler::WriteValue<unsigned char>(BTT_MAKEPUNCTUATION_PREFIX(BTT_PUNC_GREATERTHAN), mBinaryFile, swapBytes);
  2484.             }
  2485.             else if(*tok == ">=")
  2486.             {
  2487.                 TextCompiler::WriteValue<unsigned char>(BTT_MAKEPUNCTUATION_PREFIX(BTT_PUNC_GREATEROREQUAL), mBinaryFile, swapBytes);
  2488.             }
  2489.             else if(*tok == ">>")
  2490.             {
  2491.                 TextCompiler::WriteValue<unsigned char>(BTT_MAKEPUNCTUATION_PREFIX(BTT_PUNC_SHIFTRIGHT), mBinaryFile, swapBytes);
  2492.             }
  2493.             else if(*tok == "%")
  2494.             {
  2495.                 TextCompiler::WriteValue<unsigned char>(BTT_MAKEPUNCTUATION_PREFIX(BTT_PUNC_PERCENT), mBinaryFile, swapBytes);
  2496.             }
  2497.             else if(*tok == "[")
  2498.             {
  2499.                 TextCompiler::WriteValue<unsigned char>(BTT_MAKEPUNCTUATION_PREFIX(BTT_PUNC_LEFTBRACKET), mBinaryFile, swapBytes);
  2500.             }
  2501.             else if(*tok == "]")
  2502.             {
  2503.                 TextCompiler::WriteValue<unsigned char>(BTT_MAKEPUNCTUATION_PREFIX(BTT_PUNC_RIGHTBRACKET), mBinaryFile, swapBytes);
  2504.             }
  2505.             else if(*tok == "-")
  2506.             {
  2507.                 TextCompiler::WriteValue<unsigned char>(BTT_MAKEPUNCTUATION_PREFIX(BTT_PUNC_MINUS), mBinaryFile, swapBytes);
  2508.             }
  2509.             else if(*tok == "--")
  2510.             {
  2511.                 TextCompiler::WriteValue<unsigned char>(BTT_MAKEPUNCTUATION_PREFIX(BTT_PUNC_MINUSMINUS), mBinaryFile, swapBytes);
  2512.             }
  2513.             else if(*tok == "-=")
  2514.             {
  2515.                 TextCompiler::WriteValue<unsigned char>(BTT_MAKEPUNCTUATION_PREFIX(BTT_PUNC_MINUSEQUAL), mBinaryFile, swapBytes);
  2516.             }
  2517.             else if(*tok == "+")
  2518.             {
  2519.                 TextCompiler::WriteValue<unsigned char>(BTT_MAKEPUNCTUATION_PREFIX(BTT_PUNC_PLUS), mBinaryFile, swapBytes);
  2520.             }
  2521.             else if(*tok == "+=")
  2522.             {
  2523.                 TextCompiler::WriteValue<unsigned char>(BTT_MAKEPUNCTUATION_PREFIX(BTT_PUNC_PLUSEQUAL), mBinaryFile, swapBytes);
  2524.             }
  2525.             else if(*tok == "++")
  2526.             {
  2527.                 TextCompiler::WriteValue<unsigned char>(BTT_MAKEPUNCTUATION_PREFIX(BTT_PUNC_PLUSPLUS), mBinaryFile, swapBytes);
  2528.             }
  2529.             else if(*tok == "(")
  2530.             {
  2531.                 TextCompiler::WriteValue<unsigned char>(BTT_MAKEPUNCTUATION_PREFIX(BTT_PUNC_LEFTPAREN), mBinaryFile, swapBytes);
  2532.             }
  2533.             else if(*tok == ")")
  2534.             {
  2535.                 TextCompiler::WriteValue<unsigned char>(BTT_MAKEPUNCTUATION_PREFIX(BTT_PUNC_RIGHTPAREN), mBinaryFile, swapBytes);
  2536.             }
  2537.             else if(*tok == "{")
  2538.             {
  2539.                 TextCompiler::WriteValue<unsigned char>(BTT_MAKEPUNCTUATION_PREFIX(BTT_PUNC_LEFTBRACE), mBinaryFile, swapBytes);
  2540.             }
  2541.             else if(*tok == "}")
  2542.             {
  2543.                 TextCompiler::WriteValue<unsigned char>(BTT_MAKEPUNCTUATION_PREFIX(BTT_PUNC_RIGHTBRACE), mBinaryFile, swapBytes);
  2544.             }
  2545.             else if(*tok == ",")
  2546.             {
  2547.                 TextCompiler::WriteValue<unsigned char>(BTT_MAKEPUNCTUATION_PREFIX(BTT_PUNC_COMMA), mBinaryFile, swapBytes);
  2548.             }
  2549.             else if(*tok == "::")
  2550.             {
  2551.                 TextCompiler::WriteValue<unsigned char>(BTT_MAKEPUNCTUATION_PREFIX(BTT_PUNC_DOUBLECOLON), mBinaryFile, swapBytes);
  2552.             }
  2553.             else if(*tok == "#")
  2554.             {
  2555.                 TextCompiler::WriteValue<unsigned char>(BTT_MAKEPUNCTUATION_PREFIX(BTT_PUNC_HASH), mBinaryFile, swapBytes);
  2556.             }
  2557.             else if(*tok == "##")
  2558.             {
  2559.                 TextCompiler::WriteValue<unsigned char>(BTT_MAKEPUNCTUATION_PREFIX(BTT_PUNC_DOUBLEHASH), mBinaryFile, swapBytes);
  2560.             }
  2561.             else if(*tok == "/")
  2562.             {
  2563.                 TextCompiler::WriteValue<unsigned char>(BTT_MAKEPUNCTUATION_PREFIX(BTT_PUNC_FORWARDSLASH), mBinaryFile, swapBytes);
  2564.             }
  2565.             else if(*tok == "\\")
  2566.             {
  2567.                 TextCompiler::WriteValue<unsigned char>(BTT_MAKEPUNCTUATION_PREFIX(BTT_PUNC_BACKSLASH), mBinaryFile, swapBytes);
  2568.             }
  2569.             else if(*tok == ";")
  2570.             {
  2571.                 TextCompiler::WriteValue<unsigned char>(BTT_MAKEPUNCTUATION_PREFIX(BTT_PUNC_SEMICOLON), mBinaryFile, swapBytes);
  2572.             }
  2573.             else if(*tok == ".")
  2574.             {
  2575.                 TextCompiler::WriteValue<unsigned char>(BTT_MAKEPUNCTUATION_PREFIX(BTT_PUNC_PERIOD), mBinaryFile, swapBytes);
  2576.             }
  2577.             else if(*tok == "$")
  2578.             {
  2579.                 TextCompiler::WriteValue<unsigned char>(BTT_MAKEPUNCTUATION_PREFIX(BTT_PUNC_DOLLARSIGN), mBinaryFile, swapBytes);
  2580.             }
  2581.             else if(*tok == "~")
  2582.             {
  2583.                 TextCompiler::WriteValue<unsigned char>(BTT_MAKEPUNCTUATION_PREFIX(BTT_PUNC_TILDE), mBinaryFile, swapBytes);
  2584.             }
  2585.             else if(*tok == "|")
  2586.             {
  2587.                 TextCompiler::WriteValue<unsigned char>(BTT_MAKEPUNCTUATION_PREFIX(BTT_PUNC_PIPE), mBinaryFile, swapBytes);
  2588.             }
  2589.             else if(*tok == "||")
  2590.             {
  2591.                 TextCompiler::WriteValue<unsigned char>(BTT_MAKEPUNCTUATION_PREFIX(BTT_PUNC_DOUBLEPIPE), mBinaryFile, swapBytes);
  2592.             }
  2593.             else if(*tok == "*")
  2594.             {
  2595.                 TextCompiler::WriteValue<unsigned char>(BTT_MAKEPUNCTUATION_PREFIX(BTT_PUNC_ASTERISK), mBinaryFile, swapBytes);
  2596.             }
  2597.             else if(*tok == "*=")
  2598.             {
  2599.                 TextCompiler::WriteValue<unsigned char>(BTT_MAKEPUNCTUATION_PREFIX(BTT_PUNC_TIMESEQUAL), mBinaryFile, swapBytes);
  2600.             }
  2601.             else if(*tok == "í")
  2602.             {
  2603.                 TextCompiler::WriteValue<unsigned char>(BTT_PUNC_INVERTEDPLING, mBinaryFile, swapBytes);
  2604.             }
  2605.             else if(*tok == "┐")
  2606.             {
  2607.                 TextCompiler::WriteValue<unsigned char>(BTT_PUNC_INVERTEDQUERY, mBinaryFile, swapBytes);
  2608.             }
  2609.             else
  2610.             {
  2611.                 common->Warning("Unsupported punctuation: '%s'", tok->c_str());
  2612.                 
  2613.                 TextCompiler::WriteValue<unsigned char>(BTT_MAKEPUNCTUATION_PREFIX(BTT_PUNC_ASNULLTERMINATED), mBinaryFile, swapBytes);
  2614.                 TextCompiler::WriteValue<idStr>(tok, mBinaryFile, swapBytes);
  2615.             }
  2616.             break;
  2617.         default:
  2618.             // wtf?
  2619.             assert(false);
  2620.         }
  2621.     }
  2622. }
  2623.  
  2624. // jsinger: this method takes an existing file and causes a binary tokenized
  2625. //          version of the file to be generated
  2626. void idLexer::WriteBinaryFile(char const * const filename)
  2627. {
  2628.     if(!cvarSystem->GetCVarBool("com_BinaryRead"))
  2629.     {
  2630.         unsigned int swap=0;
  2631.  
  2632.         switch(cvarSystem->GetCVarInteger("com_BinaryWrite"))
  2633.         {
  2634.         case 1:
  2635.             swap = 0;
  2636.             break;
  2637.         case 2:
  2638.             swap = LEXFL_BYTESWAP;
  2639.             break;
  2640.         }
  2641.  
  2642.         idLexer src(filename, LEXFL_WRITEBINARY | swap);
  2643.         idToken token;
  2644.         if(src.IsLoaded())
  2645.         {
  2646.             while(src.ReadToken(&token))
  2647.             {
  2648.                 // we don't need to do anything, because just reading the file
  2649.                 // causes it to write a tokenized version
  2650.             }
  2651.         }
  2652.     }
  2653. }
  2654.  
  2655. // jsinger: implementation for a class that can load binary tokenized files
  2656. using namespace TextCompiler;
  2657. Lexer::Lexer(int flags)
  2658. {
  2659. #ifndef LEXER_READ_AHEAD
  2660.     mFile = NULL;
  2661. #endif
  2662.     offset = 0;
  2663.     tokenAvailable = false;
  2664.     mFlags = flags;
  2665.     if(flags & LEXFL_READBINARY)
  2666.     {
  2667.         mDelegate = NULL;
  2668.     }
  2669.     else
  2670.     {
  2671.         mDelegate = new idLexer(flags);
  2672.     }
  2673. }
  2674.  
  2675. Lexer::Lexer(char const * const ptr, int length, char const * const name, int flags)
  2676. {
  2677.     if(flags & LEXFL_READBINARY)
  2678.     {
  2679. #ifdef LEXER_READ_AHEAD
  2680.         mLexerIOWrapper.InitFromMemory(ptr, length);
  2681. #else
  2682.         assert(false);
  2683. #endif
  2684.         mDelegate = NULL;
  2685.     }
  2686.     else
  2687.     {
  2688.         mDelegate = new idLexer(ptr, length, name, flags);
  2689.     }
  2690. }
  2691.  
  2692. Lexer::Lexer(char const * const filename, int flags, bool OSPath)
  2693. {
  2694. #ifndef LEXER_READ_AHEAD
  2695.     mFile = NULL;
  2696. #endif
  2697.     offset = 0;
  2698.     tokenAvailable = false;
  2699.     mFlags = flags;
  2700.     if(flags & LEXFL_READBINARY)
  2701.     {
  2702.         mDelegate = NULL;
  2703.         LoadFile(filename, OSPath);
  2704.     }
  2705.     else
  2706.     {
  2707.         mDelegate = new idLexer(filename, flags, OSPath);
  2708.     }
  2709. }
  2710.  
  2711. Lexer::~Lexer()
  2712. {
  2713.     FreeSource();
  2714. }
  2715.  
  2716. char const * Lexer::GetFileName()
  2717. {
  2718.     if(mDelegate)
  2719.     {
  2720.         return mDelegate->GetFileName();
  2721.     }
  2722.     else
  2723.     {
  2724.         assert(false);
  2725.         return "";
  2726.     }
  2727. }
  2728.  
  2729. bool Lexer::HadError() const
  2730. {
  2731.     if(mDelegate)
  2732.     {
  2733.         return mDelegate->HadError();
  2734.     }
  2735.     else
  2736.     {
  2737.         assert(false);
  2738.         return false;
  2739.     }
  2740. }
  2741.  
  2742. void Lexer::Warning(char const *str, ...)
  2743. {
  2744.     char text[MAX_STRING_CHARS];
  2745.     va_list ap;
  2746.  
  2747.     va_start( ap, str );
  2748.     vsprintf( text, str, ap );
  2749.     va_end( ap );
  2750.     idLib::common->Warning( "Lexer: '%s'", text );
  2751. }
  2752.  
  2753. void Lexer::Error(char const *str, ...)
  2754. {
  2755.     char text[MAX_STRING_CHARS];
  2756.     va_list ap;
  2757.  
  2758.     va_start( ap, str );
  2759.     vsprintf( text, str, ap );
  2760.     va_end( ap );
  2761.  
  2762.     assert(false);
  2763.  
  2764.     idLib::common->Error( "Lexer: offset: %d '%s'", offset, text );
  2765. }
  2766.  
  2767. int const Lexer::GetLineNum()
  2768. {
  2769.     if(mDelegate)
  2770.     {
  2771.         return mDelegate->GetLineNum();
  2772.     }
  2773.     else
  2774.     {
  2775.         return 1;
  2776.     }
  2777. }
  2778.  
  2779. unsigned int const Lexer::GetFileTime()
  2780. {
  2781.     if(mDelegate)
  2782.     {
  2783.         return mDelegate->GetFileTime();
  2784.     }
  2785.     else
  2786.     {
  2787.         return 0;
  2788.     }
  2789. }
  2790.  
  2791. int const Lexer::GetFileOffset()
  2792. {
  2793.     if(mDelegate)
  2794.     {
  2795.         return mDelegate->GetFileOffset();
  2796.     }
  2797.     {
  2798.         assert(false);
  2799.         return 0;
  2800.     }
  2801. }
  2802.  
  2803. int Lexer::EndOfFile()
  2804. {
  2805.     if(mDelegate)
  2806.     {
  2807.         return mDelegate->EndOfFile();
  2808.     }
  2809.     else
  2810.     {
  2811. #ifdef LEXER_READ_AHEAD
  2812.         return mLexerIOWrapper.Tell()>= mLexerIOWrapper.Length();
  2813. #else
  2814.         return mFile->Tell()>= mFile->Length();
  2815. #endif
  2816.     }
  2817. }
  2818.  
  2819. void Lexer::Reset()
  2820. {
  2821.     if(mDelegate)
  2822.     {
  2823.         mDelegate->Reset();
  2824.     }
  2825.     else
  2826.     {
  2827. #ifdef LEXER_READ_AHEAD
  2828.         mLexerIOWrapper.Seek(0,     FS_SEEK_SET);
  2829. #else
  2830.         mFile->Seek(0,     FS_SEEK_SET);
  2831. #endif
  2832.         offset = 0;
  2833.         tokenAvailable = false;
  2834.     }
  2835. }
  2836.  
  2837. int Lexer::GetFlags()
  2838. {
  2839.     if(mDelegate)
  2840.     {
  2841.         return mDelegate->GetFlags();
  2842.     }
  2843.     else
  2844.     {
  2845.         return mFlags;
  2846.     }
  2847. }
  2848.  
  2849. void Lexer::SetFlags(int flags)
  2850. {
  2851.     if(mDelegate)
  2852.     {
  2853.         mDelegate->SetFlags(flags);
  2854.     }
  2855.     else
  2856.     {
  2857.         mFlags = flags;
  2858.     }
  2859. }
  2860.  
  2861. int Lexer::GetPunctuationId(char const *str)
  2862. {
  2863.     if(mDelegate)
  2864.     {
  2865.         return mDelegate->GetPunctuationId(str);
  2866.     }
  2867.     else
  2868.     {
  2869.         // this method is not supported
  2870.         assert(false);
  2871.         return 0;
  2872.     }
  2873. }
  2874.  
  2875. void Lexer::SetPunctuations(struct punctuation_s const *punc)
  2876. {
  2877.     if(mDelegate)
  2878.     {
  2879.         mDelegate->SetPunctuations(punc);
  2880.     }
  2881.     else
  2882.     {
  2883.         // this method is not supported
  2884.         assert(false);
  2885.     }
  2886. }
  2887.  
  2888. int Lexer::GetLastWhiteSpaceEnd() const
  2889. {
  2890.     if(mDelegate)
  2891.     {
  2892.         return mDelegate->GetLastWhiteSpaceEnd();
  2893.     }
  2894.     else
  2895.     {
  2896.         return 0;
  2897.     }
  2898. }
  2899.  
  2900. char const *Lexer::GetPunctuationFromId(int val)
  2901. {
  2902.     if(mDelegate)
  2903.     {
  2904.         return mDelegate->GetPunctuationFromId(val);
  2905.     }
  2906.     else
  2907.     {
  2908.         // this method is not supported
  2909.         assert(false);
  2910.         return "";
  2911.     }
  2912. }
  2913.  
  2914. int Lexer::GetLastWhiteSpaceStart() const
  2915. {
  2916.     if(mDelegate)
  2917.     {
  2918.         return mDelegate->GetLastWhiteSpaceStart();
  2919.     }
  2920.     else
  2921.     {
  2922.         return 0;
  2923.     }
  2924. }
  2925.  
  2926. int Lexer::GetLastWhiteSpace(idStr &str) const
  2927. {
  2928.     if(mDelegate)
  2929.     {
  2930.         return mDelegate->GetLastWhiteSpace(str);
  2931.     }
  2932.     else
  2933.     {
  2934.         return 0;
  2935.     }
  2936. }
  2937.  
  2938. char const *Lexer::ParseRestOfLine(idStr &str)
  2939. {
  2940.     if(mDelegate)
  2941.     {
  2942.         return mDelegate->ParseRestOfLine(str);
  2943.     }
  2944.     else
  2945.     {
  2946.         // this method is not supported
  2947.         assert(false);
  2948.         return "";
  2949.     }
  2950. }
  2951.  
  2952. char const *Lexer::ParseBracedSectionExact(idStr &str, int val)
  2953. {
  2954.     if(mDelegate)
  2955.     {
  2956.         return mDelegate->ParseBracedSectionExact(str, val);
  2957.     }
  2958.     else
  2959.     {
  2960.         // this method is not supported
  2961.         assert(false);
  2962.         return "";
  2963.     }
  2964. }
  2965.  
  2966. char const *Lexer::ParseBracedSection(idStr &str)
  2967. {
  2968.     if(mDelegate)
  2969.     {
  2970.         return mDelegate->ParseBracedSection(str);
  2971.     }
  2972.     else
  2973.     {
  2974.         // this method is not supported
  2975.         assert(false);
  2976.         return "";
  2977.     }
  2978. }
  2979.  
  2980. int Lexer::Parse3DMatrix(int z, int y, int x, float *m)
  2981. {
  2982.     if(mDelegate)
  2983.     {
  2984.         return mDelegate->Parse3DMatrix(z, y, x, m);
  2985.     }
  2986.     else
  2987.     {
  2988.         int i;
  2989.  
  2990.         if ( !ExpectTokenString( "(" ) ) {
  2991.             return false;
  2992.         }
  2993.  
  2994.         for ( i = 0 ; i < z; i++ ) {
  2995.             if ( !Parse2DMatrix( y, x, m + i * x*y ) ) {
  2996.                 return false;
  2997.             }
  2998.         }
  2999.  
  3000.         if ( !ExpectTokenString( ")" ) ) {
  3001.             return false;
  3002.         }
  3003.         return true;
  3004.     }
  3005. }
  3006.  
  3007. int Lexer::Parse2DMatrix(int y, int x, float *m)
  3008. {
  3009.     if(mDelegate)
  3010.     {
  3011.         return mDelegate->Parse2DMatrix(y, x, m);
  3012.     }
  3013.     else
  3014.     {
  3015.         int i;
  3016.  
  3017.         if ( !ExpectTokenString( "(" ) ) {
  3018.             return false;
  3019.         }
  3020.  
  3021.         for ( i = 0; i < y; i++ ) {
  3022.             if ( !Parse1DMatrix( x, m + i * x ) ) {
  3023.                 return false;
  3024.             }
  3025.         }
  3026.  
  3027.         if ( !ExpectTokenString( ")" ) ) {
  3028.             return false;
  3029.         }
  3030.         return true;
  3031.     }
  3032. }
  3033.  
  3034. int Lexer::Parse1DMatrixOpenEnded(int MaxCount, float *m)
  3035. {
  3036.     if(mDelegate)
  3037.     {
  3038.         return mDelegate->Parse1DMatrixOpenEnded(MaxCount, m);
  3039.     }
  3040.     else
  3041.     {
  3042.         int i;
  3043.  
  3044.         if ( !ExpectTokenString( "(" ) ) {
  3045.             return 0;
  3046.         }
  3047.  
  3048.         for ( i = 0; i < MaxCount; i++ ) {
  3049.             idToken tok;
  3050.  
  3051.             if (!ReadToken( &tok )) {
  3052.                 return 0;
  3053.             }
  3054.  
  3055.             if ( tok == ")" ) {
  3056.                 return i;
  3057.             }
  3058.  
  3059.             UnreadToken( &tok );
  3060.  
  3061.             m[i] = ParseFloat();
  3062.         }
  3063.  
  3064.         if ( !ExpectTokenString( ")" ) ) {
  3065.             return 0;
  3066.         }
  3067.  
  3068.         return i;
  3069.     }
  3070. }
  3071.  
  3072. int Lexer::Parse1DMatrix(int x, float *m)
  3073. {
  3074.     if(mDelegate)
  3075.     {
  3076.         return mDelegate->Parse1DMatrix(x, m);
  3077.     }
  3078.     else
  3079.     {
  3080.         int i;
  3081.  
  3082.         if ( !ExpectTokenString( "(" ) ) {
  3083.             return false;
  3084.         }
  3085.  
  3086.         for ( i = 0; i < x; i++ ) {
  3087.             m[i] = ParseFloat();
  3088.         }
  3089.  
  3090.         if ( !ExpectTokenString( ")" ) ) {
  3091.             return false;
  3092.         }
  3093.         return true;
  3094.     }
  3095. }
  3096.  
  3097. float Lexer::ParseFloat(bool *errorFlag)
  3098. {
  3099.     if(mDelegate)
  3100.     {
  3101.         return mDelegate->ParseFloat(errorFlag);
  3102.     }
  3103.     else
  3104.     {
  3105.         idToken token;
  3106.  
  3107.         if(EndOfFile())
  3108.         {
  3109.             return 0;
  3110.         }
  3111.  
  3112.         if ( !ReadToken( &token ) ) {
  3113.             Warning( "couldn't read expected floating point number" );
  3114.             return 0;
  3115.         }
  3116.         if ( token.type == TT_PUNCTUATION && token == "-" ) {
  3117.             ExpectTokenType( TT_NUMBER, 0, &token );
  3118.             return -token.GetFloatValue();
  3119.         }
  3120.         else if ( token.type != TT_NUMBER ) {
  3121.             Warning( "expected float value, found '%s'", token.c_str() );
  3122.             }
  3123.         return token.GetFloatValue();
  3124.     }
  3125. }
  3126.  
  3127. bool Lexer::ParseBool()
  3128. {
  3129.     if(mDelegate)
  3130.     {
  3131.         return mDelegate->ParseBool();
  3132.     }
  3133.     else
  3134.     {
  3135.         idToken token;
  3136.  
  3137.         if ( !ExpectTokenType( TT_NUMBER, 0, &token ) ) {
  3138.             Error( "couldn't read expected boolean" );
  3139.             return false;
  3140.         }
  3141.         return ( token.GetIntValue() != 0 );
  3142.     }
  3143. }
  3144.  
  3145. int Lexer::ParseInt()
  3146. {
  3147.     if(mDelegate)
  3148.     {
  3149.         return mDelegate->ParseInt();
  3150.     }
  3151.     else
  3152.     {
  3153.         idToken token;
  3154.  
  3155.         if ( !ReadToken( &token ) ) {
  3156.             Error( "couldn't read expected integer" );
  3157.             return 0;
  3158.         }
  3159.         if ( token.type == TT_PUNCTUATION && token == "-" ) {
  3160.             ExpectTokenType( TT_NUMBER, TT_INTEGER, &token );
  3161.             return -((signed int) token.GetIntValue());
  3162.         }
  3163.         else if ( token.type != TT_NUMBER || token.subtype == TT_FLOAT ) {
  3164.             Error( "expected integer value, found '%s'", token.c_str() );
  3165.         }
  3166.         return token.GetIntValue();
  3167.     }
  3168. }
  3169.  
  3170. int Lexer::ReadTokenOnLine(idToken *token)
  3171. {
  3172.     if(mDelegate)
  3173.     {
  3174.         return mDelegate->ReadTokenOnLine(token);
  3175.     }
  3176.     else
  3177.     {
  3178.         // lines are meaningless in the binary format
  3179.         return ReadToken(token);
  3180.     }
  3181. }
  3182.  
  3183. void Lexer::UnreadToken(idToken const *token)
  3184. {
  3185.     if(mDelegate)
  3186.     {
  3187.         mDelegate->UnreadToken(token);
  3188.     }
  3189.     else
  3190.     {
  3191.         if ( tokenAvailable ) 
  3192.         {
  3193.             idLib::common->FatalError( "Lexer::unreadToken, unread token twice\n" );
  3194.         }
  3195.         unreadToken = *token;
  3196.         tokenAvailable = true;
  3197.         offset -= unreadSize+1;        // the +1 is for the prefix
  3198.     }
  3199. }
  3200.  
  3201. int Lexer::SkipBracedSection(bool parseFirstBrace)
  3202. {
  3203.     if(mDelegate)
  3204.     {
  3205.         return mDelegate->SkipBracedSection(parseFirstBrace);
  3206.     }
  3207.     else
  3208.     {
  3209.         idToken token;
  3210.         int depth;
  3211.  
  3212.         depth = parseFirstBrace ? 0 : 1;
  3213.         do {
  3214.             if ( !ReadToken( &token ) ) {
  3215.                 return false;
  3216.             }
  3217.             if ( token.type == TT_PUNCTUATION ) {
  3218.                 if ( token == "{" ) {
  3219.                     depth++;
  3220.                 } else if ( token == "}" ) {
  3221.                     depth--;
  3222.                 }
  3223.             }
  3224.         } while( depth );
  3225.         return true;
  3226.     }
  3227. }
  3228.  
  3229. int Lexer::SkipRestOfLine()
  3230. {
  3231.     if(mDelegate)
  3232.     {
  3233.         return mDelegate->SkipRestOfLine();
  3234.     }
  3235.     else
  3236.     {
  3237.         // this method is not supported
  3238.         assert(false);
  3239.         return 0;
  3240.     }
  3241. }
  3242.  
  3243. int Lexer::SkipUntilString(char const *str)
  3244. {
  3245.     if(mDelegate)
  3246.     {
  3247.         return mDelegate->SkipUntilString(str);
  3248.     }
  3249.     else
  3250.     {
  3251.         // this method is not supported
  3252.         assert(false);
  3253.         return 0;
  3254.     }
  3255. }
  3256.  
  3257. int Lexer::CheckTokenType(int type, int subtype, idToken *token)
  3258. {
  3259.     if(mDelegate)
  3260.     {
  3261.         return mDelegate->CheckTokenType(type, subtype, token);
  3262.     }
  3263.     else
  3264.     {
  3265.         idToken tok;
  3266.  
  3267.         if (!ReadToken( &tok )) {
  3268.             return 0;
  3269.         }
  3270.         // if the type matches
  3271.         if (tok.type == type && (tok.subtype & subtype) == subtype) {
  3272.             *token = tok;
  3273.             return 1;
  3274.         }
  3275.         return 0;
  3276.     }
  3277. }
  3278.  
  3279. int Lexer::CheckTokenString(char const *string)
  3280. {
  3281.     if(mDelegate)
  3282.     {
  3283.         return mDelegate->CheckTokenString(string);
  3284.     }
  3285.     else
  3286.     {
  3287.         idToken tok;
  3288.  
  3289.         if (!ReadToken( &tok )) {
  3290.             return 0;
  3291.         }
  3292.  
  3293.         // if the token is available
  3294.         if ( tok == string ) {
  3295.             return 1;
  3296.         }
  3297.  
  3298.         UnreadToken( &tok );
  3299.         return 0;
  3300.     }
  3301. }
  3302.  
  3303. int Lexer::ExpectAnyToken(idToken *token)
  3304. {
  3305.     if(mDelegate)
  3306.     {
  3307.         return mDelegate->ExpectAnyToken(token);
  3308.     }
  3309.     else
  3310.     {
  3311.         if (!ReadToken( token )) {
  3312.             Error( "couldn't read expected token" );
  3313.             return 0;
  3314.         }
  3315.         else {
  3316.             return 1;
  3317.         }
  3318.     }
  3319. }
  3320.  
  3321. int Lexer::ExpectTokenType(int type, int subtype, idToken *token)
  3322. {
  3323.     if(mDelegate)
  3324.     {
  3325.         return mDelegate->ExpectTokenType(type, subtype, token);
  3326.     }
  3327.     else
  3328.     {
  3329.         idStr str;
  3330.  
  3331.         if ( !ReadToken( token ) ) {
  3332.             Error( "couldn't read expected token" );
  3333.             return 0;
  3334.         }
  3335.  
  3336.         if ( token->type != type ) {
  3337.             switch( type ) {
  3338.                 case TT_STRING: str = "string"; break;
  3339.                 case TT_LITERAL: str = "literal"; break;
  3340.                 case TT_NUMBER: str = "number"; break;
  3341.                 case TT_NAME: str = "name"; break;
  3342.                 case TT_PUNCTUATION: str = "punctuation"; break;
  3343.                 default: str = "unknown type"; break;
  3344.             }
  3345.             Error( "expected a %s but found '%s'", str.c_str(), token->c_str() );
  3346.             return 0;
  3347.         }
  3348.         if ( token->type == TT_NUMBER ) {
  3349.             if ( (token->subtype & subtype) != subtype ) {
  3350.                 str.Clear();
  3351.                 if ( subtype & TT_DECIMAL ) str = "decimal ";
  3352.                 if ( subtype & TT_HEX ) str = "hex ";
  3353.                 if ( subtype & TT_OCTAL ) str = "octal ";
  3354.                 if ( subtype & TT_BINARY ) str = "binary ";
  3355.                 if ( subtype & TT_UNSIGNED ) str += "unsigned ";
  3356.                 if ( subtype & TT_LONG ) str += "long ";
  3357.                 if ( subtype & TT_FLOAT ) str += "float ";
  3358.                 if ( subtype & TT_INTEGER ) str += "integer ";
  3359.                 str.StripTrailing( ' ' );
  3360.                 Error( "expected %s but found '%s'", str.c_str(), token->c_str() );
  3361.                 return 0;
  3362.             }
  3363.         }
  3364.         else if ( token->type == TT_PUNCTUATION ) {
  3365.             if ( subtype < 0 ) {
  3366.                 Error( "BUG: wrong punctuation subtype" );
  3367.                 return 0;
  3368.             }
  3369.             if ( token->subtype != subtype ) {
  3370.                 Error( "expected '%s' but found '%s'", GetPunctuationFromId( subtype ), token->c_str() );
  3371.                 return 0;
  3372.             }
  3373.         }
  3374.         return 1;
  3375.     }
  3376. }
  3377.  
  3378. int Lexer::ExpectTokenString(char const *string)
  3379. {
  3380.     if(mDelegate)
  3381.     {
  3382.         return mDelegate->ExpectTokenString(string);
  3383.     }
  3384.     else
  3385.     {
  3386.         idToken token;
  3387.  
  3388.         if(!ReadToken( &token ))
  3389.         {
  3390.             Error( "couldn't find expected '%s'", string );
  3391.             return 0;
  3392.         }
  3393.         if( token != string )
  3394.         {
  3395.             Error( "expected '%s' but found '%s'", string, token.c_str() );
  3396.             return 0;
  3397.         }
  3398.         return 1;
  3399.     }
  3400. }
  3401.  
  3402. int Lexer::ReadToken(idToken *token)
  3403. {
  3404.  
  3405. #ifdef LEXER_READ_AHEAD
  3406. #define OBJ &mLexerIOWrapper
  3407. #else
  3408. #define OBJ mFile
  3409. #endif
  3410.     
  3411.     if(mDelegate)
  3412.     {
  3413.         return mDelegate->ReadToken(token);
  3414.     }
  3415.     else
  3416.     {
  3417.         // if there are any unread tokens, use them first
  3418.         if(tokenAvailable)
  3419.         {
  3420.             *token = unreadToken;
  3421.             tokenAvailable = false;
  3422.         }
  3423.         else
  3424.         {
  3425. #ifndef LEXER_READ_AHEAD
  3426.             assert(mFile);
  3427. #endif
  3428.             if(EndOfFile())
  3429.                 return 0;
  3430.             unsigned char prefix = ReadValue<unsigned char>(OBJ);
  3431.             token->Clear();
  3432.  
  3433.             // BTT types are equivalent to the TT types except they are one less, so we add one to get the token type
  3434.             if(BTT_GET_TYPE(prefix) == BTT_PUNCTUATION2)
  3435.                 token->type = TT_PUNCTUATION;
  3436.             else
  3437.                 token->type = BTT_GET_TYPE(prefix)+1;
  3438.  
  3439.             // this is used only to determine if the punctuation was encoded in the prefix or came afterwards as a null terminated string
  3440.             bool encoded = true;
  3441.  
  3442.             switch(token->type)
  3443.             {
  3444.             case TT_STRING: 
  3445.             case TT_NAME:
  3446.             case TT_LITERAL:
  3447.                 if(BTT_GET_STRING_LENGTH(prefix) != 0)
  3448.                 {
  3449.                     // short string
  3450.                     int length = BTT_GET_STRING_LENGTH(prefix);
  3451.                     if ( length == 0 ) {
  3452.                         assert( false );
  3453.                     }
  3454.                     int i;
  3455.                     for( i = 0; i < length; i++ )
  3456.                     {
  3457.                         char c = ReadValue<char>(OBJ);
  3458.                         token->Append(c);
  3459.                     }
  3460.                     // short strings have no null, so we must add one
  3461.                     //token->Append('\0');
  3462.  
  3463.                     token->subtype = token->Length();
  3464.                     unreadSize = token->Length();
  3465.                 }
  3466.                 else
  3467.                 {
  3468.                     // null terminated string
  3469.                     *token = ReadValue<idStr>(OBJ);
  3470.                     token->subtype = token->Length();
  3471.                     unreadSize = token->Length()+1;
  3472.                 }
  3473.                 break;
  3474.             case TT_PUNCTUATION:
  3475.                 switch(BTT_GET_PUNCTUATION(prefix))
  3476.                 {
  3477.                 case BTT_PUNC_ASNULLTERMINATED:
  3478.                     // null terminated string
  3479.                     *token = ReadValue<idStr>(OBJ);
  3480.                     token->subtype = token->Length();
  3481.                     unreadSize = token->Length()+1;
  3482.                     encoded = false;
  3483.                     break;
  3484.                 case BTT_PUNC_RIGHTPAREN:
  3485.                     *token = ")";
  3486.                     break;
  3487.                 case BTT_PUNC_LEFTBRACE:
  3488.                     *token = "{";
  3489.                     break;
  3490.                 case BTT_PUNC_RIGHTBRACE:
  3491.                     *token = "}";
  3492.                     break;
  3493.                 case BTT_PUNC_MINUS:
  3494.                     *token = "-";
  3495.                     break;
  3496.                 case BTT_PUNC_PLUS:
  3497.                     *token = "+";
  3498.                     break;
  3499.                 case BTT_PUNC_COMMA:
  3500.                     *token = ",";
  3501.                     break;
  3502.                 case BTT_PUNC_PLUSPLUS:
  3503.                     *token = "++";
  3504.                     break;
  3505.                 case BTT_PUNC_LEFTBRACKET:
  3506.                     *token = "[";
  3507.                     break;
  3508.                 case BTT_PUNC_RIGHTBRACKET:
  3509.                     *token = "]";
  3510.                     break;
  3511.                 case BTT_PUNC_EQUAL:
  3512.                     *token = "=";
  3513.                     break;
  3514.                 case BTT_PUNC_EQUALEQUAL:
  3515.                     *token = "==";
  3516.                     break;
  3517.                 case BTT_PUNC_NOTEQUAL:
  3518.                     *token = "!=";
  3519.                     break;
  3520.                 case BTT_PUNC_PERCENT:
  3521.                     *token = "%";
  3522.                     break;
  3523.                 case BTT_PUNC_LESSTHAN:
  3524.                     *token = "<";
  3525.                     break;
  3526.                 case BTT_PUNC_GREATERTHAN:
  3527.                     *token = ">";
  3528.                     break;
  3529.                 case BTT_PUNC_LOGICALAND:
  3530.                     *token = "&&";
  3531.                     break;
  3532.                 case BTT_PUNC_AMPERSAND:
  3533.                     *token = "&";
  3534.                     break;
  3535.                 case BTT_PUNC_MINUSMINUS:
  3536.                     *token = "--";
  3537.                     break;
  3538.                 case BTT_PUNC_HASH:
  3539.                     *token = "#";
  3540.                     break;
  3541.                 case BTT_PUNC_LESSOREQUAL:
  3542.                     *token = "<=";
  3543.                     break;
  3544.                 case BTT_PUNC_GREATEROREQUAL:
  3545.                     *token = ">=";
  3546.                     break;
  3547.                 case BTT_PUNC_FORWARDSLASH:
  3548.                     *token = "/";
  3549.                     break;
  3550.                 case BTT_PUNC_SHIFTLEFT:
  3551.                     *token = "<<";
  3552.                     break;
  3553.                 case BTT_PUNC_SHIFTRIGHT:
  3554.                     *token = ">>";
  3555.                     break;
  3556.                 case BTT_PUNC_LEFTPAREN:
  3557.                     *token = "(";
  3558.                     break;
  3559.                 case BTT_PUNC_SEMICOLON:
  3560.                     *token = ";";
  3561.                     break;
  3562.                 case BTT_PUNC_ASTERISK:
  3563.                     *token = "*";
  3564.                     break;
  3565.                 case BTT_PUNC_PERIOD:
  3566.                     *token = ".";
  3567.                     break;
  3568.                 case BTT_PUNC_DOLLARSIGN:
  3569.                     *token = "$";
  3570.                     break;
  3571.                 case BTT_PUNC_PLUSEQUAL:
  3572.                     *token = "+=";
  3573.                     break;
  3574.                 case BTT_PUNC_MINUSEQUAL:
  3575.                     *token = "-=";
  3576.                     break;
  3577.                 case BTT_PUNC_TILDE:
  3578.                     *token = "~";
  3579.                     break;
  3580.                 case BTT_PUNC_EXCLAMATION:
  3581.                     *token = "!";
  3582.                     break;
  3583.                 case BTT_PUNC_PIPE:
  3584.                     *token = "|";
  3585.                     break;
  3586.                 case BTT_PUNC_BACKSLASH:
  3587.                     *token = "\\";
  3588.                     break;
  3589.                 case BTT_PUNC_DOUBLEHASH:
  3590.                     *token = "##";
  3591.                     break;
  3592.                 case BTT_PUNC_TIMESEQUAL:
  3593.                     *token = "*=";
  3594.                     break;
  3595.                 case BTT_PUNC_DOUBLEPIPE:
  3596.                     *token = "||";
  3597.                     break;
  3598.                 case BTT_PUNC_INVERTEDPLING:
  3599.                     *token = "í";
  3600.                     break;
  3601.                 case BTT_PUNC_INVERTEDQUERY:
  3602.                     *token = "┐";
  3603.                     break;
  3604.                 default:
  3605.                     assert(false);        // unrecognized punctuation
  3606.                 }
  3607.                 if(encoded)
  3608.                     unreadSize = 0;        // punctuation encoded in prefix
  3609.                 break;
  3610.             case TT_NUMBER:
  3611.                 {
  3612.                     // number of bytes actually in the stream used to represent this value
  3613.                     unsigned int size = BTT_GET_STORED_SIZE(prefix);
  3614.                     const int buffersize = 100;
  3615.                     char buffer[buffersize];    // big enough buffer for the int to string conversion routines
  3616.  
  3617.                     switch(BTT_GET_SUBTYPE(prefix))
  3618.                     {
  3619.                         case BTT_SUBTYPE_INT:
  3620.                             switch(size)
  3621.                             {
  3622.                             case BTT_STORED_1BYTE:
  3623.                                 token->intvalue = ReadValue<char>(OBJ);
  3624.                                 unreadSize = sizeof(char);
  3625.                                 break;
  3626.                             case BTT_STORED_2BYTE:
  3627.                                 token->intvalue = ReadValue<short>(OBJ);
  3628.                                 unreadSize = sizeof(short);
  3629.                                 break;
  3630.                             case BTT_STORED_4BYTE:
  3631.                                 token->intvalue = ReadValue<int>(OBJ);
  3632.                                 unreadSize = sizeof(int);
  3633.                                 break;
  3634.                             default:
  3635.                                 // invalid stored size for an integer
  3636.                                 assert(false);
  3637.                             }
  3638.  
  3639.                             token->floatvalue = token->intvalue;
  3640.                             // I hate the fact that I have to copy into the string, but there's no way
  3641.                             // of easily knowing whether it is used later or not
  3642.  
  3643.                             // This conversion to a string assumes that long and int are the same
  3644.                             assert(sizeof(long) == sizeof(int));
  3645.  
  3646.                             //ltoa(token->intvalue, buffer, 10);
  3647.                             idStr::snPrintf( buffer, buffersize, "%ld", token->intvalue );
  3648.                             assert(token->intvalue == atol(buffer));
  3649.                             *token = buffer;
  3650.                             token->subtype = TT_INTEGER | TT_DECIMAL | TT_VALUESVALID;
  3651.                         break;
  3652.                         
  3653.                         case BTT_SUBTYPE_UNSIGNEDINT:
  3654.                             switch(size)
  3655.                             {
  3656.                             case BTT_STORED_1BYTE:
  3657.                                 token->intvalue = ReadValue<unsigned char>(OBJ);
  3658.                                 unreadSize = sizeof(unsigned char);
  3659.                                 break;
  3660.                             case BTT_STORED_2BYTE:
  3661.                                 token->intvalue = ReadValue<unsigned short>(OBJ);
  3662.                                 unreadSize = sizeof(unsigned short);
  3663.                                 break;
  3664.                             case BTT_STORED_4BYTE:
  3665.                                 token->intvalue = ReadValue<unsigned int>(OBJ);
  3666.                                 unreadSize = sizeof(unsigned int);
  3667.                                 break;
  3668.                             default:
  3669.                                 // invalid stored size for an unsigned integer
  3670.                                 assert(false);
  3671.                             }
  3672.                             token->floatvalue = token->intvalue;
  3673.  
  3674.                             // I hate the fact that I have to copy into the string, but there's no way
  3675.                             // of easily knowing whether it is used later or not
  3676.  
  3677.                             // This conversion to a string assumes that long and int are the same
  3678.                             assert(sizeof(long) == sizeof(int));
  3679.  
  3680.                             //ultoa(token->intvalue, buffer, 10);
  3681.                             idStr::snPrintf( buffer, buffersize, "%lu", token->intvalue );
  3682.                             assert(token->intvalue == ((unsigned long)atol(buffer)));
  3683.                             *token = buffer;
  3684.                             token->subtype = TT_INTEGER | TT_UNSIGNED | TT_DECIMAL | TT_VALUESVALID;
  3685.                             break;
  3686.  
  3687.                         case BTT_SUBTYPE_FLOAT:
  3688.                             // invalid stored size for a float
  3689.                             assert(sizeof(float) == 4);
  3690.  
  3691.                             switch(size)
  3692.                             {
  3693.                             case BTT_STORED_4BYTE:
  3694.                                 token->floatvalue = ReadValue<float>(OBJ);
  3695.                                 token->intvalue = token->floatvalue;
  3696.                                 unreadSize = sizeof(float);
  3697.                                 break;
  3698.                             case BTT_STORED_2BYTE:        // requested a float, but it was integral, so it was saved that way
  3699.                                 token->floatvalue = ReadValue<short>(OBJ);
  3700.                                 token->intvalue = token->floatvalue;
  3701.                                 unreadSize = sizeof(short);
  3702.                                 break;
  3703.                             case BTT_STORED_1BYTE:        // requested a float, but it was integral, so it was saved that way
  3704.                                 token->floatvalue = ReadValue<char>(OBJ);
  3705.                                 token->intvalue = token->floatvalue;
  3706.                                 unreadSize = sizeof(char);
  3707.                                 break;
  3708.                             default:
  3709.                                 assert(false);
  3710.                             }
  3711.  
  3712.                             // I hate the fact that I have to copy into the string, but there's no way
  3713.                             // of easily knowing whether it is used later or not
  3714.                             idStr::snPrintf( buffer, buffersize, "%f", token->floatvalue );
  3715.                             *token = buffer;
  3716.                             token->subtype = TT_FLOAT | TT_DECIMAL | TT_SINGLE_PRECISION | TT_VALUESVALID;
  3717.  
  3718.                             unreadSize = sizeof(float);
  3719.                             break;
  3720.  
  3721.                         case BTT_SUBTYPE_DOUBLE:
  3722.                             // invalid stored size for a double
  3723.                             assert(sizeof(double) == 8);
  3724.  
  3725.                             // doubles are stored as floats since the original text file never uses full double precision anyway
  3726.                             //assert(size == BTT_STORED_4BYTE);
  3727.  
  3728.                             switch(size)
  3729.                             {
  3730.                             case BTT_STORED_4BYTE:
  3731.                                 token->floatvalue = ReadValue<float>(OBJ);
  3732.                                 token->intvalue = token->floatvalue;
  3733.                                 unreadSize = sizeof(float);
  3734.                                 break;
  3735.                             case BTT_STORED_2BYTE:        // requested a float, but it was integral, so it was saved that way
  3736.                                 token->floatvalue = ReadValue<short>(OBJ);
  3737.                                 token->intvalue = token->floatvalue;
  3738.                                 unreadSize = sizeof(short);
  3739.                                 break;
  3740.                             case BTT_STORED_1BYTE:        // requested a float, but it was integral, so it was saved that way
  3741.                                 token->floatvalue = ReadValue<char>(OBJ);
  3742.                                 token->intvalue = token->floatvalue;
  3743.                                 unreadSize = sizeof(char);
  3744.                                 break;
  3745.                             default:
  3746.                                 assert(false);
  3747.                             }
  3748.  
  3749.                             // I hate the fact that I have to copy into the string, but there's no way
  3750.                             // of easily knowing whether it is used later or not
  3751.                             idStr::snPrintf( buffer, buffersize, "%f", token->floatvalue );
  3752.                             *token = buffer;
  3753.                             token->subtype = TT_FLOAT | TT_DECIMAL | TT_DOUBLE_PRECISION | TT_VALUESVALID;
  3754.                             break;
  3755.                     }
  3756.                 }
  3757.                 break;
  3758.  
  3759.             default:
  3760.                 // unsupported binary type
  3761.                 assert(false);
  3762.             }
  3763.         }
  3764.  
  3765.         //common->Warning("Read Token: '%s    (int: %d)(float: %f)'\n", token->c_str(), token->GetIntValue(), token->GetFloatValue());
  3766.         //common->Printf("Read Token: %s\n", token->c_str());
  3767.         offset += unreadSize+1;        // the +1 is for the prefix
  3768.         return 1;
  3769.     }
  3770. }
  3771.  
  3772. int Lexer::IsLoaded()
  3773. {
  3774.     if(mDelegate)
  3775.     {
  3776.         return mDelegate->IsLoaded();
  3777.     }
  3778.     else
  3779.     {
  3780. #ifdef LEXER_READ_AHEAD
  3781.         return mLexerIOWrapper.IsLoaded();
  3782. #else
  3783.         return mFile != NULL;
  3784. #endif
  3785.     }
  3786. }
  3787.  
  3788. void Lexer::FreeSource()
  3789. {
  3790. #ifdef LEXER_READ_AHEAD
  3791.     mLexerIOWrapper.Close();
  3792. #else
  3793.     if(mFile)
  3794.     {
  3795.         idLib::fileSystem->CloseFile(mFile);
  3796.         mFile = NULL;
  3797.     }
  3798. #endif
  3799.  
  3800.     if(mDelegate)
  3801.     {
  3802.         delete mDelegate;
  3803.         mDelegate = NULL;
  3804.     }
  3805. }
  3806.  
  3807. int Lexer::LoadMemory(char const *ptr, int length, char const *name, int startLine)
  3808. {
  3809.     if(mDelegate)
  3810.     {
  3811.         return mDelegate->LoadMemory(ptr, length, name, startLine);
  3812.     }
  3813.     else
  3814.     {
  3815.         // this is essentially a no op
  3816.         return true;
  3817.     }
  3818. }
  3819.  
  3820. int Lexer::LoadFile(char const *filename, bool OSPath)
  3821. {
  3822.     if(mDelegate)
  3823.     {
  3824.         return mDelegate->LoadFile(filename, OSPath);
  3825.     }
  3826.     else
  3827.     {
  3828.         idStr fullName;
  3829.         FreeSource();
  3830.  
  3831.         fullName = filename;
  3832.         fullName.Append(sCompiledFileSuffix);
  3833.         bool binaryFound = OpenFile(fullName, OSPath);
  3834.         if(binaryFound)
  3835.         {
  3836.             return binaryFound;
  3837.         }
  3838.         else
  3839.         {
  3840.             mDelegate = new idLexer(filename, mFlags, OSPath);
  3841.             int isLoaded = mDelegate->IsLoaded();
  3842.             if(isLoaded)
  3843.             {
  3844.                 // don't do this right now until I clean up the Lexer class
  3845.                 if(mFlags & LEXFL_READBINARY)
  3846.                 {
  3847.                     Warning("%s not found, loading ascii version", fullName.c_str());
  3848.                 }
  3849.             }
  3850.             else
  3851.             {
  3852.                 // didn't find the ascii version either, make sure and clean up in case this lexer is reused
  3853.                 delete mDelegate;
  3854.                 mDelegate = NULL;
  3855.             }
  3856.  
  3857.             return isLoaded;
  3858.         }
  3859.     }
  3860. }
  3861.  
  3862. void Lexer::WriteBinaryToken(idToken *tok)
  3863. {
  3864.     if(mDelegate)
  3865.     {
  3866.         mDelegate->WriteBinaryToken(tok);
  3867.     }
  3868.     else
  3869.     {
  3870.         // can't write out an already binary file
  3871.         assert(false);
  3872.     }
  3873. }
  3874.  
  3875. bool Lexer::OpenFile(char const *filename, bool OSPath)
  3876. {
  3877.     idStr pathname;
  3878.     
  3879.     if ( !OSPath && ( baseFolder[0] != '\0' ) ) {
  3880.         pathname = va( "%s/%s", baseFolder, filename );
  3881.     } else {
  3882.         pathname = filename;
  3883.     }
  3884.     
  3885. #ifdef LEXER_READ_AHEAD
  3886.     if ( !mLexerIOWrapper.OpenFile( pathname, OSPath ) ) {
  3887.         return false;
  3888.     }
  3889. #else
  3890.     if ( OSPath ) {
  3891.         mFile = idLib::fileSystem->OpenExplicitFileRead( pathname );
  3892.     } else {
  3893.         mFile = idLib::fileSystem->OpenFileRead( pathname );
  3894.     }
  3895.     if ( !mFile ) {
  3896.         return false;
  3897.     }
  3898. #endif
  3899.  
  3900.     return true;
  3901. }
  3902.  
  3903. void Lexer::SetBaseFolder( const char *path ) {
  3904.     idStr::Copynz( baseFolder, path, sizeof( baseFolder ) );
  3905. }
  3906.  
  3907.  
  3908. // RAVEN BEGIN
  3909. // dluetscher: added method to parse a structure array that is made up of numerics (floats, ints), and stores them in the given storage
  3910. void Lexer::ParseNumericStructArray( int numStructElements, int tokenSubTypeStructElements[], int arrayCount, byte *arrayStorage )
  3911. {
  3912.     if(mDelegate)
  3913.     {
  3914.         mDelegate->ParseNumericStructArray( numStructElements, tokenSubTypeStructElements, arrayCount, arrayStorage );
  3915.     }
  3916.     else
  3917.     {
  3918.         int arrayOffset, curElement;
  3919.  
  3920.         for ( arrayOffset = 0; arrayOffset < arrayCount; arrayOffset++ )
  3921.         {
  3922.             for ( curElement = 0; curElement < numStructElements; curElement++ )
  3923.             {
  3924.                 if ( tokenSubTypeStructElements[curElement] & TT_FLOAT )
  3925.                 {
  3926.                     *(float*)arrayStorage = Lexer::ParseFloat();
  3927.                     arrayStorage += sizeof(float);
  3928.                 }
  3929.                 else
  3930.                 {
  3931.                     *(int*)arrayStorage = Lexer::ParseInt();
  3932.                     arrayStorage += sizeof(int);
  3933.                 }
  3934.             }
  3935.         }
  3936.     }
  3937. }
  3938. // RAVEN END
  3939.  
  3940.  
  3941. char idLexer::baseFolder[256];
  3942. // RAVEN END
  3943.