Make SimpleLexer be able to be initialized via constructor and not only via inheritence

This commit is contained in:
Jan 2021-10-23 15:16:09 +02:00
parent c8214f769b
commit 56c35cb030
5 changed files with 23 additions and 48 deletions

View File

@ -1,9 +0,0 @@
#include "LocalizeFileLexer.h"
LocalizeFileLexer::LocalizeFileLexer(IParserLineStream* stream)
: SimpleLexer(stream)
{
SetShouldEmitNewLineTokens(true);
SetShouldReadNumbers(false);
SetShouldReadStrings(true);
}

View File

@ -1,9 +0,0 @@
#pragma once
#include "Parsing/Simple/SimpleLexer.h"
class LocalizeFileLexer final : public SimpleLexer
{
public:
explicit LocalizeFileLexer(IParserLineStream* stream);
};

View File

@ -1,6 +1,5 @@
#include "LocalizeFileReader.h" #include "LocalizeFileReader.h"
#include "LocalizeFileLexer.h"
#include "LocalizeFileParser.h" #include "LocalizeFileParser.h"
#include "Parsing/Impl/CommentRemovingStreamProxy.h" #include "Parsing/Impl/CommentRemovingStreamProxy.h"
#include "Parsing/Impl/ParserInputStream.h" #include "Parsing/Impl/ParserInputStream.h"
@ -30,7 +29,7 @@ void LocalizeFileReader::SetupStreamProxies()
std::vector<LocalizeFileEntry> LocalizeFileReader::ReadLocalizeFile() std::vector<LocalizeFileEntry> LocalizeFileReader::ReadLocalizeFile()
{ {
const auto lexer = std::make_unique<LocalizeFileLexer>(m_stream); const auto lexer = std::make_unique<SimpleLexer>(m_stream, SimpleLexer::Config{true, true, false});
const auto parser = std::make_unique<LocalizeFileParser>(lexer.get(), m_language); const auto parser = std::make_unique<LocalizeFileParser>(lexer.get(), m_language);
if (parser->Parse()) if (parser->Parse())

View File

@ -2,33 +2,23 @@
SimpleLexer::SimpleLexer(IParserLineStream* stream) SimpleLexer::SimpleLexer(IParserLineStream* stream)
: AbstractLexer(stream), : AbstractLexer(stream),
m_emit_new_line_tokens(false), m_config{false, true, true},
m_read_strings(true),
m_read_numbers(true),
m_last_line(1) m_last_line(1)
{ {
} }
void SimpleLexer::SetShouldEmitNewLineTokens(const bool value) SimpleLexer::SimpleLexer(IParserLineStream* stream, Config config)
: AbstractLexer(stream),
m_config(config),
m_last_line(1)
{ {
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() SimpleParserValue SimpleLexer::GetNextToken()
{ {
auto c = PeekChar(); auto c = PeekChar();
const auto nextCharPos = GetNextCharacterPos(); const auto nextCharPos = GetNextCharacterPos();
if (m_emit_new_line_tokens && nextCharPos.m_line > m_last_line) if (m_config.m_emit_new_line_tokens && nextCharPos.m_line > m_last_line)
{ {
m_last_line++; m_last_line++;
return SimpleParserValue::NewLine(GetPreviousCharacterPos()); return SimpleParserValue::NewLine(GetPreviousCharacterPos());
@ -36,7 +26,7 @@ SimpleParserValue SimpleLexer::GetNextToken()
while (isspace(c)) while (isspace(c))
{ {
if (m_emit_new_line_tokens && c == '\n') if (m_config.m_emit_new_line_tokens && c == '\n')
return SimpleParserValue::NewLine(GetPreviousCharacterPos()); return SimpleParserValue::NewLine(GetPreviousCharacterPos());
NextChar(); NextChar();
@ -44,7 +34,7 @@ SimpleParserValue SimpleLexer::GetNextToken()
} }
const auto pos = GetNextCharacterPos(); const auto pos = GetNextCharacterPos();
if (m_emit_new_line_tokens && pos.m_line > m_last_line) if (m_config.m_emit_new_line_tokens && pos.m_line > m_last_line)
{ {
m_last_line++; m_last_line++;
return SimpleParserValue::NewLine(GetPreviousCharacterPos()); return SimpleParserValue::NewLine(GetPreviousCharacterPos());
@ -55,10 +45,10 @@ SimpleParserValue SimpleLexer::GetNextToken()
if (c == EOF) if (c == EOF)
return SimpleParserValue::EndOfFile(TokenPos()); return SimpleParserValue::EndOfFile(TokenPos());
if (m_read_strings && c == '\"') if (m_config.m_read_strings && c == '\"')
return SimpleParserValue::String(GetPreviousCharacterPos(), new std::string(ReadString())); return SimpleParserValue::String(GetPreviousCharacterPos(), new std::string(ReadString()));
if (m_read_numbers && isdigit(c)) if (m_config.m_read_numbers && isdigit(c))
{ {
bool isFloatingPointValue; bool isFloatingPointValue;
double doubleValue; double doubleValue;

View File

@ -5,18 +5,22 @@
class SimpleLexer : public AbstractLexer<SimpleParserValue> class SimpleLexer : public AbstractLexer<SimpleParserValue>
{ {
public:
class Config
{
public:
bool m_emit_new_line_tokens; bool m_emit_new_line_tokens;
bool m_read_strings; bool m_read_strings;
bool m_read_numbers; bool m_read_numbers;
};
Config m_config;
int m_last_line; int m_last_line;
protected: protected:
SimpleParserValue GetNextToken() override; SimpleParserValue GetNextToken() override;
void SetShouldEmitNewLineTokens(bool value);
void SetShouldReadStrings(bool value);
void SetShouldReadNumbers(bool value);
public: public:
explicit SimpleLexer(IParserLineStream* stream); explicit SimpleLexer(IParserLineStream* stream);
SimpleLexer(IParserLineStream* stream, Config config);
}; };