From: Kevin Brown Date: Fri, 8 May 2020 13:54:35 +0000 (-0400) Subject: Added grammar for `for` loops X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=f69ad20fb5ed907fe164f69259a952a5ea646716;p=thirdparty%2Fjinja.git Added grammar for `for` loops Since this needs to support assignment targets, it is difficult to properly support this within a generic block syntax because of the ability to create implicit tuples. --- diff --git a/tatsu_grammar.txt b/tatsu_grammar.txt index 45f8ecab..e974a080 100644 --- a/tatsu_grammar.txt +++ b/tatsu_grammar.txt @@ -11,7 +11,7 @@ expressions expression = - content | raw_block_expression | block_expression | variable_expression | comment_expression + content | raw_block_expression | functional_block_expression | variable_expression | comment_expression ; raw_block_expression @@ -31,6 +31,32 @@ raw_block_end block_open "endraw" block_close ; +functional_block_expression + = + for_block_expression | block_expression + ; + +for_block_expression + = + for_block_start {SP}* expressions {SP}* for_block_end + ; + +for_block_start + = + block_open type:`blockstart` name:"for" {SP}* target:assignment_target {SP}* "in" {SP}* iter:variable_accessor_call_parameter_value [ {SP}* recursive:`True` "recursive" ] block_close + ; + +for_block_end + = + block_open "endfor" block_close + ; + +assignment_target + = + tuple+:block_parameter {"," {SP}* tuple+:block_parameter}+ + | block_parameter + ; + block_expression = block_start {SP}* expressions {SP}* block_end @@ -39,7 +65,7 @@ block_expression block_start = - block_open type:`blockstart` !("end") name:IDENTIFIER parameters:[{block_parameter}*] block_close + block_open type:`blockstart` !("end") name:IDENTIFIER parameters:[ { block_parameter }* ] block_close ; block_end diff --git a/test_template.jinja b/test_template.jinja index 8cde1b6e..a24a0c90 100644 --- a/test_template.jinja +++ b/test_template.jinja @@ -14,7 +14,7 @@ across lines #} {% if False %} {{ '{{' }} {% endif %} -{% for href caption in [('index.html', 'Index'), ('about.html', 'About'), +{% for href, caption in [('index.html', 'Index'), ('about.html', 'About'), ('downloads.html', 'Downloads')] %}
  • {{ caption }}
  • {% endfor %}