From c3281e353d8192e6425f903216eeb6ecf9dd39fc Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Mon, 23 Oct 2023 21:15:08 +0200 Subject: Improve error for invalid name used in `endblock` --- askama_parser/src/lib.rs | 2 +- askama_parser/src/node.rs | 30 +++++++++++++++++++++++++++++- 2 files changed, 30 insertions(+), 2 deletions(-) (limited to 'askama_parser/src') 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, -- cgit