fix: hectic C: template parse

This commit is contained in:
2025-05-04 13:49:29 +00:00
parent e800f11103
commit 9b045b71f9
4 changed files with 390 additions and 172 deletions

View File

@@ -14,7 +14,6 @@
" content = %p \"Hello\"\n" \
" } %p\n" \
" } %p,\n" \
" struct TemplateNode children = NULL,\n" \
" struct TemplateNode next = {\n" \
" enum type = INTERPOLATE 1 ,\n" \
" union TemplateValue value = {\n" \
@@ -22,7 +21,6 @@
" key = %p \"name\"\n" \
" } %p\n" \
" } %p,\n" \
" struct TemplateNode children = NULL,\n" \
" struct TemplateNode next = {\n" \
" enum type = TEXT 0 ,\n" \
" union TemplateValue value = {\n" \
@@ -30,7 +28,6 @@
" content = %p \"!\"\n" \
" } %p\n" \
" } %p,\n" \
" struct TemplateNode children = NULL,\n" \
" struct TemplateNode next = NULL\n" \
" } %p\n" \
" } %p\n" \
@@ -50,31 +47,29 @@
" content = %p \"Loop content\"\n" \
" } %p\n" \
" } %p,\n" \
" struct TemplateNode children = NULL,\n" \
" struct TemplateNode next = NULL\n" \
" } %p\n" \
" } %p\n" \
" } %p,\n" \
" struct TemplateNode children = NULL,\n" \
" struct TemplateNode next = NULL\n" \
"} %p\n"
static void test_template_node_to_debug_str(Arena *arena) {
TemplateNode *root = arena_alloc(arena, sizeof(TemplateNode));
root->type = TEMPLATE_NODE_TEXT;
root->value.text.content = arena_strncpy(arena, "Hello", 5);
*root = init_template_node(arena, TEMPLATE_NODE_TEXT);
root->value->text.content = arena_strncpy(arena, "Hello", 5);
root->next = arena_alloc(arena, sizeof(TemplateNode));
root->next->type = TEMPLATE_NODE_INTERPOLATE;
root->next->value.interpolate.key = arena_strncpy(arena, "name", 4);
*root->next = init_template_node(arena, TEMPLATE_NODE_INTERPOLATE);
root->next->value->interpolate.key = arena_strncpy(arena, "name", 4);
root->next->next = arena_alloc(arena, sizeof(TemplateNode));
root->next->next->type = TEMPLATE_NODE_TEXT;
root->next->next->value.text.content = arena_strncpy(arena, "!", 1);
*root->next->next = init_template_node(arena, TEMPLATE_NODE_TEXT);
root->next->next->value->text.content = arena_strncpy(arena, "!", 1);
char *debug_str = debug_to_pretty_str(arena, TEMPLATE_NODE_TO_DEBUG_STR(arena, "root", root));
raise_log("debug_str: \n%s", debug_str);
raise_notice("debug_str: \n%s", debug_str);
{ // some debug output
Arena *debug_arena = DISPOSABLE_ARENA;
@@ -85,21 +80,21 @@ static void test_template_node_to_debug_str(Arena *arena) {
char *expected_debug_str = arena_alloc(arena, MEM_KiB);
sprintf(expected_debug_str, TEST_TEMPLATE_NODE_TO_DEBUG_STR,
(void*)root->value.text.content,
(void*)&root->value.text,
(void*)&root->value,
(void*)root->next->value.interpolate.key,
(void*)&root->next->value.interpolate,
(void*)&root->next->value,
(void*)root->next->next->value.text.content,
(void*)&root->next->next->value.text,
(void*)&root->next->next->value,
(void*)root->value->text.content,
(void*)&root->value->text,
(void*)root->value,
(void*)root->next->value->interpolate.key,
(void*)&root->next->value->interpolate,
(void*)root->next->value,
(void*)root->next->next->value->text.content,
(void*)&root->next->next->value->text,
(void*)root->next->next->value,
(void*)root->next->next,
(void*)root->next,
(void*)root
);
raise_log("expected_debug_str: \n%s", expected_debug_str);
raise_notice("expected_debug_str: \n%s", expected_debug_str);
assert(strcmp(debug_str, expected_debug_str) == 0);
}
@@ -107,21 +102,15 @@ static void test_template_node_to_debug_str(Arena *arena) {
static void test_template_section_node_to_debug_str(Arena *arena) {
// Create a section node with a child text node
TemplateNode *root = arena_alloc(arena, sizeof(TemplateNode));
root->type = TEMPLATE_NODE_SECTION;
root->value.section.iterator = arena_strncpy(arena, "item", 4);
root->value.section.collection = arena_strncpy(arena, "items", 5);
// Create a body node (child of section)
root->value.section.body = arena_alloc(arena, sizeof(TemplateNode));
root->value.section.body->type = TEMPLATE_NODE_TEXT;
root->value.section.body->value.text.content = arena_strncpy(arena, "Loop content", 12);
*root = init_template_node(arena, TEMPLATE_NODE_SECTION);
root->value->section.iterator = arena_strncpy(arena, "item", 4);
root->value->section.collection = arena_strncpy(arena, "items", 5);
// Create a body node (child of section)
root->value->section.body = arena_alloc(arena, sizeof(TemplateNode));
*root->value->section.body = init_template_node(arena, TEMPLATE_NODE_TEXT);
root->value->section.body->value->text.content = arena_strncpy(arena, "Loop content", 12);
// SAFETY(yukkop): if any of these are not NULL, the node will be corrupted
root->value.section.body->next = NULL;
root->value.section.body->children = NULL;
root->next = NULL;
root->children = NULL;
char *debug_str = debug_to_pretty_str(arena, TEMPLATE_NODE_TO_DEBUG_STR(arena, "root", root));
raise_log("debug_str: \n%s", debug_str);
@@ -134,14 +123,14 @@ static void test_template_section_node_to_debug_str(Arena *arena) {
char *expected_debug_str = arena_alloc(arena, MEM_KiB);
sprintf(expected_debug_str, TEST_TEMPLATE_SECTION_NODE_TO_DEBUG_STR,
(void*)root->value.section.iterator,
(void*)root->value.section.collection,
(void*)root->value.section.body->value.text.content,
(void*)&root->value.section.body->value.text,
(void*)&root->value.section.body->value,
(void*)root->value.section.body,
(void*)&root->value.section,
(void*)&root->value,
(void*)root->value->section.iterator,
(void*)root->value->section.collection,
(void*)root->value->section.body->value->text.content,
(void*)&root->value->section.body->value->text,
(void*)root->value->section.body->value,
(void*)root->value->section.body,
(void*)&root->value->section,
(void*)root->value,
(void*)root
);
@@ -150,6 +139,47 @@ static void test_template_section_node_to_debug_str(Arena *arena) {
assert(strcmp(debug_str, expected_debug_str) == 0);
}
static void simplest_test_template_parse(Arena *arena, TemplateConfig *config) {
const char *template_str = "{{ name }}";
TemplateResult template_result = template_parse(arena, &template_str, config);
if (IS_RESULT_ERROR(template_result)) {
raise_exception("template_parse failed");
return;
}
TemplateNode node = RESULT_SOME_VALUE(template_result);
{ // some debug output
Arena *debug_arena = DISPOSABLE_ARENA;
const char *json_str = TEMPLATE_NODE_TO_JSON_STR(debug_arena, &node);
Json *json = json_parse(debug_arena, &json_str);
raise_notice("json_str: \n%s", JSON_TO_PRETTY_STR(debug_arena, json));
}
}
static void simplest_interpolation_test_template_parse(Arena *arena, TemplateConfig *config) {
const char *template_str = "{% name %} {% name2 %}";
TemplateResult template_result = template_parse(arena, &template_str, config);
if (IS_RESULT_ERROR(template_result)) {
raise_exception("template_parse failed");
return;
}
TemplateNode node = RESULT_SOME_VALUE(template_result);
{ // some debug output
Arena *debug_arena = DISPOSABLE_ARENA;
const char *json_str = TEMPLATE_NODE_TO_JSON_STR(debug_arena, &node);
Json *json = json_parse(debug_arena, &json_str);
raise_log("json_str: \n%s", json_str);
raise_notice("json_str: \n%s", JSON_TO_PRETTY_STR(debug_arena, json));
}
}
int main(void) {
printf("%sRunning %s%s%s\n", OPTIONAL_COLOR(COLOR_GREEN), OPTIONAL_COLOR(COLOR_CYAN), __FILE__, OPTIONAL_COLOR(COLOR_RESET));
debug_color_mode = COLOR_MODE_DISABLE;
@@ -157,8 +187,6 @@ int main(void) {
Arena arena = arena_init(ARENA_SIZE);
//TemplateConfig config = template_default_config();
printf("%sRunning template parser tests...%s\n", OPTIONAL_COLOR(COLOR_GREEN), OPTIONAL_COLOR(COLOR_RESET));
test_template_node_to_debug_str(&arena);
@@ -169,11 +197,19 @@ int main(void) {
printf("%sTest 1: template_section_node_to_debug_str passed%s\n", OPTIONAL_COLOR(COLOR_GREEN), OPTIONAL_COLOR(COLOR_RESET));
arena_reset(&arena);
//test_template_parse(&arena, &config);
//printf("%sTest 1: template_parse passed%s\n", OPTIONAL_COLOR(COLOR_GREEN), OPTIONAL_COLOR(COLOR_RESET));
//arena_reset(&arena);
Arena config_arena = arena_init(ARENA_SIZE);
TemplateConfig config = template_default_config(&config_arena);
simplest_test_template_parse(&arena, &config);
printf("%sTest 2: simplest_test_template_parse passed%s\n", OPTIONAL_COLOR(COLOR_GREEN), OPTIONAL_COLOR(COLOR_RESET));
arena_reset(&arena);
simplest_interpolation_test_template_parse(&arena, &config);
printf("%sTest 3: simplest_interpolation_test_template_parse passed%s\n", OPTIONAL_COLOR(COLOR_GREEN), OPTIONAL_COLOR(COLOR_RESET));
arena_reset(&arena);
logger_free();
arena_free(&config_arena);
arena_free(&arena);
printf("%sall tests passed %s%s%s\n", OPTIONAL_COLOR(COLOR_GREEN), OPTIONAL_COLOR(COLOR_CYAN), __FILE__, OPTIONAL_COLOR(COLOR_RESET));
return 0;