Parser test implementation
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
#include "aliases.h"
|
||||
#include "lexer.h"
|
||||
#include "parser.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
@@ -43,6 +44,8 @@ int main(int argc, char *argv[]) {
|
||||
while (result.token.type != TK_NO_TOKEN) {
|
||||
print_token(result.token);
|
||||
|
||||
parse_token(result.token);
|
||||
|
||||
result = get_next_token(lexer, NULL);
|
||||
|
||||
if (result.error.errno) {
|
||||
|
@@ -1,6 +1,258 @@
|
||||
#include "parser.h"
|
||||
#include "aliases.h"
|
||||
#include "dstring.h"
|
||||
#include "json_entities.h"
|
||||
#include "lexer.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
void parse_token(token_t token) {}
|
||||
INTERNAL jentity_t *current = NULL;
|
||||
|
||||
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->first_child = NULL;
|
||||
|
||||
if (!current) {
|
||||
current = (jentity_t *)malloc(sizeof(jentity_t));
|
||||
current->type = JENTITY_SINGLE;
|
||||
current->value = value;
|
||||
} else if (current->type == JENTITY_PAIR &&
|
||||
current->pair.value.type == JVAL_EMPTY) {
|
||||
current->pair.value = value;
|
||||
} else {
|
||||
current->next = (jentity_t *)malloc(sizeof(jentity_t));
|
||||
current->next->type = JENTITY_SINGLE;
|
||||
current->next->value = value;
|
||||
current->next->parent = current->parent;
|
||||
|
||||
current = current->next;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
case TK_R_BRACE:
|
||||
if (current->parent) {
|
||||
current = current->parent;
|
||||
}
|
||||
|
||||
break;
|
||||
case TK_L_BRACKET: {
|
||||
jval_t value = (jval_t){
|
||||
.type = JVAL_COLLECTION,
|
||||
.collection = (jcoll_t *)malloc(sizeof(jcoll_t)),
|
||||
};
|
||||
|
||||
value.collection->type = JCOLL_ARRAY;
|
||||
value.collection->size = 0;
|
||||
value.collection->first_child = NULL;
|
||||
|
||||
if (!current) {
|
||||
current->type = JENTITY_SINGLE;
|
||||
current->value = value;
|
||||
} else if (current->type == JENTITY_PAIR &&
|
||||
current->pair.value.type == JVAL_EMPTY) {
|
||||
current->pair.value = value;
|
||||
} else {
|
||||
current->next = (jentity_t *)malloc(sizeof(jentity_t));
|
||||
current->next->type = JENTITY_SINGLE;
|
||||
current->next->value = value;
|
||||
current->next->parent = current->parent;
|
||||
|
||||
current = current->next;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
case TK_R_BRACKET:
|
||||
if (current->parent) {
|
||||
current = current->parent;
|
||||
}
|
||||
|
||||
break;
|
||||
case TK_NULL: {
|
||||
jval_t value = (jval_t){.type = JVAL_NULL, .null_val = NULL};
|
||||
|
||||
if (current->type == JENTITY_PAIR &&
|
||||
current->pair.value.type == JVAL_EMPTY) {
|
||||
current->pair.value = value;
|
||||
} else if (current->type == JENTITY_SINGLE &&
|
||||
current->value.type == JVAL_COLLECTION &&
|
||||
current->value.collection->type == JCOLL_ARRAY &&
|
||||
!(current->value.collection->first_child)) {
|
||||
current->value.collection->first_child =
|
||||
(jentity_t *)malloc(sizeof(jentity_t));
|
||||
|
||||
current->value.collection->first_child->type = JENTITY_SINGLE;
|
||||
current->value.collection->first_child->value = value;
|
||||
current->value.collection->first_child->parent = current;
|
||||
|
||||
current = current->value.collection->first_child;
|
||||
} else {
|
||||
current->next = (jentity_t *)malloc(sizeof(jentity_t));
|
||||
current->next->type = JENTITY_SINGLE;
|
||||
current->next->value = value;
|
||||
current->next->parent = current->parent;
|
||||
|
||||
current = current->next;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
case TK_BOOL: {
|
||||
jval_t value =
|
||||
(jval_t){.type = JVAL_BOOLEAN, .boolean = token.value.boolean};
|
||||
|
||||
if (current->type == JENTITY_PAIR &&
|
||||
current->pair.value.type == JVAL_EMPTY) {
|
||||
current->pair.value = value;
|
||||
} else if (current->type == JENTITY_SINGLE &&
|
||||
current->value.type == JVAL_COLLECTION &&
|
||||
current->value.collection->type == JCOLL_ARRAY &&
|
||||
!(current->value.collection->first_child)) {
|
||||
current->value.collection->first_child =
|
||||
(jentity_t *)malloc(sizeof(jentity_t));
|
||||
|
||||
current->value.collection->first_child->type = JENTITY_SINGLE;
|
||||
current->value.collection->first_child->value = value;
|
||||
current->value.collection->first_child->parent = current;
|
||||
|
||||
current = current->value.collection->first_child;
|
||||
} else {
|
||||
current->next = (jentity_t *)malloc(sizeof(jentity_t));
|
||||
current->next->type = JENTITY_SINGLE;
|
||||
current->next->value = value;
|
||||
current->next->parent = current->parent;
|
||||
|
||||
current = current->next;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
case TK_STR_KEY: {
|
||||
jval_t value = (jval_t){.type = JVAL_EMPTY, .null_val = NULL};
|
||||
|
||||
if (current->type == JENTITY_PAIR) {
|
||||
current->next = (jentity_t *)malloc(sizeof(jentity_t));
|
||||
current->next->type = JENTITY_PAIR;
|
||||
current->next->pair.key = dstr_from_string(token.value.string);
|
||||
current->next->pair.value = value;
|
||||
current->next->parent = current->parent;
|
||||
|
||||
current = current->next;
|
||||
} else if (current->type == JENTITY_SINGLE &&
|
||||
current->value.type == JVAL_COLLECTION &&
|
||||
!(current->value.collection->first_child)) {
|
||||
current->value.collection->first_child =
|
||||
(jentity_t *)malloc(sizeof(jentity_t));
|
||||
|
||||
current->value.collection->first_child->type = JENTITY_PAIR;
|
||||
current->value.collection->first_child->pair.key =
|
||||
dstr_from_string(token.value.string);
|
||||
current->value.collection->first_child->pair.value = value;
|
||||
current->value.collection->first_child->parent = current;
|
||||
|
||||
current = current->value.collection->first_child;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case TK_STR_VAL: {
|
||||
jval_t value = (jval_t){.type = JVAL_STRING,
|
||||
.string = dstr_from_string(token.value.string)};
|
||||
|
||||
if (current->type == JENTITY_PAIR &&
|
||||
current->pair.value.type == JVAL_EMPTY) {
|
||||
current->pair.value = value;
|
||||
} else if (current->type == JENTITY_SINGLE &&
|
||||
current->value.type == JVAL_COLLECTION &&
|
||||
current->value.collection->type == JCOLL_ARRAY &&
|
||||
!(current->value.collection->first_child)) {
|
||||
current->value.collection->first_child =
|
||||
(jentity_t *)malloc(sizeof(jentity_t));
|
||||
|
||||
current->value.collection->first_child->type = JENTITY_SINGLE;
|
||||
current->value.collection->first_child->value = value;
|
||||
current->value.collection->first_child->parent = current;
|
||||
|
||||
current = current->value.collection->first_child;
|
||||
} else {
|
||||
current->next = (jentity_t *)malloc(sizeof(jentity_t));
|
||||
current->next->type = JENTITY_SINGLE;
|
||||
current->next->value = value;
|
||||
current->next->parent = current->parent;
|
||||
|
||||
current = current->next;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
case TK_INTEGER: {
|
||||
jval_t value =
|
||||
(jval_t){.type = JVAL_INTEGER, .num_int = token.value.num_int};
|
||||
|
||||
if (current->type == JENTITY_PAIR &&
|
||||
current->pair.value.type == JVAL_EMPTY) {
|
||||
current->pair.value = value;
|
||||
} else if (current->type == JENTITY_SINGLE &&
|
||||
current->value.type == JVAL_COLLECTION &&
|
||||
current->value.collection->type == JCOLL_ARRAY &&
|
||||
!(current->value.collection->first_child)) {
|
||||
current->value.collection->first_child =
|
||||
(jentity_t *)malloc(sizeof(jentity_t));
|
||||
|
||||
current->value.collection->first_child->type = JENTITY_SINGLE;
|
||||
current->value.collection->first_child->value = value;
|
||||
current->value.collection->first_child->parent = current;
|
||||
|
||||
current = current->value.collection->first_child;
|
||||
} else {
|
||||
current->next = (jentity_t *)malloc(sizeof(jentity_t));
|
||||
current->next->type = JENTITY_SINGLE;
|
||||
current->next->value = value;
|
||||
current->next->parent = current->parent;
|
||||
|
||||
current = current->next;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
case TK_DOUBLE: {
|
||||
jval_t value =
|
||||
(jval_t){.type = JVAL_DOUBLE, .num_dbl = token.value.num_frac};
|
||||
|
||||
if (current->type == JENTITY_PAIR &&
|
||||
current->pair.value.type == JVAL_EMPTY) {
|
||||
current->pair.value = value;
|
||||
} else if (current->type == JENTITY_SINGLE &&
|
||||
current->value.type == JVAL_COLLECTION &&
|
||||
current->value.collection->type == JCOLL_ARRAY &&
|
||||
!(current->value.collection->first_child)) {
|
||||
current->value.collection->first_child =
|
||||
(jentity_t *)malloc(sizeof(jentity_t));
|
||||
|
||||
current->value.collection->first_child->type = JENTITY_SINGLE;
|
||||
current->value.collection->first_child->value = value;
|
||||
current->value.collection->first_child->parent = current;
|
||||
|
||||
current = current->value.collection->first_child;
|
||||
} else {
|
||||
current->next = (jentity_t *)malloc(sizeof(jentity_t));
|
||||
current->next->type = JENTITY_SINGLE;
|
||||
current->next->value = value;
|
||||
current->next->parent = current->parent;
|
||||
|
||||
current = current->next;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
case TK_NO_TOKEN:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user