]> git.ipfire.org Git - thirdparty/jinja.git/commitdiff
Added semantics for pairing blocks together
authorKevin <kevin@kevin-brown.com>
Wed, 13 May 2020 01:27:40 +0000 (21:27 -0400)
committerKevin <kevin@kevin-brown.com>
Wed, 13 May 2020 01:27:40 +0000 (21:27 -0400)
This required us to modify how the parser works so that once it
detects a pair of blocks, it kicks it back to our specific function
which allows us to detect if the pair of blocks it detected were a
matching pair. This is required in order to allow single blocks to
be included within paired blocks, as otherwise it would always match
the last single block to the end block.

This required changing the grammar so the pair blocks had their own
named expression. This allows us to reject the parse as invalid with
incorrect semantics and allows it to try to just parse the first
block alone.

grammar.ebnf
test_tatsu.py

index fd844244db4db2c7b6c81e8999c7f0bb07924d5c..1939e5e0b4764842260e00af1f016751a25564eb 100644 (file)
@@ -39,8 +39,18 @@ raw_block_end
 \r
 block_expression\r
     =\r
-    | ( start:block_start contents:expressions end:block_end )\r
-    | block:block_start\r
+    | block_expression_pair\r
+    | block_expression_single\r
+    ;\r
+\r
+block_expression_pair\r
+    =\r
+    start:block_start contents:expressions end:block_end\r
+    ;\r
+\r
+block_expression_single\r
+    =\r
+    block:block_start\r
     ;\r
 \r
 block_start\r
index 0258952aa1e894636bd1180cbec29c1e1595c46c..4fb2ac651e9ccd3748aeffadb00f2f8c1f5f9d30 100644 (file)
@@ -1,4 +1,5 @@
 from datetime import datetime\r
+from tatsu.exceptions import FailedSemantics\r
 from tatsu.util import asjson\r
 import json\r
 import pprint\r
@@ -8,6 +9,18 @@ from new_parser import parse_template
 from jinja2.environment import Environment\r
 \r
 \r
+class JinjaSemantics(object):\r
+\r
+    def block_expression_pair(self, ast):\r
+        start_block = ast['start']\r
+        end_block = ast['end']\r
+\r
+        if start_block['name'] != end_block['name']:\r
+            raise FailedSemantics()\r
+\r
+        return ast\r
+\r
+\r
 with open('grammar.ebnf', 'r') as tatsu_grammar:\r
     with open('test_template.jinja', 'r') as test_template:\r
         template_string = test_template.read()\r
@@ -20,7 +33,7 @@ with open('grammar.ebnf', 'r') as tatsu_grammar:
 \r
         parse_start = datetime.now()\r
 \r
-        ast = grammar.parse(template_string, whitespace='', parseinfo=True)\r
+        ast = grammar.parse(template_string, whitespace='', parseinfo=True, semantics=JinjaSemantics())\r
 \r
         parse_end = datetime.now()\r
 \r