diff --git a/src/util/json/job.cpp b/src/util/json/job.cpp index 0c22e5f..461a00a 100644 --- a/src/util/json/job.cpp +++ b/src/util/json/job.cpp @@ -9,6 +9,9 @@ #include // getline #include "util/json/job.h" +#include "util/json/lexer.h" +#include "util/json/parser.h" +#include "util/json/value.h" namespace Json { @@ -25,8 +28,25 @@ Job::~Job() // ------------------------------------------ +Value Job::fire() +{ + Lexer lexer(this); + lexer.analyze(); + + Parser parser(this); + Value value = parser.parse(); + + if (!m_success) { + return { nullptr }; + } + + return value; +} + void Job::printErrorLine(Token token, const char* message) { + m_success = false; + // Error message std::string errorFormat = "\033[;1m" // Bold "JSON:%zu:%zu: " diff --git a/src/util/json/job.h b/src/util/json/job.h index 42aca97..62e92ba 100644 --- a/src/util/json/job.h +++ b/src/util/json/job.h @@ -9,11 +9,14 @@ #include // size_t #include +#include #include "util/json/lexer.h" namespace Json { +class Value; + class Job { public: Job(const std::string& input); @@ -28,13 +31,21 @@ public: Comment, }; + Value fire(); + void printErrorLine(Token token, const char* message); - const std::string& input() { return m_input; } + bool success() const { return m_success; } + const std::string& input() const { return m_input; } + std::vector* tokens() { return &m_tokens; } private: + bool m_success { true }; + std::string m_input; size_t m_lineNumbersWidth { 0 }; + + std::vector m_tokens; }; } // namespace Json diff --git a/src/util/json/lexer.cpp b/src/util/json/lexer.cpp index f356994..58d7cd6 100644 --- a/src/util/json/lexer.cpp +++ b/src/util/json/lexer.cpp @@ -12,8 +12,9 @@ namespace Json { -Lexer::Lexer(std::shared_ptr job) +Lexer::Lexer(Job* job) : m_job(job) + , m_tokens(job->tokens()) { } @@ -34,27 +35,27 @@ void Lexer::analyze() switch (peek()) { case '{': printf("Pushing -> BraceOpen: \"{\"\t%zu[%zu]\n", m_line, m_column); - m_tokens.push_back({ Token::Type::BraceOpen, m_line, m_column, "" }); + m_tokens->push_back({ Token::Type::BraceOpen, m_line, m_column, "{" }); break; case '}': printf("Pushing -> BraceClose: \"}\"\t%zu[%zu]\n", m_line, m_column); - m_tokens.push_back({ Token::Type::BraceClose, m_line, m_column, "" }); + m_tokens->push_back({ Token::Type::BraceClose, m_line, m_column, "}" }); break; case '[': printf("Pushing -> BracketOpen: \"[\"\t%zu[%zu]\n", m_line, m_column); - m_tokens.push_back({ Token::Type::BracketOpen, m_line, m_column, "" }); + m_tokens->push_back({ Token::Type::BracketOpen, m_line, m_column, "[" }); break; case ']': printf("Pushing -> BracketClose: \"]\"\t%zu[%zu]\n", m_line, m_column); - m_tokens.push_back({ Token::Type::BracketClose, m_line, m_column, "" }); + m_tokens->push_back({ Token::Type::BracketClose, m_line, m_column, "]" }); break; case ':': printf("Pushing -> Colon: \":\"\t%zu[%zu]\n", m_line, m_column); - m_tokens.push_back({ Token::Type::Colon, m_line, m_column, "" }); + m_tokens->push_back({ Token::Type::Colon, m_line, m_column, ":" }); break; case ',': printf("Pushing -> Comma: \",\"\t%zu[%zu]\n", m_line, m_column); - m_tokens.push_back({ Token::Type::Comma, m_line, m_column, "" }); + m_tokens->push_back({ Token::Type::Comma, m_line, m_column, "," }); break; case '"': if (!getString()) { @@ -168,7 +169,7 @@ bool Lexer::getString() } printf("Pushing -> String: \"%s\"\t%zu[%zu]\n", symbol.c_str(), m_line, column); - m_tokens.push_back({ Token::Type::String, m_line, column, symbol }); + m_tokens->push_back({ Token::Type::String, m_line, column, symbol }); return true; } @@ -211,7 +212,7 @@ bool Lexer::getNumber() m_column--; printf("Pushing -> Number: \"%s\"\t%zu[%zu]\n", symbol.c_str(), m_line, column); - m_tokens.push_back({ Token::Type::Number, m_line, column, symbol }); + m_tokens->push_back({ Token::Type::Number, m_line, column, symbol }); return true; } @@ -247,7 +248,7 @@ bool Lexer::getLiteral() } printf("Pushing -> Literal: \"%s\"\t%zu[%zu]\n", symbol.c_str(), m_line, column); - m_tokens.push_back({ Token::Type::Literal, m_line, column, symbol }); + m_tokens->push_back({ Token::Type::Literal, m_line, column, symbol }); return true; } diff --git a/src/util/json/lexer.h b/src/util/json/lexer.h index e773dea..b66d3f1 100644 --- a/src/util/json/lexer.h +++ b/src/util/json/lexer.h @@ -42,13 +42,11 @@ struct Token { // Lexical analyzer class Lexer { public: - Lexer(std::shared_ptr job); + Lexer(Job* job); virtual ~Lexer(); void analyze(); - const std::vector& tokens() const { return m_tokens; } - private: char peek(); char peekNext(); @@ -60,13 +58,13 @@ private: bool getNumber(); bool getLiteral(); - std::shared_ptr m_job { nullptr }; + Job* m_job { nullptr }; size_t m_index { 0 }; size_t m_column { 1 }; size_t m_line { 1 }; - std::vector m_tokens; + std::vector* m_tokens { nullptr }; }; } // namespace Json diff --git a/src/util/json/parser.cpp b/src/util/json/parser.cpp index 2acbb69..a392c74 100644 --- a/src/util/json/parser.cpp +++ b/src/util/json/parser.cpp @@ -19,8 +19,9 @@ namespace Json { -Parser::Parser(std::shared_ptr job) +Parser::Parser(Job* job) : m_job(job) + , m_tokens(m_job->tokens()) { } @@ -32,17 +33,13 @@ Parser::~Parser() Value Parser::parse() { - Lexer lexer(m_job); - lexer.analyze(); - m_tokens = lexer.tokens(); - printf("---------\n"); printf("Parsing:\n"); Value result; Token token; - while (m_index < m_tokens.size()) { + while (m_index < m_tokens->size()) { token = peek(); switch (token.type) { @@ -92,7 +89,7 @@ Value Parser::parse() Token Parser::peek() { - return m_tokens[m_index]; + return (*m_tokens)[m_index]; } Token Parser::consume() diff --git a/src/util/json/parser.h b/src/util/json/parser.h index db25c33..7f247cd 100644 --- a/src/util/json/parser.h +++ b/src/util/json/parser.h @@ -24,7 +24,7 @@ class Job; class Parser { public: - Parser(std::shared_ptr job); + Parser(Job* job); virtual ~Parser(); Value parse(); @@ -38,10 +38,11 @@ private: Value getArray(); Value getObject(); - std::shared_ptr m_job { nullptr }; + Job* m_job { nullptr }; - std::vector m_tokens; size_t m_index { 0 }; + + std::vector* m_tokens { nullptr }; }; } // namespace Json diff --git a/src/util/json/value.cpp b/src/util/json/value.cpp index ca272c6..4822794 100644 --- a/src/util/json/value.cpp +++ b/src/util/json/value.cpp @@ -104,10 +104,8 @@ Value::Value(const std::initializer_list& values) Value Value::parse(const std::string& input) { - std::shared_ptr job = std::make_shared(input); - - Parser parser(job); - Value value = parser.parse(); + Job job(input); + Value value = job.fire(); return value; } @@ -232,10 +230,8 @@ std::istream& operator>>(std::istream& input, Value& value) } inputString.append(buffer, input.gcount()); - std::shared_ptr job = std::make_shared(inputString); - - Parser parser(job); - value = parser.parse(); + Job job(inputString); + value = job.fire(); return input; }