Recognize script numeric and int values as strings

This commit is contained in:
Jan
2021-11-20 12:01:04 +01:00
parent ed329e6453
commit ef1ad18332
5 changed files with 110 additions and 14 deletions

View File

@ -0,0 +1,29 @@
#include "MenuMatcherScriptInt.h"
MenuMatcherScriptInt::MenuMatcherScriptInt()
= default;
MatcherResult<SimpleParserValue> MenuMatcherScriptInt::CanMatch(ILexer<SimpleParserValue>*lexer, const unsigned tokenOffset)
{
const auto& token = lexer->GetToken(tokenOffset);
if (token.m_type == SimpleParserValueType::INTEGER)
return MatcherResult<SimpleParserValue>::Match(1);
if (token.m_type != SimpleParserValueType::STRING)
return MatcherResult<SimpleParserValue>::NoMatch();
const auto& stringValue = token.StringValue();
if (stringValue.empty())
return MatcherResult<SimpleParserValue>::NoMatch();
char* endPtr;
// The return result does not matter here
const auto _ = strtol(&stringValue[0], &endPtr, 10);
if (endPtr != &stringValue[stringValue.size() - 1])
return MatcherResult<SimpleParserValue>::NoMatch();
return MatcherResult<SimpleParserValue>::Match(1);
}

View File

@ -0,0 +1,13 @@
#pragma once
#include "Parsing/Simple/SimpleParserValue.h"
#include "Parsing/Matcher/AbstractMatcher.h"
class MenuMatcherScriptInt final : public AbstractMatcher<SimpleParserValue>
{
protected:
MatcherResult<SimpleParserValue> CanMatch(ILexer<SimpleParserValue>* lexer, unsigned tokenOffset) override;
public:
MenuMatcherScriptInt();
};

View File

@ -0,0 +1,29 @@
#include "MenuMatcherScriptNumeric.h"
MenuMatcherScriptNumeric::MenuMatcherScriptNumeric()
= default;
MatcherResult<SimpleParserValue> MenuMatcherScriptNumeric::CanMatch(ILexer<SimpleParserValue>* lexer, const unsigned tokenOffset)
{
const auto& token = lexer->GetToken(tokenOffset);
if (token.m_type == SimpleParserValueType::FLOATING_POINT || token.m_type == SimpleParserValueType::INTEGER)
return MatcherResult<SimpleParserValue>::Match(1);
if (token.m_type != SimpleParserValueType::STRING)
return MatcherResult<SimpleParserValue>::NoMatch();
const auto& stringValue = token.StringValue();
if (stringValue.empty())
return MatcherResult<SimpleParserValue>::NoMatch();
char* endPtr;
// The return result does not matter here
const auto _ = strtod(&stringValue[0], &endPtr);
if(endPtr != &stringValue[stringValue.size()])
return MatcherResult<SimpleParserValue>::NoMatch();
return MatcherResult<SimpleParserValue>::Match(1);
}

View File

@ -0,0 +1,13 @@
#pragma once
#include "Parsing/Simple/SimpleParserValue.h"
#include "Parsing/Matcher/AbstractMatcher.h"
class MenuMatcherScriptNumeric final : public AbstractMatcher<SimpleParserValue>
{
protected:
MatcherResult<SimpleParserValue> CanMatch(ILexer<SimpleParserValue>* lexer, unsigned tokenOffset) override;
public:
MenuMatcherScriptNumeric();
};