]> git.ipfire.org Git - thirdparty/jinja.git/commitdiff
Implement proper line block closing logic
authorKevin Brown <kevin@kevin-brown.com>
Sat, 16 May 2020 02:21:55 +0000 (22:21 -0400)
committerKevin Brown <kevin@kevin-brown.com>
Sat, 16 May 2020 02:21:55 +0000 (22:21 -0400)
The documentation implies that line statements are terminated once a
newline statement is found, which would make sense when you think of
what a line block does. The lexer though does not quite implement this
logic, and instead will strip out additional whitespace past the newline
when the line statement is the last non-whitespace in the file.

This has to do with how the current regex is "\s*(\n|$)" which means
"strip any whitespace until a newline or end of file is reached".
Because the regex is greedy, this will strip any whitespace (including
newlines) to the end of the file, or it will only strip whitespace to
the first newline found if the end of the file is not possible. In
order to remain consistent with the old parser, the grammar has been
updated to reflect this behaviour.

grammar.ebnf

index 95a003517c3a05f483551eb4006bba65f2c5a489..039d57fb2cc2ce0daaa8ca8f861c9be4a567c57c 100644 (file)
@@ -105,12 +105,12 @@ line_block_expression_single
 \r
 line_block_start\r
     =\r
-    line_block_open !("end") name:IDENTIFIER {SP}* parameters:[ line_block_parameters ] { !"\n" SP }* [ ":" { !"\n" SP }* ] "\n"\r
+    line_block_open !("end") name:IDENTIFIER { !"\n" SP}* parameters:[ line_block_parameters ] [ { !"\n" SP }* ":" ] line_block_close\r
     ;\r
 \r
 line_block_end\r
     =\r
-    line_block_open "end" name:IDENTIFIER ("\n" | $)\r
+    line_block_open "end" name:IDENTIFIER line_block_close\r
     ;\r
 \r
 line_block_open\r
@@ -123,6 +123,12 @@ line_block_open_symbol
     "#"\r
     ;\r
 \r
+line_block_close\r
+    =\r
+    | ( {SP}* $ )\r
+    | ( { !"\n" SP }* "\n" )\r
+    ;\r
+\r
 line_block_parameters\r
     =\r
     @+:block_parameter { { !"\n" SP }* @+:block_parameter }*\r