diff --git a/src/util/json/lexer.cpp b/src/util/json/lexer.cpp index 8bfb2a6..a55f859 100644 --- a/src/util/json/lexer.cpp +++ b/src/util/json/lexer.cpp @@ -13,7 +13,8 @@ namespace Json { Lexer::Lexer(Job* job) - : m_job(job) + : GenericLexer(job->input()) + , m_job(job) , m_tokens(job->tokens()) { } @@ -26,7 +27,7 @@ Lexer::~Lexer() void Lexer::analyze() { - while (m_index < m_job->input().length()) { + while (m_index < m_input.length()) { switch (peek()) { case '{': m_tokens->push_back({ Token::Type::BraceOpen, m_line, m_column, "{" }); @@ -100,7 +101,7 @@ void Lexer::analyze() case '\t': break; case '\r': - if (peekNext() == '\n') { // CRLF \r\n + if (peek(1) == '\n') { // CRLF \r\n break; } m_column = -1; @@ -119,41 +120,13 @@ void Lexer::analyze() break; } - increment(); + ignore(); + m_column++; } } // ----------------------------------------- -char Lexer::peek() -{ - return m_job->input()[m_index]; -} - -char Lexer::peekNext() -{ - return m_job->input()[m_index + 1]; -} - -void Lexer::increment() -{ - m_index++; - m_column++; -} - -void Lexer::decrement() -{ - m_index--; - m_column--; -} - -char Lexer::consume() -{ - char character = peek(); - increment(); - return character; -} - bool Lexer::getString() { size_t column = m_column; @@ -166,7 +139,7 @@ bool Lexer::getString() if (!escape && character == '\\') { symbol += '\\'; - increment(); + ignore(); escape = true; continue; } @@ -180,7 +153,7 @@ bool Lexer::getString() } symbol += character; - increment(); + ignore(); if (escape) { escape = false; @@ -221,13 +194,13 @@ bool Lexer::getNumberOrLiteral(Token::Type type) break; } - increment(); + ignore(); } m_tokens->push_back({ type, m_line, column, - m_job->input().substr(index, m_index - index) }); + std::string(m_input.substr(index, m_index - index)) }); - decrement(); + retreat(); return true; } diff --git a/src/util/json/lexer.h b/src/util/json/lexer.h index f33812d..79e49eb 100644 --- a/src/util/json/lexer.h +++ b/src/util/json/lexer.h @@ -11,10 +11,11 @@ // https://www.rfc-editor.org/rfc/pdfrfc/rfc8259.txt.pdf #include // size_t -#include // shared_ptr #include #include +#include "util/genericlexer.h" + namespace Json { class Job; @@ -40,7 +41,7 @@ struct Token { }; // Lexical analyzer -class Lexer { +class Lexer final : public Util::GenericLexer { public: Lexer(Job* job); virtual ~Lexer(); @@ -48,13 +49,6 @@ public: void analyze(); private: - char peek(); - char peekNext(); - - void increment(); - void decrement(); - char consume(); - bool getString(); bool getNumberOrLiteral(Token::Type type); bool getNumber(); @@ -62,7 +56,6 @@ private: Job* m_job { nullptr }; - size_t m_index { 0 }; size_t m_column { 0 }; size_t m_line { 0 };