From 3e4497d3a6e8bce7c73bbf46707f84a0ab0c7543 Mon Sep 17 00:00:00 2001 From: teodor Date: Tue, 7 Oct 2008 13:22:45 +0000 Subject: [PATCH 1/1] Add <@ elsif expression @> --- data/template.tmpl | 8 +++++ expected/tmpl | 8 +++++ template.h | 1 + tmpl_gram.y | 76 +++++++++++++++++++++++++++++++++++++--------- tmpl_scan.l | 1 + 5 files changed, 80 insertions(+), 14 deletions(-) diff --git a/data/template.tmpl b/data/template.tmpl index 7b65074..d954d08 100644 --- a/data/template.tmpl +++ b/data/template.tmpl @@ -75,3 +75,11 @@ str ? "yes" : -1 = <% str ? "yes" : -1 %> <@ LOOP oneloop@><@if __FIRST @>
    <@endif@>
  1. <% __COUNTER %> oneloop:<% NODE %>#<% CNT %> ID:<% ^id %><@if __LAST @>
<@endif@><@ ENDLOOP @> <@if __LAST @><@endif@> <@ ENDLOOP @> + +<@ IF id == 17 @> + 17 +<@ else if id ==23 @> + 23 +<@ else @> + unknown; +<@endif@> diff --git a/expected/tmpl b/expected/tmpl index 7bf4401..9c6cca7 100644 --- a/expected/tmpl +++ b/expected/tmpl @@ -79,6 +79,10 @@ str ? "yes" : -1 = yes + + + 17 + ================================================ id: 23 - simple idhex: 0x00000017 - HEX @@ -209,3 +213,7 @@ str ? "yes" : -1 = -1 + + + 23 + diff --git a/template.h b/template.h index cd80d75..1109dac 100644 --- a/template.h +++ b/template.h @@ -38,6 +38,7 @@ * for all other. Currently, bool values have only "true"/"false" * string values * <@IF EXPRESSION @> + * [<@ ELSE IF EXPRESSION | ELSIF EXPRESSION @>] * [ <@ELSE@> ] * <@ENDIF@> * diff --git a/tmpl_gram.y b/tmpl_gram.y index b3a56ed..82267c3 100644 --- a/tmpl_gram.y +++ b/tmpl_gram.y @@ -33,11 +33,13 @@ static GList *makeList2(void *a, void *b); } %type node +%type ifnode elsenode elsifnode %type listnodes %type template %type expression %type list_expression %type varname +%type elsif %type opt_escape %type opt_format @@ -49,7 +51,7 @@ static GList *makeList2(void *a, void *b); %token LEXEME %token VAR_OPEN VAR_CLOSE EXPR_OPEN EXPR_CLOSE INCLUDE_OPEN INCLUDE_CLOSE -%token HTMLESCAPE URLESCAPE IF_P ELSE_P LOOP_P ENDIF_P ENDLOOP_P SELF_P +%token HTMLESCAPE URLESCAPE IF_P ELSIF_P ELSE_P LOOP_P ENDIF_P ENDLOOP_P SELF_P %token CMP_P %token INTEGER @@ -206,6 +208,64 @@ expression: | '(' expression ')' { $$=$2; } ; +elsif: + ELSIF_P + | ELSE_P IF_P + ; + +elsenode: + EXPR_OPEN ELSE_P EXPR_CLOSE listnodes EXPR_OPEN ENDIF_P EXPR_CLOSE { + $$ = $4; + } + ; + +elsifnode: + EXPR_OPEN elsif expression EXPR_CLOSE listnodes EXPR_OPEN ENDIF_P EXPR_CLOSE { + $$ = mc0alloc( curTmpl->templateContext, sizeof(TemplateNodeData) ); + $$->type = ConditionNode; + $$->nodeData.condition.expressionNode = $3; + $$->nodeData.condition.ifNode = $5; + } + | EXPR_OPEN elsif expression EXPR_CLOSE listnodes elsenode { + $$ = mc0alloc( curTmpl->templateContext, sizeof(TemplateNodeData) ); + $$->type = ConditionNode; + $$->nodeData.condition.expressionNode = $3; + $$->nodeData.condition.ifNode = $5; + $$->nodeData.condition.elseNode = $6; + } + | EXPR_OPEN elsif expression EXPR_CLOSE listnodes elsifnode { + $$ = mc0alloc( curTmpl->templateContext, sizeof(TemplateNodeData) ); + $$->type = ConditionNode; + $$->nodeData.condition.expressionNode = $3; + $$->nodeData.condition.ifNode = $5; + $$->nodeData.condition.elseNode = $6; + } + ; + +ifnode: + EXPR_OPEN IF_P expression EXPR_CLOSE listnodes EXPR_OPEN ENDIF_P EXPR_CLOSE { + $$ = mc0alloc( curTmpl->templateContext, sizeof(TemplateNodeData) ); + $$->type = ConditionNode; + $$->nodeData.condition.expressionNode = $3; + $$->nodeData.condition.ifNode = $5; + } + | EXPR_OPEN IF_P expression EXPR_CLOSE listnodes elsenode { + $$ = mc0alloc( curTmpl->templateContext, sizeof(TemplateNodeData) ); + $$->type = ConditionNode; + $$->nodeData.condition.expressionNode = $3; + $$->nodeData.condition.ifNode = $5; + $$->nodeData.condition.elseNode = $6; + } + | EXPR_OPEN IF_P expression EXPR_CLOSE listnodes elsifnode { + $$ = mc0alloc( curTmpl->templateContext, sizeof(TemplateNodeData) ); + $$->type = ConditionNode; + $$->nodeData.condition.expressionNode = $3; + $$->nodeData.condition.ifNode = $5; + $$->nodeData.condition.elseNode = $6; + } + ; + + node: TEXT_P { $$ = mc0alloc( curTmpl->templateContext, sizeof(TemplateNodeData) ); @@ -237,19 +297,7 @@ node: $$->nodeData.loop.varNameLength = strlen($3); $$->nodeData.loop.bodyNode = $5; } - | EXPR_OPEN IF_P expression EXPR_CLOSE listnodes EXPR_OPEN ENDIF_P EXPR_CLOSE { - $$ = mc0alloc( curTmpl->templateContext, sizeof(TemplateNodeData) ); - $$->type = ConditionNode; - $$->nodeData.condition.expressionNode = $3; - $$->nodeData.condition.ifNode = $5; - } - | EXPR_OPEN IF_P expression EXPR_CLOSE listnodes EXPR_OPEN ELSE_P EXPR_CLOSE listnodes EXPR_OPEN ENDIF_P EXPR_CLOSE { - $$ = mc0alloc( curTmpl->templateContext, sizeof(TemplateNodeData) ); - $$->type = ConditionNode; - $$->nodeData.condition.expressionNode = $3; - $$->nodeData.condition.ifNode = $5; - $$->nodeData.condition.elseNode = $9; - } + | ifnode {$$=$1;} ; %% diff --git a/tmpl_scan.l b/tmpl_scan.l index 5024589..81be52f 100644 --- a/tmpl_scan.l +++ b/tmpl_scan.l @@ -206,6 +206,7 @@ static KeyWord keywords[] = { { "ELSE", 0, ELSE_P }, { "LOOP", 0, LOOP_P }, { "SELF", 0, SELF_P }, + { "ELSIF", 0, ELSIF_P }, { "ENDIF", 0, ENDIF_P }, { "ENDLOOP", 0, ENDLOOP_P } }; -- 2.46.1