2007-11-04 03:51:54 +00:00
|
|
|
#include "StdAfx.h"
|
|
|
|
#include "ScriptParser.h"
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
CScriptParser::CScriptParser( void ) :
|
|
|
|
m_pScript( NULL ),
|
|
|
|
m_pScriptSection( NULL ),
|
|
|
|
m_pLastScriptSection( NULL ),
|
|
|
|
m_pToken( NULL ) {
|
2007-11-04 03:51:54 +00:00
|
|
|
ClearBuffer();
|
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
CScriptParser::~CScriptParser( void ) {
|
2007-11-04 03:51:54 +00:00
|
|
|
ClearBuffer();
|
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
void CScriptParser::ClearBuffer( void ) {
|
|
|
|
if ( m_pScript ) {
|
2007-11-04 03:51:54 +00:00
|
|
|
delete[] m_pScript;
|
|
|
|
m_pScript = NULL;
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( m_pToken ) {
|
2007-11-04 03:51:54 +00:00
|
|
|
delete[] m_pToken;
|
|
|
|
m_pToken = NULL;
|
|
|
|
}
|
|
|
|
m_pScriptSection = NULL;
|
|
|
|
m_pLastScriptSection = NULL;
|
2012-03-17 20:01:54 +00:00
|
|
|
memset( m_breakChars, 0, sizeof( m_breakChars ) );
|
2007-11-04 03:51:54 +00:00
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
const char* CScriptParser::MakeToken( const char* pToken ) {
|
|
|
|
if ( m_pToken ) {
|
2007-11-04 03:51:54 +00:00
|
|
|
delete[] m_pToken;
|
|
|
|
m_pToken = NULL;
|
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( !pToken ) {
|
2007-11-04 03:51:54 +00:00
|
|
|
pToken = "";
|
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
int len = static_cast<int>( strlen( pToken ) );
|
2007-11-04 03:51:54 +00:00
|
|
|
|
|
|
|
m_pToken = new char[len + 1];
|
|
|
|
m_pToken[len] = '\0';
|
2012-03-17 20:01:54 +00:00
|
|
|
strcpy( m_pToken, pToken );
|
2007-11-04 03:51:54 +00:00
|
|
|
|
|
|
|
return m_pToken;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define MAX_TOKEN_STRING 1024
|
|
|
|
// Should NEVER return NULL
|
2012-03-17 20:01:54 +00:00
|
|
|
const char* CScriptParser::GetToken( bool bAllowLinebreaks ) {
|
2007-11-04 03:51:54 +00:00
|
|
|
int c = 0, len;
|
|
|
|
char token[MAX_TOKEN_STRING];
|
|
|
|
bool bNewLines = false;
|
|
|
|
|
|
|
|
m_pLastScriptSection = m_pScriptSection;
|
|
|
|
|
|
|
|
len = 0;
|
|
|
|
*token = '\0';
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( !m_pScript || !m_pScriptSection ) {
|
|
|
|
return MakeToken( token );
|
2007-11-04 03:51:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
while ( true ) {
|
|
|
|
SkipWhitespace( &bNewLines );
|
|
|
|
if ( !*m_pScriptSection ) {
|
2012-03-17 20:01:54 +00:00
|
|
|
return MakeToken( token );
|
2007-11-04 03:51:54 +00:00
|
|
|
}
|
|
|
|
if ( bNewLines && !bAllowLinebreaks ) {
|
2012-03-17 20:01:54 +00:00
|
|
|
return MakeToken( token );
|
2007-11-04 03:51:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
c = *m_pScriptSection;
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:51:54 +00:00
|
|
|
if ( c == '/' && m_pScriptSection[1] == '/' ) { // C style comments
|
|
|
|
m_pScriptSection += 2;
|
2012-03-17 20:01:54 +00:00
|
|
|
while ( *m_pScriptSection && *m_pScriptSection != '\n' ) {
|
2007-11-04 03:51:54 +00:00
|
|
|
m_pScriptSection++;
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
else if ( c == '/' && m_pScriptSection[1] == '*' ) { // C++ style comments
|
2007-11-04 03:51:54 +00:00
|
|
|
m_pScriptSection += 2;
|
|
|
|
while ( *m_pScriptSection && ( *m_pScriptSection != '*' || m_pScriptSection[1] != '/' ) ) {
|
|
|
|
m_pScriptSection++;
|
|
|
|
}
|
|
|
|
if ( *m_pScriptSection ) {
|
|
|
|
m_pScriptSection += 2;
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
else {
|
2007-11-04 03:51:54 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( c == '\"' ) {
|
2007-11-04 03:51:54 +00:00
|
|
|
m_pScriptSection++;
|
|
|
|
while ( true ) {
|
|
|
|
c = *m_pScriptSection++;
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( c == '\"' || !c ) {
|
2007-11-04 03:51:54 +00:00
|
|
|
token[len] = 0;
|
2012-03-17 20:01:54 +00:00
|
|
|
return MakeToken( token );
|
2007-11-04 03:51:54 +00:00
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( len < MAX_TOKEN_STRING ) {
|
2007-11-04 03:51:54 +00:00
|
|
|
token[len] = c;
|
|
|
|
len++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( len > 0 && IsBreakChar( *m_pScriptSection ) ) {
|
2007-11-04 03:51:54 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( len < MAX_TOKEN_STRING ) {
|
2007-11-04 03:51:54 +00:00
|
|
|
token[len] = c;
|
|
|
|
len++;
|
|
|
|
}
|
|
|
|
m_pScriptSection++;
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( IsBreakChar( c ) ) {
|
2007-11-04 03:51:54 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
c = *m_pScriptSection;
|
2012-03-17 20:01:54 +00:00
|
|
|
} while ( c > 32 );
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( len == MAX_TOKEN_STRING ) {
|
2007-11-04 03:51:54 +00:00
|
|
|
len = 0;
|
|
|
|
}
|
|
|
|
token[len] = 0;
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
return MakeToken( token );
|
2007-11-04 03:51:54 +00:00
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
void CScriptParser::SkipWhitespace( bool* pbNewLines ) {
|
2007-11-04 03:51:54 +00:00
|
|
|
int c;
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( !m_pScript || !m_pScriptSection ) {
|
2007-11-04 03:51:54 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
while ( ( c = *m_pScriptSection ) <= ' ' ) {
|
|
|
|
if ( !c ) {
|
2007-11-04 03:51:54 +00:00
|
|
|
return;
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( c == '\n' ) {
|
2007-11-04 03:51:54 +00:00
|
|
|
*pbNewLines = true;
|
|
|
|
}
|
|
|
|
m_pScriptSection++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
void CScriptParser::SkipBracedSection( void ) {
|
|
|
|
const char *token;
|
|
|
|
int depth;
|
2007-11-04 03:51:54 +00:00
|
|
|
|
|
|
|
depth = 0;
|
|
|
|
do {
|
|
|
|
token = GetToken( true );
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( token[1] == 0 ) {
|
|
|
|
if ( *token == '{' ) {
|
2007-11-04 03:51:54 +00:00
|
|
|
depth++;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
else if ( *token == '}' ) {
|
2007-11-04 03:51:54 +00:00
|
|
|
depth--;
|
|
|
|
}
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
} while ( depth && *m_pScriptSection );
|
2007-11-04 03:51:54 +00:00
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
void CScriptParser::SkipRestOfLine( void ) {
|
|
|
|
char *p;
|
|
|
|
int c;
|
2007-11-04 03:51:54 +00:00
|
|
|
|
|
|
|
p = m_pScriptSection;
|
2012-03-17 20:01:54 +00:00
|
|
|
while ( ( c = *p++ ) != 0 ) {
|
2007-11-04 03:51:54 +00:00
|
|
|
if ( c == '\n' ) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
m_pScriptSection = p;
|
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
void CScriptParser::UndoGetToken( void ) {
|
|
|
|
if ( !m_pLastScriptSection ) {
|
2007-11-04 03:51:54 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
m_pScriptSection = m_pLastScriptSection;
|
|
|
|
m_pLastScriptSection = NULL;
|
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
void CScriptParser::ResetParseSession( void ) {
|
|
|
|
if ( !m_pScript ) {
|
2007-11-04 03:51:54 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_pScriptSection = m_pScript;
|
|
|
|
m_pLastScriptSection = NULL;
|
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
char* CScriptParser::GetBufferCopy( void ) {
|
|
|
|
if ( !m_pScript ) {
|
2007-11-04 03:51:54 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
int len = static_cast<int>( strlen( m_pScript ) );
|
2007-11-04 03:51:54 +00:00
|
|
|
char* pBuffer = new char[len + 1];
|
2012-03-17 20:01:54 +00:00
|
|
|
strcpy( pBuffer, m_pScript );
|
2007-11-04 03:51:54 +00:00
|
|
|
return pBuffer;
|
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
int CScriptParser::GetTokenOffset( void ) {
|
|
|
|
if ( !m_pScript || !m_pScriptSection ) {
|
2007-11-04 03:51:54 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
return static_cast<int>( m_pScriptSection - m_pScript );
|
2007-11-04 03:51:54 +00:00
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
void CScriptParser::LoadScript( const char* pScript ) {
|
2007-11-04 03:51:54 +00:00
|
|
|
ClearBuffer();
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
int len = static_cast<int>( strlen( pScript ) );
|
|
|
|
if ( len <= 0 ) {
|
2007-11-04 03:51:54 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_pScript = new char[len + 1];
|
|
|
|
m_pScript[len] = '\0';
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
strcpy( m_pScript, pScript );
|
2007-11-04 03:51:54 +00:00
|
|
|
m_pScriptSection = m_pScript;
|
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
void CScriptParser::AddBreakChar( char c ) {
|
|
|
|
for ( int i = 0; i < SP_MAX_BREAKCHARS; i++ ) {
|
|
|
|
if ( !m_breakChars[i] ) {
|
2007-11-04 03:51:54 +00:00
|
|
|
m_breakChars[i] = c;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Error: max break chars hit
|
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
bool CScriptParser::IsBreakChar( char c ) {
|
|
|
|
for ( int i = 0; i < SP_MAX_BREAKCHARS; i++ ) {
|
|
|
|
if ( !m_breakChars[i] ) {
|
2007-11-04 03:51:54 +00:00
|
|
|
return false;
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( m_breakChars[i] == c ) {
|
2007-11-04 03:51:54 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
void CScriptParser::SetScript( char* pScript ) {
|
2007-11-04 03:51:54 +00:00
|
|
|
ClearBuffer();
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
int len = static_cast<int>( strlen( pScript ) );
|
|
|
|
if ( len <= 0 ) {
|
2007-11-04 03:51:54 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_pScript = pScript;
|
|
|
|
m_pScriptSection = m_pScript;
|
|
|
|
}
|