]> git.ipfire.org Git - thirdparty/jinja.git/commitdiff
Added grammar for `for` loops
authorKevin Brown <kevin@kevin-brown.com>
Fri, 8 May 2020 13:54:35 +0000 (09:54 -0400)
committerKevin <kevin@kevin-brown.com>
Sun, 10 May 2020 01:50:06 +0000 (21:50 -0400)
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.

tatsu_grammar.txt
test_template.jinja

index 45f8ecab21415b60a9e4bf929fb648f74e1dbb61..e974a080db5dc89607a5a11d7c369c4e8f48a92f 100644 (file)
@@ -11,7 +11,7 @@ expressions
 \r
 expression\r
     =\r
-    content | raw_block_expression | block_expression | variable_expression | comment_expression\r
+    content | raw_block_expression | functional_block_expression | variable_expression | comment_expression\r
     ;\r
 \r
 raw_block_expression\r
@@ -31,6 +31,32 @@ raw_block_end
     block_open "endraw" block_close\r
     ;\r
 \r
+functional_block_expression\r
+    =\r
+    for_block_expression | block_expression\r
+    ;\r
+\r
+for_block_expression\r
+    =\r
+    for_block_start {SP}* expressions {SP}* for_block_end\r
+    ;\r
+\r
+for_block_start\r
+    =\r
+    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\r
+    ;\r
+\r
+for_block_end\r
+    =\r
+    block_open "endfor" block_close\r
+    ;\r
+\r
+assignment_target\r
+    =\r
+    tuple+:block_parameter {"," {SP}* tuple+:block_parameter}+\r
+    | block_parameter\r
+    ;\r
+\r
 block_expression\r
     =\r
     block_start {SP}* expressions {SP}* block_end\r
@@ -39,7 +65,7 @@ block_expression
 \r
 block_start\r
     =\r
-    block_open type:`blockstart` !("end") name:IDENTIFIER parameters:[{block_parameter}*] block_close\r
+    block_open type:`blockstart` !("end") name:IDENTIFIER parameters:[ { block_parameter }* ] block_close\r
     ;\r
 \r
 block_end\r
index 8cde1b6ed5a9b398dd5c82570b20a55d57bec467..a24a0c907a608ffba0eaae34344163a8eeaae054 100644 (file)
@@ -14,7 +14,7 @@ across lines #}
 {% if False %}\r
 {{ '{{' }}\r
 {% endif %}\r
-{% for href caption in [('index.html', 'Index'), ('about.html', 'About'),\r
+{% for href, caption in [('index.html', 'Index'), ('about.html', 'About'),\r
                          ('downloads.html', 'Downloads')] %}\r
     <li><a href="{{ href }}">{{ caption }}</a></li>\r
 {% endfor %}\r