From 68bc95fdf13cadd706d38a05bfc8e0d887f70d2a Mon Sep 17 00:00:00 2001 From: Riyyi Date: Fri, 24 Jun 2022 14:51:45 +0200 Subject: [PATCH] Util: Implement new JSON Job class --- src/util/json/lexer.cpp | 16 ++++++++++------ src/util/json/lexer.h | 8 ++++++-- src/util/json/parser.cpp | 15 +++++++++------ src/util/json/parser.h | 7 +++++-- src/util/json/value.cpp | 12 ++++++++---- src/util/json/value.h | 6 +++--- 6 files changed, 41 insertions(+), 23 deletions(-) diff --git a/src/util/json/lexer.cpp b/src/util/json/lexer.cpp index db4f934..af72995 100644 --- a/src/util/json/lexer.cpp +++ b/src/util/json/lexer.cpp @@ -7,12 +7,13 @@ #include #include +#include "util/json/job.h" #include "util/json/lexer.h" namespace Json { -Lexer::Lexer(const std::string& input) - : m_input(input) +Lexer::Lexer(std::shared_ptr job) + : m_job(job) { } @@ -25,11 +26,11 @@ Lexer::~Lexer() void Lexer::analyze() { printf("---------\n"); - printf("Input JSON:\n%s\n", m_input.c_str()); + printf("Input JSON:\n%s\n", m_job->input().c_str()); printf("---------\n"); printf("Lexing:\n"); - while (m_index < m_input.length()) { + while (m_index < m_job->input().length()) { switch (peek()) { case '{': printf("Pushing -> BraceOpen: \"{\"\t%zu[%zu]\n", m_line, m_column); @@ -112,18 +113,21 @@ void Lexer::analyze() m_index++; m_column++; } + + // FIXME: handle case where the file doenst have a trailing newline + m_job->setLineNumbersWidth(std::to_string(m_line - 1).length()); } // ----------------------------------------- char Lexer::peek() { - return m_input[m_index]; + return m_job->input()[m_index]; } char Lexer::peekNext() { - return m_input[m_index + 1]; + return m_job->input()[m_index + 1]; } char Lexer::consume() diff --git a/src/util/json/lexer.h b/src/util/json/lexer.h index c743b76..e773dea 100644 --- a/src/util/json/lexer.h +++ b/src/util/json/lexer.h @@ -11,11 +11,14 @@ // home/rick/code/cpp/manafiles/ https://www.rfc-editor.org/rfc/pdfrfc/rfc8259.txt.pdf #include // size_t +#include // shared_ptr #include #include namespace Json { +class Job; + struct Token { enum class Type { None, @@ -39,7 +42,7 @@ struct Token { // Lexical analyzer class Lexer { public: - Lexer(const std::string& input); + Lexer(std::shared_ptr job); virtual ~Lexer(); void analyze(); @@ -57,7 +60,8 @@ private: bool getNumber(); bool getLiteral(); - std::string m_input; + std::shared_ptr m_job { nullptr }; + size_t m_index { 0 }; size_t m_column { 1 }; size_t m_line { 1 }; diff --git a/src/util/json/parser.cpp b/src/util/json/parser.cpp index 87b3ec5..2acbb69 100644 --- a/src/util/json/parser.cpp +++ b/src/util/json/parser.cpp @@ -6,19 +6,21 @@ #include // size_t #include // uint32_t -#include // printf +#include // printf, sprintf #include +#include // shared_ptr #include // stod #include "util/json/array.h" +#include "util/json/job.h" #include "util/json/lexer.h" #include "util/json/object.h" #include "util/json/parser.h" namespace Json { -Parser::Parser(const std::string& input) - : m_input(input) +Parser::Parser(std::shared_ptr job) + : m_job(job) { } @@ -30,7 +32,7 @@ Parser::~Parser() Value Parser::parse() { - Lexer lexer(m_input); + Lexer lexer(m_job); lexer.analyze(); m_tokens = lexer.tokens(); @@ -73,13 +75,14 @@ Value Parser::parse() case Token::Type::Comma: // Error! // Multiple JSON root elements + m_job->printErrorLine(token, "multiple root elements"); + m_index++; break; default: // Error! + m_index++; break; } - - break; } return result; diff --git a/src/util/json/parser.h b/src/util/json/parser.h index b21734e..db25c33 100644 --- a/src/util/json/parser.h +++ b/src/util/json/parser.h @@ -9,6 +9,7 @@ #include // size_t #include +#include // shared_ptr #include #include #include @@ -19,9 +20,11 @@ namespace Json { +class Job; + class Parser { public: - Parser(const std::string& input); + Parser(std::shared_ptr job); virtual ~Parser(); Value parse(); @@ -35,7 +38,7 @@ private: Value getArray(); Value getObject(); - std::string m_input; + std::shared_ptr m_job { nullptr }; std::vector m_tokens; size_t m_index { 0 }; diff --git a/src/util/json/value.cpp b/src/util/json/value.cpp index 431208c..ca272c6 100644 --- a/src/util/json/value.cpp +++ b/src/util/json/value.cpp @@ -8,10 +8,12 @@ #include // assert #include // uint32_t #include +#include // make_shared, shared_ptr #include #include // move #include "util/json/array.h" +#include "util/json/job.h" #include "util/json/object.h" #include "util/json/stringify.h" #include "util/json/value.h" @@ -102,10 +104,10 @@ Value::Value(const std::initializer_list& values) Value Value::parse(const std::string& input) { - Parser parser(input); + std::shared_ptr job = std::make_shared(input); - Value value; - value = parser.parse(); + Parser parser(job); + Value value = parser.parse(); return value; } @@ -230,7 +232,9 @@ std::istream& operator>>(std::istream& input, Value& value) } inputString.append(buffer, input.gcount()); - Parser parser(inputString); + std::shared_ptr job = std::make_shared(inputString); + + Parser parser(job); value = parser.parse(); return input; diff --git a/src/util/json/value.h b/src/util/json/value.h index 92e90cb..9400771 100644 --- a/src/util/json/value.h +++ b/src/util/json/value.h @@ -46,7 +46,7 @@ public: Value(const Object& value); Value(const std::initializer_list& values); - // ------------------------------------------ + // -------------------------------------- static Value parse(const std::string& input); std::string dump(const uint32_t indent = 0, const char indentCharacter = ' ') const; @@ -54,7 +54,7 @@ public: void emplace_back(Value value); void emplace(const std::string& key, Value value); - // ------------------------------------------ + // -------------------------------------- // Array index operator Value& operator[](size_t index); @@ -62,7 +62,7 @@ public: const Value& operator[](size_t index) const; const Value& operator[](const std::string& key) const; - // ------------------------------------------ + // -------------------------------------- Type type() const { return m_type; }