/* ================ DeclParser::Parse ================ */ void DeclParser::Parse( Lexer &lexer ) { Dict *resultDict = OG_NULL; bool getKeyValue = false; int index; const Token *token; String key, value; const char *p; while ( (token = lexer.ReadToken()) != OG_NULL ) { p = token->GetString(); if ( p ) { if ( *p == '\0' ) lexer.Error("Unexpected Empty Token"); if ( !getKeyValue ) { index = declTypes.Find(p); if ( index != -1 ) { value = lexer.ReadString(); if ( value.IsEmpty() ) lexer.Error("Empty name!"); DictEx<Dict> &result = declTypes[index]->declList; index = result.Find(p); if ( index == -1 ) resultDict = &result[p]; else { resultDict = &result[index]; resultDict->Clear(); } } else { resultDict = OG_NULL; lexer.Warning( Format("Unknown decl Type '$*'") << p ); } lexer.ExpectToken("{"); getKeyValue = true; } else { if ( *p == '}' ) getKeyValue = false; else if ( resultDict ) { key = p; (*resultDict).Set( key.c_str(), lexer.ReadString() ); } } } } if ( getKeyValue ) throw LexerError( LexerError::END_OF_FILE ); }
/* ================ ReadGeoMObject ================ */ static void ReadGeoMObject( Lexer &lexer, ListEx<aseMesh> &myMeshList ) { lexer.ExpectToken("{"); const char *p; const Token *token; String name; while ( (token = lexer.ReadToken()) != OG_NULL ) { p = token->GetString(); if ( !p || !*p ) continue; if ( String::Icmp( p, "}" ) == 0 ) return; if ( String::Icmp( p, "*" ) != 0 ) lexer.Error( Format("expected *, got '$*'") << p ); if ( lexer.CheckToken( "NODE_NAME" ) ) name = lexer.ReadString(); else if ( lexer.CheckToken( "MESH" ) ) { aseMesh *inMesh = &myMeshList.Alloc(); inMesh->name = name; ReadMesh( lexer, inMesh ); } else if ( !SkipUnknown(lexer) ) break; } }
/* ================ DeclParser::MakeBinary ================ */ bool DeclParser::MakeBinary( const char *filename ) { if ( commonFS == OG_NULL ) return false; Lexer lexer; if ( !lexer.LoadFile( filename ) ) return false; File *f = commonFS->OpenWrite( Format( "$*.bin" ) << filename ); if ( !f ) return false; try { f->Write( DECL_DESCRIPTOR_STR, DECL_DESCRIPTOR_LENGTH ); Dict dict; bool getKeyValue = false; const Token *token; String key, value; String str; const char *p; while ( (token = lexer.ReadToken()) != OG_NULL ) { //! @todo maybe token should be stored as String, so we don't have to recalc len/bytelen p = token->GetString(); if ( p ) { if ( *p == '\0' ) { lexer.Error("Unexpected Empty Token"); return false; } if ( !getKeyValue ) { str = p; str.WriteToFile( f ); value = lexer.ReadString(); if ( value.IsEmpty() ) { lexer.Error("Empty name!"); return false; } value.WriteToFile( f ); lexer.ExpectToken("{"); getKeyValue = true; } else { if ( *p == '}' ) { getKeyValue = false; dict.WriteToFile( f ); dict.Clear(); } else { key = p; dict.Set( key.c_str(), lexer.ReadString() ); } } } } if ( getKeyValue ) { lexer.Error("Unexpected End Of File"); return false; } return true; } catch( FileReadWriteError &err ) { f->Close(); User::Error( ERR_FILE_WRITEFAIL, Format("Binary Decl: $*.bin" ) << err.ToString(), filename ); return false; } catch( LexerError &err ) { f->Close(); String errStr; err.ToString( errStr ); User::Error( ERR_LEXER_FAILURE, errStr.c_str(), filename ); return false; } }
/* ================ XDeclParser::Parse ================ */ void XDeclParser::Parse( Lexer &lexer ) { const Token *token; String name, value; XDeclNode *currentNode = &rootNode; XDeclNode *newNode; bool getKeyValue = false; bool addNode; Stack<XDeclNode *> nodeStack; const char *p; while ( (token = lexer.ReadToken()) != NULL ) { p = token->GetString(); if ( p ) { if ( *p == '\0' ) lexer.Error("Unexpected Empty Token"); if ( !getKeyValue ) { if ( String::Cmp( p, "}" ) == 0 ) { currentNode = nodeStack.Top(); nodeStack.Pop(); } else { name = p; addNode = false; if ( lexer.CheckToken("=") ) { currentNode->dict.Set( name.c_str(), lexer.ReadString() ); lexer.ExpectToken(";"); } else if ( lexer.CheckToken("(") ) { addNode = true; getKeyValue = true; } else if ( lexer.CheckToken("{") ) { addNode = true; } else { lexer.Error("Syntax Error, was expecting '(', '=' or '{'"); } if ( addNode ) { newNode = allocator.Alloc(); newNode->name = name; currentNode->numChildren++; if ( currentNode->firstChild == NULL ) currentNode->firstChild = newNode; else currentNode->lastChild->next = newNode; currentNode->lastChild= newNode; nodeStack.Push( currentNode ); currentNode = newNode; } } } else { name = p; lexer.ExpectToken("="); currentNode->dict.Set( name.c_str(), lexer.ReadString() ); if ( !lexer.CheckToken(",") ) { lexer.ExpectToken(")"); if ( lexer.CheckToken(";") ) { currentNode = nodeStack.Top(); nodeStack.Pop(); getKeyValue = false; } else if ( lexer.CheckToken("{") ) { getKeyValue = false; } else { lexer.Error("Syntax Error, was expecting ';' or '{'"); } } } } } if ( getKeyValue || nodeStack.Num() ) throw LexerError( LexerError::END_OF_FILE ); }