From 6aee631ac1afe14bd8130df311472d3c552d8bfa Mon Sep 17 00:00:00 2001 From: Danny van Kooten Date: Sun, 15 Mar 2020 13:59:52 +0100 Subject: add number & string literals --- src/template.c | 48 ++++++++++++++++++++++++++++++++++++------------ tests/test_template.c | 16 +++++++++++++++- 2 files changed, 51 insertions(+), 13 deletions(-) diff --git a/src/template.c b/src/template.c index 6211b19..f357d45 100644 --- a/src/template.c +++ b/src/template.c @@ -53,10 +53,16 @@ int eval(char *dest, mpc_ast_t* t, struct hashmap *ctx) { if (strstr(t->children[3]->contents, "-")) { trim_whitespace = 1; } - + char *value = NULL; - char *key = t->children[2]->contents; - value = hashmap_resolve(ctx, key); + if (ctx != NULL && strstr(t->children[2]->tag, "symbol")) { + char *key = t->children[2]->contents; + value = hashmap_resolve(ctx, key); + } else if(strstr(t->children[2]->tag, "number")) { + value = t->children[2]->contents; + } else if(strstr(t->children[2]->tag, "string")) { + value = t->children[2]->children[1]->contents; + } // TODO: Handle unexisting keys if (value == NULL) { @@ -66,7 +72,7 @@ int eval(char *dest, mpc_ast_t* t, struct hashmap *ctx) { return 0; } - if (strstr(t->tag, "content|for")) { + if (strstr(t->tag, "content|statement|for")) { char *tmp_key = t->children[2]->contents; char *iterator_key = t->children[4]->contents; struct vector *list = hashmap_resolve(ctx, iterator_key); @@ -97,34 +103,52 @@ int eval(char *dest, mpc_ast_t* t, struct hashmap *ctx) { mpc_parser_t *parser_init() { mpc_parser_t *symbol = mpc_new("symbol"); + mpc_parser_t *number = mpc_new("number"); + mpc_parser_t *string = mpc_new("string"); mpc_parser_t *text = mpc_new("text"); mpc_parser_t *expression = mpc_new("expression"); - mpc_parser_t *statement = mpc_new("statement_open"); + mpc_parser_t *comment = mpc_new("comment"); + mpc_parser_t *statement = mpc_new("statement"); mpc_parser_t *statement_open = mpc_new("statement_open"); mpc_parser_t *statement_close = mpc_new("statement_close"); - mpc_parser_t *comment = mpc_new("comment"); - mpc_parser_t *for_statement = mpc_new("for"); + mpc_parser_t *statement_for = mpc_new("for"); + mpc_parser_t *statement_if = mpc_new("if"); + mpc_parser_t *statement_block = mpc_new("block"); + mpc_parser_t *statement_extends = mpc_new("extends"); mpc_parser_t *body = mpc_new("body"); mpc_parser_t *content = mpc_new("content"); mpc_parser_t *template = mpc_new("template"); mpca_lang(MPCA_LANG_WHITESPACE_SENSITIVE, " symbol : /[a-zA-Z_.]+/ ;" + " number : /[0-9]+/ ;" + " string : '\"' /([^\"])*/ '\"' ;" " text : /[^{][^{%#]*/ ;" - " expression : \"{{\" /-? */ / *-?/ \"}}\" ;" + " expression : \"{{\" /-? */ ( | | ) / *-?/ \"}}\" ;" " comment : \"{#\" /[^#][^#}]*/ \"#}\" ;" " statement_open: \"{%\" /-? */;" " statement_close: / *-?/ \"%}\";" " for : \"for \" \" in \" \"endfor\" ;" - " content : | | | ;" + " block : \"block \" \"endblock\" ;" + " extends : \"extends \" ;" + /* TODO: Extend parser to include expression */ + " if : \"if \" \"endif\" ;" + " statement : | | | ;" + " content : | | | ;" " body : * ;" " template : /^/ /$/ ;", symbol, + number, + string, expression, + text, + comment, statement_open, statement_close, - comment, - for_statement, - text, + statement, + statement_if, + statement_for, + statement_block, + statement_extends, content, body, template, diff --git a/tests/test_template.c b/tests/test_template.c index 90190cc..5c17dd4 100644 --- a/tests/test_template.c +++ b/tests/test_template.c @@ -10,7 +10,21 @@ TEST(text_only) { free(output); } -TEST(var) { +TEST(expr_number) { + char *input = "Hello {{ 5 }}."; + char *output = template(input, NULL); + assert_str(output, "Hello 5."); + free(output); +} + +TEST(expr_string) { + char *input = "Hello {{ \"world\" }}."; + char *output = template(input, NULL); + assert_str(output, "Hello world."); + free(output); +} + +TEST(expr_symbol) { char *input = "Hello {{name}}."; struct hashmap *ctx = hashmap_new(); hashmap_insert(ctx, "name", "world"); -- cgit v1.2.3