Skip to content

Commit

Permalink
GenericDocument: Added INT token type.
Browse files Browse the repository at this point in the history
  • Loading branch information
ohlidalp committed Oct 14, 2024
1 parent cb55992 commit 21a1d7e
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ void RoR::RegisterGenericFileFormat(asIScriptEngine* engine)
engine->RegisterEnumValue("TokenType", "TOKEN_TYPE_LINEBREAK", (int)TokenType::LINEBREAK);
engine->RegisterEnumValue("TokenType", "TOKEN_TYPE_COMMENT", (int)TokenType::COMMENT);
engine->RegisterEnumValue("TokenType", "TOKEN_TYPE_STRING", (int)TokenType::STRING);
engine->RegisterEnumValue("TokenType", "TOKEN_TYPE_NUMBER", (int)TokenType::NUMBER);
engine->RegisterEnumValue("TokenType", "TOKEN_TYPE_FLOAT", (int)TokenType::FLOAT);
engine->RegisterEnumValue("TokenType", "TOKEN_TYPE_INT", (int)TokenType::INT);
engine->RegisterEnumValue("TokenType", "TOKEN_TYPE_BOOL", (int)TokenType::BOOL);
engine->RegisterEnumValue("TokenType", "TOKEN_TYPE_KEYWORD", (int)TokenType::KEYWORD);

Expand Down Expand Up @@ -89,12 +90,14 @@ void RoR::RegisterGenericFileFormat(asIScriptEngine* engine)

engine->RegisterObjectMethod("GenericDocContextClass", "string getTokString(int offset = 0)", asMETHOD(GenericDocContext, getTokString), asCALL_THISCALL);
engine->RegisterObjectMethod("GenericDocContextClass", "float getTokFloat(int offset = 0)", asMETHOD(GenericDocContext, getTokFloat), asCALL_THISCALL);
engine->RegisterObjectMethod("GenericDocContextClass", "int getTokInt(int offset = 0)", asMETHOD(GenericDocContext, getTokInt), asCALL_THISCALL);
engine->RegisterObjectMethod("GenericDocContextClass", "bool getTokBool(int offset = 0)", asMETHOD(GenericDocContext, getTokBool), asCALL_THISCALL);
engine->RegisterObjectMethod("GenericDocContextClass", "string getTokKeyword(int offset = 0)", asMETHOD(GenericDocContext, getTokKeyword), asCALL_THISCALL);
engine->RegisterObjectMethod("GenericDocContextClass", "string getTokComment(int offset = 0)", asMETHOD(GenericDocContext, getTokComment), asCALL_THISCALL);

engine->RegisterObjectMethod("GenericDocContextClass", "bool isTokString(int offset = 0)", asMETHOD(GenericDocContext, isTokString), asCALL_THISCALL);
engine->RegisterObjectMethod("GenericDocContextClass", "bool isTokFloat(int offset = 0)", asMETHOD(GenericDocContext, isTokFloat), asCALL_THISCALL);
engine->RegisterObjectMethod("GenericDocContextClass", "bool isTokInt(int offset = 0)", asMETHOD(GenericDocContext, isTokInt), asCALL_THISCALL);
engine->RegisterObjectMethod("GenericDocContextClass", "bool isTokBool(int offset = 0)", asMETHOD(GenericDocContext, isTokBool), asCALL_THISCALL);
engine->RegisterObjectMethod("GenericDocContextClass", "bool isTokKeyword(int offset = 0)", asMETHOD(GenericDocContext, isTokKeyword), asCALL_THISCALL);
engine->RegisterObjectMethod("GenericDocContextClass", "bool isTokComment(int offset = 0)", asMETHOD(GenericDocContext, isTokComment), asCALL_THISCALL);
Expand All @@ -105,6 +108,7 @@ void RoR::RegisterGenericFileFormat(asIScriptEngine* engine)

engine->RegisterObjectMethod("GenericDocContextClass", "bool setTokString(int offset, const string &in)", asMETHOD(GenericDocContext, setTokString), asCALL_THISCALL);
engine->RegisterObjectMethod("GenericDocContextClass", "bool setTokFloat(int offset, float)", asMETHOD(GenericDocContext, setTokFloat), asCALL_THISCALL);
engine->RegisterObjectMethod("GenericDocContextClass", "bool setTokInt(int offset, int)", asMETHOD(GenericDocContext, setTokInt), asCALL_THISCALL);
engine->RegisterObjectMethod("GenericDocContextClass", "bool setTokBool(int offset, bool)", asMETHOD(GenericDocContext, setTokBool), asCALL_THISCALL);
engine->RegisterObjectMethod("GenericDocContextClass", "bool setTokKeyword(int offset, const string &in)", asMETHOD(GenericDocContext, setTokKeyword), asCALL_THISCALL);
engine->RegisterObjectMethod("GenericDocContextClass", "bool setTokComment(int offset, const string &in)", asMETHOD(GenericDocContext, setTokComment), asCALL_THISCALL);
Expand Down
18 changes: 16 additions & 2 deletions source/main/utils/GenericFileFormat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -916,7 +916,14 @@ void DocumentParser::FlushStringishToken(RoR::TokenType type)
void DocumentParser::FlushNumericToken()
{
tok.push_back('\0');
doc.tokens.push_back({ TokenType::NUMBER, (float)Ogre::StringConverter::parseReal(tok.data()) });
if (partial_tok_type == PartialToken::NUMBER_INTEGER)
{
doc.tokens.push_back({ TokenType::INT, (float)Ogre::StringConverter::parseInt(tok.data()) });
}
else
{
doc.tokens.push_back({ TokenType::FLOAT, (float)Ogre::StringConverter::parseReal(tok.data()) });
}
tok.clear();
partial_tok_type = PartialToken::NONE;
}
Expand Down Expand Up @@ -1057,13 +1064,20 @@ void GenericDocument::saveToDataStream(Ogre::DataStreamPtr datastream)
separator = ",";
break;

