2
0
mirror of https://github.com/Laupetin/OpenAssetTools.git synced 2025-09-01 22:47:26 +00:00

Add Simple Parsing implementations for basic parsers

This commit is contained in:
Jan
2021-03-09 11:04:04 +01:00
parent 8d9080066f
commit 88ff98f334
14 changed files with 503 additions and 0 deletions

View File

@@ -0,0 +1,14 @@
#include "SimpleMatcherCharacter.h"
SimpleMatcherCharacter::SimpleMatcherCharacter(const char c)
: m_char(c)
{
}
MatcherResult<SimpleParserValue> SimpleMatcherCharacter::CanMatch(ILexer<SimpleParserValue>* lexer, const unsigned tokenOffset)
{
const auto& token = lexer->GetToken(tokenOffset);
return token.m_type == SimpleParserValueType::CHARACTER && token.CharacterValue() == m_char
? MatcherResult<SimpleParserValue>::Match(1)
: MatcherResult<SimpleParserValue>::NoMatch();
}

View File

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

View File

@@ -0,0 +1,40 @@
#include "SimpleMatcherFactory.h"
#include "SimpleMatcherCharacter.h"
#include "SimpleMatcherKeyword.h"
#include "SimpleMatcherValueType.h"
SimpleMatcherFactory::SimpleMatcherFactory(const IMatcherForLabelSupplier<SimpleParserValue>* labelSupplier)
: AbstractMatcherFactory(labelSupplier)
{
}
MatcherFactoryWrapper<SimpleParserValue> SimpleMatcherFactory::Type(SimpleParserValueType type) const
{
return MatcherFactoryWrapper<SimpleParserValue>(std::make_unique<SimpleMatcherValueType>(type));
}
MatcherFactoryWrapper<SimpleParserValue> SimpleMatcherFactory::Keyword(std::string value) const
{
return MatcherFactoryWrapper<SimpleParserValue>(std::make_unique<SimpleMatcherKeyword>(std::move(value)));
}
MatcherFactoryWrapper<SimpleParserValue> SimpleMatcherFactory::Identifier() const
{
return MatcherFactoryWrapper<SimpleParserValue>(std::make_unique<SimpleMatcherValueType>(SimpleParserValueType::IDENTIFIER));
}
MatcherFactoryWrapper<SimpleParserValue> SimpleMatcherFactory::Integer() const
{
return MatcherFactoryWrapper<SimpleParserValue>(std::make_unique<SimpleMatcherValueType>(SimpleParserValueType::INTEGER));
}
MatcherFactoryWrapper<SimpleParserValue> SimpleMatcherFactory::FloatingPoint() const
{
return MatcherFactoryWrapper<SimpleParserValue>(std::make_unique<SimpleMatcherValueType>(SimpleParserValueType::FLOATING_POINT));
}
MatcherFactoryWrapper<SimpleParserValue> SimpleMatcherFactory::Char(char c) const
{
return MatcherFactoryWrapper<SimpleParserValue>(std::make_unique<SimpleMatcherCharacter>(c));
}

View File

@@ -0,0 +1,19 @@
#pragma once
#include <string>
#include "Parsing/Simple/SimpleParserValue.h"
#include "Parsing/Matcher/AbstractMatcherFactory.h"
class SimpleMatcherFactory final : public AbstractMatcherFactory<SimpleParserValue>
{
public:
explicit SimpleMatcherFactory(const IMatcherForLabelSupplier<SimpleParserValue>* labelSupplier);
_NODISCARD MatcherFactoryWrapper<SimpleParserValue> Type(SimpleParserValueType type) const;
_NODISCARD MatcherFactoryWrapper<SimpleParserValue> Keyword(std::string value) const;
_NODISCARD MatcherFactoryWrapper<SimpleParserValue> Identifier() const;
_NODISCARD MatcherFactoryWrapper<SimpleParserValue> Integer() const;
_NODISCARD MatcherFactoryWrapper<SimpleParserValue> FloatingPoint() const;
_NODISCARD MatcherFactoryWrapper<SimpleParserValue> Char(char c) const;
};

View File

@@ -0,0 +1,16 @@
#include "SimpleMatcherKeyword.h"
SimpleMatcherKeyword::SimpleMatcherKeyword(std::string value)
: m_value(std::move(value))
{
const std::hash<std::string> hash;
m_hash = hash(m_value);
}
MatcherResult<SimpleParserValue> SimpleMatcherKeyword::CanMatch(ILexer<SimpleParserValue>* lexer, const unsigned tokenOffset)
{
const auto& token = lexer->GetToken(tokenOffset);
return token.m_type == SimpleParserValueType::IDENTIFIER && token.IdentifierHash() == m_hash && token.IdentifierValue() == m_value
? MatcherResult<SimpleParserValue>::Match(1)
: MatcherResult<SimpleParserValue>::NoMatch();
}

View File

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

View File

@@ -0,0 +1,13 @@
#include "SimpleMatcherValueType.h"
SimpleMatcherValueType::SimpleMatcherValueType(const SimpleParserValueType type)
: m_type(type)
{
}
MatcherResult<SimpleParserValue> SimpleMatcherValueType::CanMatch(ILexer<SimpleParserValue>* lexer, const unsigned tokenOffset)
{
return lexer->GetToken(tokenOffset).m_type == m_type
? MatcherResult<SimpleParserValue>::Match(1)
: MatcherResult<SimpleParserValue>::NoMatch();
}

View File

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