Util: Move vector<Token> ownership to Job class
This commit is contained in:
@@ -9,6 +9,9 @@
|
|||||||
#include <string> // getline
|
#include <string> // getline
|
||||||
|
|
||||||
#include "util/json/job.h"
|
#include "util/json/job.h"
|
||||||
|
#include "util/json/lexer.h"
|
||||||
|
#include "util/json/parser.h"
|
||||||
|
#include "util/json/value.h"
|
||||||
|
|
||||||
namespace Json {
|
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)
|
void Job::printErrorLine(Token token, const char* message)
|
||||||
{
|
{
|
||||||
|
m_success = false;
|
||||||
|
|
||||||
// Error message
|
// Error message
|
||||||
std::string errorFormat = "\033[;1m" // Bold
|
std::string errorFormat = "\033[;1m" // Bold
|
||||||
"JSON:%zu:%zu: "
|
"JSON:%zu:%zu: "
|
||||||
|
|||||||
+12
-1
@@ -9,11 +9,14 @@
|
|||||||
|
|
||||||
#include <cstddef> // size_t
|
#include <cstddef> // size_t
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
#include "util/json/lexer.h"
|
#include "util/json/lexer.h"
|
||||||
|
|
||||||
namespace Json {
|
namespace Json {
|
||||||
|
|
||||||
|
class Value;
|
||||||
|
|
||||||
class Job {
|
class Job {
|
||||||
public:
|
public:
|
||||||
Job(const std::string& input);
|
Job(const std::string& input);
|
||||||
@@ -28,13 +31,21 @@ public:
|
|||||||
Comment,
|
Comment,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Value fire();
|
||||||
|
|
||||||
void printErrorLine(Token token, const char* message);
|
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:
|
private:
|
||||||
|
bool m_success { true };
|
||||||
|
|
||||||
std::string m_input;
|
std::string m_input;
|
||||||
size_t m_lineNumbersWidth { 0 };
|
size_t m_lineNumbersWidth { 0 };
|
||||||
|
|
||||||
|
std::vector<Token> m_tokens;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace Json
|
} // namespace Json
|
||||||
|
|||||||
+11
-10
@@ -12,8 +12,9 @@
|
|||||||
|
|
||||||
namespace Json {
|
namespace Json {
|
||||||
|
|
||||||
Lexer::Lexer(std::shared_ptr<Job> job)
|
Lexer::Lexer(Job* job)
|
||||||
: m_job(job)
|
: m_job(job)
|
||||||
|
, m_tokens(job->tokens())
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -34,27 +35,27 @@ void Lexer::analyze()
|
|||||||
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);
|
||||||
m_tokens.push_back({ Token::Type::BraceOpen, m_line, m_column, "" });
|
m_tokens->push_back({ Token::Type::BraceOpen, m_line, m_column, "{" });
|
||||||
break;
|
break;
|
||||||
case '}':
|
case '}':
|
||||||
printf("Pushing -> BraceClose: \"}\"\t%zu[%zu]\n", m_line, m_column);
|
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;
|
break;
|
||||||
case '[':
|
case '[':
|
||||||
printf("Pushing -> BracketOpen: \"[\"\t%zu[%zu]\n", m_line, m_column);
|
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;
|
break;
|
||||||
case ']':
|
case ']':
|
||||||
printf("Pushing -> BracketClose: \"]\"\t%zu[%zu]\n", m_line, m_column);
|
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;
|
break;
|
||||||
case ':':
|
case ':':
|
||||||
printf("Pushing -> Colon: \":\"\t%zu[%zu]\n", m_line, m_column);
|
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;
|
break;
|
||||||
case ',':
|
case ',':
|
||||||
printf("Pushing -> Comma: \",\"\t%zu[%zu]\n", m_line, m_column);
|
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;
|
break;
|
||||||
case '"':
|
case '"':
|
||||||
if (!getString()) {
|
if (!getString()) {
|
||||||
@@ -168,7 +169,7 @@ bool Lexer::getString()
|
|||||||
}
|
}
|
||||||
|
|
||||||
printf("Pushing -> String: \"%s\"\t%zu[%zu]\n", symbol.c_str(), m_line, column);
|
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;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -211,7 +212,7 @@ bool Lexer::getNumber()
|
|||||||
m_column--;
|
m_column--;
|
||||||
|
|
||||||
printf("Pushing -> Number: \"%s\"\t%zu[%zu]\n", symbol.c_str(), m_line, 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;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -247,7 +248,7 @@ bool Lexer::getLiteral()
|
|||||||
}
|
}
|
||||||
|
|
||||||
printf("Pushing -> Literal: \"%s\"\t%zu[%zu]\n", symbol.c_str(), m_line, column);
|
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;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -42,13 +42,11 @@ struct Token {
|
|||||||
// Lexical analyzer
|
// Lexical analyzer
|
||||||
class Lexer {
|
class Lexer {
|
||||||
public:
|
public:
|
||||||
Lexer(std::shared_ptr<Job> job);
|
Lexer(Job* job);
|
||||||
virtual ~Lexer();
|
virtual ~Lexer();
|
||||||
|
|
||||||
void analyze();
|
void analyze();
|
||||||
|
|
||||||
const std::vector<Token>& tokens() const { return m_tokens; }
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
char peek();
|
char peek();
|
||||||
char peekNext();
|
char peekNext();
|
||||||
@@ -60,13 +58,13 @@ private:
|
|||||||
bool getNumber();
|
bool getNumber();
|
||||||
bool getLiteral();
|
bool getLiteral();
|
||||||
|
|
||||||
std::shared_ptr<Job> m_job { nullptr };
|
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 };
|
||||||
|
|
||||||
std::vector<Token> m_tokens;
|
std::vector<Token>* m_tokens { nullptr };
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace Json
|
} // namespace Json
|
||||||
|
|||||||
@@ -19,8 +19,9 @@
|
|||||||
|
|
||||||
namespace Json {
|
namespace Json {
|
||||||
|
|
||||||
Parser::Parser(std::shared_ptr<Job> job)
|
Parser::Parser(Job* job)
|
||||||
: m_job(job)
|
: m_job(job)
|
||||||
|
, m_tokens(m_job->tokens())
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -32,17 +33,13 @@ Parser::~Parser()
|
|||||||
|
|
||||||
Value Parser::parse()
|
Value Parser::parse()
|
||||||
{
|
{
|
||||||
Lexer lexer(m_job);
|
|
||||||
lexer.analyze();
|
|
||||||
m_tokens = lexer.tokens();
|
|
||||||
|
|
||||||
printf("---------\n");
|
printf("---------\n");
|
||||||
printf("Parsing:\n");
|
printf("Parsing:\n");
|
||||||
|
|
||||||
Value result;
|
Value result;
|
||||||
|
|
||||||
Token token;
|
Token token;
|
||||||
while (m_index < m_tokens.size()) {
|
while (m_index < m_tokens->size()) {
|
||||||
token = peek();
|
token = peek();
|
||||||
|
|
||||||
switch (token.type) {
|
switch (token.type) {
|
||||||
@@ -92,7 +89,7 @@ Value Parser::parse()
|
|||||||
|
|
||||||
Token Parser::peek()
|
Token Parser::peek()
|
||||||
{
|
{
|
||||||
return m_tokens[m_index];
|
return (*m_tokens)[m_index];
|
||||||
}
|
}
|
||||||
|
|
||||||
Token Parser::consume()
|
Token Parser::consume()
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ class Job;
|
|||||||
|
|
||||||
class Parser {
|
class Parser {
|
||||||
public:
|
public:
|
||||||
Parser(std::shared_ptr<Job> job);
|
Parser(Job* job);
|
||||||
virtual ~Parser();
|
virtual ~Parser();
|
||||||
|
|
||||||
Value parse();
|
Value parse();
|
||||||
@@ -38,10 +38,11 @@ private:
|
|||||||
Value getArray();
|
Value getArray();
|
||||||
Value getObject();
|
Value getObject();
|
||||||
|
|
||||||
std::shared_ptr<Job> m_job { nullptr };
|
Job* m_job { nullptr };
|
||||||
|
|
||||||
std::vector<Token> m_tokens;
|
|
||||||
size_t m_index { 0 };
|
size_t m_index { 0 };
|
||||||
|
|
||||||
|
std::vector<Token>* m_tokens { nullptr };
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace Json
|
} // namespace Json
|
||||||
|
|||||||
@@ -104,10 +104,8 @@ Value::Value(const std::initializer_list<Value>& values)
|
|||||||
|
|
||||||
Value Value::parse(const std::string& input)
|
Value Value::parse(const std::string& input)
|
||||||
{
|
{
|
||||||
std::shared_ptr<Job> job = std::make_shared<Job>(input);
|
Job job(input);
|
||||||
|
Value value = job.fire();
|
||||||
Parser parser(job);
|
|
||||||
Value value = parser.parse();
|
|
||||||
|
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
@@ -232,10 +230,8 @@ std::istream& operator>>(std::istream& input, Value& value)
|
|||||||
}
|
}
|
||||||
inputString.append(buffer, input.gcount());
|
inputString.append(buffer, input.gcount());
|
||||||
|
|
||||||
std::shared_ptr<Job> job = std::make_shared<Job>(inputString);
|
Job job(inputString);
|
||||||
|
value = job.fire();
|
||||||
Parser parser(job);
|
|
||||||
value = parser.parse();
|
|
||||||
|
|
||||||
return input;
|
return input;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user