json, toml parsers/writers update

This commit is contained in:
MihailRis 2023-12-03 00:41:06 +03:00
parent afa05ca99d
commit 8588e582b6
5 changed files with 237 additions and 64 deletions

View File

@ -181,23 +181,27 @@ int64_t BasicParser::parseSimpleInt(int base) {
return value; return value;
} }
double BasicParser::parseNumber(int sign) { bool BasicParser::parseNumber(int sign, number_u& out) {
char c = peek(); char c = peek();
int base = 10; int base = 10;
if (c == '0' && pos + 1 < source.length() && if (c == '0' && pos + 1 < source.length() &&
(base = is_box(source[pos+1])) != 10) { (base = is_box(source[pos+1])) != 10) {
pos += 2; pos += 2;
return parseSimpleInt(base); out.ival = parseSimpleInt(base);
return true;
} else if (c == 'i' && pos + 2 < source.length() && source[pos+1] == 'n' && source[pos+2] == 'f') { } else if (c == 'i' && pos + 2 < source.length() && source[pos+1] == 'n' && source[pos+2] == 'f') {
pos += 3; pos += 3;
return INFINITY * sign; out.fval = INFINITY * sign;
return false;
} else if (c == 'n' && pos + 2 < source.length() && source[pos+1] == 'a' && source[pos+2] == 'n') { } else if (c == 'n' && pos + 2 < source.length() && source[pos+1] == 'a' && source[pos+2] == 'n') {
pos += 3; pos += 3;
return NAN * sign; out.fval = NAN * sign;
return false;
} }
int64_t value = parseSimpleInt(base); int64_t value = parseSimpleInt(base);
if (!hasNext()) { if (!hasNext()) {
return value * sign; out.ival = value * sign;
return true;
} }
c = source[pos]; c = source[pos];
if (c == 'e' || c == 'E') { if (c == 'e' || c == 'E') {
@ -209,7 +213,8 @@ double BasicParser::parseNumber(int sign) {
} else if (peek() == '+'){ } else if (peek() == '+'){
pos++; pos++;
} }
return sign * value * power(10.0, s * parseSimpleInt(10)); out.fval = sign * value * power(10.0, s * parseSimpleInt(10));
return false;
} }
if (c == '.') { if (c == '.') {
pos++; pos++;
@ -235,11 +240,14 @@ double BasicParser::parseNumber(int sign) {
} else if (peek() == '+'){ } else if (peek() == '+'){
pos++; pos++;
} }
return sign * dvalue * power(10.0, s * parseSimpleInt(10)); out.fval = sign * dvalue * power(10.0, s * parseSimpleInt(10));
return false;
} }
return dvalue; out.fval = dvalue;
return false;
} }
return value; out.ival = value;
return true;
} }
string BasicParser::parseString(char quote) { string BasicParser::parseString(char quote) {

View File

@ -5,6 +5,11 @@
#include <stdexcept> #include <stdexcept>
#include "../typedefs.h" #include "../typedefs.h"
union number_u {
double fval;
int64_t ival;
};
inline int is_box(int c) { inline int is_box(int c) {
switch (c) { switch (c) {
case 'B': case 'B':
@ -73,7 +78,7 @@ protected:
std::string parseName(); std::string parseName();
int64_t parseSimpleInt(int base); int64_t parseSimpleInt(int base);
double parseNumber(int sign); bool parseNumber(int sign, number_u& out);
std::string parseString(char chr); std::string parseString(char chr);
parsing_error error(std::string message); parsing_error error(std::string message);

View File

@ -15,7 +15,7 @@ using std::unordered_map;
using std::stringstream; using std::stringstream;
using std::make_pair; using std::make_pair;
inline void newline(std::stringstream& ss, bool nice, uint indent, const std::string indentstr) { inline void newline(stringstream& ss, bool nice, uint indent, const string indentstr) {
if (nice) { if (nice) {
ss << "\n"; ss << "\n";
for (uint i = 0; i < indent; i++) { for (uint i = 0; i < indent; i++) {
@ -26,10 +26,23 @@ inline void newline(std::stringstream& ss, bool nice, uint indent, const std::st
} }
} }
void stringify(Value* value, stringstream& ss, int indent, string indentstr, bool nice); void stringify(Value* value,
void stringifyObj(JObject* obj, stringstream& ss, int indent, string indentstr, bool nice); stringstream& ss,
int indent,
string indentstr,
bool nice);
void stringify(Value* value, stringstream& ss, int indent, string indentstr, bool nice) { void stringifyObj(JObject* obj,
stringstream& ss,
int indent,
string indentstr,
bool nice);
void stringify(Value* value,
stringstream& ss,
int indent,
string indentstr,
bool nice) {
if (value->type == valtype::object) { if (value->type == valtype::object) {
stringifyObj(value->value.obj, ss, indent, indentstr, nice); stringifyObj(value->value.obj, ss, indent, indentstr, nice);
} }
@ -57,7 +70,9 @@ void stringify(Value* value, stringstream& ss, int indent, string indentstr, boo
} else if (value->type == valtype::boolean) { } else if (value->type == valtype::boolean) {
ss << (value->value.boolean ? "true" : "false"); ss << (value->value.boolean ? "true" : "false");
} else if (value->type == valtype::number) { } else if (value->type == valtype::number) {
ss << value->value.num; ss << value->value.decimal;
} else if (value->type == valtype::integer) {
ss << value->value.integer;
} else if (value->type == valtype::string) { } else if (value->type == valtype::string) {
ss << escape_string(*value->value.str); ss << escape_string(*value->value.str);
} }
@ -103,11 +118,39 @@ JArray::~JArray() {
} }
std::string JArray::str(size_t index) const { std::string JArray::str(size_t index) const {
return *values[index]->value.str; const auto& val = values[index];
switch (val->type) {
case valtype::string: return *val->value.str;
case valtype::boolean: return val->value.boolean ? "true" : "false";
case valtype::number: return std::to_string(val->value.decimal);
case valtype::integer: return std::to_string(val->value.integer);
default:
throw std::runtime_error("type error");
}
} }
double JArray::num(size_t index) const { double JArray::num(size_t index) const {
return values[index]->value.num; const auto& val = values[index];
switch (val->type) {
case valtype::number: return val->value.decimal;
case valtype::integer: return val->value.integer;
case valtype::string: return std::stoll(*val->value.str);
case valtype::boolean: return val->value.boolean;
default:
throw std::runtime_error("type error");
}
}
int64_t JArray::integer(size_t index) const {
const auto& val = values[index];
switch (val->type) {
case valtype::number: return val->value.decimal;
case valtype::integer: return val->value.integer;
case valtype::string: return std::stoll(*val->value.str);
case valtype::boolean: return val->value.boolean;
default:
throw std::runtime_error("type error");
}
} }
JObject* JArray::obj(size_t index) const { JObject* JArray::obj(size_t index) const {
@ -130,25 +173,33 @@ JArray& JArray::put(string value) {
} }
JArray& JArray::put(uint value) { JArray& JArray::put(uint value) {
return put((double)value); return put((int64_t)value);
} }
JArray& JArray::put(int value) { JArray& JArray::put(int value) {
return put((double)value); return put((int64_t)value);
}
JArray& JArray::put(int64_t value) {
valvalue val;
val.integer = value;
values.push_back(new Value(valtype::integer, val));
return *this;
}
JArray& JArray::put(uint64_t value) {
return put((int64_t)value);
} }
JArray& JArray::put(double value) { JArray& JArray::put(double value) {
valvalue val; valvalue val;
val.num = value; val.decimal = value;
values.push_back(new Value(valtype::number, val)); values.push_back(new Value(valtype::number, val));
return *this; return *this;
} }
JArray& JArray::put(float value) { JArray& JArray::put(float value) {
valvalue val; return put((double)value);
val.num = value;
values.push_back(new Value(valtype::number, val));
return *this;
} }
JArray& JArray::put(bool value) { JArray& JArray::put(bool value) {
@ -172,46 +223,96 @@ JArray& JArray::put(JArray* value) {
return *this; return *this;
} }
JArray& JArray::putArray() {
JArray* arr = new JArray();
put(arr);
return *arr;
}
JObject& JArray::putObj() {
JObject* obj = new JObject();
put(obj);
return *obj;
}
JObject::~JObject() { JObject::~JObject() {
for (auto entry : map) { for (auto entry : map) {
delete entry.second; delete entry.second;
} }
} }
void JObject::str(std::string key, std::string& dst) const { void JObject::str(string key, string& dst) const {
auto found = map.find(key); dst = getStr(key, dst);
if (found != map.end())
dst = *found->second->value.str;
} }
void JObject::num(std::string key, double& dst) const { string JObject::getStr(string key, const string& def) const {
auto found = map.find(key); auto found = map.find(key);
if (found != map.end()) if (found == map.end())
dst = found->second->value.num; return def;
auto& val = found->second;
switch (val->type) {
case valtype::string: return *val->value.str;
case valtype::boolean: return val->value.boolean ? "true" : "false";
case valtype::number: return std::to_string(val->value.decimal);
case valtype::integer: return std::to_string(val->value.integer);
default: throw std::runtime_error("type error");
}
} }
void JObject::num(std::string key, ubyte& dst) const { double JObject::getNum(string key, double def) const {
auto found = map.find(key); auto found = map.find(key);
if (found != map.end()) if (found == map.end())
dst = found->second->value.num; return def;
auto& val = found->second;
switch (val->type) {
case valtype::number: return val->value.decimal;
case valtype::integer: return val->value.integer;
case valtype::string: return std::stoull(*val->value.str);
case valtype::boolean: return val->value.boolean;
default: throw std::runtime_error("type error");
}
}
int64_t JObject::getInteger(string key, int64_t def) const {
auto found = map.find(key);
if (found == map.end())
return def;
auto& val = found->second;
switch (val->type) {
case valtype::number: return val->value.decimal;
case valtype::integer: return val->value.integer;
case valtype::string: return std::stoull(*val->value.str);
case valtype::boolean: return val->value.boolean;
default: throw std::runtime_error("type error");
}
}
void JObject::num(string key, double& dst) const {
dst = getNum(key, dst);
} }
void JObject::num(std::string key, float& dst) const { void JObject::num(std::string key, float& dst) const {
auto found = map.find(key); dst = getNum(key, dst);
if (found != map.end()) }
dst = found->second->value.num;
void JObject::num(std::string key, ubyte& dst) const {
dst = getInteger(key, dst);
} }
void JObject::num(std::string key, int& dst) const { void JObject::num(std::string key, int& dst) const {
auto found = map.find(key); dst = getInteger(key, dst);
if (found != map.end()) }
dst = found->second->value.num;
void JObject::num(std::string key, int64_t& dst) const {
dst = getInteger(key, dst);
}
void JObject::num(std::string key, uint64_t& dst) const {
dst = getInteger(key, dst);
} }
void JObject::num(std::string key, uint& dst) const { void JObject::num(std::string key, uint& dst) const {
auto found = map.find(key); dst = getInteger(key, dst);
if (found != map.end())
dst = found->second->value.num;
} }
JObject* JObject::obj(std::string key) const { JObject* JObject::obj(std::string key) const {
@ -235,11 +336,24 @@ void JObject::flag(std::string key, bool& dst) const {
} }
JObject& JObject::put(string key, uint value) { JObject& JObject::put(string key, uint value) {
return put(key, (double)value); return put(key, (int64_t)value);
} }
JObject& JObject::put(string key, int value) { JObject& JObject::put(string key, int value) {
return put(key, (double)value); return put(key, (int64_t)value);
}
JObject& JObject::put(string key, int64_t value) {
auto found = map.find(key);
if (found != map.end()) delete found->second;
valvalue val;
val.integer = value;
map.insert(make_pair(key, new Value(valtype::integer, val)));
return *this;
}
JObject& JObject::put(string key, uint64_t value) {
return put(key, (int64_t)value);
} }
JObject& JObject::put(string key, float value) { JObject& JObject::put(string key, float value) {
@ -250,7 +364,7 @@ JObject& JObject::put(string key, double value) {
auto found = map.find(key); auto found = map.find(key);
if (found != map.end()) delete found->second; if (found != map.end()) delete found->second;
valvalue val; valvalue val;
val.num = value; val.decimal = value;
map.insert(make_pair(key, new Value(valtype::number, val))); map.insert(make_pair(key, new Value(valtype::number, val)));
return *this; return *this;
} }
@ -295,12 +409,18 @@ JObject& JObject::put(string key, bool value){
return *this; return *this;
} }
JArray& JObject::putArray(std::string key) { JArray& JObject::putArray(string key) {
JArray* arr = new JArray(); JArray* arr = new JArray();
put(key, arr); put(key, arr);
return *arr; return *arr;
} }
JObject& JObject::putObj(string key) {
JObject* obj = new JObject();
put(key, obj);
return *obj;
}
bool JObject::has(string key) { bool JObject::has(string key) {
return map.find(key) != map.end(); return map.find(key) != map.end();
} }
@ -386,10 +506,10 @@ Value* Parser::parseValue() {
val.boolean = false; val.boolean = false;
return new Value(valtype::boolean, val); return new Value(valtype::boolean, val);
} else if (literal == "inf") { } else if (literal == "inf") {
val.num = INFINITY; val.decimal = INFINITY;
return new Value(valtype::number, val); return new Value(valtype::number, val);
} else if (literal == "nan") { } else if (literal == "nan") {
val.num = NAN; val.decimal = NAN;
return new Value(valtype::number, val); return new Value(valtype::number, val);
} }
throw error("invalid literal"); throw error("invalid literal");
@ -404,12 +524,28 @@ Value* Parser::parseValue() {
} }
if (next == '-' || next == '+') { if (next == '-' || next == '+') {
pos++; pos++;
val.num = parseNumber(next == '-' ? -1 : 1); number_u num;
return new Value(valtype::number, val); valtype type;
if (parseNumber(next == '-' ? -1 : 1, num)) {
val.integer = num.ival;
type = valtype::integer;
} else {
val.decimal = num.fval;
type = valtype::number;
}
return new Value(type, val);
} }
if (is_digit(next)) { if (is_digit(next)) {
val.num = parseNumber(1); number_u num;
return new Value(valtype::number, val); valtype type;
if (parseNumber(1, num)) {
val.integer = num.ival;
type = valtype::integer;
} else {
val.decimal = num.fval;
type = valtype::number;
}
return new Value(type, val);
} }
if (next == '"' || next == '\'') { if (next == '"' || next == '\'') {
pos++; pos++;
@ -419,11 +555,11 @@ Value* Parser::parseValue() {
throw error("unexpected character '"+string({next})+"'"); throw error("unexpected character '"+string({next})+"'");
} }
JObject* json::parse(std::string filename, std::string source) { JObject* json::parse(string filename, string source) {
Parser parser(filename, source); Parser parser(filename, source);
return parser.parse(); return parser.parse();
} }
JObject* json::parse(std::string source) { JObject* json::parse(string source) {
return parse("<string>", source); return parse("<string>", source);
} }

View File

@ -19,14 +19,15 @@ namespace json {
extern std::string stringify(JObject* obj, bool nice, std::string indent); extern std::string stringify(JObject* obj, bool nice, std::string indent);
enum class valtype { enum class valtype {
object, array, number, string, boolean object, array, number, integer, string, boolean
}; };
union valvalue { union valvalue {
JObject* obj; JObject* obj;
JArray* arr; JArray* arr;
std::string* str; std::string* str;
double num; double decimal;
int64_t integer;
bool boolean; bool boolean;
}; };
@ -45,6 +46,7 @@ namespace json {
std::string str(size_t index) const; std::string str(size_t index) const;
double num(size_t index) const; double num(size_t index) const;
int64_t integer(size_t num) const;
JObject* obj(size_t index) const; JObject* obj(size_t index) const;
JArray* arr(size_t index) const; JArray* arr(size_t index) const;
bool flag(size_t index) const; bool flag(size_t index) const;
@ -55,12 +57,17 @@ namespace json {
JArray& put(uint value); JArray& put(uint value);
JArray& put(int value); JArray& put(int value);
JArray& put(uint64_t value);
JArray& put(int64_t value);
JArray& put(float value); JArray& put(float value);
JArray& put(double value); JArray& put(double value);
JArray& put(std::string value); JArray& put(std::string value);
JArray& put(JObject* value); JArray& put(JObject* value);
JArray& put(JArray* value); JArray& put(JArray* value);
JArray& put(bool value); JArray& put(bool value);
JArray& putArray();
JObject& putObj();
}; };
class JObject { class JObject {
@ -68,10 +75,15 @@ namespace json {
std::unordered_map<std::string, Value*> map; std::unordered_map<std::string, Value*> map;
~JObject(); ~JObject();
std::string getStr(std::string key, const std::string& def) const;
double getNum(std::string key, double def) const;
int64_t getInteger(std::string key, int64_t def) const;
void str(std::string key, std::string& dst) const; void str(std::string key, std::string& dst) const;
void num(std::string key, int& dst) const; void num(std::string key, int& dst) const;
void num(std::string key, float& dst) const; void num(std::string key, float& dst) const;
void num(std::string key, uint& dst) const; void num(std::string key, uint& dst) const;
void num(std::string key, int64_t& dst) const;
void num(std::string key, uint64_t& dst) const;
void num(std::string key, ubyte& dst) const; void num(std::string key, ubyte& dst) const;
void num(std::string key, double& dst) const; void num(std::string key, double& dst) const;
JObject* obj(std::string key) const; JObject* obj(std::string key) const;
@ -80,6 +92,8 @@ namespace json {
JObject& put(std::string key, uint value); JObject& put(std::string key, uint value);
JObject& put(std::string key, int value); JObject& put(std::string key, int value);
JObject& put(std::string key, int64_t value);
JObject& put(std::string key, uint64_t value);
JObject& put(std::string key, float value); JObject& put(std::string key, float value);
JObject& put(std::string key, double value); JObject& put(std::string key, double value);
JObject& put(std::string key, const char* value); JObject& put(std::string key, const char* value);
@ -87,7 +101,9 @@ namespace json {
JObject& put(std::string key, JObject* value); JObject& put(std::string key, JObject* value);
JObject& put(std::string key, JArray* value); JObject& put(std::string key, JArray* value);
JObject& put(std::string key, bool value); JObject& put(std::string key, bool value);
JArray& putArray(std::string key); JArray& putArray(std::string key);
JObject& putObj(std::string key);
bool has(std::string key); bool has(std::string key);
}; };

View File

@ -208,16 +208,24 @@ void Reader::readSection(Section* section /*nullable*/) {
expect('='); expect('=');
c = peek(); c = peek();
if (is_digit(c)) { if (is_digit(c)) {
double number = parseNumber(1); number_u num;
if (section) { if (parseNumber(1, num)) {
section->set(name, number); if (section)
section->set(name, (double)num.ival);
} else {
if (section)
section->set(name, num.fval);
} }
} else if (c == '-' || c == '+') { } else if (c == '-' || c == '+') {
int sign = c == '-' ? -1 : 1; int sign = c == '-' ? -1 : 1;
pos++; pos++;
double number = parseNumber(sign); number_u num;
if (section) { if (parseNumber(sign, num)) {
section->set(name, number); if (section)
section->set(name, (double)num.ival);
} else {
if (section)
section->set(name, num.fval);
} }
} else if (is_identifier_start(c)) { } else if (is_identifier_start(c)) {
string identifier = parseName(); string identifier = parseName();