mirror of
https://github.com/Laupetin/OpenAssetTools.git
synced 2025-06-09 22:38:06 -05:00
Matcher and sequence testing stuffs
This commit is contained in:
@ -1,7 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include "CommandsParserValue.h"
|
||||
#include "Parsing/AbstractLexer.h"
|
||||
#include "Parsing/Impl/AbstractLexer.h"
|
||||
|
||||
class CommandsLexer final : public AbstractLexer<CommandsParserValue>
|
||||
{
|
||||
|
@ -3,7 +3,7 @@
|
||||
#include <unordered_map>
|
||||
|
||||
#include "HeaderParserValue.h"
|
||||
#include "Parsing/AbstractLexer.h"
|
||||
#include "Parsing/Impl/AbstractLexer.h"
|
||||
|
||||
class HeaderLexer final : public AbstractLexer<HeaderParserValue>
|
||||
{
|
||||
|
@ -2,7 +2,7 @@
|
||||
|
||||
#include "HeaderLexer.h"
|
||||
#include "HeaderParserState.h"
|
||||
#include "Parsing/AbstractParser.h"
|
||||
#include "Parsing/Impl/AbstractParser.h"
|
||||
#include "Persistence/IDataRepository.h"
|
||||
|
||||
class HeaderParser final : public AbstractParser<HeaderParserValue, HeaderParserState>
|
||||
|
@ -5,7 +5,7 @@ HeaderMatcherCharacter::HeaderMatcherCharacter(const char c)
|
||||
{
|
||||
}
|
||||
|
||||
MatcherResult<HeaderParserValue> HeaderMatcherCharacter::CanMatch(AbstractLexer<HeaderParserValue>* lexer, const unsigned tokenOffset)
|
||||
MatcherResult<HeaderParserValue> HeaderMatcherCharacter::CanMatch(ILexer<HeaderParserValue>* lexer, const unsigned tokenOffset)
|
||||
{
|
||||
const auto& token = lexer->GetToken(tokenOffset);
|
||||
return token.m_type == HeaderParserValueType::CHARACTER && token.CharacterValue() == m_char
|
||||
|
@ -8,7 +8,7 @@ class HeaderMatcherCharacter final : public AbstractMatcher<HeaderParserValue>
|
||||
char m_char;
|
||||
|
||||
protected:
|
||||
MatcherResult<HeaderParserValue> CanMatch(AbstractLexer<HeaderParserValue>* lexer, unsigned tokenOffset) override;
|
||||
MatcherResult<HeaderParserValue> CanMatch(ILexer<HeaderParserValue>* lexer, unsigned tokenOffset) override;
|
||||
|
||||
public:
|
||||
explicit HeaderMatcherCharacter(char c);
|
||||
|
@ -13,6 +13,11 @@ MatcherFactoryWrapper<HeaderParserValue> HeaderMatcherFactory::Type(HeaderParser
|
||||
return MatcherFactoryWrapper<HeaderParserValue>(std::make_unique<HeaderMatcherValueType>(type));
|
||||
}
|
||||
|
||||
MatcherFactoryWrapper<HeaderParserValue> HeaderMatcherFactory::Identifier() const
|
||||
{
|
||||
return MatcherFactoryWrapper<HeaderParserValue>(std::make_unique<HeaderMatcherValueType>(HeaderParserValueType::IDENTIFIER));
|
||||
}
|
||||
|
||||
MatcherFactoryWrapper<HeaderParserValue> HeaderMatcherFactory::Char(char c) const
|
||||
{
|
||||
return MatcherFactoryWrapper<HeaderParserValue>(std::make_unique<HeaderMatcherCharacter>(c));
|
||||
|
@ -9,5 +9,6 @@ public:
|
||||
explicit HeaderMatcherFactory(const IMatcherForLabelSupplier<HeaderParserValue>* labelSupplier);
|
||||
|
||||
_NODISCARD MatcherFactoryWrapper<HeaderParserValue> Type(HeaderParserValueType type) const;
|
||||
_NODISCARD MatcherFactoryWrapper<HeaderParserValue> Identifier() const;
|
||||
_NODISCARD MatcherFactoryWrapper<HeaderParserValue> Char(char c) const;
|
||||
};
|
||||
|
@ -5,7 +5,7 @@ HeaderMatcherValueType::HeaderMatcherValueType(HeaderParserValueType type)
|
||||
{
|
||||
}
|
||||
|
||||
MatcherResult<HeaderParserValue> HeaderMatcherValueType::CanMatch(AbstractLexer<HeaderParserValue>* lexer, const unsigned tokenOffset)
|
||||
MatcherResult<HeaderParserValue> HeaderMatcherValueType::CanMatch(ILexer<HeaderParserValue>* lexer, const unsigned tokenOffset)
|
||||
{
|
||||
return lexer->GetToken(tokenOffset).m_type == m_type
|
||||
? MatcherResult<HeaderParserValue>::Match(1)
|
||||
|
@ -8,7 +8,7 @@ class HeaderMatcherValueType final : public AbstractMatcher<HeaderParserValue>
|
||||
HeaderParserValueType m_type;
|
||||
|
||||
protected:
|
||||
MatcherResult<HeaderParserValue> CanMatch(AbstractLexer<HeaderParserValue>* lexer, unsigned tokenOffset) override;
|
||||
MatcherResult<HeaderParserValue> CanMatch(ILexer<HeaderParserValue>* lexer, unsigned tokenOffset) override;
|
||||
|
||||
public:
|
||||
explicit HeaderMatcherValueType(HeaderParserValueType type);
|
||||
|
@ -13,6 +13,6 @@ SequenceNamespace::SequenceNamespace()
|
||||
});
|
||||
}
|
||||
|
||||
void SequenceNamespace::ProcessMatch(HeaderParserState* state, const SequenceResult<HeaderParserValue>& result) const
|
||||
void SequenceNamespace::ProcessMatch(HeaderParserState* state, SequenceResult<HeaderParserValue>& result) const
|
||||
{
|
||||
}
|
||||
|
@ -1,15 +1,16 @@
|
||||
#pragma once
|
||||
|
||||
#include "Parsing/Header/Impl/HeaderParser.h"
|
||||
#include "Parsing/Sequence/AbstractSequence.h"
|
||||
#include "Parsing/Header/Impl/HeaderParserState.h"
|
||||
#include "Parsing/Header/Impl/HeaderParserValue.h"
|
||||
|
||||
class SequenceNamespace final : public AbstractSequence<HeaderParserValue, HeaderParserState>
|
||||
class SequenceNamespace final : public HeaderParser::sequence_t
|
||||
{
|
||||
static constexpr int CAPTURE_NAME = 0;
|
||||
|
||||
protected:
|
||||
void ProcessMatch(HeaderParserState* state, const SequenceResult<HeaderParserValue>& result) const override;
|
||||
void ProcessMatch(HeaderParserState* state, SequenceResult<HeaderParserValue>& result) const override;
|
||||
|
||||
public:
|
||||
SequenceNamespace();
|
||||
|
@ -1,15 +1,24 @@
|
||||
#pragma once
|
||||
|
||||
#include "Parsing/IParserValue.h"
|
||||
|
||||
template<typename TokenType>
|
||||
class ILexer
|
||||
{
|
||||
public:
|
||||
// TokenType must inherit IParserValue
|
||||
static_assert(std::is_base_of<IParserValue, TokenType>::value);
|
||||
|
||||
public:
|
||||
ILexer() = default;
|
||||
virtual ~ILexer() = default;
|
||||
ILexer(const ILexer& other) = default;
|
||||
ILexer(ILexer&& other) noexcept = default;
|
||||
ILexer& operator=(const ILexer& other) = default;
|
||||
ILexer& operator=(ILexer&& other) noexcept = default;
|
||||
|
||||
|
||||
virtual const TokenType& GetToken(unsigned index) = 0;
|
||||
virtual void PopTokens(int amount) = 0;
|
||||
|
||||
_NODISCARD virtual bool IsEof() = 0;
|
||||
_NODISCARD virtual const TokenPos& GetPos() = 0;
|
||||
};
|
||||
|
14
src/ZoneCodeGeneratorLib/Parsing/IParser.h
Normal file
14
src/ZoneCodeGeneratorLib/Parsing/IParser.h
Normal file
@ -0,0 +1,14 @@
|
||||
#pragma once
|
||||
|
||||
class IParser
|
||||
{
|
||||
public:
|
||||
IParser() = default;
|
||||
virtual ~IParser() = default;
|
||||
IParser(const IParser& other) = default;
|
||||
IParser(IParser&& other) noexcept = default;
|
||||
IParser& operator=(const IParser& other) = default;
|
||||
IParser& operator=(IParser&& other) noexcept = default;
|
||||
|
||||
virtual bool Parse() = 0;
|
||||
};
|
@ -4,13 +4,12 @@
|
||||
#include <deque>
|
||||
|
||||
#include "Utils/ClassUtils.h"
|
||||
#include "ILexer.h"
|
||||
#include "IParserValue.h"
|
||||
#include "IParserLineStream.h"
|
||||
#include "ParsingException.h"
|
||||
#include "Parsing/ILexer.h"
|
||||
#include "Parsing/IParserLineStream.h"
|
||||
#include "Parsing/ParsingException.h"
|
||||
|
||||
template <typename TokenType>
|
||||
class AbstractLexer : public ILexer
|
||||
class AbstractLexer : public ILexer<TokenType>
|
||||
{
|
||||
// TokenType must inherit IParserValue
|
||||
static_assert(std::is_base_of<IParserValue, TokenType>::value);
|
||||
@ -278,7 +277,7 @@ protected:
|
||||
}
|
||||
|
||||
public:
|
||||
const TokenType& GetToken(unsigned index)
|
||||
const TokenType& GetToken(unsigned index) override
|
||||
{
|
||||
assert(index >= 0);
|
||||
while (index >= m_token_cache.size())
|
||||
@ -292,12 +291,12 @@ public:
|
||||
m_token_cache.erase(m_token_cache.begin(), m_token_cache.begin() + amount);
|
||||
}
|
||||
|
||||
_NODISCARD bool IsEof()
|
||||
_NODISCARD bool IsEof() override
|
||||
{
|
||||
return GetToken(0).IsEof();
|
||||
}
|
||||
|
||||
_NODISCARD const TokenPos& GetPos()
|
||||
_NODISCARD const TokenPos& GetPos() override
|
||||
{
|
||||
return GetToken(0).GetPos();
|
||||
}
|
@ -3,11 +3,13 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
#include "AbstractLexer.h"
|
||||
#include "Sequence/AbstractSequence.h"
|
||||
#include "Parsing/IParser.h"
|
||||
#include "Parsing/ILexer.h"
|
||||
#include "Parsing/Sequence/AbstractSequence.h"
|
||||
#include "Parsing/ParsingException.h"
|
||||
|
||||
template <typename TokenType, typename ParserState>
|
||||
class AbstractParser
|
||||
class AbstractParser : public IParser
|
||||
{
|
||||
// TokenType must inherit IParserValue
|
||||
static_assert(std::is_base_of<IParserValue, TokenType>::value);
|
||||
@ -16,10 +18,10 @@ public:
|
||||
typedef AbstractSequence<TokenType, ParserState> sequence_t;
|
||||
|
||||
protected:
|
||||
AbstractLexer<TokenType>* m_lexer;
|
||||
ILexer<TokenType>* m_lexer;
|
||||
std::unique_ptr<ParserState> m_state;
|
||||
|
||||
explicit AbstractParser(AbstractLexer<TokenType>* lexer, std::unique_ptr<ParserState> state)
|
||||
explicit AbstractParser(ILexer<TokenType>* lexer, std::unique_ptr<ParserState> state)
|
||||
: m_lexer(lexer),
|
||||
m_state(std::move(state))
|
||||
{
|
||||
@ -34,7 +36,7 @@ public:
|
||||
AbstractParser& operator=(const AbstractParser& other) = default;
|
||||
AbstractParser& operator=(AbstractParser&& other) noexcept = default;
|
||||
|
||||
bool Parse()
|
||||
bool Parse() override
|
||||
{
|
||||
try
|
||||
{
|
@ -3,7 +3,7 @@
|
||||
#include <functional>
|
||||
|
||||
#include "Parsing/IParserValue.h"
|
||||
#include "Parsing/AbstractLexer.h"
|
||||
#include "Parsing/ILexer.h"
|
||||
#include "Parsing/Matcher/MatcherResult.h"
|
||||
|
||||
template <typename TokenType>
|
||||
@ -19,7 +19,7 @@ private:
|
||||
int m_tag_id;
|
||||
int m_capture_id;
|
||||
bool m_no_consume;
|
||||
std::function<TokenType(std::vector<std::reference_wrapper<const TokenType>>)> m_transform_func;
|
||||
std::function<TokenType(std::vector<std::reference_wrapper<const TokenType>>&)> m_transform_func;
|
||||
|
||||
protected:
|
||||
AbstractMatcher()
|
||||
@ -29,7 +29,7 @@ protected:
|
||||
{
|
||||
}
|
||||
|
||||
virtual MatcherResult<TokenType> CanMatch(AbstractLexer<TokenType>* lexer, unsigned tokenOffset) = 0;
|
||||
virtual MatcherResult<TokenType> CanMatch(ILexer<TokenType>* lexer, unsigned tokenOffset) = 0;
|
||||
|
||||
public:
|
||||
virtual ~AbstractMatcher() = default;
|
||||
@ -53,12 +53,12 @@ public:
|
||||
m_no_consume = !value;
|
||||
}
|
||||
|
||||
void SetTransform(std::function<TokenType(std::vector<std::reference_wrapper<const TokenType>>)> transform)
|
||||
void SetTransform(std::function<TokenType(std::vector<std::reference_wrapper<const TokenType>>&)> transform)
|
||||
{
|
||||
m_transform_func = std::move(transform);
|
||||
}
|
||||
|
||||
MatcherResult<TokenType> Match(AbstractLexer<TokenType>* lexer, const unsigned tokenOffset)
|
||||
MatcherResult<TokenType> Match(ILexer<TokenType>* lexer, const unsigned tokenOffset)
|
||||
{
|
||||
MatcherResult<TokenType> result = CanMatch(lexer, tokenOffset);
|
||||
|
||||
|
@ -43,7 +43,7 @@ public:
|
||||
return *this;
|
||||
}
|
||||
|
||||
MatcherFactoryWrapper<TokenType>& Transform(std::function<TokenType(std::vector<std::reference_wrapper<const TokenType>>)> transform)
|
||||
MatcherFactoryWrapper<TokenType>& Transform(std::function<TokenType(std::vector<std::reference_wrapper<const TokenType>>&)> transform)
|
||||
{
|
||||
m_matcher->SetTransform(std::move(transform));
|
||||
return *this;
|
||||
@ -81,14 +81,14 @@ public:
|
||||
{
|
||||
}
|
||||
|
||||
_NODISCARD MatcherFactoryWrapper<TokenType> And(std::initializer_list<std::unique_ptr<AbstractMatcher<TokenType>>> matchers) const
|
||||
_NODISCARD MatcherFactoryWrapper<TokenType> And(std::initializer_list<Movable<std::unique_ptr<AbstractMatcher<TokenType>>>> matchers) const
|
||||
{
|
||||
return MatcherFactoryWrapper<TokenType>(std::make_unique<MatcherAnd<TokenType>>(std::move(matchers)));
|
||||
return MatcherFactoryWrapper<TokenType>(std::make_unique<MatcherAnd<TokenType>>(matchers));
|
||||
}
|
||||
|
||||
_NODISCARD MatcherFactoryWrapper<TokenType> Or(std::initializer_list<std::unique_ptr<AbstractMatcher<TokenType>>> matchers) const
|
||||
_NODISCARD MatcherFactoryWrapper<TokenType> Or(std::initializer_list<Movable<std::unique_ptr<AbstractMatcher<TokenType>>>> matchers) const
|
||||
{
|
||||
return MatcherFactoryWrapper<TokenType>(std::make_unique<MatcherOr<TokenType>>(std::move(matchers)));
|
||||
return MatcherFactoryWrapper<TokenType>(std::make_unique<MatcherOr<TokenType>>(matchers));
|
||||
}
|
||||
|
||||
_NODISCARD MatcherFactoryWrapper<TokenType> Loop(std::unique_ptr<AbstractMatcher<TokenType>> matcher) const
|
||||
@ -108,6 +108,6 @@ public:
|
||||
|
||||
_NODISCARD MatcherFactoryWrapper<TokenType> Label(const int label) const
|
||||
{
|
||||
return MatcherFactoryWrapper<TokenType>(std::make_unique<MatcherLabel<TokenType>>(label));
|
||||
return MatcherFactoryWrapper<TokenType>(std::make_unique<MatcherLabel<TokenType>>(m_label_supplier, label));
|
||||
}
|
||||
};
|
||||
|
@ -15,7 +15,7 @@ class MatcherAnd final : public AbstractMatcher<TokenType>
|
||||
std::vector<std::unique_ptr<AbstractMatcher<TokenType>>> m_matchers;
|
||||
|
||||
protected:
|
||||
MatcherResult<TokenType> CanMatch(AbstractLexer<TokenType>* lexer, const unsigned tokenOffset) override
|
||||
MatcherResult<TokenType> CanMatch(ILexer<TokenType>* lexer, const unsigned tokenOffset) override
|
||||
{
|
||||
auto matchResult = MatcherResult<TokenType>::Match(0);
|
||||
|
||||
|
@ -31,7 +31,7 @@ class MatcherLabel final : public AbstractMatcher<TokenType>
|
||||
int m_label;
|
||||
|
||||
protected:
|
||||
MatcherResult<TokenType> CanMatch(AbstractLexer<TokenType>* lexer, unsigned tokenOffset) override
|
||||
MatcherResult<TokenType> CanMatch(ILexer<TokenType>* lexer, unsigned tokenOffset) override
|
||||
{
|
||||
AbstractMatcher<TokenType>* matcher = m_supplier->GetMatcherForLabel(m_label);
|
||||
|
||||
|
@ -14,7 +14,7 @@ class MatcherLoop final : public AbstractMatcher<TokenType>
|
||||
std::unique_ptr<AbstractMatcher<TokenType>> m_matcher;
|
||||
|
||||
protected:
|
||||
MatcherResult<TokenType> CanMatch(AbstractLexer<TokenType>* lexer, const unsigned tokenOffset) override
|
||||
MatcherResult<TokenType> CanMatch(ILexer<TokenType>* lexer, const unsigned tokenOffset) override
|
||||
{
|
||||
auto matchResult = MatcherResult<TokenType>::Match(0);
|
||||
auto loopedAtLeastOnce = false;
|
||||
|
@ -1,6 +1,5 @@
|
||||
#pragma once
|
||||
|
||||
#include <iterator>
|
||||
#include <memory>
|
||||
|
||||
#include "Parsing/IParserValue.h"
|
||||
@ -15,7 +14,7 @@ class MatcherOptional final : public AbstractMatcher<TokenType>
|
||||
std::unique_ptr<AbstractMatcher<TokenType>> m_matcher;
|
||||
|
||||
protected:
|
||||
MatcherResult<TokenType> CanMatch(AbstractLexer<TokenType>* lexer, unsigned tokenOffset) override
|
||||
MatcherResult<TokenType> CanMatch(ILexer<TokenType>* lexer, unsigned tokenOffset) override
|
||||
{
|
||||
auto result = m_matcher->Match(lexer, tokenOffset);
|
||||
|
||||
|
@ -15,11 +15,11 @@ class MatcherOr final : public AbstractMatcher<TokenType>
|
||||
std::vector<std::unique_ptr<AbstractMatcher<TokenType>>> m_matchers;
|
||||
|
||||
protected:
|
||||
MatcherResult<TokenType> CanMatch(AbstractLexer<TokenType>* lexer, unsigned tokenOffset) override
|
||||
MatcherResult<TokenType> CanMatch(ILexer<TokenType>* lexer, unsigned tokenOffset) override
|
||||
{
|
||||
for (const auto& matcher : m_matchers)
|
||||
for (const std::unique_ptr<AbstractMatcher<TokenType>>& matcher : m_matchers)
|
||||
{
|
||||
const auto result = matcher->Match(lexer, tokenOffset);
|
||||
MatcherResult<TokenType> result = matcher->Match(lexer, tokenOffset);
|
||||
|
||||
if (!result.m_matches)
|
||||
continue;
|
||||
|
@ -1,6 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <unordered_map>
|
||||
#include <cassert>
|
||||
|
||||
#include "SequenceResult.h"
|
||||
#include "Utils/ClassUtils.h"
|
||||
@ -27,7 +28,7 @@ protected:
|
||||
|
||||
AbstractSequence() = default;
|
||||
|
||||
virtual void ProcessMatch(ParserState* state, const SequenceResult<TokenType>& result) const = 0;
|
||||
virtual void ProcessMatch(ParserState* state, SequenceResult<TokenType>& result) const = 0;
|
||||
|
||||
void AddMatchers(std::initializer_list<Movable<std::unique_ptr<matcher_t>>> matchers)
|
||||
{
|
||||
@ -35,7 +36,7 @@ protected:
|
||||
m_entry = std::make_unique<MatcherAnd<TokenType>>(matchers);
|
||||
}
|
||||
|
||||
void AddLabeledMatchers(int label, std::initializer_list<Movable<std::unique_ptr<matcher_t>>> matchers)
|
||||
void AddLabeledMatchers(std::initializer_list<Movable<std::unique_ptr<matcher_t>>> matchers, const int label)
|
||||
{
|
||||
assert(m_matchers.find(label) == m_matchers.end());
|
||||
m_matchers.emplace(label, std::make_unique<MatcherAnd<TokenType>>(matchers));
|
||||
@ -61,7 +62,7 @@ public:
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
_NODISCARD bool MatchSequence(AbstractLexer<TokenType>* lexer, ParserState* state, unsigned& consumedTokenCount) const
|
||||
_NODISCARD bool MatchSequence(ILexer<TokenType>* lexer, ParserState* state, unsigned& consumedTokenCount) const
|
||||
{
|
||||
if (!m_entry)
|
||||
return false;
|
||||
|
@ -5,6 +5,7 @@
|
||||
#include "Utils/ClassUtils.h"
|
||||
#include "Parsing/Matcher/AbstractMatcher.h"
|
||||
#include "Parsing/Matcher/MatcherResult.h"
|
||||
#include "Parsing/ParsingException.h"
|
||||
|
||||
template <typename TokenType>
|
||||
class SequenceResult
|
||||
@ -30,7 +31,7 @@ class SequenceResult
|
||||
unsigned m_tag_offset;
|
||||
|
||||
public:
|
||||
SequenceResult(AbstractLexer<TokenType>* lexer, const MatcherResult<TokenType>& result)
|
||||
SequenceResult(ILexer<TokenType>* lexer, const MatcherResult<TokenType>& result)
|
||||
: m_tags(result.m_tags),
|
||||
m_tag_offset(0)
|
||||
{
|
||||
|
Reference in New Issue
Block a user