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:
14
src/Parser/Parsing/Simple/Matcher/SimpleMatcherCharacter.cpp
Normal file
14
src/Parser/Parsing/Simple/Matcher/SimpleMatcherCharacter.cpp
Normal 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();
|
||||
}
|
15
src/Parser/Parsing/Simple/Matcher/SimpleMatcherCharacter.h
Normal file
15
src/Parser/Parsing/Simple/Matcher/SimpleMatcherCharacter.h
Normal 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);
|
||||
};
|
40
src/Parser/Parsing/Simple/Matcher/SimpleMatcherFactory.cpp
Normal file
40
src/Parser/Parsing/Simple/Matcher/SimpleMatcherFactory.cpp
Normal 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));
|
||||
}
|
19
src/Parser/Parsing/Simple/Matcher/SimpleMatcherFactory.h
Normal file
19
src/Parser/Parsing/Simple/Matcher/SimpleMatcherFactory.h
Normal 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;
|
||||
};
|
16
src/Parser/Parsing/Simple/Matcher/SimpleMatcherKeyword.cpp
Normal file
16
src/Parser/Parsing/Simple/Matcher/SimpleMatcherKeyword.cpp
Normal 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();
|
||||
}
|
18
src/Parser/Parsing/Simple/Matcher/SimpleMatcherKeyword.h
Normal file
18
src/Parser/Parsing/Simple/Matcher/SimpleMatcherKeyword.h
Normal 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);
|
||||
};
|
13
src/Parser/Parsing/Simple/Matcher/SimpleMatcherValueType.cpp
Normal file
13
src/Parser/Parsing/Simple/Matcher/SimpleMatcherValueType.cpp
Normal 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();
|
||||
}
|
15
src/Parser/Parsing/Simple/Matcher/SimpleMatcherValueType.h
Normal file
15
src/Parser/Parsing/Simple/Matcher/SimpleMatcherValueType.h
Normal 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);
|
||||
};
|
Reference in New Issue
Block a user