diff options
-rw-r--r-- | askama_parser/src/lib.rs | 2 | ||||
-rw-r--r-- | askama_parser/src/node.rs | 30 | ||||
-rw-r--r-- | testing/tests/ui/name_mismatch_endblock.stderr | 3 |
3 files changed, 32 insertions, 3 deletions
diff --git a/askama_parser/src/lib.rs b/askama_parser/src/lib.rs index ba5f6a4..5abc771 100644 --- a/askama_parser/src/lib.rs +++ b/askama_parser/src/lib.rs @@ -101,7 +101,7 @@ impl<'a> Ast<'a> { let msg = format!( "{}problems parsing template source at row {}, column {} near:\n{}", if let Some(message) = message { - format!("error: {message}\n") + format!("{message}\n") } else { String::new() }, diff --git a/askama_parser/src/node.rs b/askama_parser/src/node.rs index 596b99a..b321c80 100644 --- a/askama_parser/src/node.rs +++ b/askama_parser/src/node.rs @@ -1,3 +1,4 @@ +use std::borrow::Cow; use std::str; use nom::branch::alt; @@ -650,7 +651,13 @@ impl<'a> BlockDef<'a> { |i| s.tag_block_start(i), opt(Whitespace::parse), ws(keyword("endblock")), - cut(tuple((opt(ws(keyword(name))), opt(Whitespace::parse)))), + cut(tuple(( + opt(|before| { + let (after, end_name) = ws(identifier)(before)?; + check_end_name(before, after, name, end_name, "block") + }), + opt(Whitespace::parse), + ))), ))), ))); let (i, (nodes, (_, pws2, _, (_, nws2)))) = end(i)?; @@ -667,6 +674,27 @@ impl<'a> BlockDef<'a> { } } +fn check_end_name<'a>( + before: &'a str, + after: &'a str, + name: &'a str, + end_name: &'a str, + kind: &str, +) -> IResult<&'a str, &'a str, ErrorContext<&'a str>> { + if name == end_name { + return Ok((after, end_name)); + } + let message = if name.is_empty() && !end_name.is_empty() { + format!("unexpected name `{end_name}` in `end{kind}` tag for unnamed `{kind}`") + } else { + format!("expected name `{name}` in `end{kind}` tag, found `{end_name}`") + }; + Err(nom::Err::Failure(ErrorContext { + input: before, + message: Some(Cow::Owned(message)), + })) +} + #[derive(Debug, PartialEq)] pub struct Lit<'a> { pub lws: &'a str, diff --git a/testing/tests/ui/name_mismatch_endblock.stderr b/testing/tests/ui/name_mismatch_endblock.stderr index 2cd7aaf..958c413 100644 --- a/testing/tests/ui/name_mismatch_endblock.stderr +++ b/testing/tests/ui/name_mismatch_endblock.stderr @@ -1,4 +1,5 @@ -error: problems parsing template source at row 1, column 27 near: +error: expected name `foo` in `endblock` tag, found `not_foo` + problems parsing template source at row 1, column 27 near: "not_foo %}" --> tests/ui/name_mismatch_endblock.rs:3:10 | |