#pragma once #include #include "Utils/ClassUtils.h" #include "AbstractMatcher.h" #include "MatcherAnd.h" #include "MatcherLabel.h" #include "MatcherLoop.h" #include "MatcherOptional.h" #include "MatcherOr.h" #include "Parsing/IParserValue.h" template class MatcherFactoryWrapper { // TokenType must inherit IParserValue static_assert(std::is_base_of::value); std::unique_ptr> m_matcher; public: typedef typename AbstractMatcher::token_list_t token_list_t; explicit MatcherFactoryWrapper(std::unique_ptr> matcher) : m_matcher(std::move(matcher)) { } MatcherFactoryWrapper& Tag(const int tagId) { m_matcher->SetTag(tagId); return *this; } MatcherFactoryWrapper& Capture(const int captureId) { m_matcher->SetCapture(captureId); return *this; } MatcherFactoryWrapper& NoConsume() { m_matcher->SetConsume(false); return *this; } MatcherFactoryWrapper& Transform(std::function transform) { m_matcher->SetTransform(std::move(transform)); return *this; } std::unique_ptr> Build() { return std::move(m_matcher); } // ReSharper disable once CppNonExplicitConversionOperator operator std::unique_ptr>() { return Build(); } // ReSharper disable once CppNonExplicitConversionOperator operator Movable>>() { return Build(); } }; template class AbstractMatcherFactory { // TokenType must inherit IParserValue static_assert(std::is_base_of::value); const IMatcherForLabelSupplier* m_label_supplier; public: typedef typename AbstractMatcher::token_list_t token_list_t; explicit AbstractMatcherFactory(const IMatcherForLabelSupplier* labelSupplier) : m_label_supplier(labelSupplier) { } _NODISCARD MatcherFactoryWrapper And(std::initializer_list>>> matchers) const { return MatcherFactoryWrapper(std::make_unique>(matchers)); } _NODISCARD MatcherFactoryWrapper Or(std::initializer_list>>> matchers) const { return MatcherFactoryWrapper(std::make_unique>(matchers)); } _NODISCARD MatcherFactoryWrapper Loop(std::unique_ptr> matcher) const { return MatcherFactoryWrapper(std::make_unique>(std::move(matcher))); } _NODISCARD MatcherFactoryWrapper OptionalLoop(std::unique_ptr> matcher) const { return MatcherFactoryWrapper(std::make_unique>(std::make_unique>(std::move(matcher)))); } _NODISCARD MatcherFactoryWrapper Optional(std::unique_ptr> matcher) const { return MatcherFactoryWrapper(std::make_unique>(std::move(matcher))); } _NODISCARD MatcherFactoryWrapper Label(const int label) const { return MatcherFactoryWrapper(std::make_unique>(m_label_supplier, label)); } };