Browse Source

Util: Implement new JSON Job class

master
Riyyi 2 years ago
parent
commit
68bc95fdf1
  1. 16
      src/util/json/lexer.cpp
  2. 8
      src/util/json/lexer.h
  3. 15
      src/util/json/parser.cpp
  4. 7
      src/util/json/parser.h
  5. 12
      src/util/json/value.cpp
  6. 6
      src/util/json/value.h

16
src/util/json/lexer.cpp

@ -7,12 +7,13 @@
#include <cstddef> #include <cstddef>
#include <string> #include <string>
#include "util/json/job.h"
#include "util/json/lexer.h" #include "util/json/lexer.h"
namespace Json { namespace Json {
Lexer::Lexer(const std::string& input) Lexer::Lexer(std::shared_ptr<Job> job)
: m_input(input) : m_job(job)
{ {
} }
@ -25,11 +26,11 @@ Lexer::~Lexer()
void Lexer::analyze() void Lexer::analyze()
{ {
printf("---------\n"); 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("---------\n");
printf("Lexing:\n"); printf("Lexing:\n");
while (m_index < m_input.length()) { while (m_index < m_job->input().length()) {
switch (peek()) { switch (peek()) {
case '{': case '{':
printf("Pushing -> BraceOpen: \"{\"\t%zu[%zu]\n", m_line, m_column); printf("Pushing -> BraceOpen: \"{\"\t%zu[%zu]\n", m_line, m_column);
@ -112,18 +113,21 @@ void Lexer::analyze()
m_index++; m_index++;
m_column++; 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() char Lexer::peek()
{ {
return m_input[m_index]; return m_job->input()[m_index];
} }
char Lexer::peekNext() char Lexer::peekNext()
{ {
return m_input[m_index + 1]; return m_job->input()[m_index + 1];
} }
char Lexer::consume() char Lexer::consume()

8
src/util/json/lexer.h

@ -11,11 +11,14 @@
// home/rick/code/cpp/manafiles/ https://www.rfc-editor.org/rfc/pdfrfc/rfc8259.txt.pdf // home/rick/code/cpp/manafiles/ https://www.rfc-editor.org/rfc/pdfrfc/rfc8259.txt.pdf
#include <cstddef> // size_t #include <cstddef> // size_t
#include <memory> // shared_ptr
#include <string> #include <string>
#include <vector> #include <vector>
namespace Json { namespace Json {
class Job;
struct Token { struct Token {
enum class Type { enum class Type {
None, None,
@ -39,7 +42,7 @@ struct Token {
// Lexical analyzer // Lexical analyzer
class Lexer { class Lexer {
public: public:
Lexer(const std::string& input); Lexer(std::shared_ptr<Job> job);
virtual ~Lexer(); virtual ~Lexer();
void analyze(); void analyze();
@ -57,7 +60,8 @@ private:
bool getNumber(); bool getNumber();
bool getLiteral(); bool getLiteral();
std::string m_input; std::shared_ptr<Job> m_job { nullptr };
size_t m_index { 0 }; size_t m_index { 0 };
size_t m_column { 1 }; size_t m_column { 1 };
size_t m_line { 1 }; size_t m_line { 1 };

15
src/util/json/parser.cpp

@ -6,19 +6,21 @@
#include <cstddef> // size_t #include <cstddef> // size_t
#include <cstdint> // uint32_t #include <cstdint> // uint32_t
#include <cstdio> // printf #include <cstdio> // printf, sprintf
#include <map> #include <map>
#include <memory> // shared_ptr
#include <string> // stod #include <string> // stod
#include "util/json/array.h" #include "util/json/array.h"
#include "util/json/job.h"
#include "util/json/lexer.h" #include "util/json/lexer.h"
#include "util/json/object.h" #include "util/json/object.h"
#include "util/json/parser.h" #include "util/json/parser.h"
namespace Json { namespace Json {
Parser::Parser(const std::string& input) Parser::Parser(std::shared_ptr<Job> job)
: m_input(input) : m_job(job)
{ {
} }
@ -30,7 +32,7 @@ Parser::~Parser()
Value Parser::parse() Value Parser::parse()
{ {
Lexer lexer(m_input); Lexer lexer(m_job);
lexer.analyze(); lexer.analyze();
m_tokens = lexer.tokens(); m_tokens = lexer.tokens();
@ -73,13 +75,14 @@ Value Parser::parse()
case Token::Type::Comma: case Token::Type::Comma:
// Error! // Error!
// Multiple JSON root elements // Multiple JSON root elements
m_job->printErrorLine(token, "multiple root elements");
m_index++;
break; break;
default: default:
// Error! // Error!
m_index++;
break; break;
} }
break;
} }
return result; return result;

7
src/util/json/parser.h

@ -9,6 +9,7 @@
#include <cstddef> // size_t #include <cstddef> // size_t
#include <map> #include <map>
#include <memory> // shared_ptr
#include <string> #include <string>
#include <type_traits> #include <type_traits>
#include <typeinfo> #include <typeinfo>
@ -19,9 +20,11 @@
namespace Json { namespace Json {
class Job;
class Parser { class Parser {
public: public:
Parser(const std::string& input); Parser(std::shared_ptr<Job> job);
virtual ~Parser(); virtual ~Parser();
Value parse(); Value parse();
@ -35,7 +38,7 @@ private:
Value getArray(); Value getArray();
Value getObject(); Value getObject();
std::string m_input; std::shared_ptr<Job> m_job { nullptr };
std::vector<Token> m_tokens; std::vector<Token> m_tokens;
size_t m_index { 0 }; size_t m_index { 0 };

12
src/util/json/value.cpp

@ -8,10 +8,12 @@
#include <cassert> // assert #include <cassert> // assert
#include <cstdint> // uint32_t #include <cstdint> // uint32_t
#include <iostream> #include <iostream>
#include <memory> // make_shared, shared_ptr
#include <string> #include <string>
#include <utility> // move #include <utility> // move
#include "util/json/array.h" #include "util/json/array.h"
#include "util/json/job.h"
#include "util/json/object.h" #include "util/json/object.h"
#include "util/json/stringify.h" #include "util/json/stringify.h"
#include "util/json/value.h" #include "util/json/value.h"
@ -102,10 +104,10 @@ Value::Value(const std::initializer_list<Value>& values)
Value Value::parse(const std::string& input) Value Value::parse(const std::string& input)
{ {
Parser parser(input); std::shared_ptr<Job> job = std::make_shared<Job>(input);
Value value; Parser parser(job);
value = parser.parse(); Value value = parser.parse();
return value; return value;
} }
@ -230,7 +232,9 @@ std::istream& operator>>(std::istream& input, Value& value)
} }
inputString.append(buffer, input.gcount()); inputString.append(buffer, input.gcount());
Parser parser(inputString); std::shared_ptr<Job> job = std::make_shared<Job>(inputString);
Parser parser(job);
value = parser.parse(); value = parser.parse();
return input; return input;

6
src/util/json/value.h

@ -46,7 +46,7 @@ public:
Value(const Object& value); Value(const Object& value);
Value(const std::initializer_list<Value>& values); Value(const std::initializer_list<Value>& values);
// ------------------------------------------ // --------------------------------------
static Value parse(const std::string& input); static Value parse(const std::string& input);
std::string dump(const uint32_t indent = 0, const char indentCharacter = ' ') const; std::string dump(const uint32_t indent = 0, const char indentCharacter = ' ') const;
@ -54,7 +54,7 @@ public:
void emplace_back(Value value); void emplace_back(Value value);
void emplace(const std::string& key, Value value); void emplace(const std::string& key, Value value);
// ------------------------------------------ // --------------------------------------
// Array index operator // Array index operator
Value& operator[](size_t index); Value& operator[](size_t index);
@ -62,7 +62,7 @@ public:
const Value& operator[](size_t index) const; const Value& operator[](size_t index) const;
const Value& operator[](const std::string& key) const; const Value& operator[](const std::string& key) const;
// ------------------------------------------ // --------------------------------------
Type type() const { return m_type; } Type type() const { return m_type; }

Loading…
Cancel
Save