fix (JSON): fixed array parsing

This commit is contained in:
antifallobst 2023-06-06 20:32:30 +02:00
parent 83f5e77d52
commit 3fbc9dc274
4 changed files with 16 additions and 21 deletions

View File

@ -31,6 +31,7 @@
#define JSON_INCREMENT_TOKEN_ID \ #define JSON_INCREMENT_TOKEN_ID \
*token_id += 1; \ *token_id += 1; \
/* DEBUG("token: %d - %s", *token_id, json->tokens[*token_id].string);*/ \
if (*token_id >= json->num_tokens) { \ if (*token_id >= json->num_tokens) { \
log(LOG_ERROR, "failed to parse json -> unexpected EOF"); \ log(LOG_ERROR, "failed to parse json -> unexpected EOF"); \
json_node_dump(json->root_node, 0); \ json_node_dump(json->root_node, 0); \

View File

@ -1,8 +1,10 @@
{ {
"PS2_ACPI_VALIDATION": true, "PS2_ACPI_VALIDATION": true,
"LOG_GRAPHICAL": true, "LOG_GRAPHICAL": false,
"modules": { "drivers": [
"FAT32": "/initrd/modules/fat32.nxkm" "/initrd/drv/test.nxkm",
} "/initrd/drv/nx_std_fat32.nxkm"
],
"test": 1
} }

View File

@ -9,6 +9,8 @@ json_T* json_from_string(string_t str) {
json_tokenize(json, str); json_tokenize(json, str);
// DEBUG("TOKENS:");ss
bool status = json_parse(json); bool status = json_parse(json);
if (!status) { if (!status) {
json_destruct(json); json_destruct(json);
@ -270,6 +272,8 @@ void json_tokenize(json_T* json, string_t str) {
} }
bool json_parse_assignment(json_T* json, uint32_t* token_id, json_node_T* node) { bool json_parse_assignment(json_T* json, uint32_t* token_id, json_node_T* node) {
// DEBUG("parsing assignment");
JSON_INCREMENT_TOKEN_ID
if (json->tokens[*token_id].type != JSON_TOKEN_SPECIAL || json->tokens[*token_id].string[0] != ':') { if (json->tokens[*token_id].type != JSON_TOKEN_SPECIAL || json->tokens[*token_id].string[0] != ':') {
log(LOG_ERROR, "failed to parse json at position %d:%d -> expected : got \"%s\"", log(LOG_ERROR, "failed to parse json at position %d:%d -> expected : got \"%s\"",
json->tokens[*token_id].line, json->tokens[*token_id].column, json->tokens[*token_id].string); json->tokens[*token_id].line, json->tokens[*token_id].column, json->tokens[*token_id].string);
@ -314,7 +318,6 @@ bool json_parse_assignment(json_T* json, uint32_t* token_id, json_node_T* node)
case '{': { case '{': {
json_node_T* object_node = json_node_alloc(node, JSON_NODE_OBJECT, &json->tokens[*token_id]); json_node_T* object_node = json_node_alloc(node, JSON_NODE_OBJECT, &json->tokens[*token_id]);
JSON_INCREMENT_TOKEN_ID
bool status = json_parse_object(json, token_id, object_node); bool status = json_parse_object(json, token_id, object_node);
if (!status) return false; if (!status) return false;
break; break;
@ -323,7 +326,6 @@ bool json_parse_assignment(json_T* json, uint32_t* token_id, json_node_T* node)
case '[': { case '[': {
json_node_T* array_node = json_node_alloc(node, JSON_NODE_ARRAY, &json->tokens[*token_id]); json_node_T* array_node = json_node_alloc(node, JSON_NODE_ARRAY, &json->tokens[*token_id]);
JSON_INCREMENT_TOKEN_ID
bool status = json_parse_array(json, token_id, array_node); bool status = json_parse_array(json, token_id, array_node);
if (!status) return false; if (!status) return false;
break; break;
@ -343,12 +345,13 @@ bool json_parse_assignment(json_T* json, uint32_t* token_id, json_node_T* node)
} }
bool json_parse_object(json_T* json, uint32_t* token_id, json_node_T* node) { bool json_parse_object(json_T* json, uint32_t* token_id, json_node_T* node) {
// DEBUG("parsing object");
while (true) { while (true) {
JSON_INCREMENT_TOKEN_ID
switch (json->tokens[*token_id].type) { switch (json->tokens[*token_id].type) {
case JSON_TOKEN_STRING: { case JSON_TOKEN_STRING: {
json_node_T* string_node = json_node_alloc(node, JSON_NODE_STRING, &json->tokens[*token_id]); json_node_T* string_node = json_node_alloc(node, JSON_NODE_STRING, &json->tokens[*token_id]);
JSON_INCREMENT_TOKEN_ID
bool status = json_parse_assignment(json, token_id, string_node); bool status = json_parse_assignment(json, token_id, string_node);
if (!status) return false; if (!status) return false;
break; break;
@ -378,15 +381,14 @@ bool json_parse_object(json_T* json, uint32_t* token_id, json_node_T* node) {
return false; return false;
} }
return true; return true;
} else {
JSON_INCREMENT_TOKEN_ID
} }
} }
} }
bool json_parse_array(json_T* json, uint32_t* token_id, json_node_T* node) { bool json_parse_array(json_T* json, uint32_t* token_id, json_node_T* node) {
// DEBUG("parsing array");
while (true) { while (true) {
JSON_INCREMENT_TOKEN_ID
switch (json->tokens[*token_id].type) { switch (json->tokens[*token_id].type) {
case JSON_TOKEN_STRING: { case JSON_TOKEN_STRING: {
json_node_alloc(node, JSON_NODE_STRING, &json->tokens[*token_id]); json_node_alloc(node, JSON_NODE_STRING, &json->tokens[*token_id]);
@ -421,7 +423,6 @@ bool json_parse_array(json_T* json, uint32_t* token_id, json_node_T* node) {
case '{': { case '{': {
json_node_T* object_node = json_node_alloc(node, JSON_NODE_OBJECT, &json->tokens[*token_id]); json_node_T* object_node = json_node_alloc(node, JSON_NODE_OBJECT, &json->tokens[*token_id]);
JSON_INCREMENT_TOKEN_ID
bool status = json_parse_object(json, token_id, object_node); bool status = json_parse_object(json, token_id, object_node);
if (!status) return false; if (!status) return false;
break; break;
@ -430,7 +431,6 @@ bool json_parse_array(json_T* json, uint32_t* token_id, json_node_T* node) {
case '[': { case '[': {
json_node_T* array_node = json_node_alloc(node, JSON_NODE_ARRAY, &json->tokens[*token_id]); json_node_T* array_node = json_node_alloc(node, JSON_NODE_ARRAY, &json->tokens[*token_id]);
JSON_INCREMENT_TOKEN_ID
bool status = json_parse_array(json, token_id, array_node); bool status = json_parse_array(json, token_id, array_node);
if (!status) return false; if (!status) return false;
break; break;
@ -453,11 +453,8 @@ bool json_parse_array(json_T* json, uint32_t* token_id, json_node_T* node) {
json->tokens[*token_id].line, json->tokens[*token_id].column, json->tokens[*token_id].string); json->tokens[*token_id].line, json->tokens[*token_id].column, json->tokens[*token_id].string);
return false; return false;
} }
JSON_INCREMENT_TOKEN_ID
return true; return true;
} }
JSON_INCREMENT_TOKEN_ID
} }
} }
@ -469,11 +466,6 @@ bool json_parse(json_T* json) {
log(LOG_ERROR, "failed to parse json at position %d:%d -> expected { got \"%s\"", json->tokens[0].line, json->tokens[0].column, json->tokens[0].string); log(LOG_ERROR, "failed to parse json at position %d:%d -> expected { got \"%s\"", json->tokens[0].line, json->tokens[0].column, json->tokens[0].string);
return false; return false;
} }
token_id++;
if (token_id >= json->num_tokens) { \
log(LOG_ERROR, "failed to parse json -> unexpected EOF");
return false;
}
return json_parse_object(json, &token_id, json->root_node); return json_parse_object(json, &token_id, json->root_node);
} }

View File

@ -436,7 +436,7 @@ bool string_is_char_number(char chr) {
bool string_is_char_special(char chr) { bool string_is_char_special(char chr) {
if ((chr >= '!' && chr <= '/') || if ((chr >= '!' && chr <= '/') ||
(chr >= ':' && chr <= '@') || (chr >= ':' && chr <= '@') ||
(chr >= '^' && chr <= '`') || (chr >= '[' && chr <= '`') ||
(chr >= '{' && chr <= '~')) { (chr >= '{' && chr <= '~')) {
return true; return true;
} }