2012-11-26 18:58:24 +00:00
|
|
|
/*
|
|
|
|
===========================================================================
|
|
|
|
|
|
|
|
Doom 3 BFG Edition GPL Source Code
|
2012-11-28 15:47:07 +00:00
|
|
|
Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company.
|
2012-11-26 18:58:24 +00:00
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").
|
2012-11-26 18:58:24 +00:00
|
|
|
|
|
|
|
Doom 3 BFG Edition Source Code is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Doom 3 BFG Edition Source Code is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with Doom 3 BFG Edition Source Code. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
In addition, the Doom 3 BFG Edition Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 BFG Edition Source Code. If not, please request a copy in writing from id Software at the address below.
|
|
|
|
|
|
|
|
If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
|
|
|
|
|
|
|
|
===========================================================================
|
|
|
|
*/
|
|
|
|
|
2012-12-22 15:18:19 +00:00
|
|
|
#include "precompiled.h"
|
2012-11-26 18:58:24 +00:00
|
|
|
#pragma hdrstop
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
void idTokenParser::LoadFromParser( idParser& parser, const char* guiName )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
idToken tok;
|
|
|
|
idTokenIndexes tokIdxs;
|
|
|
|
tokIdxs.SetName( guiName );
|
2012-11-28 15:47:07 +00:00
|
|
|
while( parser.ReadToken( &tok ) )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
tokIdxs.Append( tokens.AddUnique( idBinaryToken( tok ) ) );
|
|
|
|
}
|
|
|
|
guiTokenIndexes.Append( tokIdxs );
|
|
|
|
currentToken = 0;
|
|
|
|
}
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
void idTokenParser::LoadFromFile( const char* filename )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
Clear();
|
2012-11-28 15:47:07 +00:00
|
|
|
idFile* inFile = fileSystem->OpenFileReadMemory( filename );
|
|
|
|
if( inFile != NULL )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
int num;
|
|
|
|
inFile->ReadBig( num );
|
|
|
|
guiTokenIndexes.SetNum( num );
|
2012-11-28 15:47:07 +00:00
|
|
|
for( int i = 0; i < num; i++ )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
guiTokenIndexes[ i ].Read( inFile );
|
|
|
|
}
|
|
|
|
inFile->ReadBig( num );
|
|
|
|
tokens.SetNum( num );
|
2012-11-28 15:47:07 +00:00
|
|
|
for( int i = 0; i < num; i++ )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
tokens[ i ].Read( inFile );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
delete inFile;
|
|
|
|
preloaded = ( tokens.Num() > 0 );
|
|
|
|
}
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
void idTokenParser::WriteToFile( const char* filename )
|
|
|
|
{
|
|
|
|
if( preloaded )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
idFile* outFile = fileSystem->OpenFileWrite( filename, "fs_basepath" );
|
|
|
|
if( outFile != NULL )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
outFile->WriteBig( ( int )guiTokenIndexes.Num() );
|
2012-11-28 15:47:07 +00:00
|
|
|
for( int i = 0; i < guiTokenIndexes.Num(); i++ )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
guiTokenIndexes[ i ].Write( outFile );
|
|
|
|
}
|
|
|
|
outFile->WriteBig( ( int )tokens.Num() );
|
2012-11-28 15:47:07 +00:00
|
|
|
for( int i = 0; i < tokens.Num(); i++ )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
tokens[ i ].Write( outFile );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
delete outFile;
|
|
|
|
}
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
bool idTokenParser::StartParsing( const char* filename )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
currentTokenList = -1;
|
2012-11-28 15:47:07 +00:00
|
|
|
for( int i = 0; i < guiTokenIndexes.Num(); i++ )
|
|
|
|
{
|
|
|
|
if( idStr::Icmp( filename, guiTokenIndexes[ i ].GetName() ) == 0 )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
currentTokenList = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
currentToken = 0;
|
|
|
|
return ( currentTokenList != -1 );
|
|
|
|
}
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
bool idTokenParser::ReadToken( idToken* tok )
|
|
|
|
{
|
|
|
|
if( currentToken >= 0 && currentToken < guiTokenIndexes[ currentTokenList ].Num() )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
tok->Clear();
|
2012-11-28 15:47:07 +00:00
|
|
|
idBinaryToken& btok = tokens[ guiTokenIndexes[ currentTokenList ][ currentToken ] ];
|
2012-11-26 18:58:24 +00:00
|
|
|
*tok = btok.token;
|
|
|
|
tok->type = btok.tokenType;
|
|
|
|
tok->subtype = btok.tokenSubType;
|
|
|
|
currentToken++;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
int idTokenParser::ExpectTokenString( const char* string )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
idToken token;
|
2012-11-28 15:47:07 +00:00
|
|
|
if( !ReadToken( &token ) )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
Error( "couldn't find expected '%s'", string );
|
|
|
|
return 0;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
if( token != string )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
Error( "expected '%s' but found '%s'", string, token.c_str() );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
// expect a certain token type
|
2012-11-28 15:47:07 +00:00
|
|
|
int idTokenParser::ExpectTokenType( int type, int subtype, idToken* token )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
idStr str;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( !ReadToken( token ) )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
Error( "couldn't read expected token" );
|
|
|
|
return 0;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( token->type != type )
|
|
|
|
{
|
|
|
|
switch( type )
|
|
|
|
{
|
|
|
|
case TT_STRING:
|
|
|
|
str = "string";
|
|
|
|
break;
|
|
|
|
case TT_LITERAL:
|
|
|
|
str = "literal";
|
|
|
|
break;
|
|
|
|
case TT_NUMBER:
|
|
|
|
str = "number";
|
|
|
|
break;
|
|
|
|
case TT_NAME:
|
|
|
|
str = "name";
|
|
|
|
break;
|
|
|
|
case TT_PUNCTUATION:
|
|
|
|
str = "punctuation";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
str = "unknown type";
|
|
|
|
break;
|
2012-11-26 18:58:24 +00:00
|
|
|
}
|
|
|
|
Error( "expected a %s but found '%s'", str.c_str(), token->c_str() );
|
|
|
|
return 0;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
if( token->type == TT_NUMBER )
|
|
|
|
{
|
|
|
|
if( ( token->subtype & subtype ) != subtype )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
str.Clear();
|
2012-11-28 15:47:07 +00:00
|
|
|
if( subtype & TT_DECIMAL ) str = "decimal ";
|
|
|
|
if( subtype & TT_HEX ) str = "hex ";
|
|
|
|
if( subtype & TT_OCTAL ) str = "octal ";
|
|
|
|
if( subtype & TT_BINARY ) str = "binary ";
|
|
|
|
if( subtype & TT_UNSIGNED ) str += "unsigned ";
|
|
|
|
if( subtype & TT_LONG ) str += "long ";
|
|
|
|
if( subtype & TT_FLOAT ) str += "float ";
|
|
|
|
if( subtype & TT_INTEGER ) str += "integer ";
|
2012-11-26 18:58:24 +00:00
|
|
|
str.StripTrailing( ' ' );
|
|
|
|
Error( "expected %s but found '%s'", str.c_str(), token->c_str() );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
else if( token->type == TT_PUNCTUATION )
|
|
|
|
{
|
|
|
|
if( subtype < 0 )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
Error( "BUG: wrong punctuation subtype" );
|
|
|
|
return 0;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
if( token->subtype != subtype )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
//Error( "expected '%s' but found '%s'", idLexer::GetPunctuationFromId( subtype ), token->c_str() );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
// expect a token
|
2012-11-28 15:47:07 +00:00
|
|
|
int idTokenParser::ExpectAnyToken( idToken* token )
|
|
|
|
{
|
|
|
|
if( !ReadToken( token ) )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
Error( "couldn't read expected token" );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
void idTokenParser::UnreadToken( const idToken* token )
|
|
|
|
{
|
|
|
|
if( currentToken == 0 || currentToken >= guiTokenIndexes[ currentTokenList ].Num() )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
idLib::common->FatalError( "idTokenParser::unreadToken, unread token twice\n" );
|
|
|
|
}
|
|
|
|
currentToken--;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
void idTokenParser::Error( VERIFY_FORMAT_STRING const char* str, ... )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
char text[MAX_STRING_CHARS];
|
|
|
|
va_list ap;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
va_start( ap, str );
|
|
|
|
vsprintf( text, str, ap );
|
|
|
|
va_end( ap );
|
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
idLib::common->Warning( text );
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
void idTokenParser::Warning( VERIFY_FORMAT_STRING const char* str, ... )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
char text[MAX_STRING_CHARS];
|
|
|
|
va_list ap;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
va_start( ap, str );
|
|
|
|
vsprintf( text, str, ap );
|
|
|
|
va_end( ap );
|
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
idLib::common->Warning( text );
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
int idTokenParser::ParseInt()
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
idToken token;
|
2012-11-28 15:47:07 +00:00
|
|
|
if( !ReadToken( &token ) )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
Error( "couldn't read expected integer" );
|
|
|
|
return 0;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
if( token.type == TT_PUNCTUATION && token == "-" )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
ExpectTokenType( TT_NUMBER, TT_INTEGER, &token );
|
2012-11-28 15:47:07 +00:00
|
|
|
return -( ( signed int ) token.GetIntValue() );
|
2012-11-26 18:58:24 +00:00
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
else if( token.type != TT_NUMBER || token.subtype == TT_FLOAT )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
Error( "expected integer value, found '%s'", token.c_str() );
|
|
|
|
}
|
|
|
|
return token.GetIntValue();
|
|
|
|
}
|
|
|
|
// read a boolean
|
2012-11-28 15:47:07 +00:00
|
|
|
bool idTokenParser::ParseBool()
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
idToken token;
|
2012-11-28 15:47:07 +00:00
|
|
|
if( !ExpectTokenType( TT_NUMBER, 0, &token ) )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
Error( "couldn't read expected boolean" );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return ( token.GetIntValue() != 0 );
|
|
|
|
}
|
|
|
|
// read a floating point number. If errorFlag is NULL, a non-numeric token will
|
|
|
|
// issue an Error(). If it isn't NULL, it will issue a Warning() and set *errorFlag = true
|
2012-11-28 15:47:07 +00:00
|
|
|
float idTokenParser::ParseFloat( bool* errorFlag )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
idToken token;
|
2012-11-28 15:47:07 +00:00
|
|
|
if( errorFlag )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
*errorFlag = false;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
if( !ReadToken( &token ) )
|
|
|
|
{
|
|
|
|
if( errorFlag )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
Warning( "couldn't read expected floating point number" );
|
|
|
|
*errorFlag = true;
|
2012-11-28 15:47:07 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
Error( "couldn't read expected floating point number" );
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
if( token.type == TT_PUNCTUATION && token == "-" )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
ExpectTokenType( TT_NUMBER, 0, &token );
|
|
|
|
return -token.GetFloatValue();
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
else if( token.type != TT_NUMBER )
|
|
|
|
{
|
|
|
|
if( errorFlag )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
Warning( "expected float value, found '%s'", token.c_str() );
|
|
|
|
*errorFlag = true;
|
2012-11-28 15:47:07 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
Error( "expected float value, found '%s'", token.c_str() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return token.GetFloatValue();
|
|
|
|
}
|