Add IgnoreCase Keyword matcher for simple parser

This commit is contained in:
Jan 2021-10-31 15:22:03 +01:00
parent dcffa3d14d
commit 037e13b874
4 changed files with 53 additions and 0 deletions

View File

@ -3,6 +3,7 @@
#include "SimpleMatcherAnyCharacterBesides.h"
#include "SimpleMatcherCharacter.h"
#include "SimpleMatcherKeyword.h"
#include "SimpleMatcherKeywordIgnoreCase.h"
#include "SimpleMatcherKeywordPrefix.h"
#include "SimpleMatcherValueType.h"
@ -21,6 +22,11 @@ MatcherFactoryWrapper<SimpleParserValue> SimpleMatcherFactory::Keyword(std::stri
return MatcherFactoryWrapper<SimpleParserValue>(std::make_unique<SimpleMatcherKeyword>(std::move(value)));
}
MatcherFactoryWrapper<SimpleParserValue> SimpleMatcherFactory::KeywordIgnoreCase(std::string value) const
{
return MatcherFactoryWrapper<SimpleParserValue>(std::make_unique<SimpleMatcherKeywordIgnoreCase>(std::move(value)));
}
MatcherFactoryWrapper<SimpleParserValue> SimpleMatcherFactory::KeywordPrefix(std::string value) const
{
return MatcherFactoryWrapper<SimpleParserValue>(std::make_unique<SimpleMatcherKeywordPrefix>(std::move(value)));

View File

@ -12,6 +12,7 @@ public:
_NODISCARD MatcherFactoryWrapper<SimpleParserValue> Type(SimpleParserValueType type) const;
_NODISCARD MatcherFactoryWrapper<SimpleParserValue> Keyword(std::string value) const;
_NODISCARD MatcherFactoryWrapper<SimpleParserValue> KeywordIgnoreCase(std::string value) const;
_NODISCARD MatcherFactoryWrapper<SimpleParserValue> KeywordPrefix(std::string value) const;
_NODISCARD MatcherFactoryWrapper<SimpleParserValue> Identifier() const;
_NODISCARD MatcherFactoryWrapper<SimpleParserValue> String() const;

View File

@ -0,0 +1,29 @@
#include "SimpleMatcherKeywordIgnoreCase.h"
#include <algorithm>
SimpleMatcherKeywordIgnoreCase::SimpleMatcherKeywordIgnoreCase(std::string value)
: m_value(std::move(value))
{
for (auto& c : m_value)
c = static_cast<char>(tolower(c));
}
MatcherResult<SimpleParserValue> SimpleMatcherKeywordIgnoreCase::CanMatch(ILexer<SimpleParserValue>* lexer, const unsigned tokenOffset)
{
const auto& token = lexer->GetToken(tokenOffset);
if (token.m_type != SimpleParserValueType::IDENTIFIER)
return MatcherResult<SimpleParserValue>::NoMatch();
const auto& identifierValue = token.IdentifierValue();
const auto isEqual = std::equal(identifierValue.begin(), identifierValue.end(), m_value.begin(), m_value.end(), [](const char a, const char b)
{
return a == tolower(b);
});
if (isEqual)
return MatcherResult<SimpleParserValue>::Match(1);
return MatcherResult<SimpleParserValue>::NoMatch();
}

View File

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