Skip to content

Commit

Permalink
GenericDocument: Fixed "-" always being treated as 'minus' (may be na…
Browse files Browse the repository at this point in the history
…ked string).
  • Loading branch information
ohlidalp committed Feb 4, 2024
1 parent 5e95e38 commit 515e8f6
Showing 1 changed file with 50 additions and 6 deletions.
56 changes: 50 additions & 6 deletions source/main/utils/GenericFileFormat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ enum class PartialToken
STRING_NAKED, // String without '"' on either end
STRING_NAKED_CAPTURING_SPACES, // Only for OPTION_PARENTHESES_CAPTURE_SPACES - A naked string seeking the closing ')'.
TITLE_STRING, // A whole-line string, with spaces
NUMBER_STUB_MINUS, // Sole '-' character, may start a number or a naked string.
NUMBER_INTEGER, // Just digits and optionally leading '-'
NUMBER_DECIMAL, // Like INTEGER but already containing '.'
NUMBER_SCIENTIFIC_STUB, // Like DECIMAL, already containing 'e' or 'E' but not the exponent value.
Expand Down Expand Up @@ -222,12 +223,17 @@ void DocumentParser::BeginToken(const char c)
case '7':
case '8':
case '9':
case '-':
partial_tok_type = PartialToken::NUMBER_INTEGER;
tok.push_back(c);
line_pos++;
break;

case '-':
partial_tok_type = PartialToken::NUMBER_STUB_MINUS;
tok.push_back(c);
line_pos++;
break;

default:
if (isalpha(c) &&
(doc.tokens.size() == 0 || doc.tokens.back().type == TokenType::LINEBREAK)) // on line start?
Expand Down Expand Up @@ -424,12 +430,28 @@ void DocumentParser::UpdateNumber(const char c)
case ' ':
case ',':
case '\t':
this->FlushNumericToken();
if (partial_tok_type == PartialToken::NUMBER_STUB_MINUS
&& options & GenericDocument::OPTION_ALLOW_NAKED_STRINGS)
{
this->FlushStringishToken(TokenType::STRING);
}
else
{
this->FlushNumericToken();
}
line_pos++;
break;

case '\n':
this->FlushNumericToken();
if (partial_tok_type == PartialToken::NUMBER_STUB_MINUS
&& options & GenericDocument::OPTION_ALLOW_NAKED_STRINGS)
{
this->FlushStringishToken(TokenType::STRING);
}
else
{
this->FlushNumericToken();
}
// Break line
doc.tokens.push_back({ TokenType::LINEBREAK, 0.f });
line_num++;
Expand All @@ -439,7 +461,15 @@ void DocumentParser::UpdateNumber(const char c)
case ':':
if (options & GenericDocument::OPTION_ALLOW_SEPARATOR_COLON)
{
this->FlushNumericToken();
if (partial_tok_type == PartialToken::NUMBER_STUB_MINUS
&& options & GenericDocument::OPTION_ALLOW_NAKED_STRINGS)
{
this->FlushStringishToken(TokenType::STRING);
}
else
{
this->FlushNumericToken();
}
}
else
{
Expand All @@ -452,7 +482,15 @@ void DocumentParser::UpdateNumber(const char c)
case '=':
if (options & GenericDocument::OPTION_ALLOW_SEPARATOR_EQUALS)
{
this->FlushNumericToken();
if (partial_tok_type == PartialToken::NUMBER_STUB_MINUS
&& options & GenericDocument::OPTION_ALLOW_NAKED_STRINGS)
{
this->FlushStringishToken(TokenType::STRING);
}
else
{
this->FlushNumericToken();
}
}
else
{
Expand All @@ -463,7 +501,8 @@ void DocumentParser::UpdateNumber(const char c)
break;

case '.':
if (partial_tok_type == PartialToken::NUMBER_INTEGER)
if (partial_tok_type == PartialToken::NUMBER_INTEGER
|| partial_tok_type == PartialToken::NUMBER_STUB_MINUS)
{
partial_tok_type = PartialToken::NUMBER_DECIMAL;
}
Expand Down Expand Up @@ -518,6 +557,10 @@ void DocumentParser::UpdateNumber(const char c)
{
partial_tok_type = PartialToken::NUMBER_SCIENTIFIC;
}
else if (partial_tok_type == PartialToken::NUMBER_STUB_MINUS)
{
partial_tok_type = PartialToken::NUMBER_INTEGER;
}
tok.push_back(c);
line_pos++;
break;
Expand Down Expand Up @@ -896,6 +939,7 @@ void DocumentParser::ProcessChar(const char c)
break;

case PartialToken::NUMBER_INTEGER:
case PartialToken::NUMBER_STUB_MINUS:
case PartialToken::NUMBER_DECIMAL:
case PartialToken::NUMBER_SCIENTIFIC:
case PartialToken::NUMBER_SCIENTIFIC_STUB:
Expand Down

0 comments on commit 515e8f6

Please sign in to comment.