Skip to content

Commit

Permalink
GenericDocument: added option "allow colon as separator"
Browse files Browse the repository at this point in the history
  • Loading branch information
ohlidalp committed Dec 13, 2022
1 parent 2cd2fb9 commit 179bd86
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 3 deletions.
6 changes: 3 additions & 3 deletions resources/scripts/demo_script.as
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@ void main()
void frameStep(float dt)
{
// Open demo window
ImGui::SetNextWindowSize(vector2(400, 320));
ImGui::Begin("Demo Script", /*open:*/true, /*flags:*/0);
ImGui::Begin("Demo Script", /*open:*/true, ImGuiWindowFlags_AlwaysAutoResize);

// show some stats
ImGui::Text("Total frames: " + g_total_frames);
Expand Down Expand Up @@ -114,7 +113,8 @@ void frameStep(float dt)
GenericDocumentClass@ doc = GenericDocumentClass();
int flags = GENERIC_DOCUMENT_OPTION_ALLOW_NAKED_STRINGS
| GENERIC_DOCUMENT_OPTION_ALLOW_SLASH_COMMENTS
| GENERIC_DOCUMENT_OPTION_FIRST_LINE_IS_TITLE;
| GENERIC_DOCUMENT_OPTION_FIRST_LINE_IS_TITLE
| GENERIC_DOCUMENT_OPTION_ALLOW_SEPARATOR_COLON;
if (doc.LoadFromResource(actor.getTruckFileName(), actor.getTruckFileResourceGroup(), flags))
{
@g_displayed_document = @doc;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ void RoR::RegisterGenericFileFormat(asIScriptEngine* engine)
engine->RegisterEnumValue("GenericDocumentOptions", "GENERIC_DOCUMENT_OPTION_ALLOW_NAKED_STRINGS", GenericDocument::OPTION_ALLOW_NAKED_STRINGS);
engine->RegisterEnumValue("GenericDocumentOptions", "GENERIC_DOCUMENT_OPTION_ALLOW_SLASH_COMMENTS", GenericDocument::OPTION_ALLOW_SLASH_COMMENTS);
engine->RegisterEnumValue("GenericDocumentOptions", "GENERIC_DOCUMENT_OPTION_FIRST_LINE_IS_TITLE", GenericDocument::OPTION_FIRST_LINE_IS_TITLE);
engine->RegisterEnumValue("GenericDocumentOptions", "GENERIC_DOCUMENT_OPTION_ALLOW_SEPARATOR_COLON", GenericDocument::OPTION_ALLOW_SEPARATOR_COLON);


// class GenericDocument
Expand Down
87 changes: 87 additions & 0 deletions source/main/utils/GenericFileFormat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,22 @@ void DocumentParser::BeginToken(const char c)
line_pos++;
break;

case ':':
if (options & GenericDocument::OPTION_ALLOW_SEPARATOR_COLON)
{
line_pos++;
}
else
{
if (options & GenericDocument::OPTION_ALLOW_NAKED_STRINGS)
partial_tok_type = PartialToken::STRING_NAKED;
else
partial_tok_type = PartialToken::GARBAGE;
tok.push_back(c);
line_pos++;
}
break;

case '\n':
doc.tokens.push_back({ TokenType::LINEBREAK, 0.f });
line_num++;
Expand Down Expand Up @@ -267,6 +283,24 @@ void DocumentParser::UpdateString(const char c)
line_pos = 0;
break;

case ':':
if (options & GenericDocument::OPTION_ALLOW_SEPARATOR_COLON
&& partial_tok_type == PartialToken::STRING_NAKED)
{
// Flush string
doc.tokens.push_back({ TokenType::STRING, (float)doc.string_pool.size() });
tok.push_back('\0');
std::copy(tok.begin(), tok.end(), std::back_inserter(doc.string_pool));
tok.clear();
partial_tok_type = PartialToken::NONE;
}
else
{
tok.push_back(c);
}
line_pos++;
break;

case '"':
if (partial_tok_type == PartialToken::STRING_QUOTED)
{
Expand Down Expand Up @@ -328,6 +362,23 @@ void DocumentParser::UpdateNumber(const char c)
line_pos = 0;
break;

case ':':
if (options & GenericDocument::OPTION_ALLOW_SEPARATOR_COLON)
{
// Flush number
tok.push_back('\0');
doc.tokens.push_back({ TokenType::NUMBER, (float)Ogre::StringConverter::parseReal(tok.data()) });
tok.clear();
partial_tok_type = PartialToken::NONE;
}
else
{
partial_tok_type = PartialToken::GARBAGE;
tok.push_back(c);
}
line_pos++;
break;

case '.':
if (partial_tok_type == PartialToken::NUMBER)
{
Expand Down Expand Up @@ -413,6 +464,24 @@ void DocumentParser::UpdateBool(const char c)
line_pos = 0;
break;

case ':':
if (options & GenericDocument::OPTION_ALLOW_SEPARATOR_COLON)
{
// Discard token
tok.push_back('\0');
App::GetConsole()->putMessage(Console::CONSOLE_MSGTYPE_INFO, Console::CONSOLE_SYSTEM_WARNING,
fmt::format("{}, line {}, pos {}: discarding incomplete boolean token '{}'", datastream->getName(), line_num, line_pos, tok.data()));
tok.clear();
partial_tok_type = PartialToken::NONE;
}
else
{
partial_tok_type = PartialToken::GARBAGE;
tok.push_back(c);
}
line_pos++;
break;

case 'r':
if (partial_tok_type != PartialToken::BOOL_TRUE || tok.size() != 1)
{
Expand Down Expand Up @@ -535,6 +604,24 @@ void DocumentParser::UpdateKeyword(const char c)
line_pos = 0;
break;

case ':':
if (options & GenericDocument::OPTION_ALLOW_SEPARATOR_COLON)
{
// Flush keyword
doc.tokens.push_back({ TokenType::KEYWORD, (float)doc.string_pool.size() });
tok.push_back('\0');
std::copy(tok.begin(), tok.end(), std::back_inserter(doc.string_pool));
tok.clear();
partial_tok_type = PartialToken::NONE;
}
else
{
partial_tok_type = PartialToken::GARBAGE;
tok.push_back(c);
}
line_pos++;
break;

case '_':
tok.push_back(c);
line_pos++;
Expand Down
1 change: 1 addition & 0 deletions source/main/utils/GenericFileFormat.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ struct GenericDocument: public RefCountingObject<GenericDocument>
static const BitMask_t OPTION_ALLOW_NAKED_STRINGS = BITMASK(1); //!< Allow strings without quotes, for backwards compatibility.
static const BitMask_t OPTION_ALLOW_SLASH_COMMENTS = BITMASK(2); //!< Allow comments starting with `//`.
static const BitMask_t OPTION_FIRST_LINE_IS_TITLE = BITMASK(3); //!< First non-empty & non-comment line is a naked string with spaces.
static const BitMask_t OPTION_ALLOW_SEPARATOR_COLON = BITMASK(4); //!< Allow ':' as separator between tokens.

virtual ~GenericDocument() {};

Expand Down

0 comments on commit 179bd86

Please sign in to comment.