]> git.ipfire.org Git - thirdparty/jinja.git/commitdiff
Variable filters should not capture whitespace
authorKevin Brown <kevin@kevin-brown.com>
Sat, 9 May 2020 03:53:44 +0000 (23:53 -0400)
committerKevin <kevin@kevin-brown.com>
Sun, 10 May 2020 01:50:06 +0000 (21:50 -0400)
This should have no functional difference on the behaviour of the
parser but it does make filters consistent with other areas where
it expects to start with the first character instead of starting
with whitespace.

This also changes the test script to time the individual steps so
we can confirm that the grammar compilation is considerably slower
than the parsing of the template using the grammar.

tatsu_grammar.txt
test_tatsu.py

index 8195fe0deb1b3a5056dee371c2a2a1e8ba42920a..c08c5d6df65b34fcaba735670a94263b78f5526f 100644 (file)
@@ -126,7 +126,7 @@ block_parameter_value_only
 \r
 variable_expression\r
     =\r
-    variable_open type:`variable` name:variable_identifier { !variable_close filters+:variable_filter }* variable_close\r
+    variable_open type:`variable` name:variable_identifier { !variable_close {SP}* filters+:variable_filter }* variable_close\r
     ;\r
 variable_open\r
     =\r
@@ -229,7 +229,7 @@ variable_tests_logical_operator
 \r
 variable_filter\r
     =\r
-    {SP}* "|" {SP}* @:filter\r
+    "|" {SP}* @:filter\r
     ;\r
 filter =\r
     name:IDENTIFIER\r
index 411ce60f3554d3c7c9474e624d98928163f4b1d6..5ad41abf3d2e030c7c3b2c6da1e567f201a03219 100644 (file)
@@ -1,10 +1,24 @@
 from tatsu.util import asjson\r
 import json\r
 import tatsu\r
+from datetime import datetime\r
 \r
 \r
 with open('tatsu_grammar.txt', 'r') as tatsu_grammar:\r
     with open('test_template.jinja', 'r') as test_template:\r
-        ast = tatsu.parse(tatsu_grammar.read(), test_template.read(), whitespace='')\r
+        grammar_start = datetime.now()\r
 \r
-        print(json.dumps(asjson(ast), indent=4))
\ No newline at end of file
+        grammar = tatsu.compile(tatsu_grammar.read())\r
+\r
+        grammar_end = datetime.now()\r
+\r
+        parse_start = datetime.now()\r
+\r
+        ast = grammar.parse(test_template.read(), whitespace='')\r
+\r
+        parse_end = datetime.now()\r
+\r
+        print(json.dumps(asjson(ast), indent=2))\r
+\r
+        print("Grammar", grammar_end - grammar_start)\r
+        print("Parser", parse_end - parse_start)
\ No newline at end of file