Refactored parse_token function

This commit is contained in:
2023-07-01 04:43:52 +01:00
parent 7e4d82888a
commit 99f5c5bfe5
3 changed files with 511 additions and 317 deletions

View File

@@ -3,129 +3,40 @@
#include "dstring.h"
#include "json_entities.h"
#include "lexer.h"
#include <stdio.h>
#include <stdlib.h>
INTERNAL jentity_t *current = NULL;
INTERNAL jcoll_t *get_collection_from_entity(const jentity_t *entity);
INTERNAL jentity_t *create_new_single_entity(const jval_t value,
jentity_t *parent);
INTERNAL jentity_t *create_new_pair_entity(dstr_t *key, const jval_t value,
jentity_t *parent);
INTERNAL void add_key(jentity_t **entity, dstr_t *key, const jval_t *value);
INTERNAL jentity_t *add_value(jentity_t **entity, const jval_t *value);
INTERNAL void add_collection(jentity_t **entity, const jval_t *value);
void parse_token(token_t token) {
switch (token.type) {
case TK_L_BRACE: {
jval_t value = (jval_t){
.type = JVAL_COLLECTION,
.collection = (jcoll_t *)malloc(sizeof(jcoll_t)),
};
value.collection->type = JCOLL_OBJECT;
value.collection->size = 0;
value.collection->begin = NULL;
value.collection->end = NULL;
if (!current) {
current = (jentity_t *)malloc(sizeof(jentity_t));
current->type = JENTITY_SINGLE;
current->value = value;
current->parent = NULL;
current->next = NULL;
} else {
jcoll_t *collection = NULL;
if (current->type == JENTITY_SINGLE) {
collection = current->value.collection;
} else {
collection = current->pair.value.collection;
}
if (!(collection->end)) {
collection->begin = collection->end =
(jentity_t *)malloc(sizeof(jentity_t));
collection->end->type = JENTITY_SINGLE;
collection->end->value = value;
collection->end->parent = current;
collection->end->next = NULL;
current = collection->end;
} else {
if (collection->end->type == JENTITY_PAIR &&
collection->end->pair.value.type == JVAL_EMPTY) {
collection->end->pair.value = value;
current = collection->end;
} else {
jentity_t *entity = (jentity_t *)malloc(sizeof(jentity_t));
entity->type = JENTITY_SINGLE;
entity->value = value;
entity->parent = current;
entity->next = NULL;
collection->end->next = entity;
collection->end = entity;
current = entity;
}
}
}
break;
}
case TK_L_BRACE:
case TK_L_BRACKET: {
jval_t value = (jval_t){
.type = JVAL_COLLECTION,
.collection = (jcoll_t *)malloc(sizeof(jcoll_t)),
};
value.collection->type = JCOLL_ARRAY;
if (token.type == TK_L_BRACE) {
value.collection->type = JCOLL_OBJECT;
} else {
value.collection->type = JCOLL_ARRAY;
}
value.collection->size = 0;
value.collection->begin = NULL;
value.collection->end = NULL;
if (!current) {
current = (jentity_t *)malloc(sizeof(jentity_t));
current->type = JENTITY_SINGLE;
current->value = value;
current->parent = NULL;
current->next = NULL;
} else {
jcoll_t *collection = NULL;
if (current->type == JENTITY_SINGLE) {
collection = current->value.collection;
} else {
collection = current->pair.value.collection;
}
if (!(collection->end)) {
collection->begin = collection->end =
(jentity_t *)malloc(sizeof(jentity_t));
collection->end->type = JENTITY_SINGLE;
collection->end->value = value;
collection->end->parent = current;
collection->end->next = current;
current = collection->end;
} else {
if (collection->end->type == JENTITY_PAIR &&
collection->end->pair.value.type == JVAL_EMPTY) {
collection->end->pair.value = value;
current = collection->end;
} else {
jentity_t *entity = (jentity_t *)malloc(sizeof(jentity_t));
entity->type = JENTITY_SINGLE;
entity->value = value;
entity->parent = current;
entity->next = NULL;
collection->end->next = entity;
collection->end = entity;
current = entity;
}
}
}
add_collection(&current, &value);
break;
}
@@ -136,41 +47,17 @@ void parse_token(token_t token) {
}
break;
case TK_STR_KEY: {
jval_t value = (jval_t){.type = JVAL_EMPTY, .null_val = NULL};
add_key(&current, dstr_from_string(token.value.string), &value);
break;
}
case TK_NULL: {
jval_t value = (jval_t){.type = JVAL_NULL, .null_val = NULL};
jcoll_t *collection = NULL;
if (current->type == JENTITY_SINGLE) {
collection = current->value.collection;
} else {
collection = current->pair.value.collection;
}
if (!(collection->end)) {
collection->begin = collection->end =
(jentity_t *)malloc(sizeof(jentity_t));
collection->end->type = JENTITY_SINGLE;
collection->end->value = value;
collection->end->parent = current;
collection->end->next = NULL;
} else {
if (collection->end->type == JENTITY_PAIR &&
collection->end->pair.value.type == JVAL_EMPTY) {
collection->end->pair.value = value;
} else {
jentity_t *entity = (jentity_t *)malloc(sizeof(jentity_t));
entity->type = JENTITY_SINGLE;
entity->value = value;
entity->parent = current;
entity->next = NULL;
collection->end->next = entity;
collection->end = entity;
}
}
add_value(&current, &value);
break;
}
@@ -178,73 +65,7 @@ void parse_token(token_t token) {
jval_t value =
(jval_t){.type = JVAL_BOOLEAN, .boolean = token.value.boolean};
jcoll_t *collection = NULL;
if (current->type == JENTITY_SINGLE) {
collection = current->value.collection;
} else {
collection = current->pair.value.collection;
}
if (!(collection->end)) {
collection->begin = collection->end =
(jentity_t *)malloc(sizeof(jentity_t));
collection->end->type = JENTITY_SINGLE;
collection->end->value = value;
collection->end->parent = current;
collection->end->next = NULL;
} else {
if (collection->end->type == JENTITY_PAIR &&
collection->end->pair.value.type == JVAL_EMPTY) {
collection->end->pair.value = value;
} else {
jentity_t *entity = (jentity_t *)malloc(sizeof(jentity_t));
entity->type = JENTITY_SINGLE;
entity->value = value;
entity->parent = current;
entity->next = NULL;
collection->end->next = entity;
collection->end = entity;
}
}
break;
}
case TK_STR_KEY: {
jval_t value = (jval_t){.type = JVAL_EMPTY, .null_val = NULL};
jcoll_t *collection = NULL;
if (current->type == JENTITY_SINGLE) {
collection = current->value.collection;
} else {
collection = current->pair.value.collection;
}
if (!(collection->end)) {
collection->begin = collection->end =
(jentity_t *)malloc(sizeof(jentity_t));
collection->end->type = JENTITY_PAIR;
collection->end->pair.key = dstr_from_string(token.value.string);
collection->end->pair.value = value;
collection->end->parent = current;
collection->end->next = NULL;
} else {
jentity_t *entity = (jentity_t *)malloc(sizeof(jentity_t));
entity->type = JENTITY_PAIR;
entity->pair.key = dstr_from_string(token.value.string);
entity->pair.value = value;
entity->parent = current;
entity->next = NULL;
collection->end->next = entity;
collection->end = entity;
}
add_value(&current, &value);
break;
}
@@ -252,38 +73,7 @@ void parse_token(token_t token) {
jval_t value = (jval_t){.type = JVAL_STRING,
.string = dstr_from_string(token.value.string)};
jcoll_t *collection = NULL;
if (current->type == JENTITY_SINGLE) {
collection = current->value.collection;
} else {
collection = current->pair.value.collection;
}
if (!(collection->end)) {
collection->begin = collection->end =
(jentity_t *)malloc(sizeof(jentity_t));
collection->end->type = JENTITY_SINGLE;
collection->end->value = value;
collection->end->parent = current;
collection->end->next = NULL;
} else {
if (collection->end->type == JENTITY_PAIR &&
collection->end->pair.value.type == JVAL_EMPTY) {
collection->end->pair.value = value;
} else {
jentity_t *entity = (jentity_t *)malloc(sizeof(jentity_t));
entity->type = JENTITY_SINGLE;
entity->value = value;
entity->parent = current;
entity->next = NULL;
collection->end->next = entity;
collection->end = entity;
}
}
add_value(&current, &value);
break;
}
@@ -291,38 +81,7 @@ void parse_token(token_t token) {
jval_t value =
(jval_t){.type = JVAL_INTEGER, .num_int = token.value.num_int};
jcoll_t *collection = NULL;
if (current->type == JENTITY_SINGLE) {
collection = current->value.collection;
} else {
collection = current->pair.value.collection;
}
if (!(collection->end)) {
collection->begin = collection->end =
(jentity_t *)malloc(sizeof(jentity_t));
collection->end->type = JENTITY_SINGLE;
collection->end->value = value;
collection->end->parent = current;
collection->end->next = NULL;
} else {
if (collection->end->type == JENTITY_PAIR &&
collection->end->pair.value.type == JVAL_EMPTY) {
collection->end->pair.value = value;
} else {
jentity_t *entity = (jentity_t *)malloc(sizeof(jentity_t));
entity->type = JENTITY_SINGLE;
entity->value = value;
entity->parent = current;
entity->next = NULL;
collection->end->next = entity;
collection->end = entity;
}
}
add_value(&current, &value);
break;
}
@@ -330,38 +89,7 @@ void parse_token(token_t token) {
jval_t value =
(jval_t){.type = JVAL_DOUBLE, .num_dbl = token.value.num_frac};
jcoll_t *collection = NULL;
if (current->type == JENTITY_SINGLE) {
collection = current->value.collection;
} else {
collection = current->pair.value.collection;
}
if (!(collection->end)) {
collection->begin = collection->end =
(jentity_t *)malloc(sizeof(jentity_t));
collection->end->type = JENTITY_SINGLE;
collection->end->value = value;
collection->end->parent = current;
collection->end->parent = NULL;
} else {
if (collection->end->type == JENTITY_PAIR &&
collection->end->pair.value.type == JVAL_EMPTY) {
collection->end->pair.value = value;
} else {
jentity_t *entity = (jentity_t *)malloc(sizeof(jentity_t));
entity->type = JENTITY_SINGLE;
entity->value = value;
entity->parent = current;
entity->next = NULL;
collection->end->next = entity;
collection->end = entity;
}
}
add_value(&current, &value);
break;
}
@@ -369,3 +97,103 @@ void parse_token(token_t token) {
break;
}
}
jcoll_t *get_collection_from_entity(const jentity_t *entity) {
return entity->type == JENTITY_SINGLE ? entity->value.collection
: entity->pair.value.collection;
}
jentity_t *create_new_single_entity(const jval_t value, jentity_t *parent) {
jentity_t *entity = (jentity_t *)malloc(sizeof(jentity_t));
if (!entity) {
return NULL;
}
entity->type = JENTITY_SINGLE;
entity->value = value;
entity->parent = parent;
entity->next = NULL;
return entity;
}
jentity_t *create_new_pair_entity(dstr_t *key, const jval_t value,
jentity_t *parent) {
jentity_t *entity = (jentity_t *)malloc(sizeof(jentity_t));
if (!entity) {
return NULL;
}
entity->type = JENTITY_PAIR;
entity->pair.key = key;
entity->pair.value = value;
entity->parent = parent;
entity->next = NULL;
return entity;
}
void add_key(jentity_t **entity, dstr_t *key, const jval_t *value) {
jcoll_t *collection = get_collection_from_entity(*entity);
if (!collection) {
return;
}
if (!(collection->end)) {
collection->begin = collection->end =
create_new_pair_entity(key, *value, *entity);
collection->size = 1;
} else {
jentity_t *new_entity = create_new_pair_entity(key, *value, *entity);
collection->end->next = new_entity;
collection->end = new_entity;
++(collection->size);
}
}
jentity_t *add_value(jentity_t **entity, const jval_t *value) {
jcoll_t *collection = get_collection_from_entity(*entity);
if (!collection) {
return NULL;
}
if (!(collection->end)) {
collection->begin = collection->end =
create_new_single_entity(*value, *entity);
collection->size = 1;
} else {
if (collection->end->type == JENTITY_PAIR &&
collection->end->pair.value.type == JVAL_EMPTY) {
collection->end->pair.value = *value;
} else {
jentity_t *new_entity = create_new_single_entity(*value, *entity);
collection->end->next = new_entity;
collection->end = new_entity;
++(collection->size);
}
}
return collection->end;
}
void add_collection(jentity_t **entity, const jval_t *value) {
if (!(*entity)) {
*entity = create_new_single_entity(*value, NULL);
return;
}
*entity = add_value(entity, value);
}