Add ZoneDefinition Lexer

This commit is contained in:
Jan
2021-03-10 14:58:02 +01:00
parent c47ea48b6b
commit 8798779b39
21 changed files with 232 additions and 14 deletions

View File

@ -0,0 +1,14 @@
#include "SimpleMatcherAnyCharacterBesides.h"
SimpleMatcherAnyCharacterBesides::SimpleMatcherAnyCharacterBesides(std::vector<char> chars)
: m_chars(std::move(chars))
{
}
MatcherResult<SimpleParserValue> SimpleMatcherAnyCharacterBesides::CanMatch(ILexer<SimpleParserValue>* lexer, const unsigned tokenOffset)
{
const auto& token = lexer->GetToken(tokenOffset);
return token.m_type == SimpleParserValueType::CHARACTER && std::find(m_chars.begin(), m_chars.end(), token.CharacterValue()) == m_chars.end()
? MatcherResult<SimpleParserValue>::Match(1)
: MatcherResult<SimpleParserValue>::NoMatch();
}

View File

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

View File

@ -1,5 +1,7 @@
#include "SimpleMatcherFactory.h"
#include "SimpleMatcherAnyCharacterBesides.h"
#include "SimpleMatcherCharacter.h"
#include "SimpleMatcherKeyword.h"
#include "SimpleMatcherValueType.h"
@ -38,3 +40,8 @@ MatcherFactoryWrapper<SimpleParserValue> SimpleMatcherFactory::Char(char c) cons
{
return MatcherFactoryWrapper<SimpleParserValue>(std::make_unique<SimpleMatcherCharacter>(c));
}
MatcherFactoryWrapper<SimpleParserValue> SimpleMatcherFactory::AnyCharBesides(std::vector<char> chars) const
{
return MatcherFactoryWrapper<SimpleParserValue>(std::make_unique<SimpleMatcherAnyCharacterBesides>(std::move(chars)));
}

View File

@ -16,4 +16,5 @@ public:
_NODISCARD MatcherFactoryWrapper<SimpleParserValue> Integer() const;
_NODISCARD MatcherFactoryWrapper<SimpleParserValue> FloatingPoint() const;
_NODISCARD MatcherFactoryWrapper<SimpleParserValue> Char(char c) const;
_NODISCARD MatcherFactoryWrapper<SimpleParserValue> AnyCharBesides(std::vector<char> chars) const;
};

View File

@ -1,25 +1,56 @@
#include "SimpleLexer.h"
SimpleLexer::SimpleLexer(IParserLineStream* stream)
: AbstractLexer(stream)
: AbstractLexer(stream),
m_emit_new_line_tokens(false),
m_read_strings(true),
m_last_line(1)
{
}
void SimpleLexer::SetShouldEmitNewLineTokens(const bool value)
{
m_emit_new_line_tokens = value;
}
void SimpleLexer::SetShouldReadStrings(const bool value)
{
m_read_strings = value;
}
void SimpleLexer::SetShouldReadNumbers(const bool value)
{
m_read_numbers = value;
}
SimpleParserValue SimpleLexer::GetNextToken()
{
PeekChar();
const auto nextCharPos = GetNextCharacterPos();
if(m_emit_new_line_tokens && nextCharPos.m_line > m_last_line)
{
m_last_line++;
return SimpleParserValue::NewLine(GetPreviousCharacterPos());
}
auto c = NextChar();
while (isspace(c))
c = NextChar();
{
if (m_emit_new_line_tokens && c == '\n')
return SimpleParserValue::NewLine(GetPreviousCharacterPos());
if(c == EOF)
c = NextChar();
}
if (c == EOF)
return SimpleParserValue::EndOfFile(TokenPos());
if (c == '\"')
if (m_read_strings && c == '\"')
return SimpleParserValue::String(GetPreviousCharacterPos(), new std::string(ReadString()));
const auto pos = GetPreviousCharacterPos();
if (isdigit(c))
if (m_read_numbers && isdigit(c))
{
bool isFloatingPointValue;
double doubleValue;

View File

@ -3,11 +3,20 @@
#include "SimpleParserValue.h"
#include "Parsing/Impl/AbstractLexer.h"
class SimpleLexer final : public AbstractLexer<SimpleParserValue>
class SimpleLexer : public AbstractLexer<SimpleParserValue>
{
bool m_emit_new_line_tokens;
bool m_read_strings;
bool m_read_numbers;
int m_last_line;
protected:
SimpleParserValue GetNextToken() override;
void SetShouldEmitNewLineTokens(bool value);
void SetShouldReadStrings(bool value);
void SetShouldReadNumbers(bool value);
public:
explicit SimpleLexer(IParserLineStream* stream);
};

View File

@ -14,6 +14,12 @@ SimpleParserValue SimpleParserValue::EndOfFile(const TokenPos pos)
return pv;
}
SimpleParserValue SimpleParserValue::NewLine(const TokenPos pos)
{
SimpleParserValue pv(pos, SimpleParserValueType::NEW_LINE);
return pv;
}
SimpleParserValue SimpleParserValue::Character(const TokenPos pos, const char c)
{
SimpleParserValue pv(pos, SimpleParserValueType::CHARACTER);

View File

@ -11,6 +11,7 @@ enum class SimpleParserValueType
// Meta tokens
INVALID,
END_OF_FILE,
NEW_LINE,
// Single character
CHARACTER,
@ -41,6 +42,7 @@ public:
static SimpleParserValue Invalid(TokenPos pos);
static SimpleParserValue EndOfFile(TokenPos pos);
static SimpleParserValue NewLine(TokenPos pos);
static SimpleParserValue Character(TokenPos pos, char c);
static SimpleParserValue Integer(TokenPos pos, int value);
static SimpleParserValue FloatingPoint(TokenPos pos, double value);