Kim Kulling 2015-03-25 19:55:39 +01:00
parent 23b3728254
commit d19a677f88
2 changed files with 47 additions and 28 deletions

View File

@ -190,6 +190,8 @@ bool OpenDDLParser::parse() {
normalizeBuffer( m_buffer ); normalizeBuffer( m_buffer );
std::cout << &m_buffer[0] << std::endl;
m_context = new Context; m_context = new Context;
m_context->m_root = DDLNode::create( "root", "", ddl_nullptr ); m_context->m_root = DDLNode::create( "root", "", ddl_nullptr );
pushNode( m_context->m_root ); pushNode( m_context->m_root );
@ -230,7 +232,7 @@ char *OpenDDLParser::parseHeader( char *in, char *end ) {
dumpId( id ); dumpId( id );
#endif // DEBUG_HEADER_NAME #endif // DEBUG_HEADER_NAME
in = getNextToken( in, end ); in = lookForNextToken( in, end );
Property *first( ddl_nullptr ); Property *first( ddl_nullptr );
if( ddl_nullptr != id ) { if( ddl_nullptr != id ) {
if( *in == '(' ) { if( *in == '(' ) {
@ -238,7 +240,7 @@ char *OpenDDLParser::parseHeader( char *in, char *end ) {
Property *prop( ddl_nullptr ), *prev( ddl_nullptr ); Property *prop( ddl_nullptr ), *prev( ddl_nullptr );
while( *in != ')' && in != end ) { while( *in != ')' && in != end ) {
in = OpenDDLParser::parseProperty( in, end, &prop ); in = OpenDDLParser::parseProperty( in, end, &prop );
in = getNextToken( in, end ); in = lookForNextToken( in, end );
if( *in != ',' && *in != ')' ) { if( *in != ',' && *in != ')' ) {
logInvalidTokenError( in, ")", m_logCallback ); logInvalidTokenError( in, ")", m_logCallback );
@ -288,21 +290,20 @@ char *OpenDDLParser::parseStructure( char *in, char *end ) {
} }
bool error( false ); bool error( false );
in = getNextToken( in, end ); in = lookForNextToken( in, end );
if( *in == '{' ) { if( *in == '{' ) {
// loop over all children ( data and nodes ) // loop over all children ( data and nodes )
do { do {
in = parseStructureBody( in, end, error ); in = parseStructureBody( in, end, error );
} while ( *in != '}' ); } while ( *in != '}' );
in++; in++;
} } else {
else {
in++; in++;
logInvalidTokenError( in, std::string( Grammar::OpenBracketToken ), m_logCallback ); logInvalidTokenError( in, std::string( Grammar::OpenBracketToken ), m_logCallback );
error = true; error = true;
return in; return in;
} }
in = getNextToken( in, end ); in = lookForNextToken( in, end );
// pop node from stack after successful parsing // pop node from stack after successful parsing
if( !error ) { if( !error ) {
@ -341,12 +342,13 @@ char *OpenDDLParser::parseStructureBody( char *in, char *end, bool &error ) {
in++; in++;
} }
in = getNextToken( in, end ); in = lookForNextToken( in, end );
Value::ValueType type( Value::ddl_none ); Value::ValueType type( Value::ddl_none );
size_t arrayLen( 0 ); size_t arrayLen( 0 );
in = OpenDDLParser::parsePrimitiveDataType( in, end, type, arrayLen ); in = OpenDDLParser::parsePrimitiveDataType( in, end, type, arrayLen );
if( Value::ddl_none != type ) { if( Value::ddl_none != type ) {
in = getNextToken( in, end ); // parse a primitive data type
in = lookForNextToken( in, end );
if( *in == '{' ) { if( *in == '{' ) {
Reference *refs( ddl_nullptr ); Reference *refs( ddl_nullptr );
DataArrayList *dtArrayList( ddl_nullptr ); DataArrayList *dtArrayList( ddl_nullptr );
@ -365,13 +367,14 @@ char *OpenDDLParser::parseStructureBody( char *in, char *end, bool &error ) {
} }
} }
in = getNextToken( in, end ); in = lookForNextToken( in, end );
if( *in != '}' ) { if( *in != '}' ) {
logInvalidTokenError( in, std::string( Grammar::CloseBracketToken ), m_logCallback ); logInvalidTokenError( in, std::string( Grammar::CloseBracketToken ), m_logCallback );
} else { } else {
//in++; //in++;
} }
} else { } else {
// parse a complex data type
in = parseNextNode( in, end ); in = parseNextNode( in, end );
} }
@ -429,15 +432,16 @@ void OpenDDLParser::normalizeBuffer( std::vector<char> &buffer) {
for( size_t readIdx = 0; readIdx<len; ++readIdx ) { for( size_t readIdx = 0; readIdx<len; ++readIdx ) {
char *c( &buffer[readIdx] ); char *c( &buffer[readIdx] );
// check for a comment // check for a comment
if( !isComment<char>( c, end ) ) { if( !isComment<char>( c, end ) && !isNewLine( *c ) ) {
newBuffer.push_back( buffer[ readIdx ] ); newBuffer.push_back( buffer[ readIdx ] );
} else { } else {
readIdx++; if( isComment<char>( c, end ) ) {
// skip the comment and the rest of the line
while( !isEndofLine( buffer[ readIdx ] ) ) {
readIdx++; readIdx++;
// skip the comment and the rest of the line
while( !isEndofLine( buffer[ readIdx ] ) ) {
readIdx++;
}
} }
newBuffer.push_back( '\n' );
} }
} }
buffer = newBuffer; buffer = newBuffer;
@ -450,7 +454,7 @@ char *OpenDDLParser::parseName( char *in, char *end, Name **name ) {
} }
// ignore blanks // ignore blanks
in = getNextToken( in, end ); in = lookForNextToken( in, end );
if( *in != '$' && *in != '%' ) { if( *in != '$' && *in != '%' ) {
return in; return in;
} }
@ -480,7 +484,7 @@ char *OpenDDLParser::parseIdentifier( char *in, char *end, Identifier **id ) {
} }
// ignore blanks // ignore blanks
in = getNextToken( in, end ); in = lookForNextToken( in, end );
// staring with a number is forbidden // staring with a number is forbidden
if( isNumeric<const char>( *in ) ) { if( isNumeric<const char>( *in ) ) {
@ -521,7 +525,7 @@ char *OpenDDLParser::parsePrimitiveDataType( char *in, char *end, Value::ValueTy
} }
if( Value::ddl_none == type ) { if( Value::ddl_none == type ) {
in = getNextToken( in, end ); in = lookForNextToken( in, end );
return in; return in;
} else { } else {
in += prim_len; in += prim_len;
@ -582,7 +586,7 @@ char *OpenDDLParser::parseBooleanLiteral( char *in, char *end, Value **boolean )
return in; return in;
} }
in = getNextToken( in, end ); in = lookForNextToken( in, end );
char *start( in ); char *start( in );
size_t len( 0 ); size_t len( 0 );
while( !isSeparator( *in ) && in != end ) { while( !isSeparator( *in ) && in != end ) {
@ -617,7 +621,7 @@ char *OpenDDLParser::parseIntegerLiteral( char *in, char *end, Value **integer,
return in; return in;
} }
in = getNextToken( in, end ); in = lookForNextToken( in, end );
char *start( in ); char *start( in );
while( !isSeparator( *in ) && in != end ) { while( !isSeparator( *in ) && in != end ) {
in++; in++;
@ -653,7 +657,7 @@ char *OpenDDLParser::parseFloatingLiteral( char *in, char *end, Value **floating
return in; return in;
} }
in = getNextToken( in, end ); in = lookForNextToken( in, end );
char *start( in ); char *start( in );
while( !isSeparator( *in ) && in != end ) { while( !isSeparator( *in ) && in != end ) {
in++; in++;
@ -686,7 +690,7 @@ char *OpenDDLParser::parseStringLiteral( char *in, char *end, Value **stringData
return in; return in;
} }
in = getNextToken( in, end ); in = lookForNextToken( in, end );
size_t len( 0 ); size_t len( 0 );
char *start( in ); char *start( in );
if( *start == '\"' ) { if( *start == '\"' ) {
@ -719,7 +723,7 @@ char *OpenDDLParser::parseHexaLiteral( char *in, char *end, Value **data ) {
return in; return in;
} }
in = getNextToken( in, end ); in = lookForNextToken( in, end );
if( *in != '0' ) { if( *in != '0' ) {
return in; return in;
} }
@ -765,11 +769,11 @@ char *OpenDDLParser::parseProperty( char *in, char *end, Property **prop ) {
return in; return in;
} }
in = getNextToken( in, end ); in = lookForNextToken( in, end );
Identifier *id( ddl_nullptr ); Identifier *id( ddl_nullptr );
in = parseIdentifier( in, end, &id ); in = parseIdentifier( in, end, &id );
if( ddl_nullptr != id ) { if( ddl_nullptr != id ) {
in = getNextToken( in, end ); in = lookForNextToken( in, end );
if( *in == '=' ) { if( *in == '=' ) {
in++; in++;
in = getNextToken( in, end ); in = getNextToken( in, end );
@ -805,13 +809,13 @@ char *OpenDDLParser::parseDataList( char *in, char *end, Value **data, size_t &n
return in; return in;
} }
in = getNextToken( in, end ); in = lookForNextToken( in, end );
if( *in == '{' ) { if( *in == '{' ) {
in++; in++;
Value *current( ddl_nullptr ), *prev( ddl_nullptr ); Value *current( ddl_nullptr ), *prev( ddl_nullptr );
while( '}' != *in ) { while( '}' != *in ) {
current = ddl_nullptr; current = ddl_nullptr;
in = getNextToken( in, end ); in = lookForNextToken( in, end );
if( isInteger( in, end ) ) { if( isInteger( in, end ) ) {
in = parseIntegerLiteral( in, end, &current ); in = parseIntegerLiteral( in, end, &current );
} else if( isFloat( in, end ) ) { } else if( isFloat( in, end ) ) {
@ -858,7 +862,7 @@ char *OpenDDLParser::parseDataArrayList( char *in, char *end, DataArrayList **da
return in; return in;
} }
in = getNextToken( in, end ); in = lookForNextToken( in, end );
if( *in == Grammar::OpenBracketToken[ 0 ] ) { if( *in == Grammar::OpenBracketToken[ 0 ] ) {
in++; in++;
Value *current( ddl_nullptr ); Value *current( ddl_nullptr );
@ -882,6 +886,8 @@ char *OpenDDLParser::parseDataArrayList( char *in, char *end, DataArrayList **da
} }
} }
} while( ',' == *in && in != end ); } while( ',' == *in && in != end );
in = lookForNextToken( in, end );
in++;
} }
return in; return in;

View File

@ -43,13 +43,26 @@ struct Property;
template<class T> template<class T>
inline inline
T *getNextToken( T *in, T *end ) { T *lookForNextToken( T *in, T *end ) {
while( ( isSpace( *in ) || isNewLine( *in ) || ',' == *in ) && ( in != end ) ) { while( ( isSpace( *in ) || isNewLine( *in ) || ',' == *in ) && ( in != end ) ) {
in++; in++;
} }
return in; return in;
} }
template<class T>
inline
T *getNextToken( T *in, T *end ) {
T *tmp( in );
while( ( isSpace( *in ) || isNewLine( *in ) || ',' == *in ) && ( in != end ) ) {
in++;
}
if( tmp == in ) {
in++;
}
return in;
}
/// @brief Defines the log severity. /// @brief Defines the log severity.
enum LogSeverity { enum LogSeverity {
ddl_debug_msg = 0, ///< Debug message, for debugging ddl_debug_msg = 0, ///< Debug message, for debugging