Working Json Parsing system

main
anulax1225 ago%!(EXTRA string=1 year)
parent 4c92c8db77
commit 7fb4812d0b
  1. 5
      src/bakatools/file_system/directory.cpp
  2. 1
      src/bakatools/file_system/directory.h
  3. 48
      src/bakatools/json/json_node.cpp
  4. 6
      src/bakatools/json/json_node.h
  5. 40
      src/bakatools/json/json_parser.cpp
  6. 4
      src/bakatools/json/json_parser.h

@ -1,6 +1,11 @@
#include "directory.h" #include "directory.h"
namespace Bk { namespace Bk {
bool Directory::create()
{
return std::filesystem::create_directory(ent.path());
}
std::string Directory::current() std::string Directory::current()
{ {
return std::filesystem::current_path(); return std::filesystem::current_path();

@ -15,6 +15,7 @@ namespace Bk {
virtual ~Directory() {} virtual ~Directory() {}
bool create();
int remove() override; int remove() override;
void copy(std::string path, CopyOption opt = CopyOption::recursive) override; void copy(std::string path, CopyOption opt = CopyOption::recursive) override;

@ -1,10 +1,12 @@
#include "json_node.h" #include "json_node.h"
namespace Bk::Json namespace Bk::Json
{ {
std::string Node::to_string(int indent) { std::string Node::to_string(int indent)
{
std::string space_string = std::string(indent, ' '); std::string space_string = std::string(indent, ' ');
std::string output_string = ""; std::string output_string = "";
switch (type) { switch (type)
{
case Type::STRING: case Type::STRING:
{ {
output_string += '"' + *values.s + '"'; output_string += '"' + *values.s + '"';
@ -27,15 +29,13 @@ namespace Bk::Json
} }
case Type::LIST: case Type::LIST:
{ {
output_string += "\n" + space_string + "[\n";; output_string += space_string + "[\n";
for (int i = 0; i < (*values.list).size() - 1; i++) for (int i = 0; i < (*values.list).size(); i++)
{ {
output_string += get_list()[i]->to_string(indent + 4); output_string += get_list()[i]->to_string(indent + 4);
if (i < (*values.list).size() - 2) if (i < (*values.list).size() - 1)
{ {
output_string += ",\n"; output_string += ",\n";
} else {
break;
} }
} }
output_string += "\n" + space_string + "]"; output_string += "\n" + space_string + "]";
@ -67,6 +67,13 @@ namespace Bk::Json
type = Type::OBJECT; type = Type::OBJECT;
} }
Object* Node::get_p_object()
{
if (type == Type::OBJECT)
return values.object;
throw std::logic_error("Improper return");
}
Object Node::get_object() Object Node::get_object()
{ {
if (type == Type::OBJECT) if (type == Type::OBJECT)
@ -80,6 +87,13 @@ namespace Bk::Json
type = Type::LIST; type = Type::LIST;
} }
List* Node::get_p_list()
{
if (type == Type::LIST)
return values.list;
throw std::logic_error("Improper return");
}
List Node::get_list() List Node::get_list()
{ {
if (type == Type::LIST) if (type == Type::LIST)
@ -93,6 +107,13 @@ namespace Bk::Json
type = Type::STRING; type = Type::STRING;
} }
std::string* Node::get_p_string()
{
if (type == Type::STRING)
return values.s;
throw std::logic_error("Improper return");
}
std::string Node::get_string() std::string Node::get_string()
{ {
if (type == Type::STRING) if (type == Type::STRING)
@ -126,6 +147,19 @@ namespace Bk::Json
throw std::logic_error("Improper return"); throw std::logic_error("Improper return");
} }
void Node::set_int(int value)
{
values.fValue = value;
type = Type::NUMBER;
}
int Node::get_int()
{
if (type == Type::NUMBER)
return (int)values.fValue;
throw std::logic_error("Improper return");
}
void Node::set_null() void Node::set_null()
{ {
type = Type::NULL_TYPE; type = Type::NULL_TYPE;

@ -22,12 +22,15 @@ namespace Bk::Json
NULL_TYPE NULL_TYPE
}; };
void set_object(Object* object); void set_object(Object* object);
Object* get_p_object();
Object get_object(); Object get_object();
void set_list(List* list); void set_list(List* list);
List* get_p_list();
List get_list(); List get_list();
void set_string(std::string* str); void set_string(std::string* str);
std::string* get_p_string();
std::string get_string(); std::string get_string();
void set_bool(bool value); void set_bool(bool value);
@ -36,6 +39,9 @@ namespace Bk::Json
void set_float(float value); void set_float(float value);
float get_float(); float get_float();
void set_int(int value);
int get_int();
void set_null(); void set_null();
bool is_null(); bool is_null();

@ -16,7 +16,7 @@ namespace Bk::Json
Parser::Parser(const char* str, int length) Parser::Parser(const char* str, int length)
{ {
Type::DataStream data; Type::DataStream data;
data.payload = std::vector<char>(str, str + length); data.push<char>(str, length);
data.reverse(); data.reverse();
lexer = Lexer(data); lexer = Lexer(data);
} }
@ -41,36 +41,31 @@ namespace Bk::Json
case TokenType::CURLY_OPEN: case TokenType::CURLY_OPEN:
{ {
std::shared_ptr<Json::Node> parsed_object = parse_object(); std::shared_ptr<Json::Node> parsed_object = parse_object();
if (!root) { root = parsed_object;
root = parsed_object;
}
break; break;
} }
case TokenType::ARRAY_OPEN: { case TokenType::ARRAY_OPEN:
{
std::shared_ptr<Json::Node> parsed_list = parse_list(); std::shared_ptr<Json::Node> parsed_list = parse_list();
if (!root) { root = parsed_list;
root = parsed_list;
}
break; break;
} }
case TokenType::STRING: { case TokenType::STRING:
{
std::shared_ptr<Json::Node> parsed_string = parse_string(); std::shared_ptr<Json::Node> parsed_string = parse_string();
if (!root) { root = parsed_string;
root = parsed_string;
}
break; break;
} }
case TokenType::NUMBER: { case TokenType::NUMBER:
{
std::shared_ptr<Json::Node> parsed_number = parse_number(); std::shared_ptr<Json::Node> parsed_number = parse_number();
if (!root) { root = parsed_number;
root = parsed_number;
}
break; break;
} }
case TokenType::BOOLEAN: { case TokenType::BOOLEAN:
{
std::shared_ptr<Json::Node> parsed_boolean = parse_boolean(); std::shared_ptr<Json::Node> parsed_boolean = parse_boolean();
break; break;
} }
@ -91,7 +86,7 @@ namespace Bk::Json
std::shared_ptr<Json::Node> Parser::parse_list() std::shared_ptr<Json::Node> Parser::parse_list()
{ {
std::shared_ptr<Json::Node> node = std::make_shared<Json::Node>(); std::shared_ptr<Json::Node> node = std::make_shared<Json::Node>();
Json::List *list = new Json::List(); Json::List* list = new Json::List();
bool has_completed = false; bool has_completed = false;
Token next_token; Token next_token;
while (!has_completed) { while (!has_completed) {
@ -104,6 +99,10 @@ namespace Bk::Json
next_token = lexer.get_token(); next_token = lexer.get_token();
if (next_token.type == TokenType::COLON || next_token.type == TokenType::COMMA) if (next_token.type == TokenType::COLON || next_token.type == TokenType::COMMA)
continue; continue;
if (next_token.type == TokenType::ARRAY_CLOSE) {
has_completed = true;
break;
}
std::shared_ptr<Json::Node> node; std::shared_ptr<Json::Node> node;
switch (next_token.type) switch (next_token.type)
{ {
@ -138,9 +137,6 @@ namespace Bk::Json
} }
} }
list->push_back(node); list->push_back(node);
if (next_token.type == TokenType::ARRAY_CLOSE) {
has_completed = true;
}
} }
} }
node->set_list(list); node->set_list(list);

@ -5,8 +5,8 @@
namespace Bk::Json namespace Bk::Json
{ {
class Parser { class Parser
Type::DataStream data; {
std::shared_ptr<Json::Node> root; std::shared_ptr<Json::Node> root;
std::unique_ptr<Json::Node> current; std::unique_ptr<Json::Node> current;
Lexer lexer; Lexer lexer;

Loading…
Cancel
Save