From 5efd31310f3d91dc69041b0a8dc5251f88aa2d9d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Kijewski?= Date: Wed, 7 Aug 2024 20:43:18 +0200 Subject: [PATCH] parser: understand arbitrarily mixed up end nodes --- rinja_parser/src/node.rs | 2 +- testing/tests/ui/typo_in_keyword.stderr | 2 +- testing/tests/ui/wrong-end.rs | 4 ++++ testing/tests/ui/wrong-end.stderr | 8 ++++++++ 4 files changed, 14 insertions(+), 2 deletions(-) diff --git a/rinja_parser/src/node.rs b/rinja_parser/src/node.rs index de0a5ccb..ef16a8b4 100644 --- a/rinja_parser/src/node.rs +++ b/rinja_parser/src/node.rs @@ -1058,7 +1058,7 @@ fn end_node<'a, 'g: 'a>( let (i, actual) = ws(identifier)(start)?; if actual == expected { Ok((i, actual)) - } else if actual == "end" { + } else if actual.starts_with("end") { Err(nom::Err::Failure(ErrorContext::new( format!("expected `{expected}` to terminate `{node}` node, found `{actual}`"), start, diff --git a/testing/tests/ui/typo_in_keyword.stderr b/testing/tests/ui/typo_in_keyword.stderr index b18eccee..bd9daecd 100644 --- a/testing/tests/ui/typo_in_keyword.stderr +++ b/testing/tests/ui/typo_in_keyword.stderr @@ -1,4 +1,4 @@ -error: failed to parse template source +error: expected `endfor` to terminate `for` node, found `endfo` --> :1:26 "endfo%}\n1234567890123456789012345678901234567890" --> tests/ui/typo_in_keyword.rs:5:14 diff --git a/testing/tests/ui/wrong-end.rs b/testing/tests/ui/wrong-end.rs index 9d267716..0e43f5f3 100644 --- a/testing/tests/ui/wrong-end.rs +++ b/testing/tests/ui/wrong-end.rs @@ -24,4 +24,8 @@ struct Block; #[template(source = "{% if true %}{% end %}", ext = "txt")] struct If; +#[derive(Template)] +#[template(source = "{% if true %}{% endfor %}", ext = "txt")] +struct IfFor; + fn main() {} diff --git a/testing/tests/ui/wrong-end.stderr b/testing/tests/ui/wrong-end.stderr index fffff991..eb61b1f6 100644 --- a/testing/tests/ui/wrong-end.stderr +++ b/testing/tests/ui/wrong-end.stderr @@ -45,3 +45,11 @@ error: expected `endif` to terminate `if` node, found `end` | 24 | #[template(source = "{% if true %}{% end %}", ext = "txt")] | ^^^^^^^^^^^^^^^^^^^^^^^^ + +error: expected `endif` to terminate `if` node, found `endfor` + --> :1:15 + " endfor %}" + --> tests/ui/wrong-end.rs:28:21 + | +28 | #[template(source = "{% if true %}{% endfor %}", ext = "txt")] + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^