Make script numeric matchers match negative numbers as well

This commit is contained in:
Jan 2023-09-25 21:45:06 +02:00
parent 870fa44b02
commit d071dc1bfd
3 changed files with 41 additions and 3 deletions

View File

@ -7,6 +7,18 @@ MatcherResult<SimpleParserValue> MenuMatcherScriptInt::CanMatch(ILexer<SimplePar
{
const auto& token = lexer->GetToken(tokenOffset);
if (token.m_type == SimpleParserValueType::CHARACTER)
{
if (token.CharacterValue() != '-')
return MatcherResult<SimpleParserValue>::NoMatch();
const auto& nextToken = lexer->GetToken(tokenOffset + 1);
if (nextToken.m_type != SimpleParserValueType::INTEGER)
return MatcherResult<SimpleParserValue>::NoMatch();
return MatcherResult<SimpleParserValue>::Match(2);
}
if (token.m_type == SimpleParserValueType::INTEGER)
return MatcherResult<SimpleParserValue>::Match(1);
@ -20,7 +32,7 @@ MatcherResult<SimpleParserValue> MenuMatcherScriptInt::CanMatch(ILexer<SimplePar
char* endPtr;
// The return result does not matter here
const auto _ = strtol(&stringValue[0], &endPtr, 10);
const auto _ = strtol(stringValue.data(), &endPtr, 10);
if (endPtr != &stringValue[stringValue.size() - 1])
return MatcherResult<SimpleParserValue>::NoMatch();

View File

@ -7,6 +7,18 @@ MatcherResult<SimpleParserValue> MenuMatcherScriptNumeric::CanMatch(ILexer<Simpl
{
const auto& token = lexer->GetToken(tokenOffset);
if (token.m_type == SimpleParserValueType::CHARACTER)
{
if (token.CharacterValue() != '-')
return MatcherResult<SimpleParserValue>::NoMatch();
const auto& nextToken = lexer->GetToken(tokenOffset + 1);
if (nextToken.m_type != SimpleParserValueType::FLOATING_POINT && nextToken.m_type != SimpleParserValueType::INTEGER)
return MatcherResult<SimpleParserValue>::NoMatch();
return MatcherResult<SimpleParserValue>::Match(2);
}
if (token.m_type == SimpleParserValueType::FLOATING_POINT || token.m_type == SimpleParserValueType::INTEGER)
return MatcherResult<SimpleParserValue>::Match(1);
@ -20,7 +32,7 @@ MatcherResult<SimpleParserValue> MenuMatcherScriptNumeric::CanMatch(ILexer<Simpl
char* endPtr;
// The return result does not matter here
const auto _ = strtod(&stringValue[0], &endPtr);
const auto _ = strtod(stringValue.data(), &endPtr);
if(endPtr != &stringValue[stringValue.size()])
return MatcherResult<SimpleParserValue>::NoMatch();

View File

@ -38,6 +38,17 @@ namespace menu
{
const auto& firstToken = tokens[0].get();
if (firstToken.m_type == SimpleParserValueType::CHARACTER)
{
const auto& secondToken = tokens[1].get();
if (secondToken.m_type == SimpleParserValueType::INTEGER)
return SimpleParserValue::String(firstToken.GetPos(), new std::string(std::to_string(-secondToken.IntegerValue())));
std::ostringstream ss;
ss << std::noshowpoint << -firstToken.FloatingPointValue();
return SimpleParserValue::String(firstToken.GetPos(), new std::string(ss.str()));
}
if (firstToken.m_type == SimpleParserValueType::INTEGER)
return SimpleParserValue::String(firstToken.GetPos(), new std::string(std::to_string(firstToken.IntegerValue())));
if (firstToken.m_type == SimpleParserValueType::FLOATING_POINT)
@ -73,6 +84,9 @@ namespace menu
{
const auto& firstToken = tokens[0].get();
if (firstToken.m_type == SimpleParserValueType::CHARACTER)
return SimpleParserValue::String(firstToken.GetPos(), new std::string(std::to_string(-tokens[1].get().IntegerValue())));
if (firstToken.m_type == SimpleParserValueType::INTEGER)
return SimpleParserValue::String(firstToken.GetPos(), new std::string(std::to_string(firstToken.IntegerValue())));
return SimpleParserValue::String(firstToken.GetPos(), new std::string(firstToken.StringValue()));