Browse Source

Util: Move vector<Token> ownership to Job class

master
Riyyi 3 years ago
parent
commit
8fb1a1a8e9
  1. 20
      src/util/json/job.cpp
  2. 13
      src/util/json/job.h
  3. 21
      src/util/json/lexer.cpp
  4. 8
      src/util/json/lexer.h
  5. 11
      src/util/json/parser.cpp
  6. 7
      src/util/json/parser.h
  7. 12
      src/util/json/value.cpp

20
src/util/json/job.cpp

@ -9,6 +9,9 @@
#include <string> // 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: "

13
src/util/json/job.h

@ -9,11 +9,14 @@
#include <cstddef> // size_t
#include <string>
#include <vector>
#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<Token>* tokens() { return &m_tokens; }
private:
bool m_success { true };
std::string m_input;
size_t m_lineNumbersWidth { 0 };
std::vector<Token> m_tokens;
};
} // namespace Json

21
src/util/json/lexer.cpp

@ -12,8 +12,9 @@
namespace Json {
Lexer::Lexer(std::shared_ptr<Job> 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;
}

8
src/util/json/lexer.h

@ -42,13 +42,11 @@ struct Token {
// Lexical analyzer
class Lexer {
public:
Lexer(std::shared_ptr<Job> job);
Lexer(Job* job);
virtual ~Lexer();
void analyze();
const std::vector<Token>& tokens() const { return m_tokens; }
private:
char peek();
char peekNext();
@ -60,13 +58,13 @@ private:
bool getNumber();
bool getLiteral();
std::shared_ptr<Job> m_job { nullptr };
Job* m_job { nullptr };
size_t m_index { 0 };
size_t m_column { 1 };
size_t m_line { 1 };
std::vector<Token> m_tokens;
std::vector<Token>* m_tokens { nullptr };
};
} // namespace Json

11
src/util/json/parser.cpp

@ -19,8 +19,9 @@
namespace Json {
Parser::Parser(std::shared_ptr<Job> 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()

7
src/util/json/parser.h

@ -24,7 +24,7 @@ class Job;
class Parser {
public:
Parser(std::shared_ptr<Job> job);
Parser(Job* job);
virtual ~Parser();
Value parse();
@ -38,10 +38,11 @@ private:
Value getArray();
Value getObject();
std::shared_ptr<Job> m_job { nullptr };
Job* m_job { nullptr };
std::vector<Token> m_tokens;
size_t m_index { 0 };
std::vector<Token>* m_tokens { nullptr };
};
} // namespace Json

12
src/util/json/value.cpp

@ -104,10 +104,8 @@ Value::Value(const std::initializer_list<Value>& values)
Value Value::parse(const std::string& input)
{
std::shared_ptr<Job> job = std::make_shared<Job>(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> job = std::make_shared<Job>(inputString);
Parser parser(job);
value = parser.parse();
Job job(inputString);
value = job.fire();
return input;
}

Loading…
Cancel
Save