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 <string>
#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> 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()

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
#include <cstddef> // size_t
#include <memory> // shared_ptr
#include <string>
#include <vector>
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> job);
virtual ~Lexer();
void analyze();
@ -57,7 +60,8 @@ private:
bool getNumber();
bool getLiteral();
std::string m_input;
std::shared_ptr<Job> m_job { nullptr };
size_t m_index { 0 };
size_t m_column { 1 };
size_t m_line { 1 };

15
src/util/json/parser.cpp

@ -6,19 +6,21 @@
#include <cstddef> // size_t
#include <cstdint> // uint32_t
#include <cstdio> // printf
#include <cstdio> // printf, sprintf
#include <map>
#include <memory> // shared_ptr
#include <string> // 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> 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;

7
src/util/json/parser.h

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

12
src/util/json/value.cpp

@ -8,10 +8,12 @@
#include <cassert> // assert
#include <cstdint> // uint32_t
#include <iostream>
#include <memory> // make_shared, shared_ptr
#include <string>
#include <utility> // 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<Value>& values)
Value Value::parse(const std::string& input)
{
Parser parser(input);
std::shared_ptr<Job> job = std::make_shared<Job>(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> job = std::make_shared<Job>(inputString);
Parser parser(job);
value = parser.parse();
return input;

6
src/util/json/value.h

@ -46,7 +46,7 @@ public:
Value(const Object& value);
Value(const std::initializer_list<Value>& 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; }

Loading…
Cancel
Save