toml format is now used for settings file

This commit is contained in:
MihailRis
2023-11-14 15:51:26 +03:00
parent 3723cf491f
commit 69b309dc60
5 changed files with 63 additions and 353 deletions
+21 -302
View File
@@ -4,6 +4,8 @@
#include <sstream>
#include <memory>
#include "commons.h"
using namespace json;
using std::string;
@@ -13,83 +15,6 @@ using std::unordered_map;
using std::stringstream;
using std::make_pair;
inline bool is_digit(int c) {
return (c >= '0' && c <= '9');
}
inline bool is_whitespace(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == '\f';
}
inline bool is_identifier_start(int c) {
return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || c == '_' || c == '-' || c == '.';
}
inline bool is_identifier_part(int c) {
return is_identifier_start(c) || is_digit(c);
}
inline int is_box(int c) {
switch (c) {
case 'B':
case 'b':
return 2;
case 'O':
case 'o':
return 8;
case 'X':
case 'x':
return 16;
}
return 10;
}
inline int char2int(int c) {
if (c >= '0' && c <= '9') {
return c - '0';
}
if (c >= 'a' && c <= 'f') {
return 10 + c - 'a';
}
if (c >= 'A' && c <= 'F') {
return 10 + c - 'A';
}
return -1;
}
inline number_t power(number_t base, int64_t power) {
number_t result = 1.0;
for (int64_t i = 0; i < power; i++) {
result *= base;
}
return result;
}
string json::escape(string s) {
std::stringstream ss;
ss << '"';
for (char c : s) {
switch (c) {
case '\n': ss << "\\n"; break;
case '\r': ss << "\\r"; break;
case '\t': ss << "\\t"; break;
case '\f': ss << "\\f"; break;
case '\b': ss << "\\b"; break;
case '"': ss << "\\\""; break;
case '\\': ss << "\\\\"; break;
default:
if (c < ' ') {
ss << "\\" << std::oct << (int)c;
break;
}
ss << c;
break;
}
}
ss << '"';
return ss.str();
}
inline void newline(std::stringstream& ss, bool nice, uint indent, const std::string indentstr) {
if (nice) {
ss << "\n";
@@ -134,7 +59,7 @@ void stringify(Value* value, stringstream& ss, int indent, string indentstr, boo
} else if (value->type == valtype::number) {
ss << value->value.num;
} else if (value->type == valtype::string) {
ss << escape(*value->value.str);
ss << escape_string(*value->value.str);
}
}
@@ -151,7 +76,7 @@ void stringifyObj(JObject* obj, stringstream& ss, int indent, string indentstr,
newline(ss, nice, indent, indentstr);
}
Value* value = entry.second;
ss << escape(key) << ": ";
ss << escape_string(key) << ": ";
stringify(value, ss, indent+1, indentstr, nice);
index++;
if (index < obj->map.size()) {
@@ -171,34 +96,6 @@ string json::stringify(JObject* obj, bool nice, string indent) {
}
parsing_error::parsing_error(string message,
string filename,
string source,
uint pos,
uint line,
uint linestart)
: std::runtime_error(message), filename(filename), source(source),
pos(pos), line(line), linestart(linestart) {
}
string parsing_error::errorLog() const {
std::stringstream ss;
uint linepos = pos - linestart;
ss << "parsing error in file '" << filename;
ss << "' at " << (line+1) << ":" << linepos << ": " << this->what() << "\n";
size_t end = source.find("\n", linestart);
if (end == string::npos) {
end = source.length();
}
ss << source.substr(linestart, end-linestart) << "\n";
for (uint i = 0; i < linepos; i++) {
ss << " ";
}
ss << "^";
return ss.str();
}
JArray::~JArray() {
for (auto value : values) {
delete value;
@@ -209,7 +106,7 @@ std::string JArray::str(size_t index) const {
return *values[index]->value.str;
}
number_t JArray::num(size_t index) const {
double JArray::num(size_t index) const {
return values[index]->value.num;
}
@@ -233,14 +130,14 @@ JArray& JArray::put(string value) {
}
JArray& JArray::put(uint value) {
return put((number_t)value);
return put((double)value);
}
JArray& JArray::put(int value) {
return put((number_t)value);
return put((double)value);
}
JArray& JArray::put(number_t value) {
JArray& JArray::put(double value) {
valvalue val;
val.num = value;
values.push_back(new Value(valtype::number, val));
@@ -287,7 +184,7 @@ void JObject::str(std::string key, std::string& dst) const {
dst = *found->second->value.str;
}
void JObject::num(std::string key, number_t& dst) const {
void JObject::num(std::string key, double& dst) const {
auto found = map.find(key);
if (found != map.end())
dst = found->second->value.num;
@@ -332,18 +229,18 @@ void JObject::flag(std::string key, bool& dst) const {
}
JObject& JObject::put(string key, uint value) {
return put(key, (number_t)value);
return put(key, (double)value);
}
JObject& JObject::put(string key, int value) {
return put(key, (number_t)value);
return put(key, (double)value);
}
JObject& JObject::put(string key, float value) {
return put(key, (number_t)value);
return put(key, (double)value);
}
JObject& JObject::put(string key, number_t value) {
JObject& JObject::put(string key, double value) {
auto found = map.find(key);
if (found != map.end()) delete found->second;
valvalue val;
@@ -401,7 +298,7 @@ Value::~Value() {
}
}
Parser::Parser(string filename, string source) : filename(filename), source(source) {
Parser::Parser(string filename, string source) : BasicParser(filename, source) {
}
JObject* Parser::parse() {
@@ -412,189 +309,6 @@ JObject* Parser::parse() {
return parseObject();
}
bool Parser::hasNext() {
return pos < source.length();
}
char Parser::nextChar() {
if (!hasNext()) {
throw error("unexpected end");
}
return source[pos++];
}
void Parser::expect(char expected) {
char c = peek();
if (c != expected) {
throw error("'"+string({expected})+"' expected");
}
pos++;
}
char Parser::peek() {
skipWhitespace();
if (pos >= source.length()) {
throw error("unexpected end");
}
return source[pos];
}
parsing_error Parser::error(std::string message) {
return parsing_error(message, filename, source, pos, line, linestart);
}
void Parser::skipWhitespace() {
while (hasNext()) {
char next = source[pos];
if (next == '\n') {
line++;
linestart = ++pos;
continue;
}
if (is_whitespace(next)) {
pos++;
} else {
break;
}
}
}
string Parser::parseName() {
char c = peek();
if (!is_identifier_start(c)) {
if (c == '"') {
pos++;
return parseString(c);
}
throw error("identifier expected");
}
int start = pos;
while (hasNext() && is_identifier_part(source[pos])) {
pos++;
}
return source.substr(start, pos-start);
}
int64_t Parser::parseSimpleInt(int base) {
char c = peek();
int index = char2int(c);
if (index == -1 || index >= base) {
throw error("invalid number literal");
}
int64_t value = index;
pos++;
while (hasNext()) {
c = source[pos];
while (c == '_') {
c = source[++pos];
}
index = char2int(c);
if (index == -1 || index >= base) {
return value;
}
value *= base;
value += index;
pos++;
}
return value;
}
number_t Parser::parseNumber(int sign) {
char c = peek();
int base = 10;
if (c == '0' && pos + 1 < source.length() &&
(base = is_box(source[pos+1])) != 10) {
pos += 2;
return parseSimpleInt(base);
}
int64_t value = parseSimpleInt(base);
if (!hasNext()) {
return value * sign;
}
c = source[pos];
if (c == 'e' || c == 'E') {
pos++;
int s = 1;
if (peek() == '-') {
s = -1;
pos++;
} else if (peek() == '+'){
pos++;
}
return sign * value * power(10.0, s * parseSimpleInt(10));
}
if (c == '.') {
pos++;
int64_t expo = 1;
while (hasNext() && source[pos] == '0') {
expo *= 10;
pos++;
}
int64_t afterdot = 0;
if (hasNext() && is_digit(source[pos])) {
afterdot = parseSimpleInt(10);
}
expo *= power(10, fmax(0, log10(afterdot) + 1));
c = source[pos];
number_t dvalue = (value + (afterdot / (number_t)expo));
if (c == 'e' || c == 'E') {
pos++;
int s = 1;
if (peek() == '-') {
s = -1;
pos++;
} else if (peek() == '+'){
pos++;
}
return sign * dvalue * power(10.0, s * parseSimpleInt(10));
}
return dvalue;
}
return value;
}
string Parser::parseString(char quote) {
std::stringstream ss;
while (hasNext()) {
char c = source[pos];
if (c == quote) {
pos++;
return ss.str();
}
if (c == '\\') {
pos++;
c = nextChar();
if (c >= '0' && c <= '7') {
pos--;
ss << (char)parseSimpleInt(8);
continue;
}
switch (c) {
case 'n': ss << '\n'; break;
case 'r': ss << '\r'; break;
case 'b': ss << '\b'; break;
case 't': ss << '\t'; break;
case 'f': ss << '\f'; break;
case '\'': ss << '\\'; break;
case '"': ss << '"'; break;
case '\\': ss << '\\'; break;
case '/': ss << '/'; break;
case '\n': pos++; continue;
default:
throw error("'\\" + string({c}) + "' is an illegal escape");
}
continue;
}
if (c == '\n') {
throw error("non-closed string literal");
}
ss << c;
pos++;
}
throw error("unexpected end");
}
JObject* Parser::parseObject() {
expect('{');
unique_ptr<JObject> obj(new JObject());
@@ -648,10 +362,15 @@ Value* Parser::parseValue() {
if (literal == "true") {
val.boolean = true;
return new Value(valtype::boolean, val);
}
if (literal == "false") {
} else if (literal == "false") {
val.boolean = false;
return new Value(valtype::boolean, val);
} else if (literal == "inf") {
val.num = INFINITY;
return new Value(valtype::number, val);
} else if (literal == "nan") {
val.num = NAN;
return new Value(valtype::number, val);
}
throw error("invalid literal");
}