aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/blogc/debug.c46
-rw-r--r--src/blogc/debug.h2
-rw-r--r--src/blogc/main.c2
-rw-r--r--src/blogc/renderer.c112
-rw-r--r--src/blogc/template-parser.c120
-rw-r--r--src/blogc/template-parser.h43
6 files changed, 164 insertions, 161 deletions
diff --git a/src/blogc/debug.c b/src/blogc/debug.c
index 7219c0c..afb2fe3 100644
--- a/src/blogc/debug.c
+++ b/src/blogc/debug.c
@@ -14,7 +14,7 @@
static const char*
-get_operator(blogc_template_stmt_operator_t op)
+get_operator(blogc_template_operator_t op)
{
if (op & BLOGC_TEMPLATE_OP_NEQ)
return "!=";
@@ -34,45 +34,45 @@ get_operator(blogc_template_stmt_operator_t op)
void
-blogc_debug_template(bc_slist_t *stmts)
+blogc_debug_template(bc_slist_t *ast)
{
- for (bc_slist_t *tmp = stmts; tmp != NULL; tmp = tmp->next) {
- blogc_template_stmt_t *data = tmp->data;
+ for (bc_slist_t *tmp = ast; tmp != NULL; tmp = tmp->next) {
+ blogc_template_node_t *data = tmp->data;
fprintf(stderr, "DEBUG: <TEMPLATE ");
switch (data->type) {
- case BLOGC_TEMPLATE_IFDEF_STMT:
- fprintf(stderr, "IFDEF: %s", data->value);
+ case BLOGC_TEMPLATE_NODE_IFDEF:
+ fprintf(stderr, "IFDEF: %s", data->data[0]);
break;
- case BLOGC_TEMPLATE_IFNDEF_STMT:
- fprintf(stderr, "IFNDEF: %s", data->value);
+ case BLOGC_TEMPLATE_NODE_IFNDEF:
+ fprintf(stderr, "IFNDEF: %s", data->data[0]);
break;
- case BLOGC_TEMPLATE_IF_STMT:
- fprintf(stderr, "IF: %s %s %s", data->value,
- get_operator(data->op), data->value2);
+ case BLOGC_TEMPLATE_NODE_IF:
+ fprintf(stderr, "IF: %s %s %s", data->data[0],
+ get_operator(data->op), data->data[1]);
break;
- case BLOGC_TEMPLATE_ELSE_STMT:
+ case BLOGC_TEMPLATE_NODE_ELSE:
fprintf(stderr, "ELSE");
break;
- case BLOGC_TEMPLATE_ENDIF_STMT:
+ case BLOGC_TEMPLATE_NODE_ENDIF:
fprintf(stderr, "ENDIF");
break;
- case BLOGC_TEMPLATE_FOREACH_STMT:
- fprintf(stderr, "FOREACH: %s", data->value);
+ case BLOGC_TEMPLATE_NODE_FOREACH:
+ fprintf(stderr, "FOREACH: %s", data->data[0]);
break;
- case BLOGC_TEMPLATE_ENDFOREACH_STMT:
+ case BLOGC_TEMPLATE_NODE_ENDFOREACH:
fprintf(stderr, "ENDFOREACH");
break;
- case BLOGC_TEMPLATE_BLOCK_STMT:
- fprintf(stderr, "BLOCK: %s", data->value);
+ case BLOGC_TEMPLATE_NODE_BLOCK:
+ fprintf(stderr, "BLOCK: %s", data->data[0]);
break;
- case BLOGC_TEMPLATE_ENDBLOCK_STMT:
+ case BLOGC_TEMPLATE_NODE_ENDBLOCK:
fprintf(stderr, "ENDBLOCK");
break;
- case BLOGC_TEMPLATE_VARIABLE_STMT:
- fprintf(stderr, "VARIABLE: %s", data->value);
+ case BLOGC_TEMPLATE_NODE_VARIABLE:
+ fprintf(stderr, "VARIABLE: %s", data->data[0]);
break;
- case BLOGC_TEMPLATE_CONTENT_STMT:
- fprintf(stderr, "CONTENT: `%s`", data->value);
+ case BLOGC_TEMPLATE_NODE_CONTENT:
+ fprintf(stderr, "CONTENT: `%s`", data->data[0]);
break;
}
fprintf(stderr, ">\n");
diff --git a/src/blogc/debug.h b/src/blogc/debug.h
index e47d840..c7c1fbe 100644
--- a/src/blogc/debug.h
+++ b/src/blogc/debug.h
@@ -11,6 +11,6 @@
#include "../common/utils.h"
-void blogc_debug_template(bc_slist_t *stmts);
+void blogc_debug_template(bc_slist_t *ast);
#endif /* ___DEBUG_H */
diff --git a/src/blogc/main.c b/src/blogc/main.c
index df9a401..d72091e 100644
--- a/src/blogc/main.c
+++ b/src/blogc/main.c
@@ -346,7 +346,7 @@ main(int argc, char **argv)
cleanup4:
free(out);
cleanup3:
- blogc_template_free_stmts(l);
+ blogc_template_free_ast(l);
cleanup2:
bc_slist_free_full(s, (bc_free_func_t) bc_trie_free);
bc_error_free(err);
diff --git a/src/blogc/renderer.c b/src/blogc/renderer.c
index cee9ef9..a49e87d 100644
--- a/src/blogc/renderer.c
+++ b/src/blogc/renderer.c
@@ -191,53 +191,53 @@ blogc_render(bc_slist_t *tmpl, bc_slist_t *sources, bc_trie_t *config, bool list
bc_slist_t *tmp = tmpl;
while (tmp != NULL) {
- blogc_template_stmt_t *stmt = tmp->data;
+ blogc_template_node_t *node = tmp->data;
- switch (stmt->type) {
+ switch (node->type) {
- case BLOGC_TEMPLATE_CONTENT_STMT:
- if (stmt->value != NULL)
- bc_string_append(str, stmt->value);
+ case BLOGC_TEMPLATE_NODE_CONTENT:
+ if (node->data[0] != NULL)
+ bc_string_append(str, node->data[0]);
break;
- case BLOGC_TEMPLATE_BLOCK_STMT:
+ case BLOGC_TEMPLATE_NODE_BLOCK:
inside_block = true;
if_count = 0;
- if (0 == strcmp("entry", stmt->value)) {
+ if (0 == strcmp("entry", node->data[0])) {
if (listing) {
// we can just skip anything and walk until the next
// 'endblock'
- while (stmt->type != BLOGC_TEMPLATE_ENDBLOCK_STMT) {
+ while (node->type != BLOGC_TEMPLATE_NODE_ENDBLOCK) {
tmp = tmp->next;
- stmt = tmp->data;
+ node = tmp->data;
}
break;
}
current_source = sources;
tmp_source = current_source->data;
}
- else if ((0 == strcmp("listing", stmt->value)) ||
- (0 == strcmp("listing_once", stmt->value))) {
+ else if ((0 == strcmp("listing", node->data[0])) ||
+ (0 == strcmp("listing_once", node->data[0]))) {
if (!listing) {
// we can just skip anything and walk until the next
// 'endblock'
- while (stmt->type != BLOGC_TEMPLATE_ENDBLOCK_STMT) {
+ while (node->type != BLOGC_TEMPLATE_NODE_ENDBLOCK) {
tmp = tmp->next;
- stmt = tmp->data;
+ node = tmp->data;
}
break;
}
}
- if (0 == strcmp("listing", stmt->value)) {
+ if (0 == strcmp("listing", node->data[0])) {
if (sources == NULL) {
// we can just skip anything and walk until the next
// 'endblock'
- while (stmt->type != BLOGC_TEMPLATE_ENDBLOCK_STMT) {
+ while (node->type != BLOGC_TEMPLATE_NODE_ENDBLOCK) {
tmp = tmp->next;
- stmt = tmp->data;
+ node = tmp->data;
}
break;
}
@@ -249,9 +249,9 @@ blogc_render(bc_slist_t *tmpl, bc_slist_t *sources, bc_trie_t *config, bool list
}
break;
- case BLOGC_TEMPLATE_VARIABLE_STMT:
- if (stmt->value != NULL) {
- config_value = blogc_format_variable(stmt->value,
+ case BLOGC_TEMPLATE_NODE_VARIABLE:
+ if (node->data[0] != NULL) {
+ config_value = blogc_format_variable(node->data[0],
config, inside_block ? tmp_source : NULL, foreach_var);
if (config_value != NULL) {
bc_string_append(str, config_value);
@@ -262,7 +262,7 @@ blogc_render(bc_slist_t *tmpl, bc_slist_t *sources, bc_trie_t *config, bool list
}
break;
- case BLOGC_TEMPLATE_ENDBLOCK_STMT:
+ case BLOGC_TEMPLATE_NODE_ENDBLOCK:
inside_block = false;
if (listing_start != NULL && current_source != NULL) {
current_source = current_source->next;
@@ -275,32 +275,32 @@ blogc_render(bc_slist_t *tmpl, bc_slist_t *sources, bc_trie_t *config, bool list
}
break;
- case BLOGC_TEMPLATE_IFNDEF_STMT:
+ case BLOGC_TEMPLATE_NODE_IFNDEF:
if_not = true;
- case BLOGC_TEMPLATE_IF_STMT:
- case BLOGC_TEMPLATE_IFDEF_STMT:
+ case BLOGC_TEMPLATE_NODE_IF:
+ case BLOGC_TEMPLATE_NODE_IFDEF:
if_count = 0;
defined = NULL;
- if (stmt->value != NULL)
- defined = blogc_format_variable(stmt->value, config,
+ if (node->data[0] != NULL)
+ defined = blogc_format_variable(node->data[0], config,
inside_block ? tmp_source : NULL, foreach_var);
evaluate = false;
- if (stmt->op != 0) {
+ if (node->op != 0) {
// Strings that start with a '"' are actually strings, the
// others are meant to be looked up as a second variable
// check.
char *defined2 = NULL;
- if (stmt->value2 != NULL) {
- if ((strlen(stmt->value2) >= 2) &&
- (stmt->value2[0] == '"') &&
- (stmt->value2[strlen(stmt->value2) - 1] == '"'))
+ if (node->data[1] != NULL) {
+ if ((strlen(node->data[1]) >= 2) &&
+ (node->data[1][0] == '"') &&
+ (node->data[1][strlen(node->data[1]) - 1] == '"'))
{
- defined2 = bc_strndup(stmt->value2 + 1,
- strlen(stmt->value2) - 2);
+ defined2 = bc_strndup(node->data[1] + 1,
+ strlen(node->data[1]) - 2);
}
else {
- defined2 = blogc_format_variable(stmt->value2,
+ defined2 = blogc_format_variable(node->data[1],
config, inside_block ? tmp_source : NULL,
foreach_var);
}
@@ -308,13 +308,13 @@ blogc_render(bc_slist_t *tmpl, bc_slist_t *sources, bc_trie_t *config, bool list
if (defined != NULL && defined2 != NULL) {
cmp = strcmp(defined, defined2);
- if (cmp != 0 && stmt->op & BLOGC_TEMPLATE_OP_NEQ)
+ if (cmp != 0 && node->op & BLOGC_TEMPLATE_OP_NEQ)
evaluate = true;
- else if (cmp == 0 && stmt->op & BLOGC_TEMPLATE_OP_EQ)
+ else if (cmp == 0 && node->op & BLOGC_TEMPLATE_OP_EQ)
evaluate = true;
- else if (cmp < 0 && stmt->op & BLOGC_TEMPLATE_OP_LT)
+ else if (cmp < 0 && node->op & BLOGC_TEMPLATE_OP_LT)
evaluate = true;
- else if (cmp > 0 && stmt->op & BLOGC_TEMPLATE_OP_GT)
+ else if (cmp > 0 && node->op & BLOGC_TEMPLATE_OP_GT)
evaluate = true;
}
@@ -333,15 +333,15 @@ blogc_render(bc_slist_t *tmpl, bc_slist_t *sources, bc_trie_t *config, bool list
// skip as well.
while (1) {
tmp = tmp->next;
- stmt = tmp->data;
- if ((stmt->type == BLOGC_TEMPLATE_IF_STMT) ||
- (stmt->type == BLOGC_TEMPLATE_IFDEF_STMT) ||
- (stmt->type == BLOGC_TEMPLATE_IFNDEF_STMT))
+ node = tmp->data;
+ if ((node->type == BLOGC_TEMPLATE_NODE_IF) ||
+ (node->type == BLOGC_TEMPLATE_NODE_IFDEF) ||
+ (node->type == BLOGC_TEMPLATE_NODE_IFNDEF))
{
if_count++;
continue;
}
- if ((stmt->type == BLOGC_TEMPLATE_ELSE_STMT) &&
+ if ((node->type == BLOGC_TEMPLATE_NODE_ELSE) &&
(if_count == 0))
{
// this is somewhat complex. only an else statement
@@ -352,7 +352,7 @@ blogc_render(bc_slist_t *tmpl, bc_slist_t *sources, bc_trie_t *config, bool list
valid_else = true;
break;
}
- if (stmt->type == BLOGC_TEMPLATE_ENDIF_STMT) {
+ if (node->type == BLOGC_TEMPLATE_NODE_ENDIF) {
if (if_count > 0) {
if_count--;
continue;
@@ -369,7 +369,7 @@ blogc_render(bc_slist_t *tmpl, bc_slist_t *sources, bc_trie_t *config, bool list
if_not = false;
break;
- case BLOGC_TEMPLATE_ELSE_STMT:
+ case BLOGC_TEMPLATE_NODE_ELSE:
if_count = 0;
if (!valid_else) {
@@ -378,17 +378,17 @@ blogc_render(bc_slist_t *tmpl, bc_slist_t *sources, bc_trie_t *config, bool list
// skip as well.
while (1) {
tmp = tmp->next;
- stmt = tmp->data;
- if ((stmt->type == BLOGC_TEMPLATE_IF_STMT) ||
- (stmt->type == BLOGC_TEMPLATE_IFDEF_STMT) ||
- (stmt->type == BLOGC_TEMPLATE_IFNDEF_STMT))
+ node = tmp->data;
+ if ((node->type == BLOGC_TEMPLATE_NODE_IF) ||
+ (node->type == BLOGC_TEMPLATE_NODE_IFDEF) ||
+ (node->type == BLOGC_TEMPLATE_NODE_IFNDEF))
{
if_count++;
continue;
}
// no need to handle else statements here, because every
// if should have an endif.
- if (stmt->type == BLOGC_TEMPLATE_ENDIF_STMT) {
+ if (node->type == BLOGC_TEMPLATE_NODE_ENDIF) {
if (if_count > 0) {
if_count--;
continue;
@@ -400,7 +400,7 @@ blogc_render(bc_slist_t *tmpl, bc_slist_t *sources, bc_trie_t *config, bool list
valid_else = false;
break;
- case BLOGC_TEMPLATE_ENDIF_STMT:
+ case BLOGC_TEMPLATE_NODE_ENDIF:
// any endif statement should invalidate valid_else, to avoid
// propagation to outter conditionals.
valid_else = false;
@@ -408,10 +408,10 @@ blogc_render(bc_slist_t *tmpl, bc_slist_t *sources, bc_trie_t *config, bool list
if_count--;
break;
- case BLOGC_TEMPLATE_FOREACH_STMT:
+ case BLOGC_TEMPLATE_NODE_FOREACH:
if (foreach_var_start == NULL) {
- if (stmt->value != NULL)
- foreach_var_start = blogc_split_list_variable(stmt->value,
+ if (node->data[0] != NULL)
+ foreach_var_start = blogc_split_list_variable(node->data[0],
config, inside_block ? tmp_source : NULL);
if (foreach_var_start != NULL) {
@@ -422,9 +422,9 @@ blogc_render(bc_slist_t *tmpl, bc_slist_t *sources, bc_trie_t *config, bool list
// we can just skip anything and walk until the next
// 'endforeach'
- while (stmt->type != BLOGC_TEMPLATE_ENDFOREACH_STMT) {
+ while (node->type != BLOGC_TEMPLATE_NODE_ENDFOREACH) {
tmp = tmp->next;
- stmt = tmp->data;
+ node = tmp->data;
}
break;
}
@@ -436,7 +436,7 @@ blogc_render(bc_slist_t *tmpl, bc_slist_t *sources, bc_trie_t *config, bool list
}
break;
- case BLOGC_TEMPLATE_ENDFOREACH_STMT:
+ case BLOGC_TEMPLATE_NODE_ENDFOREACH:
if (foreach_start != NULL && foreach_var != NULL) {
foreach_var = foreach_var->next;
if (foreach_var != NULL) {
diff --git a/src/blogc/template-parser.c b/src/blogc/template-parser.c
index 6308095..4d2cb3c 100644
--- a/src/blogc/template-parser.c
+++ b/src/blogc/template-parser.c
@@ -55,7 +55,7 @@ blogc_template_parse(const char *src, size_t src_len, bc_error_t **err)
size_t start2 = 0;
size_t end2 = 0;
- blogc_template_stmt_operator_t tmp_op = 0;
+ blogc_template_operator_t tmp_op = 0;
unsigned int if_count = 0;
unsigned int block_if_count = 0;
@@ -63,8 +63,8 @@ blogc_template_parse(const char *src, size_t src_len, bc_error_t **err)
bool foreach_open = false;
bool block_foreach_open = false;
- bc_slist_t *stmts = NULL;
- blogc_template_stmt_t *stmt = NULL;
+ bc_slist_t *ast = NULL;
+ blogc_template_node_t *node = NULL;
/*
* this is a reference to the content of previous node in the singly-linked
@@ -75,14 +75,14 @@ blogc_template_parse(const char *src, size_t src_len, bc_error_t **err)
* - template parser never walk backwards, then the list itself does not
* need to know its previous node.
*/
- blogc_template_stmt_t *previous = NULL;
+ blogc_template_node_t *previous = NULL;
bool lstrip_next = false;
char *tmp = NULL;
char *block_type = NULL;
blogc_template_parser_state_t state = TEMPLATE_START;
- blogc_template_stmt_type_t type = BLOGC_TEMPLATE_CONTENT_STMT;
+ blogc_template_node_type_t type = BLOGC_TEMPLATE_NODE_CONTENT;
bool block_open = false;
@@ -94,23 +94,23 @@ blogc_template_parse(const char *src, size_t src_len, bc_error_t **err)
case TEMPLATE_START:
if (last) {
- stmt = bc_malloc(sizeof(blogc_template_stmt_t));
- stmt->type = type;
+ node = bc_malloc(sizeof(blogc_template_node_t));
+ node->type = type;
if (lstrip_next) {
tmp = bc_strndup(src + start, src_len - start);
- stmt->value = bc_strdup(bc_str_lstrip(tmp));
+ node->data[0] = bc_strdup(bc_str_lstrip(tmp));
free(tmp);
tmp = NULL;
lstrip_next = false;
}
else {
- stmt->value = bc_strndup(src + start, src_len - start);
+ node->data[0] = bc_strndup(src + start, src_len - start);
}
- stmt->op = 0;
- stmt->value2 = NULL;
- stmts = bc_slist_append(stmts, stmt);
- previous = stmt;
- stmt = NULL;
+ node->op = 0;
+ node->data[1] = NULL;
+ ast = bc_slist_append(ast, node);
+ previous = node;
+ node = NULL;
}
if (c == '{') {
end = current;
@@ -125,23 +125,23 @@ blogc_template_parse(const char *src, size_t src_len, bc_error_t **err)
else
state = TEMPLATE_VARIABLE_START;
if (end > start) {
- stmt = bc_malloc(sizeof(blogc_template_stmt_t));
- stmt->type = type;
+ node = bc_malloc(sizeof(blogc_template_node_t));
+ node->type = type;
if (lstrip_next) {
tmp = bc_strndup(src + start, end - start);
- stmt->value = bc_strdup(bc_str_lstrip(tmp));
+ node->data[0] = bc_strdup(bc_str_lstrip(tmp));
free(tmp);
tmp = NULL;
lstrip_next = false;
}
else {
- stmt->value = bc_strndup(src + start, end - start);
+ node->data[0] = bc_strndup(src + start, end - start);
}
- stmt->op = 0;
- stmt->value2 = NULL;
- stmts = bc_slist_append(stmts, stmt);
- previous = stmt;
- stmt = NULL;
+ node->op = 0;
+ node->data[1] = NULL;
+ ast = bc_slist_append(ast, node);
+ previous = node;
+ node = NULL;
}
break;
}
@@ -151,9 +151,9 @@ blogc_template_parse(const char *src, size_t src_len, bc_error_t **err)
case TEMPLATE_BLOCK_START_WHITESPACE_CLEANER:
if (c == '-') {
if ((previous != NULL) &&
- (previous->type == BLOGC_TEMPLATE_CONTENT_STMT))
+ (previous->type == BLOGC_TEMPLATE_NODE_CONTENT))
{
- previous->value = bc_str_rstrip(previous->value); // does not need copy
+ previous->data[0] = bc_str_rstrip(previous->data[0]); // does not need copy
}
state = TEMPLATE_BLOCK_START;
break;
@@ -189,7 +189,7 @@ blogc_template_parse(const char *src, size_t src_len, bc_error_t **err)
{
if (!block_open) {
state = TEMPLATE_BLOCK_BLOCK_TYPE_START;
- type = BLOGC_TEMPLATE_BLOCK_STMT;
+ type = BLOGC_TEMPLATE_NODE_BLOCK;
start = current;
block_if_count = if_count;
block_foreach_open = foreach_open;
@@ -217,7 +217,7 @@ blogc_template_parse(const char *src, size_t src_len, bc_error_t **err)
break;
}
state = TEMPLATE_BLOCK_END_WHITESPACE_CLEANER;
- type = BLOGC_TEMPLATE_ENDBLOCK_STMT;
+ type = BLOGC_TEMPLATE_NODE_ENDBLOCK;
block_open = false;
break;
}
@@ -230,7 +230,7 @@ blogc_template_parse(const char *src, size_t src_len, bc_error_t **err)
(0 == strncmp("ifdef", src + start, 5)))
{
state = TEMPLATE_BLOCK_IF_START;
- type = BLOGC_TEMPLATE_IFDEF_STMT;
+ type = BLOGC_TEMPLATE_NODE_IFDEF;
start = current;
if_count++;
else_open = false;
@@ -240,7 +240,7 @@ blogc_template_parse(const char *src, size_t src_len, bc_error_t **err)
(0 == strncmp("ifndef", src + start, 6)))
{
state = TEMPLATE_BLOCK_IF_START;
- type = BLOGC_TEMPLATE_IFNDEF_STMT;
+ type = BLOGC_TEMPLATE_NODE_IFNDEF;
start = current;
if_count++;
else_open = false;
@@ -250,7 +250,7 @@ blogc_template_parse(const char *src, size_t src_len, bc_error_t **err)
(0 == strncmp("if", src + start, 2)))
{
state = TEMPLATE_BLOCK_IF_START;
- type = BLOGC_TEMPLATE_IF_STMT;
+ type = BLOGC_TEMPLATE_NODE_IF;
start = current;
if_count++;
else_open = false;
@@ -264,7 +264,7 @@ blogc_template_parse(const char *src, size_t src_len, bc_error_t **err)
{
if (!else_open) {
state = TEMPLATE_BLOCK_END_WHITESPACE_CLEANER;
- type = BLOGC_TEMPLATE_ELSE_STMT;
+ type = BLOGC_TEMPLATE_NODE_ELSE;
else_open = true;
break;
}
@@ -287,7 +287,7 @@ blogc_template_parse(const char *src, size_t src_len, bc_error_t **err)
(!block_open && if_count > 0))
{
state = TEMPLATE_BLOCK_END_WHITESPACE_CLEANER;
- type = BLOGC_TEMPLATE_ENDIF_STMT;
+ type = BLOGC_TEMPLATE_NODE_ENDIF;
if_count--;
else_open = false;
break;
@@ -303,7 +303,7 @@ blogc_template_parse(const char *src, size_t src_len, bc_error_t **err)
{
if (!foreach_open) {
state = TEMPLATE_BLOCK_FOREACH_START;
- type = BLOGC_TEMPLATE_FOREACH_STMT;
+ type = BLOGC_TEMPLATE_NODE_FOREACH;
start = current;
foreach_open = true;
break;
@@ -320,7 +320,7 @@ blogc_template_parse(const char *src, size_t src_len, bc_error_t **err)
(!block_open && foreach_open))
{
state = TEMPLATE_BLOCK_END_WHITESPACE_CLEANER;
- type = BLOGC_TEMPLATE_ENDFOREACH_STMT;
+ type = BLOGC_TEMPLATE_NODE_ENDFOREACH;
foreach_open = false;
break;
}
@@ -404,7 +404,7 @@ blogc_template_parse(const char *src, size_t src_len, bc_error_t **err)
break;
if (c == ' ') {
end = current;
- if (type == BLOGC_TEMPLATE_IF_STMT)
+ if (type == BLOGC_TEMPLATE_NODE_IF)
state = TEMPLATE_BLOCK_IF_OPERATOR_START;
else
state = TEMPLATE_BLOCK_END_WHITESPACE_CLEANER;
@@ -528,7 +528,7 @@ blogc_template_parse(const char *src, size_t src_len, bc_error_t **err)
break;
if (c >= 'A' && c <= 'Z') {
state = TEMPLATE_VARIABLE;
- type = BLOGC_TEMPLATE_VARIABLE_STMT;
+ type = BLOGC_TEMPLATE_NODE_VARIABLE;
start = current;
break;
}
@@ -600,25 +600,25 @@ blogc_template_parse(const char *src, size_t src_len, bc_error_t **err)
op_start = 0;
op_end = 0;
}
- stmt = bc_malloc(sizeof(blogc_template_stmt_t));
- stmt->type = type;
- stmt->value = NULL;
- stmt->op = tmp_op;
- stmt->value2 = NULL;
+ node = bc_malloc(sizeof(blogc_template_node_t));
+ node->type = type;
+ node->op = tmp_op;
+ node->data[0] = NULL;
+ node->data[1] = NULL;
if (end > start)
- stmt->value = bc_strndup(src + start, end - start);
+ node->data[0] = bc_strndup(src + start, end - start);
if (end2 > start2) {
- stmt->value2 = bc_strndup(src + start2, end2 - start2);
+ node->data[1] = bc_strndup(src + start2, end2 - start2);
start2 = 0;
end2 = 0;
}
- if (type == BLOGC_TEMPLATE_BLOCK_STMT)
- block_type = stmt->value;
- stmts = bc_slist_append(stmts, stmt);
- previous = stmt;
- stmt = NULL;
+ if (type == BLOGC_TEMPLATE_NODE_BLOCK)
+ block_type = node->data[0];
+ ast = bc_slist_append(ast, node);
+ previous = node;
+ node = NULL;
state = TEMPLATE_START;
- type = BLOGC_TEMPLATE_CONTENT_STMT;
+ type = BLOGC_TEMPLATE_NODE_CONTENT;
start = current + 1;
break;
}
@@ -652,28 +652,28 @@ blogc_template_parse(const char *src, size_t src_len, bc_error_t **err)
}
if (*err != NULL) {
- if (stmt != NULL) {
- free(stmt->value);
- free(stmt);
+ if (node != NULL) {
+ free(node->data[0]);
+ free(node);
}
- blogc_template_free_stmts(stmts);
+ blogc_template_free_ast(ast);
return NULL;
}
- return stmts;
+ return ast;
}
void
-blogc_template_free_stmts(bc_slist_t *stmts)
+blogc_template_free_ast(bc_slist_t *ast)
{
- for (bc_slist_t *tmp = stmts; tmp != NULL; tmp = tmp->next) {
- blogc_template_stmt_t *data = tmp->data;
+ for (bc_slist_t *tmp = ast; tmp != NULL; tmp = tmp->next) {
+ blogc_template_node_t *data = tmp->data;
if (data == NULL)
continue;
- free(data->value);
- free(data->value2);
+ free(data->data[0]);
+ free(data->data[1]);
free(data);
}
- bc_slist_free(stmts);
+ bc_slist_free(ast);
}
diff --git a/src/blogc/template-parser.h b/src/blogc/template-parser.h
index 63aa404..948ec9b 100644
--- a/src/blogc/template-parser.h
+++ b/src/blogc/template-parser.h
@@ -14,40 +14,43 @@
#include "../common/utils.h"
/*
- * note: whitespace cleaners are NOT added to ast. we fix strings right during
+ * note: whitespace cleaners are NOT added to AST. we fix strings right during
* template parsing. renderer does not need to care about it, for the sake of
* simplicity.
+ *
+ * another note: technically this is not an AST, because there are no childs.
*/
typedef enum {
- BLOGC_TEMPLATE_IFDEF_STMT = 1,
- BLOGC_TEMPLATE_IFNDEF_STMT,
- BLOGC_TEMPLATE_IF_STMT,
- BLOGC_TEMPLATE_ELSE_STMT,
- BLOGC_TEMPLATE_ENDIF_STMT,
- BLOGC_TEMPLATE_FOREACH_STMT,
- BLOGC_TEMPLATE_ENDFOREACH_STMT,
- BLOGC_TEMPLATE_BLOCK_STMT,
- BLOGC_TEMPLATE_ENDBLOCK_STMT,
- BLOGC_TEMPLATE_VARIABLE_STMT,
- BLOGC_TEMPLATE_CONTENT_STMT,
-} blogc_template_stmt_type_t;
+ BLOGC_TEMPLATE_NODE_IFDEF = 1,
+ BLOGC_TEMPLATE_NODE_IFNDEF,
+ BLOGC_TEMPLATE_NODE_IF,
+ BLOGC_TEMPLATE_NODE_ELSE,
+ BLOGC_TEMPLATE_NODE_ENDIF,
+ BLOGC_TEMPLATE_NODE_FOREACH,
+ BLOGC_TEMPLATE_NODE_ENDFOREACH,
+ BLOGC_TEMPLATE_NODE_BLOCK,
+ BLOGC_TEMPLATE_NODE_ENDBLOCK,
+ BLOGC_TEMPLATE_NODE_VARIABLE,
+ BLOGC_TEMPLATE_NODE_CONTENT,
+} blogc_template_node_type_t;
typedef enum {
BLOGC_TEMPLATE_OP_NEQ = 1 << 0,
BLOGC_TEMPLATE_OP_EQ = 1 << 1,
BLOGC_TEMPLATE_OP_LT = 1 << 2,
BLOGC_TEMPLATE_OP_GT = 1 << 3,
-} blogc_template_stmt_operator_t;
+} blogc_template_operator_t;
typedef struct {
- blogc_template_stmt_type_t type;
- char *value;
- char *value2;
- blogc_template_stmt_operator_t op;
-} blogc_template_stmt_t;
+ blogc_template_node_type_t type;
+ blogc_template_operator_t op;
+
+ // 2 slots to store node data.
+ char *data[2];
+} blogc_template_node_t;
bc_slist_t* blogc_template_parse(const char *src, size_t src_len,
bc_error_t **err);
-void blogc_template_free_stmts(bc_slist_t *stmts);
+void blogc_template_free_ast(bc_slist_t *ast);
#endif /* _TEMPLATE_PARSER_H */