case TokenType::NUMBER:
case TokenType::FLOAT:
datastream->write(separator.data(), separator.size());
snprintf(buf, BUF_MAX, "%f", tok.data);
datastream->write(buf, strlen(buf));
separator = ",";
break;

case TokenType::INT:
datastream->write(separator.data(), separator.size());
snprintf(buf, BUF_MAX, "%d", (int)tok.data);
datastream->write(buf, strlen(buf));
separator = ",";
break;

case TokenType::BOOL:
datastream->write(separator.data(), separator.size());
snprintf(buf, BUF_MAX, "%s", tok.data == 1.f ? "true" : "false");
Expand Down
10 changes: 7 additions & 3 deletions source/main/utils/GenericFileFormat.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ enum class TokenType
LINEBREAK, // Input: LF (CR is ignored); Output: platform-specific.
COMMENT, // Line starting with ; (skipping whitespace). Data: offset in string pool.
STRING, // Quoted string. Data: offset in string pool.
NUMBER, // Float.
FLOAT,
INT,
BOOL, // Lowercase 'true'/'false'. Data: 1.0 for true, 0.0 for false.
KEYWORD, // Unquoted string at start of line (skipping whitespace). Data: offset in string pool.
};
Expand Down Expand Up @@ -113,12 +114,14 @@ struct GenericDocContext: public RefCountingObject<GenericDocContext>

std::string getTokString(int offset = 0) const { ROR_ASSERT(isTokString(offset)); return getStringData(offset); }
float getTokFloat(int offset = 0) const { ROR_ASSERT(isTokFloat(offset)); return getFloatData(offset); }
int getTokInt(int offset = 0) const { ROR_ASSERT(isTokInt(offset)); return (int)getFloatData(offset); }
bool getTokBool(int offset = 0) const { ROR_ASSERT(isTokBool(offset)); return getFloatData(offset) == 1.f; }
std::string getTokKeyword(int offset = 0) const { ROR_ASSERT(isTokKeyword(offset)); return getStringData(offset); }
std::string getTokComment(int offset = 0) const { ROR_ASSERT(isTokComment(offset)); return getStringData(offset); }

bool isTokString(int offset = 0) const { return tokenType(offset) == TokenType::STRING; }
bool isTokFloat(int offset = 0) const { return tokenType(offset) == TokenType::NUMBER; }
bool isTokFloat(int offset = 0) const { return tokenType(offset) == TokenType::FLOAT; }
bool isTokInt(int offset = 0) const { return tokenType(offset) == TokenType::INT; }
bool isTokBool(int offset = 0) const { return tokenType(offset) == TokenType::BOOL; }
bool isTokKeyword(int offset = 0) const { return tokenType(offset) == TokenType::KEYWORD; }
bool isTokComment(int offset = 0) const { return tokenType(offset) == TokenType::COMMENT; }
Expand All @@ -130,7 +133,8 @@ struct GenericDocContext: public RefCountingObject<GenericDocContext>
bool eraseToken(int offset = 0); //!< @return false if offset is beyond EOF

bool setTokString(int offset, const std::string& str) { return setStringData(offset, TokenType::STRING, str); }
bool setTokFloat(int offset, float val) { return setFloatData(offset, TokenType::NUMBER, val); }
bool setTokFloat(int offset, float val) { return setFloatData(offset, TokenType::FLOAT, val); }
bool setTokInt(int offset, int val) { return setFloatData(offset, TokenType::INT, val); }
bool setTokBool(int offset, bool val) { return setFloatData(offset, TokenType::BOOL, val); }
bool setTokKeyword(int offset, const std::string& str) { return setStringData(offset, TokenType::KEYWORD, str); }
bool setTokComment(int offset, const std::string& str) { return setStringData(offset, TokenType::COMMENT, str); }
Expand Down

0 comments on commit 21a1d7e

Please sign in to comment.