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.
|
|
|
|
|
|
|
|
===========================================================================
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "precompiled.h"
|
|
|
|
#pragma hdrstop
|
|
|
|
|
|
|
|
#define PUNCTABLE
|
|
|
|
|
|
|
|
//longer punctuations first
|
2012-11-28 15:47:07 +00:00
|
|
|
punctuation_t default_punctuations[] =
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
//binary operators
|
2012-11-28 15:47:07 +00:00
|
|
|
{">>=", P_RSHIFT_ASSIGN},
|
|
|
|
{"<<=", P_LSHIFT_ASSIGN},
|
2012-11-26 18:58:24 +00:00
|
|
|
//
|
2012-11-28 15:47:07 +00:00
|
|
|
{"...", P_PARMS},
|
2012-11-26 18:58:24 +00:00
|
|
|
//define merge operator
|
2012-11-28 15:47:07 +00:00
|
|
|
{"##", P_PRECOMPMERGE}, // pre-compiler
|
2012-11-26 18:58:24 +00:00
|
|
|
//logic operators
|
2012-11-28 15:47:07 +00:00
|
|
|
{"&&", P_LOGIC_AND}, // pre-compiler
|
|
|
|
{"||", P_LOGIC_OR}, // pre-compiler
|
|
|
|
{">=", P_LOGIC_GEQ}, // pre-compiler
|
|
|
|
{"<=", P_LOGIC_LEQ}, // pre-compiler
|
|
|
|
{"==", P_LOGIC_EQ}, // pre-compiler
|
|
|
|
{"!=", P_LOGIC_UNEQ}, // pre-compiler
|
2012-11-26 18:58:24 +00:00
|
|
|
//arithmatic operators
|
2012-11-28 15:47:07 +00:00
|
|
|
{"*=", P_MUL_ASSIGN},
|
|
|
|
{"/=", P_DIV_ASSIGN},
|
|
|
|
{"%=", P_MOD_ASSIGN},
|
|
|
|
{"+=", P_ADD_ASSIGN},
|
|
|
|
{"-=", P_SUB_ASSIGN},
|
|
|
|
{"++", P_INC},
|
|
|
|
{"--", P_DEC},
|
2012-11-26 18:58:24 +00:00
|
|
|
//binary operators
|
2012-11-28 15:47:07 +00:00
|
|
|
{"&=", P_BIN_AND_ASSIGN},
|
|
|
|
{"|=", P_BIN_OR_ASSIGN},
|
|
|
|
{"^=", P_BIN_XOR_ASSIGN},
|
|
|
|
{">>", P_RSHIFT}, // pre-compiler
|
|
|
|
{"<<", P_LSHIFT}, // pre-compiler
|
2012-11-26 18:58:24 +00:00
|
|
|
//reference operators
|
2012-11-28 15:47:07 +00:00
|
|
|
{"->", P_POINTERREF},
|
2012-11-26 18:58:24 +00:00
|
|
|
//C++
|
2012-11-28 15:47:07 +00:00
|
|
|
{"::", P_CPP1},
|
|
|
|
{".*", P_CPP2},
|
2012-11-26 18:58:24 +00:00
|
|
|
//arithmatic operators
|
2012-11-28 15:47:07 +00:00
|
|
|
{"*", P_MUL}, // pre-compiler
|
|
|
|
{"/", P_DIV}, // pre-compiler
|
|
|
|
{"%", P_MOD}, // pre-compiler
|
|
|
|
{"+", P_ADD}, // pre-compiler
|
|
|
|
{"-", P_SUB}, // pre-compiler
|
|
|
|
{"=", P_ASSIGN},
|
2012-11-26 18:58:24 +00:00
|
|
|
//binary operators
|
2012-11-28 15:47:07 +00:00
|
|
|
{"&", P_BIN_AND}, // pre-compiler
|
|
|
|
{"|", P_BIN_OR}, // pre-compiler
|
|
|
|
{"^", P_BIN_XOR}, // pre-compiler
|
|
|
|
{"~", P_BIN_NOT}, // pre-compiler
|
2012-11-26 18:58:24 +00:00
|
|
|
//logic operators
|
2012-11-28 15:47:07 +00:00
|
|
|
{"!", P_LOGIC_NOT}, // pre-compiler
|
|
|
|
{">", P_LOGIC_GREATER}, // pre-compiler
|
|
|
|
{"<", P_LOGIC_LESS}, // pre-compiler
|
2012-11-26 18:58:24 +00:00
|
|
|
//reference operator
|
2012-11-28 15:47:07 +00:00
|
|
|
{".", P_REF},
|
2012-11-26 18:58:24 +00:00
|
|
|
//seperators
|
2012-11-28 15:47:07 +00:00
|
|
|
{",", P_COMMA}, // pre-compiler
|
|
|
|
{";", P_SEMICOLON},
|
2012-11-26 18:58:24 +00:00
|
|
|
//label indication
|
2012-11-28 15:47:07 +00:00
|
|
|
{":", P_COLON}, // pre-compiler
|
2012-11-26 18:58:24 +00:00
|
|
|
//if statement
|
2012-11-28 15:47:07 +00:00
|
|
|
{"?", P_QUESTIONMARK}, // pre-compiler
|
2012-11-26 18:58:24 +00:00
|
|
|
//embracements
|
2012-11-28 15:47:07 +00:00
|
|
|
{"(", P_PARENTHESESOPEN}, // pre-compiler
|
|
|
|
{")", P_PARENTHESESCLOSE}, // pre-compiler
|
|
|
|
{"{", P_BRACEOPEN}, // pre-compiler
|
|
|
|
{"}", P_BRACECLOSE}, // pre-compiler
|
|
|
|
{"[", P_SQBRACKETOPEN},
|
|
|
|
{"]", P_SQBRACKETCLOSE},
|
2012-11-26 18:58:24 +00:00
|
|
|
//
|
2012-11-28 15:47:07 +00:00
|
|
|
{"\\", P_BACKSLASH},
|
2012-11-26 18:58:24 +00:00
|
|
|
//precompiler operator
|
2012-11-28 15:47:07 +00:00
|
|
|
{"#", P_PRECOMP}, // pre-compiler
|
|
|
|
{"$", P_DOLLAR},
|
2012-11-26 18:58:24 +00:00
|
|
|
{NULL, 0}
|
|
|
|
};
|
|
|
|
|
|
|
|
int default_punctuationtable[256];
|
2012-11-28 15:47:07 +00:00
|
|
|
int default_nextpunctuation[sizeof( default_punctuations ) / sizeof( punctuation_t )];
|
2012-11-26 18:58:24 +00:00
|
|
|
int default_setup;
|
|
|
|
|
|
|
|
char idLexer::baseFolder[ 256 ];
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
idLexer::CreatePunctuationTable
|
|
|
|
================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
void idLexer::CreatePunctuationTable( const punctuation_t* punctuations )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
int i, n, lastp;
|
2012-11-28 15:47:07 +00:00
|
|
|
const punctuation_t* p, *newp;
|
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
//get memory for the table
|
2012-11-28 15:47:07 +00:00
|
|
|
if( punctuations == default_punctuations )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
idLexer::punctuationtable = default_punctuationtable;
|
|
|
|
idLexer::nextpunctuation = default_nextpunctuation;
|
2012-11-28 15:47:07 +00:00
|
|
|
if( default_setup )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
default_setup = true;
|
2012-11-28 15:47:07 +00:00
|
|
|
i = sizeof( default_punctuations ) / sizeof( punctuation_t );
|
2012-11-26 18:58:24 +00:00
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if( !idLexer::punctuationtable || idLexer::punctuationtable == default_punctuationtable )
|
|
|
|
{
|
|
|
|
idLexer::punctuationtable = ( int* ) Mem_Alloc( 256 * sizeof( int ), TAG_IDLIB_LEXER );
|
2012-11-26 18:58:24 +00:00
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
if( idLexer::nextpunctuation && idLexer::nextpunctuation != default_nextpunctuation )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
Mem_Free( idLexer::nextpunctuation );
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
for( i = 0; punctuations[i].p; i++ )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
idLexer::nextpunctuation = ( int* ) Mem_Alloc( i * sizeof( int ), TAG_IDLIB_LEXER );
|
2012-11-26 18:58:24 +00:00
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
memset( idLexer::punctuationtable, 0xFF, 256 * sizeof( int ) );
|
|
|
|
memset( idLexer::nextpunctuation, 0xFF, i * sizeof( int ) );
|
2012-11-26 18:58:24 +00:00
|
|
|
//add the punctuations in the list to the punctuation table
|
2012-11-28 15:47:07 +00:00
|
|
|
for( i = 0; punctuations[i].p; i++ )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
newp = &punctuations[i];
|
|
|
|
lastp = -1;
|
|
|
|
//sort the punctuations in this table entry on length (longer punctuations first)
|
2012-11-28 15:47:07 +00:00
|
|
|
for( n = idLexer::punctuationtable[( unsigned int ) newp->p[0]]; n >= 0; n = idLexer::nextpunctuation[n] )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
p = &punctuations[n];
|
2012-11-28 15:47:07 +00:00
|
|
|
if( strlen( p->p ) < strlen( newp->p ) )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
idLexer::nextpunctuation[i] = n;
|
2012-11-28 15:47:07 +00:00
|
|
|
if( lastp >= 0 )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
idLexer::nextpunctuation[lastp] = i;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
idLexer::punctuationtable[( unsigned int ) newp->p[0]] = i;
|
2012-11-26 18:58:24 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
lastp = n;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
if( n < 0 )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
idLexer::nextpunctuation[i] = -1;
|
2012-11-28 15:47:07 +00:00
|
|
|
if( lastp >= 0 )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
idLexer::nextpunctuation[lastp] = i;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
idLexer::punctuationtable[( unsigned int ) newp->p[0]] = i;
|
2012-11-26 18:58:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
idLexer::GetPunctuationFromId
|
|
|
|
================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
const char* idLexer::GetPunctuationFromId( int id )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
int i;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
for( i = 0; idLexer::punctuations[i].p; i++ )
|
|
|
|
{
|
|
|
|
if( idLexer::punctuations[i].n == id )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return idLexer::punctuations[i].p;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "unkown punctuation";
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
idLexer::GetPunctuationId
|
|
|
|
================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
int idLexer::GetPunctuationId( const char* p )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
int i;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
for( i = 0; idLexer::punctuations[i].p; i++ )
|
|
|
|
{
|
|
|
|
if( !strcmp( idLexer::punctuations[i].p, p ) )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return idLexer::punctuations[i].n;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
idLexer::Error
|
|
|
|
================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
void idLexer::Error( 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
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
hadError = true;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( idLexer::flags & LEXFL_NOERRORS )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
va_start( ap, str );
|
|
|
|
vsprintf( text, str, ap );
|
|
|
|
va_end( ap );
|
|
|
|
|
|
|
|
if( idLexer::flags & LEXFL_NOFATALERRORS )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
idLib::common->Warning( "file %s, line %d: %s", idLexer::filename.c_str(), idLexer::line, text );
|
2012-11-28 15:47:07 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
idLib::common->Error( "file %s, line %d: %s", idLexer::filename.c_str(), idLexer::line, text );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
idLexer::Warning
|
|
|
|
================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
void idLexer::Warning( 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
|
|
|
|
|
|
|
if( idLexer::flags & LEXFL_NOWARNINGS )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
va_start( ap, str );
|
|
|
|
vsprintf( text, str, ap );
|
|
|
|
va_end( ap );
|
|
|
|
idLib::common->Warning( "file %s, line %d: %s", idLexer::filename.c_str(), idLexer::line, text );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
idLexer::SetPunctuations
|
|
|
|
================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
void idLexer::SetPunctuations( const punctuation_t* p )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
#ifdef PUNCTABLE
|
2012-11-28 15:47:07 +00:00
|
|
|
if( p )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
idLexer::CreatePunctuationTable( p );
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
else
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
idLexer::CreatePunctuationTable( default_punctuations );
|
|
|
|
}
|
|
|
|
#endif //PUNCTABLE
|
2012-11-28 15:47:07 +00:00
|
|
|
if( p )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
idLexer::punctuations = p;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
else
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
idLexer::punctuations = default_punctuations;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
idLexer::ReadWhiteSpace
|
|
|
|
|
|
|
|
Reads spaces, tabs, C-like comments etc.
|
|
|
|
When a newline character is found the scripts line counter is increased.
|
|
|
|
================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
int idLexer::ReadWhiteSpace()
|
|
|
|
{
|
|
|
|
while( 1 )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
// skip white space
|
2012-11-28 15:47:07 +00:00
|
|
|
while( *idLexer::script_p <= ' ' )
|
|
|
|
{
|
|
|
|
if( !*idLexer::script_p )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
if( *idLexer::script_p == '\n' )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
idLexer::line++;
|
|
|
|
}
|
|
|
|
idLexer::script_p++;
|
|
|
|
}
|
|
|
|
// skip comments
|
2012-11-28 15:47:07 +00:00
|
|
|
if( *idLexer::script_p == '/' )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
// comments //
|
2012-11-28 15:47:07 +00:00
|
|
|
if( *( idLexer::script_p + 1 ) == '/' )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
idLexer::script_p++;
|
2012-11-28 15:47:07 +00:00
|
|
|
do
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
idLexer::script_p++;
|
2012-11-28 15:47:07 +00:00
|
|
|
if( !*idLexer::script_p )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while( *idLexer::script_p != '\n' );
|
|
|
|
idLexer::line++;
|
|
|
|
idLexer::script_p++;
|
2012-11-28 15:47:07 +00:00
|
|
|
if( !*idLexer::script_p )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// comments /* */
|
2012-11-28 15:47:07 +00:00
|
|
|
else if( *( idLexer::script_p + 1 ) == '*' )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
idLexer::script_p++;
|
2012-11-28 15:47:07 +00:00
|
|
|
while( 1 )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
idLexer::script_p++;
|
2012-11-28 15:47:07 +00:00
|
|
|
if( !*idLexer::script_p )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
if( *idLexer::script_p == '\n' )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
idLexer::line++;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
else if( *idLexer::script_p == '/' )
|
|
|
|
{
|
|
|
|
if( *( idLexer::script_p - 1 ) == '*' )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
break;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
if( *( idLexer::script_p + 1 ) == '*' )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
idLexer::Warning( "nested comment" );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
idLexer::script_p++;
|
2012-11-28 15:47:07 +00:00
|
|
|
if( !*idLexer::script_p )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
idLexer::script_p++;
|
2012-11-28 15:47:07 +00:00
|
|
|
if( !*idLexer::script_p )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
========================
|
|
|
|
idLexer::SkipWhiteSpace
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
Reads spaces, tabs, C-like comments etc. When a newline character is found, the scripts line
|
2012-11-26 18:58:24 +00:00
|
|
|
counter is increased. Returns false if there is no token left to be read.
|
|
|
|
========================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
bool idLexer::SkipWhiteSpace( bool currentLine )
|
|
|
|
{
|
|
|
|
while( 1 )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
assert( script_p <= end_p );
|
2012-11-28 15:47:07 +00:00
|
|
|
if( script_p == end_p )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// skip white space
|
2012-11-28 15:47:07 +00:00
|
|
|
while( *script_p <= ' ' )
|
|
|
|
{
|
|
|
|
if( script_p == end_p )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return false;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
if( !*script_p )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return false;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
if( *script_p == '\n' )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
line++;
|
2012-11-28 15:47:07 +00:00
|
|
|
if( currentLine )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
script_p++;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
script_p++;
|
|
|
|
}
|
|
|
|
// skip comments
|
2012-11-28 15:47:07 +00:00
|
|
|
if( *script_p == '/' )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
// comments //
|
2012-11-28 15:47:07 +00:00
|
|
|
if( *( script_p + 1 ) == '/' )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
script_p++;
|
2012-11-28 15:47:07 +00:00
|
|
|
do
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
script_p++;
|
2012-11-28 15:47:07 +00:00
|
|
|
if( !*script_p )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while( *script_p != '\n' );
|
|
|
|
line++;
|
|
|
|
script_p++;
|
2012-11-28 15:47:07 +00:00
|
|
|
if( currentLine )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return true;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
if( !*script_p )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// comments /* */
|
2012-11-28 15:47:07 +00:00
|
|
|
else if( *( script_p + 1 ) == '*' )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
script_p++;
|
2012-11-28 15:47:07 +00:00
|
|
|
while( 1 )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
script_p++;
|
2012-11-28 15:47:07 +00:00
|
|
|
if( !*script_p )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return false;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
if( *script_p == '\n' )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
line++;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
else if( *script_p == '/' )
|
|
|
|
{
|
|
|
|
if( *( script_p - 1 ) == '*' )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
break;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
if( *( script_p + 1 ) == '*' )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
Warning( "nested comment" );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
script_p++;
|
2012-11-28 15:47:07 +00:00
|
|
|
if( !*script_p )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
idLexer::ReadEscapeCharacter
|
|
|
|
================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
int idLexer::ReadEscapeCharacter( char* ch )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
int c, val, i;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
// step over the leading '\\'
|
|
|
|
idLexer::script_p++;
|
|
|
|
// determine the escape character
|
2012-11-28 15:47:07 +00:00
|
|
|
switch( *idLexer::script_p )
|
|
|
|
{
|
|
|
|
case '\\':
|
|
|
|
c = '\\';
|
|
|
|
break;
|
|
|
|
case 'n':
|
|
|
|
c = '\n';
|
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
c = '\r';
|
|
|
|
break;
|
|
|
|
case 't':
|
|
|
|
c = '\t';
|
|
|
|
break;
|
|
|
|
case 'v':
|
|
|
|
c = '\v';
|
|
|
|
break;
|
|
|
|
case 'b':
|
|
|
|
c = '\b';
|
|
|
|
break;
|
|
|
|
case 'f':
|
|
|
|
c = '\f';
|
|
|
|
break;
|
|
|
|
case 'a':
|
|
|
|
c = '\a';
|
|
|
|
break;
|
|
|
|
case '\'':
|
|
|
|
c = '\'';
|
|
|
|
break;
|
|
|
|
case '\"':
|
|
|
|
c = '\"';
|
|
|
|
break;
|
|
|
|
case '\?':
|
|
|
|
c = '\?';
|
|
|
|
break;
|
2012-11-26 18:58:24 +00:00
|
|
|
case 'x':
|
|
|
|
{
|
|
|
|
idLexer::script_p++;
|
2012-11-28 15:47:07 +00:00
|
|
|
for( i = 0, val = 0; ; i++, idLexer::script_p++ )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
c = *idLexer::script_p;
|
2012-11-28 15:47:07 +00:00
|
|
|
if( c >= '0' && c <= '9' )
|
2012-11-26 18:58:24 +00:00
|
|
|
c = c - '0';
|
2012-11-28 15:47:07 +00:00
|
|
|
else if( c >= 'A' && c <= 'Z' )
|
2012-11-26 18:58:24 +00:00
|
|
|
c = c - 'A' + 10;
|
2012-11-28 15:47:07 +00:00
|
|
|
else if( c >= 'a' && c <= 'z' )
|
2012-11-26 18:58:24 +00:00
|
|
|
c = c - 'a' + 10;
|
|
|
|
else
|
|
|
|
break;
|
2012-11-28 15:47:07 +00:00
|
|
|
val = ( val << 4 ) + c;
|
2012-11-26 18:58:24 +00:00
|
|
|
}
|
|
|
|
idLexer::script_p--;
|
2012-11-28 15:47:07 +00:00
|
|
|
if( val > 0xFF )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
idLexer::Warning( "too large value in escape character" );
|
|
|
|
val = 0xFF;
|
|
|
|
}
|
|
|
|
c = val;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: //NOTE: decimal ASCII code, NOT octal
|
|
|
|
{
|
2012-11-28 15:47:07 +00:00
|
|
|
if( *idLexer::script_p < '0' || *idLexer::script_p > '9' )
|
|
|
|
{
|
|
|
|
idLexer::Error( "unknown escape char" );
|
2012-11-26 18:58:24 +00:00
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
for( i = 0, val = 0; ; i++, idLexer::script_p++ )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
c = *idLexer::script_p;
|
2012-11-28 15:47:07 +00:00
|
|
|
if( c >= '0' && c <= '9' )
|
2012-11-26 18:58:24 +00:00
|
|
|
c = c - '0';
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
val = val * 10 + c;
|
|
|
|
}
|
|
|
|
idLexer::script_p--;
|
2012-11-28 15:47:07 +00:00
|
|
|
if( val > 0xFF )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
idLexer::Warning( "too large value in escape character" );
|
|
|
|
val = 0xFF;
|
|
|
|
}
|
|
|
|
c = val;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// step over the escape character or the last digit of the number
|
|
|
|
idLexer::script_p++;
|
|
|
|
// store the escape character
|
|
|
|
*ch = c;
|
|
|
|
// succesfully read escape character
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
idLexer::ReadString
|
|
|
|
|
|
|
|
Escape characters are interpretted.
|
|
|
|
Reads two strings with only a white space between them as one string.
|
|
|
|
================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
int idLexer::ReadString( idToken* token, int quote )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
int tmpline;
|
2012-11-28 15:47:07 +00:00
|
|
|
const char* tmpscript_p;
|
2012-11-26 18:58:24 +00:00
|
|
|
char ch;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( quote == '\"' )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
token->type = TT_STRING;
|
2012-11-28 15:47:07 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
token->type = TT_LITERAL;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
// leading quote
|
|
|
|
idLexer::script_p++;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
while( 1 )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
// if there is an escape character and escape characters are allowed
|
2012-11-28 15:47:07 +00:00
|
|
|
if( *idLexer::script_p == '\\' && !( idLexer::flags & LEXFL_NOSTRINGESCAPECHARS ) )
|
|
|
|
{
|
|
|
|
if( !idLexer::ReadEscapeCharacter( &ch ) )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
token->AppendDirty( ch );
|
|
|
|
}
|
|
|
|
// if a trailing quote
|
2012-11-28 15:47:07 +00:00
|
|
|
else if( *idLexer::script_p == quote )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
// step over the quote
|
|
|
|
idLexer::script_p++;
|
|
|
|
// if consecutive strings should not be concatenated
|
2012-11-28 15:47:07 +00:00
|
|
|
if( ( idLexer::flags & LEXFL_NOSTRINGCONCAT ) &&
|
|
|
|
( !( idLexer::flags & LEXFL_ALLOWBACKSLASHSTRINGCONCAT ) || ( quote != '\"' ) ) )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
break;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
tmpscript_p = idLexer::script_p;
|
|
|
|
tmpline = idLexer::line;
|
|
|
|
// read white space between possible two consecutive strings
|
2012-11-28 15:47:07 +00:00
|
|
|
if( !idLexer::ReadWhiteSpace() )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
idLexer::script_p = tmpscript_p;
|
|
|
|
idLexer::line = tmpline;
|
|
|
|
break;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( idLexer::flags & LEXFL_NOSTRINGCONCAT )
|
|
|
|
{
|
|
|
|
if( *idLexer::script_p != '\\' )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
idLexer::script_p = tmpscript_p;
|
|
|
|
idLexer::line = tmpline;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// step over the '\\'
|
|
|
|
idLexer::script_p++;
|
2012-11-28 15:47:07 +00:00
|
|
|
if( !idLexer::ReadWhiteSpace() || ( *idLexer::script_p != quote ) )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
idLexer::Error( "expecting string after '\' terminated line" );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
// if there's no leading qoute
|
2012-11-28 15:47:07 +00:00
|
|
|
if( *idLexer::script_p != quote )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
idLexer::script_p = tmpscript_p;
|
|
|
|
idLexer::line = tmpline;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// step over the new leading quote
|
|
|
|
idLexer::script_p++;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if( *idLexer::script_p == '\0' )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
idLexer::Error( "missing trailing quote" );
|
|
|
|
return 0;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
if( *idLexer::script_p == '\n' )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
idLexer::Error( "newline inside string" );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
token->AppendDirty( *idLexer::script_p++ );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
token->data[token->len] = '\0';
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( token->type == TT_LITERAL )
|
|
|
|
{
|
|
|
|
if( !( idLexer::flags & LEXFL_ALLOWMULTICHARLITERALS ) )
|
|
|
|
{
|
|
|
|
if( token->Length() != 1 )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
idLexer::Warning( "literal is not one character long" );
|
|
|
|
}
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
token->subtype = ( *token )[0];
|
2012-11-26 18:58:24 +00:00
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
else
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
// the sub type is the length of the string
|
|
|
|
token->subtype = token->Length();
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
idLexer::ReadName
|
|
|
|
================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
int idLexer::ReadName( idToken* token )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
char c;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
token->type = TT_NAME;
|
2012-11-28 15:47:07 +00:00
|
|
|
do
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
token->AppendDirty( *idLexer::script_p++ );
|
|
|
|
c = *idLexer::script_p;
|
2012-11-28 15:47:07 +00:00
|
|
|
}
|
|
|
|
while( ( c >= 'a' && c <= 'z' ) ||
|
|
|
|
( c >= 'A' && c <= 'Z' ) ||
|
|
|
|
( c >= '0' && c <= '9' ) ||
|
|
|
|
c == '_' ||
|
|
|
|
// if treating all tokens as strings, don't parse '-' as a seperate token
|
|
|
|
( ( idLexer::flags & LEXFL_ONLYSTRINGS ) && ( c == '-' ) ) ||
|
|
|
|
// if special path name characters are allowed
|
|
|
|
( ( idLexer::flags & LEXFL_ALLOWPATHNAMES ) && ( c == '/' || c == '\\' || c == ':' || c == '.' ) ) );
|
2012-11-26 18:58:24 +00:00
|
|
|
token->data[token->len] = '\0';
|
|
|
|
//the sub type is the length of the name
|
|
|
|
token->subtype = token->Length();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
idLexer::CheckString
|
|
|
|
================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
ID_INLINE int idLexer::CheckString( const char* str ) const
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
int i;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
for( i = 0; str[i]; i++ )
|
|
|
|
{
|
|
|
|
if( idLexer::script_p[i] != str[i] )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
idLexer::ReadNumber
|
|
|
|
================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
int idLexer::ReadNumber( idToken* token )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
int i;
|
|
|
|
int dot;
|
|
|
|
char c, c2;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
token->type = TT_NUMBER;
|
|
|
|
token->subtype = 0;
|
|
|
|
token->intvalue = 0;
|
|
|
|
token->floatvalue = 0;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
c = *idLexer::script_p;
|
2012-11-28 15:47:07 +00:00
|
|
|
c2 = *( idLexer::script_p + 1 );
|
|
|
|
|
|
|
|
if( c == '0' && c2 != '.' )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
// check for a hexadecimal number
|
2012-11-28 15:47:07 +00:00
|
|
|
if( c2 == 'x' || c2 == 'X' )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
token->AppendDirty( *idLexer::script_p++ );
|
|
|
|
token->AppendDirty( *idLexer::script_p++ );
|
|
|
|
c = *idLexer::script_p;
|
2012-11-28 15:47:07 +00:00
|
|
|
while( ( c >= '0' && c <= '9' ) ||
|
|
|
|
( c >= 'a' && c <= 'f' ) ||
|
|
|
|
( c >= 'A' && c <= 'F' ) )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
token->AppendDirty( c );
|
2012-11-28 15:47:07 +00:00
|
|
|
c = *( ++idLexer::script_p );
|
2012-11-26 18:58:24 +00:00
|
|
|
}
|
|
|
|
token->subtype = TT_HEX | TT_INTEGER;
|
|
|
|
}
|
|
|
|
// check for a binary number
|
2012-11-28 15:47:07 +00:00
|
|
|
else if( c2 == 'b' || c2 == 'B' )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
token->AppendDirty( *idLexer::script_p++ );
|
|
|
|
token->AppendDirty( *idLexer::script_p++ );
|
|
|
|
c = *idLexer::script_p;
|
2012-11-28 15:47:07 +00:00
|
|
|
while( c == '0' || c == '1' )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
token->AppendDirty( c );
|
2012-11-28 15:47:07 +00:00
|
|
|
c = *( ++idLexer::script_p );
|
2012-11-26 18:58:24 +00:00
|
|
|
}
|
|
|
|
token->subtype = TT_BINARY | TT_INTEGER;
|
|
|
|
}
|
|
|
|
// its an octal number
|
2012-11-28 15:47:07 +00:00
|
|
|
else
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
token->AppendDirty( *idLexer::script_p++ );
|
|
|
|
c = *idLexer::script_p;
|
2012-11-28 15:47:07 +00:00
|
|
|
while( c >= '0' && c <= '7' )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
token->AppendDirty( c );
|
2012-11-28 15:47:07 +00:00
|
|
|
c = *( ++idLexer::script_p );
|
2012-11-26 18:58:24 +00:00
|
|
|
}
|
|
|
|
token->subtype = TT_OCTAL | TT_INTEGER;
|
|
|
|
}
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
else
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
// decimal integer or floating point number or ip address
|
|
|
|
dot = 0;
|
2012-11-28 15:47:07 +00:00
|
|
|
while( 1 )
|
|
|
|
{
|
|
|
|
if( c >= '0' && c <= '9' )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
else if( c == '.' )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
dot++;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
else
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
token->AppendDirty( c );
|
2012-11-28 15:47:07 +00:00
|
|
|
c = *( ++idLexer::script_p );
|
2012-11-26 18:58:24 +00:00
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
if( c == 'e' && dot == 0 )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
//We have scientific notation without a decimal point
|
|
|
|
dot++;
|
|
|
|
}
|
|
|
|
// if a floating point number
|
2012-11-28 15:47:07 +00:00
|
|
|
if( dot == 1 )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
token->subtype = TT_DECIMAL | TT_FLOAT;
|
|
|
|
// check for floating point exponent
|
2012-11-28 15:47:07 +00:00
|
|
|
if( c == 'e' )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
//Append the e so that GetFloatValue code works
|
|
|
|
token->AppendDirty( c );
|
2012-11-28 15:47:07 +00:00
|
|
|
c = *( ++idLexer::script_p );
|
|
|
|
if( c == '-' )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
token->AppendDirty( c );
|
2012-11-28 15:47:07 +00:00
|
|
|
c = *( ++idLexer::script_p );
|
2012-11-26 18:58:24 +00:00
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
else if( c == '+' )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
token->AppendDirty( c );
|
2012-11-28 15:47:07 +00:00
|
|
|
c = *( ++idLexer::script_p );
|
2012-11-26 18:58:24 +00:00
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
while( c >= '0' && c <= '9' )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
token->AppendDirty( c );
|
2012-11-28 15:47:07 +00:00
|
|
|
c = *( ++idLexer::script_p );
|
2012-11-26 18:58:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// check for floating point exception infinite 1.#INF or indefinite 1.#IND or NaN
|
2012-11-28 15:47:07 +00:00
|
|
|
else if( c == '#' )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
c2 = 4;
|
2012-11-28 15:47:07 +00:00
|
|
|
if( CheckString( "INF" ) )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
token->subtype |= TT_INFINITE;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
else if( CheckString( "IND" ) )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
token->subtype |= TT_INDEFINITE;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
else if( CheckString( "NAN" ) )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
token->subtype |= TT_NAN;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
else if( CheckString( "QNAN" ) )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
token->subtype |= TT_NAN;
|
|
|
|
c2++;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
else if( CheckString( "SNAN" ) )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
token->subtype |= TT_NAN;
|
|
|
|
c2++;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
for( i = 0; i < c2; i++ )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
token->AppendDirty( c );
|
2012-11-28 15:47:07 +00:00
|
|
|
c = *( ++idLexer::script_p );
|
2012-11-26 18:58:24 +00:00
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
while( c >= '0' && c <= '9' )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
token->AppendDirty( c );
|
2012-11-28 15:47:07 +00:00
|
|
|
c = *( ++idLexer::script_p );
|
2012-11-26 18:58:24 +00:00
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
if( !( idLexer::flags & LEXFL_ALLOWFLOATEXCEPTIONS ) )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
token->AppendDirty( 0 ); // zero terminate for c_str
|
|
|
|
idLexer::Error( "parsed %s", token->c_str() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
else if( dot > 1 )
|
|
|
|
{
|
|
|
|
if( !( idLexer::flags & LEXFL_ALLOWIPADDRESSES ) )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
idLexer::Error( "more than one dot in number" );
|
|
|
|
return 0;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
if( dot != 3 )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
idLexer::Error( "ip address should have three dots" );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
token->subtype = TT_IPADDRESS;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
else
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
token->subtype = TT_DECIMAL | TT_INTEGER;
|
|
|
|
}
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( token->subtype & TT_FLOAT )
|
|
|
|
{
|
|
|
|
if( c > ' ' )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
// single-precision: float
|
2012-11-28 15:47:07 +00:00
|
|
|
if( c == 'f' || c == 'F' )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
token->subtype |= TT_SINGLE_PRECISION;
|
|
|
|
idLexer::script_p++;
|
|
|
|
}
|
|
|
|
// extended-precision: long double
|
2012-11-28 15:47:07 +00:00
|
|
|
else if( c == 'l' || c == 'L' )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
token->subtype |= TT_EXTENDED_PRECISION;
|
|
|
|
idLexer::script_p++;
|
|
|
|
}
|
|
|
|
// default is double-precision: double
|
2012-11-28 15:47:07 +00:00
|
|
|
else
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
token->subtype |= TT_DOUBLE_PRECISION;
|
|
|
|
}
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
else
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
token->subtype |= TT_DOUBLE_PRECISION;
|
|
|
|
}
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
else if( token->subtype & TT_INTEGER )
|
|
|
|
{
|
|
|
|
if( c > ' ' )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
// default: signed long
|
2012-11-28 15:47:07 +00:00
|
|
|
for( i = 0; i < 2; i++ )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
// long integer
|
2012-11-28 15:47:07 +00:00
|
|
|
if( c == 'l' || c == 'L' )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
token->subtype |= TT_LONG;
|
|
|
|
}
|
|
|
|
// unsigned integer
|
2012-11-28 15:47:07 +00:00
|
|
|
else if( c == 'u' || c == 'U' )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
token->subtype |= TT_UNSIGNED;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
else
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
break;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
c = *( ++idLexer::script_p );
|
2012-11-26 18:58:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
else if( token->subtype & TT_IPADDRESS )
|
|
|
|
{
|
|
|
|
if( c == ':' )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
token->AppendDirty( c );
|
2012-11-28 15:47:07 +00:00
|
|
|
c = *( ++idLexer::script_p );
|
|
|
|
while( c >= '0' && c <= '9' )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
token->AppendDirty( c );
|
2012-11-28 15:47:07 +00:00
|
|
|
c = *( ++idLexer::script_p );
|
2012-11-26 18:58:24 +00:00
|
|
|
}
|
|
|
|
token->subtype |= TT_IPPORT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
token->data[token->len] = '\0';
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
idLexer::ReadPunctuation
|
|
|
|
================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
int idLexer::ReadPunctuation( idToken* token )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
int l, n, i;
|
2012-11-28 15:47:07 +00:00
|
|
|
char* p;
|
|
|
|
const punctuation_t* punc;
|
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
#ifdef PUNCTABLE
|
2012-11-28 15:47:07 +00:00
|
|
|
for( n = idLexer::punctuationtable[( unsigned int ) * ( idLexer::script_p )]; n >= 0; n = idLexer::nextpunctuation[n] )
|
2012-11-26 18:58:24 +00:00
|
|
|
{
|
2012-11-28 15:47:07 +00:00
|
|
|
punc = &( idLexer::punctuations[n] );
|
2012-11-26 18:58:24 +00:00
|
|
|
#else
|
|
|
|
int i;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
for( i = 0; idLexer::punctuations[i].p; i++ )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
punc = &idLexer::punctuations[i];
|
|
|
|
#endif
|
|
|
|
p = punc->p;
|
|
|
|
// check for this punctuation in the script
|
2012-11-28 15:47:07 +00:00
|
|
|
for( l = 0; p[l] && idLexer::script_p[l]; l++ )
|
|
|
|
{
|
|
|
|
if( idLexer::script_p[l] != p[l] )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
if( !p[l] )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
//
|
2012-11-28 15:47:07 +00:00
|
|
|
token->EnsureAlloced( l + 1, false );
|
|
|
|
for( i = 0; i <= l; i++ )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
token->data[i] = p[i];
|
|
|
|
}
|
|
|
|
token->len = l;
|
|
|
|
//
|
|
|
|
idLexer::script_p += l;
|
|
|
|
token->type = TT_PUNCTUATION;
|
|
|
|
// sub type is the punctuation id
|
|
|
|
token->subtype = punc->n;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
idLexer::ReadToken
|
|
|
|
================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
int idLexer::ReadToken( idToken* token )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
int c;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( !loaded )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
idLib::common->Error( "idLexer::ReadToken: no file loaded" );
|
|
|
|
return 0;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( script_p == NULL )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
// if there is a token available (from unreadToken)
|
2012-11-28 15:47:07 +00:00
|
|
|
if( tokenavailable )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
tokenavailable = 0;
|
|
|
|
*token = idLexer::token;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
// save script pointer
|
|
|
|
lastScript_p = script_p;
|
|
|
|
// save line counter
|
|
|
|
lastline = line;
|
|
|
|
// clear the token stuff
|
|
|
|
token->data[0] = '\0';
|
|
|
|
token->len = 0;
|
|
|
|
// start of the white space
|
|
|
|
whiteSpaceStart_p = script_p;
|
|
|
|
token->whiteSpaceStart_p = script_p;
|
|
|
|
// read white space before token
|
2012-11-28 15:47:07 +00:00
|
|
|
if( !ReadWhiteSpace() )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
// end of the white space
|
|
|
|
idLexer::whiteSpaceEnd_p = script_p;
|
|
|
|
token->whiteSpaceEnd_p = script_p;
|
|
|
|
// line the token is on
|
|
|
|
token->line = line;
|
|
|
|
// number of lines crossed before token
|
|
|
|
token->linesCrossed = line - lastline;
|
|
|
|
// clear token flags
|
|
|
|
token->flags = 0;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
c = *idLexer::script_p;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
// if we're keeping everything as whitespace deliminated strings
|
2012-11-28 15:47:07 +00:00
|
|
|
if( idLexer::flags & LEXFL_ONLYSTRINGS )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
// if there is a leading quote
|
2012-11-28 15:47:07 +00:00
|
|
|
if( c == '\"' || c == '\'' )
|
|
|
|
{
|
|
|
|
if( !idLexer::ReadString( token, c ) )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
}
|
|
|
|
else if( !idLexer::ReadName( token ) )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// if there is a number
|
2012-11-28 15:47:07 +00:00
|
|
|
else if( ( c >= '0' && c <= '9' ) ||
|
|
|
|
( c == '.' && ( *( idLexer::script_p + 1 ) >= '0' && *( idLexer::script_p + 1 ) <= '9' ) ) )
|
|
|
|
{
|
|
|
|
if( !idLexer::ReadNumber( token ) )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
// if names are allowed to start with a number
|
2012-11-28 15:47:07 +00:00
|
|
|
if( idLexer::flags & LEXFL_ALLOWNUMBERNAMES )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
c = *idLexer::script_p;
|
2012-11-28 15:47:07 +00:00
|
|
|
if( ( c >= 'a' && c <= 'z' ) || ( c >= 'A' && c <= 'Z' ) || c == '_' )
|
|
|
|
{
|
|
|
|
if( !idLexer::ReadName( token ) )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// if there is a leading quote
|
2012-11-28 15:47:07 +00:00
|
|
|
else if( c == '\"' || c == '\'' )
|
|
|
|
{
|
|
|
|
if( !idLexer::ReadString( token, c ) )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// if there is a name
|
2012-11-28 15:47:07 +00:00
|
|
|
else if( ( c >= 'a' && c <= 'z' ) || ( c >= 'A' && c <= 'Z' ) || c == '_' )
|
|
|
|
{
|
|
|
|
if( !idLexer::ReadName( token ) )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// names may also start with a slash when pathnames are allowed
|
2012-11-28 15:47:07 +00:00
|
|
|
else if( ( idLexer::flags & LEXFL_ALLOWPATHNAMES ) && ( ( c == '/' || c == '\\' ) || c == '.' ) )
|
|
|
|
{
|
|
|
|
if( !idLexer::ReadName( token ) )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// check for punctuations
|
2012-11-28 15:47:07 +00:00
|
|
|
else if( !idLexer::ReadPunctuation( token ) )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
idLexer::Error( "unknown punctuation %c", c );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
// succesfully read a token
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
idLexer::ExpectTokenString
|
|
|
|
================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
int idLexer::ExpectTokenString( const char* string )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
idToken token;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( !idLexer::ReadToken( &token ) )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
idLexer::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
|
|
|
idLexer::Error( "expected '%s' but found '%s'", string, token.c_str() );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
idLexer::ExpectTokenType
|
|
|
|
================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
int idLexer::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( !idLexer::ReadToken( token ) )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
idLexer::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
|
|
|
}
|
|
|
|
idLexer::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( ' ' );
|
|
|
|
idLexer::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
|
|
|
idLexer::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
|
|
|
idLexer::Error( "expected '%s' but found '%s'", GetPunctuationFromId( subtype ), token->c_str() );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
idLexer::ExpectAnyToken
|
|
|
|
================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
int idLexer::ExpectAnyToken( idToken* token )
|
|
|
|
{
|
|
|
|
if( !idLexer::ReadToken( token ) )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
idLexer::Error( "couldn't read expected token" );
|
|
|
|
return 0;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
else
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
idLexer::CheckTokenString
|
|
|
|
================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
int idLexer::CheckTokenString( const char* string )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
idToken tok;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( !ReadToken( &tok ) )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
// if the given string is available
|
2012-11-28 15:47:07 +00:00
|
|
|
if( tok == string )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
// unread token
|
|
|
|
script_p = lastScript_p;
|
|
|
|
line = lastline;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
idLexer::CheckTokenType
|
|
|
|
================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
int idLexer::CheckTokenType( int type, int subtype, idToken* token )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
idToken tok;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( !ReadToken( &tok ) )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
// if the type matches
|
2012-11-28 15:47:07 +00:00
|
|
|
if( tok.type == type && ( tok.subtype & subtype ) == subtype )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
*token = tok;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
// unread token
|
|
|
|
script_p = lastScript_p;
|
|
|
|
line = lastline;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
idLexer::PeekTokenString
|
|
|
|
================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
int idLexer::PeekTokenString( const char* string )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
idToken tok;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( !ReadToken( &tok ) )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
// unread token
|
|
|
|
script_p = lastScript_p;
|
|
|
|
line = lastline;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
// if the given string is available
|
2012-11-28 15:47:07 +00:00
|
|
|
if( tok == string )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
idLexer::PeekTokenType
|
|
|
|
================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
int idLexer::PeekTokenType( int type, int subtype, idToken* token )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
idToken tok;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( !ReadToken( &tok ) )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
// unread token
|
|
|
|
script_p = lastScript_p;
|
|
|
|
line = lastline;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
// if the type matches
|
2012-11-28 15:47:07 +00:00
|
|
|
if( tok.type == type && ( tok.subtype & subtype ) == subtype )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
*token = tok;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
idLexer::SkipUntilString
|
|
|
|
================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
int idLexer::SkipUntilString( const char* string )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
idToken token;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
while( idLexer::ReadToken( &token ) )
|
|
|
|
{
|
|
|
|
if( token == string )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
idLexer::SkipRestOfLine
|
|
|
|
================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
int idLexer::SkipRestOfLine()
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
idToken token;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
while( idLexer::ReadToken( &token ) )
|
|
|
|
{
|
|
|
|
if( token.linesCrossed )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
idLexer::script_p = lastScript_p;
|
|
|
|
idLexer::line = lastline;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
=================
|
|
|
|
idLexer::SkipBracedSection
|
|
|
|
|
|
|
|
Skips until a matching close brace is found.
|
|
|
|
Internal brace depths are properly skipped.
|
|
|
|
=================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
int idLexer::SkipBracedSection( bool parseFirstBrace )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
idToken token;
|
|
|
|
int depth;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
depth = parseFirstBrace ? 0 : 1;
|
2012-11-28 15:47:07 +00:00
|
|
|
do
|
|
|
|
{
|
|
|
|
if( !ReadToken( &token ) )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return false;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
if( token.type == TT_PUNCTUATION )
|
|
|
|
{
|
|
|
|
if( token == "{" )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
depth++;
|
2012-11-28 15:47:07 +00:00
|
|
|
}
|
|
|
|
else if( token == "}" )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
depth--;
|
|
|
|
}
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
}
|
|
|
|
while( depth );
|
2012-11-26 18:58:24 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
idLexer::UnreadToken
|
|
|
|
================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
void idLexer::UnreadToken( const idToken* token )
|
|
|
|
{
|
|
|
|
if( idLexer::tokenavailable )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
idLib::common->FatalError( "idLexer::unreadToken, unread token twice\n" );
|
|
|
|
}
|
|
|
|
idLexer::token = *token;
|
|
|
|
idLexer::tokenavailable = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
idLexer::ReadTokenOnLine
|
|
|
|
================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
int idLexer::ReadTokenOnLine( idToken* token )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
idToken tok;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( !idLexer::ReadToken( &tok ) )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
idLexer::script_p = lastScript_p;
|
|
|
|
idLexer::line = lastline;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// if no lines were crossed before this token
|
2012-11-28 15:47:07 +00:00
|
|
|
if( !tok.linesCrossed )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
*token = tok;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// restore our position
|
|
|
|
idLexer::script_p = lastScript_p;
|
|
|
|
idLexer::line = lastline;
|
|
|
|
token->Clear();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
idLexer::ReadRestOfLine
|
|
|
|
================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
const char* idLexer::ReadRestOfLine( idStr& out )
|
|
|
|
{
|
|
|
|
while( 1 )
|
|
|
|
{
|
|
|
|
|
|
|
|
if( *idLexer::script_p == '\n' )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
idLexer::line++;
|
|
|
|
break;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( !*idLexer::script_p )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
break;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( *idLexer::script_p <= ' ' )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
out += " ";
|
2012-11-28 15:47:07 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
out += *idLexer::script_p;
|
|
|
|
}
|
|
|
|
idLexer::script_p++;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
out.Strip( ' ' );
|
2012-11-26 18:58:24 +00:00
|
|
|
return out.c_str();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
idLexer::ParseInt
|
|
|
|
================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
int idLexer::ParseInt()
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
idToken token;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( !idLexer::ReadToken( &token ) )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
idLexer::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
|
|
|
idLexer::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
|
|
|
idLexer::Error( "expected integer value, found '%s'", token.c_str() );
|
|
|
|
}
|
|
|
|
return token.GetIntValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
idLexer::ParseBool
|
|
|
|
================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
bool idLexer::ParseBool()
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
idToken token;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( !idLexer::ExpectTokenType( TT_NUMBER, 0, &token ) )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
idLexer::Error( "couldn't read expected boolean" );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return ( token.GetIntValue() != 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
idLexer::ParseFloat
|
|
|
|
================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
float idLexer::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( !idLexer::ReadToken( &token ) )
|
|
|
|
{
|
|
|
|
if( errorFlag )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
idLexer::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
|
|
|
idLexer::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
|
|
|
idLexer::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
|
|
|
idLexer::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
|
|
|
idLexer::Error( "expected float value, found '%s'", token.c_str() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return token.GetFloatValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
idLexer::Parse1DMatrix
|
|
|
|
================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
int idLexer::Parse1DMatrix( int x, float* m )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
int i;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( !idLexer::ExpectTokenString( "(" ) )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return false;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
for( i = 0; i < x; i++ )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
m[i] = idLexer::ParseFloat();
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( !idLexer::ExpectTokenString( ")" ) )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
idLexer::Parse2DMatrix
|
|
|
|
================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
int idLexer::Parse2DMatrix( int y, int x, float* m )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
int i;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( !idLexer::ExpectTokenString( "(" ) )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return false;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
for( i = 0; i < y; i++ )
|
|
|
|
{
|
|
|
|
if( !idLexer::Parse1DMatrix( x, m + i * x ) )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( !idLexer::ExpectTokenString( ")" ) )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
idLexer::Parse3DMatrix
|
|
|
|
================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
int idLexer::Parse3DMatrix( int z, int y, int x, float* m )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
int i;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( !idLexer::ExpectTokenString( "(" ) )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return false;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
for( i = 0 ; i < z; i++ )
|
|
|
|
{
|
|
|
|
if( !idLexer::Parse2DMatrix( y, x, m + i * x * y ) )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( !idLexer::ExpectTokenString( ")" ) )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
=================
|
|
|
|
idParser::ParseBracedSection
|
|
|
|
|
|
|
|
The next token should be an open brace.
|
|
|
|
Parses until a matching close brace is found.
|
|
|
|
Maintains exact characters between braces.
|
|
|
|
|
|
|
|
FIXME: this should use ReadToken and replace the token white space with correct indents and newlines
|
|
|
|
=================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
const char* idLexer::ParseBracedSectionExact( idStr& out, int tabs )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
int depth;
|
|
|
|
bool doTabs;
|
|
|
|
bool skipWhite;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
out.Empty();
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( !idLexer::ExpectTokenString( "{" ) )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return out.c_str( );
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
out = "{";
|
2012-11-28 15:47:07 +00:00
|
|
|
depth = 1;
|
2012-11-26 18:58:24 +00:00
|
|
|
skipWhite = false;
|
|
|
|
doTabs = tabs >= 0;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
while( depth && *idLexer::script_p )
|
|
|
|
{
|
|
|
|
char c = *( idLexer::script_p++ );
|
|
|
|
|
|
|
|
switch( c )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
case '\t':
|
2012-11-28 15:47:07 +00:00
|
|
|
case ' ':
|
|
|
|
{
|
|
|
|
if( skipWhite )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
case '\n':
|
|
|
|
{
|
|
|
|
if( doTabs )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
skipWhite = true;
|
|
|
|
out += c;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
case '{':
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
depth++;
|
|
|
|
tabs++;
|
|
|
|
break;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
case '}':
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
depth--;
|
|
|
|
tabs--;
|
2012-11-28 15:47:07 +00:00
|
|
|
break;
|
2012-11-26 18:58:24 +00:00
|
|
|
}
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( skipWhite )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
int i = tabs;
|
2012-11-28 15:47:07 +00:00
|
|
|
if( c == '{' )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
i--;
|
|
|
|
}
|
|
|
|
skipWhite = false;
|
2012-11-28 15:47:07 +00:00
|
|
|
for( ; i > 0; i-- )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
out += '\t';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
out += c;
|
|
|
|
}
|
|
|
|
return out.c_str();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
=================
|
|
|
|
idLexer::ParseBracedSection
|
|
|
|
|
|
|
|
The next token should be an open brace.
|
|
|
|
Parses until a matching close brace is found.
|
|
|
|
Internal brace depths are properly skipped.
|
|
|
|
=================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
const char* idLexer::ParseBracedSection( idStr& out )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
idToken token;
|
|
|
|
int i, depth;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
out.Empty();
|
2012-11-28 15:47:07 +00:00
|
|
|
if( !idLexer::ExpectTokenString( "{" ) )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return out.c_str();
|
|
|
|
}
|
|
|
|
out = "{";
|
|
|
|
depth = 1;
|
2012-11-28 15:47:07 +00:00
|
|
|
do
|
|
|
|
{
|
|
|
|
if( !idLexer::ReadToken( &token ) )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
Error( "missing closing brace" );
|
|
|
|
return out.c_str();
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
// if the token is on a new line
|
2012-11-28 15:47:07 +00:00
|
|
|
for( i = 0; i < token.linesCrossed; i++ )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
out += "\r\n";
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( token.type == TT_PUNCTUATION )
|
|
|
|
{
|
|
|
|
if( token[0] == '{' )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
depth++;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
else if( token[0] == '}' )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
depth--;
|
|
|
|
}
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
if( token.type == TT_STRING )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
out += "\"" + token + "\"";
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
else
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
out += token;
|
|
|
|
}
|
|
|
|
out += " ";
|
2012-11-28 15:47:07 +00:00
|
|
|
}
|
|
|
|
while( depth );
|
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
return out.c_str();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
=================
|
|
|
|
idLexer::ParseRestOfLine
|
|
|
|
|
|
|
|
parse the rest of the line
|
|
|
|
=================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
const char* idLexer::ParseRestOfLine( idStr& out )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
idToken token;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
out.Empty();
|
2012-11-28 15:47:07 +00:00
|
|
|
while( idLexer::ReadToken( &token ) )
|
|
|
|
{
|
|
|
|
if( token.linesCrossed )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
idLexer::script_p = lastScript_p;
|
|
|
|
idLexer::line = lastline;
|
|
|
|
break;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
if( out.Length() )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
out += " ";
|
|
|
|
}
|
|
|
|
out += token;
|
|
|
|
}
|
|
|
|
return out.c_str();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
========================
|
|
|
|
idLexer::ParseCompleteLine
|
|
|
|
|
|
|
|
Returns a string up to the \n, but doesn't eat any whitespace at the beginning of the next line.
|
|
|
|
========================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
const char* idLexer::ParseCompleteLine( idStr& out )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
idToken token;
|
2012-11-28 15:47:07 +00:00
|
|
|
const char* start;
|
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
start = script_p;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
|
|
|
while( 1 )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
// end of buffer
|
2012-11-28 15:47:07 +00:00
|
|
|
if( *script_p == 0 )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
break;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
if( *script_p == '\n' )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
line++;
|
|
|
|
script_p++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
script_p++;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
out.Empty();
|
|
|
|
out.Append( start, script_p - start );
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
return out.c_str();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
idLexer::GetLastWhiteSpace
|
|
|
|
================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
int idLexer::GetLastWhiteSpace( idStr& whiteSpace ) const
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
whiteSpace.Clear();
|
2012-11-28 15:47:07 +00:00
|
|
|
for( const char* p = whiteSpaceStart_p; p < whiteSpaceEnd_p; p++ )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
whiteSpace.Append( *p );
|
|
|
|
}
|
|
|
|
return whiteSpace.Length();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
idLexer::GetLastWhiteSpaceStart
|
|
|
|
================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
int idLexer::GetLastWhiteSpaceStart() const
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return whiteSpaceStart_p - buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
idLexer::GetLastWhiteSpaceEnd
|
|
|
|
================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
int idLexer::GetLastWhiteSpaceEnd() const
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return whiteSpaceEnd_p - buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
idLexer::Reset
|
|
|
|
================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
void idLexer::Reset()
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
// pointer in script buffer
|
|
|
|
idLexer::script_p = idLexer::buffer;
|
|
|
|
// pointer in script buffer before reading token
|
|
|
|
idLexer::lastScript_p = idLexer::buffer;
|
|
|
|
// begin of white space
|
|
|
|
idLexer::whiteSpaceStart_p = NULL;
|
|
|
|
// end of white space
|
|
|
|
idLexer::whiteSpaceEnd_p = NULL;
|
|
|
|
// set if there's a token available in idLexer::token
|
|
|
|
idLexer::tokenavailable = 0;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
idLexer::line = 1;
|
|
|
|
idLexer::lastline = 1;
|
|
|
|
// clear the saved token
|
|
|
|
idLexer::token = "";
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
idLexer::EndOfFile
|
|
|
|
================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
bool idLexer::EndOfFile()
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return idLexer::script_p >= idLexer::end_p;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
idLexer::NumLinesCrossed
|
|
|
|
================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
int idLexer::NumLinesCrossed()
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return idLexer::line - idLexer::lastline;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
idLexer::LoadFile
|
|
|
|
================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
int idLexer::LoadFile( const char* filename, bool OSPath )
|
|
|
|
{
|
|
|
|
idFile* fp;
|
2012-11-26 18:58:24 +00:00
|
|
|
idStr pathname;
|
|
|
|
int length;
|
2012-11-28 15:47:07 +00:00
|
|
|
char* buf;
|
|
|
|
|
|
|
|
if( idLexer::loaded )
|
|
|
|
{
|
|
|
|
idLib::common->Error( "idLexer::LoadFile: another script already loaded" );
|
2012-11-26 18:58:24 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-11-28 15:47:07 +00:00
|
|
|
if( !OSPath && ( baseFolder[0] != '\0' ) )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
pathname = va( "%s/%s", baseFolder, filename );
|
2012-11-28 15:47:07 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
pathname = filename;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
if( OSPath )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
fp = idLib::fileSystem->OpenExplicitFileRead( pathname );
|
2012-11-28 15:47:07 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
fp = idLib::fileSystem->OpenFileRead( pathname );
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
if( !fp )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
length = fp->Length();
|
2012-11-28 15:47:07 +00:00
|
|
|
buf = ( char* ) Mem_Alloc( length + 1, TAG_IDLIB_LEXER );
|
2012-11-26 18:58:24 +00:00
|
|
|
buf[length] = '\0';
|
|
|
|
fp->Read( buf, length );
|
|
|
|
idLexer::fileTime = fp->Timestamp();
|
|
|
|
idLexer::filename = fp->GetFullPath();
|
|
|
|
idLib::fileSystem->CloseFile( fp );
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
idLexer::buffer = buf;
|
|
|
|
idLexer::length = length;
|
|
|
|
// pointer in script buffer
|
|
|
|
idLexer::script_p = idLexer::buffer;
|
|
|
|
// pointer in script buffer before reading token
|
|
|
|
idLexer::lastScript_p = idLexer::buffer;
|
|
|
|
// pointer to end of script buffer
|
2012-11-28 15:47:07 +00:00
|
|
|
idLexer::end_p = &( idLexer::buffer[length] );
|
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
idLexer::tokenavailable = 0;
|
|
|
|
idLexer::line = 1;
|
|
|
|
idLexer::lastline = 1;
|
|
|
|
idLexer::allocated = true;
|
|
|
|
idLexer::loaded = true;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
idLexer::LoadMemory
|
|
|
|
================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
int idLexer::LoadMemory( const char* ptr, int length, const char* name, int startLine )
|
|
|
|
{
|
|
|
|
if( idLexer::loaded )
|
|
|
|
{
|
|
|
|
idLib::common->Error( "idLexer::LoadMemory: another script already loaded" );
|
2012-11-26 18:58:24 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
idLexer::filename = name;
|
|
|
|
idLexer::buffer = ptr;
|
|
|
|
idLexer::fileTime = 0;
|
|
|
|
idLexer::length = length;
|
|
|
|
// pointer in script buffer
|
|
|
|
idLexer::script_p = idLexer::buffer;
|
|
|
|
// pointer in script buffer before reading token
|
|
|
|
idLexer::lastScript_p = idLexer::buffer;
|
|
|
|
// pointer to end of script buffer
|
2012-11-28 15:47:07 +00:00
|
|
|
idLexer::end_p = &( idLexer::buffer[length] );
|
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
idLexer::tokenavailable = 0;
|
|
|
|
idLexer::line = startLine;
|
|
|
|
idLexer::lastline = startLine;
|
|
|
|
idLexer::allocated = false;
|
|
|
|
idLexer::loaded = true;
|
2012-11-28 15:47:07 +00:00
|
|
|
|
2012-11-26 18:58:24 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
idLexer::FreeSource
|
|
|
|
================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
void idLexer::FreeSource()
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
#ifdef PUNCTABLE
|
2012-11-28 15:47:07 +00:00
|
|
|
if( idLexer::punctuationtable && idLexer::punctuationtable != default_punctuationtable )
|
|
|
|
{
|
|
|
|
Mem_Free( ( void* ) idLexer::punctuationtable );
|
2012-11-26 18:58:24 +00:00
|
|
|
idLexer::punctuationtable = NULL;
|
|
|
|
}
|
2012-11-28 15:47:07 +00:00
|
|
|
if( idLexer::nextpunctuation && idLexer::nextpunctuation != default_nextpunctuation )
|
|
|
|
{
|
|
|
|
Mem_Free( ( void* ) idLexer::nextpunctuation );
|
2012-11-26 18:58:24 +00:00
|
|
|
idLexer::nextpunctuation = NULL;
|
|
|
|
}
|
|
|
|
#endif //PUNCTABLE
|
2012-11-28 15:47:07 +00:00
|
|
|
if( idLexer::allocated )
|
|
|
|
{
|
|
|
|
Mem_Free( ( void* ) idLexer::buffer );
|
2012-11-26 18:58:24 +00:00
|
|
|
idLexer::buffer = NULL;
|
|
|
|
idLexer::allocated = false;
|
|
|
|
}
|
|
|
|
idLexer::tokenavailable = 0;
|
|
|
|
idLexer::token = "";
|
|
|
|
idLexer::loaded = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
idLexer::idLexer
|
|
|
|
================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
idLexer::idLexer()
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
idLexer::loaded = false;
|
|
|
|
idLexer::filename = "";
|
|
|
|
idLexer::flags = 0;
|
|
|
|
idLexer::SetPunctuations( NULL );
|
|
|
|
idLexer::allocated = false;
|
|
|
|
idLexer::fileTime = 0;
|
|
|
|
idLexer::length = 0;
|
|
|
|
idLexer::line = 0;
|
|
|
|
idLexer::lastline = 0;
|
|
|
|
idLexer::tokenavailable = 0;
|
|
|
|
idLexer::token = "";
|
|
|
|
idLexer::next = NULL;
|
|
|
|
idLexer::hadError = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
idLexer::idLexer
|
|
|
|
================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
idLexer::idLexer( int flags )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
idLexer::loaded = false;
|
|
|
|
idLexer::filename = "";
|
|
|
|
idLexer::flags = flags;
|
|
|
|
idLexer::SetPunctuations( NULL );
|
|
|
|
idLexer::allocated = false;
|
|
|
|
idLexer::fileTime = 0;
|
|
|
|
idLexer::length = 0;
|
|
|
|
idLexer::line = 0;
|
|
|
|
idLexer::lastline = 0;
|
|
|
|
idLexer::tokenavailable = 0;
|
|
|
|
idLexer::token = "";
|
|
|
|
idLexer::next = NULL;
|
|
|
|
idLexer::hadError = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
idLexer::idLexer
|
|
|
|
================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
idLexer::idLexer( const char* filename, int flags, bool OSPath )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
idLexer::loaded = false;
|
|
|
|
idLexer::flags = flags;
|
|
|
|
idLexer::SetPunctuations( NULL );
|
|
|
|
idLexer::allocated = false;
|
|
|
|
idLexer::token = "";
|
|
|
|
idLexer::next = NULL;
|
|
|
|
idLexer::hadError = false;
|
|
|
|
idLexer::LoadFile( filename, OSPath );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
idLexer::idLexer
|
|
|
|
================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
idLexer::idLexer( const char* ptr, int length, const char* name, int flags )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
idLexer::loaded = false;
|
|
|
|
idLexer::flags = flags;
|
|
|
|
idLexer::SetPunctuations( NULL );
|
|
|
|
idLexer::allocated = false;
|
|
|
|
idLexer::token = "";
|
|
|
|
idLexer::next = NULL;
|
|
|
|
idLexer::hadError = false;
|
|
|
|
idLexer::LoadMemory( ptr, length, name );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
idLexer::~idLexer
|
|
|
|
================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
idLexer::~idLexer()
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
idLexer::FreeSource();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
idLexer::SetBaseFolder
|
|
|
|
================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
void idLexer::SetBaseFolder( const char* path )
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
idStr::Copynz( baseFolder, path, sizeof( baseFolder ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
idLexer::HadError
|
|
|
|
================
|
|
|
|
*/
|
2012-11-28 15:47:07 +00:00
|
|
|
bool idLexer::HadError() const
|
|
|
|
{
|
2012-11-26 18:58:24 +00:00
|
|
|
return hadError;
|
|
|
|
}
|
|
|
|
|