diff --git a/src/Parser/Parsing/Simple/Matcher/SimpleMatcherFactory.cpp b/src/Parser/Parsing/Simple/Matcher/SimpleMatcherFactory.cpp index 85c9c9b6..73a86094 100644 --- a/src/Parser/Parsing/Simple/Matcher/SimpleMatcherFactory.cpp +++ b/src/Parser/Parsing/Simple/Matcher/SimpleMatcherFactory.cpp @@ -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 SimpleMatcherFactory::Keyword(std::stri return MatcherFactoryWrapper(std::make_unique(std::move(value))); } +MatcherFactoryWrapper SimpleMatcherFactory::KeywordIgnoreCase(std::string value) const +{ + return MatcherFactoryWrapper(std::make_unique(std::move(value))); +} + MatcherFactoryWrapper SimpleMatcherFactory::KeywordPrefix(std::string value) const { return MatcherFactoryWrapper(std::make_unique(std::move(value))); diff --git a/src/Parser/Parsing/Simple/Matcher/SimpleMatcherFactory.h b/src/Parser/Parsing/Simple/Matcher/SimpleMatcherFactory.h index 668ee2e1..a0e7159a 100644 --- a/src/Parser/Parsing/Simple/Matcher/SimpleMatcherFactory.h +++ b/src/Parser/Parsing/Simple/Matcher/SimpleMatcherFactory.h @@ -12,6 +12,7 @@ public: _NODISCARD MatcherFactoryWrapper Type(SimpleParserValueType type) const; _NODISCARD MatcherFactoryWrapper Keyword(std::string value) const; + _NODISCARD MatcherFactoryWrapper KeywordIgnoreCase(std::string value) const; _NODISCARD MatcherFactoryWrapper KeywordPrefix(std::string value) const; _NODISCARD MatcherFactoryWrapper Identifier() const; _NODISCARD MatcherFactoryWrapper String() const; diff --git a/src/Parser/Parsing/Simple/Matcher/SimpleMatcherKeywordIgnoreCase.cpp b/src/Parser/Parsing/Simple/Matcher/SimpleMatcherKeywordIgnoreCase.cpp new file mode 100644 index 00000000..48648913 --- /dev/null +++ b/src/Parser/Parsing/Simple/Matcher/SimpleMatcherKeywordIgnoreCase.cpp @@ -0,0 +1,29 @@ +#include "SimpleMatcherKeywordIgnoreCase.h" + +#include + +SimpleMatcherKeywordIgnoreCase::SimpleMatcherKeywordIgnoreCase(std::string value) + : m_value(std::move(value)) +{ + for (auto& c : m_value) + c = static_cast(tolower(c)); +} + +MatcherResult SimpleMatcherKeywordIgnoreCase::CanMatch(ILexer* lexer, const unsigned tokenOffset) +{ + const auto& token = lexer->GetToken(tokenOffset); + + if (token.m_type != SimpleParserValueType::IDENTIFIER) + return MatcherResult::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::Match(1); + + return MatcherResult::NoMatch(); +} diff --git a/src/Parser/Parsing/Simple/Matcher/SimpleMatcherKeywordIgnoreCase.h b/src/Parser/Parsing/Simple/Matcher/SimpleMatcherKeywordIgnoreCase.h new file mode 100644 index 00000000..3b52aca8 --- /dev/null +++ b/src/Parser/Parsing/Simple/Matcher/SimpleMatcherKeywordIgnoreCase.h @@ -0,0 +1,17 @@ +#pragma once + +#include + +#include "Parsing/Simple/SimpleParserValue.h" +#include "Parsing/Matcher/AbstractMatcher.h" + +class SimpleMatcherKeywordIgnoreCase final : public AbstractMatcher +{ + std::string m_value; + +protected: + MatcherResult CanMatch(ILexer* lexer, unsigned tokenOffset) override; + +public: + explicit SimpleMatcherKeywordIgnoreCase(std::string value); +};