From 222c42917aa19162562b8a9c35350e3e992f9823 Mon Sep 17 00:00:00 2001 From: René Kijewski Date: Tue, 1 Aug 2023 03:09:35 +0200 Subject: parser: let `Macro` know its name --- askama_parser/src/node.rs | 105 +++++++++++++++++++++++----------------------- 1 file changed, 53 insertions(+), 52 deletions(-) (limited to 'askama_parser/src') diff --git a/askama_parser/src/node.rs b/askama_parser/src/node.rs index 4a0fa30..f15aa5a 100644 --- a/askama_parser/src/node.rs +++ b/askama_parser/src/node.rs @@ -29,7 +29,7 @@ pub enum Node<'a> { BlockDef(Ws, &'a str, Vec>, Ws), Include(Ws, &'a str), Import(Ws, &'a str, &'a str), - Macro(&'a str, Macro<'a>), + Macro(Macro<'a>), Raw(Ws, &'a str, &'a str, &'a str, Ws), Break(Ws), Continue(Ws), @@ -78,7 +78,7 @@ impl<'a> Node<'a> { Self::include, Self::import, |i| Self::block(i, s), - |i| Self::r#macro(i, s), + map(|i| Macro::parse(i, s), Self::Macro), |i| Self::raw(i, s), |i| Self::r#break(i, s), |i| Self::r#continue(i, s), @@ -315,56 +315,6 @@ impl<'a> Node<'a> { Ok((i, Self::Import(Ws(pws, nws), name, scope))) } - fn r#macro(i: &'a str, s: &State<'_>) -> IResult<&'a str, Self> { - fn parameters(i: &str) -> IResult<&str, Vec<&str>> { - delimited( - ws(char('(')), - separated_list0(char(','), ws(identifier)), - ws(char(')')), - )(i) - } - - let mut start = tuple(( - opt(Whitespace::parse), - ws(keyword("macro")), - cut(tuple(( - ws(identifier), - opt(ws(parameters)), - opt(Whitespace::parse), - |i| s.tag_block_end(i), - ))), - )); - let (i, (pws1, _, (name, params, nws1, _))) = start(i)?; - - let mut end = cut(tuple(( - |i| Self::many(i, s), - cut(tuple(( - |i| s.tag_block_start(i), - opt(Whitespace::parse), - ws(keyword("endmacro")), - cut(tuple((opt(ws(keyword(name))), opt(Whitespace::parse)))), - ))), - ))); - let (i, (contents, (_, pws2, _, (_, nws2)))) = end(i)?; - - assert_ne!(name, "super", "invalid macro name 'super'"); - - let params = params.unwrap_or_default(); - - Ok(( - i, - Self::Macro( - name, - Macro { - ws1: Ws(pws1, nws1), - args: params, - nodes: contents, - ws2: Ws(pws2, nws2), - }, - ), - )) - } - fn raw(i: &'a str, s: &State<'_>) -> IResult<&'a str, Self> { let endraw = tuple(( |i| s.tag_block_start(i), @@ -722,11 +672,62 @@ pub struct Loop<'a> { #[derive(Debug, PartialEq)] pub struct Macro<'a> { pub ws1: Ws, + pub name: &'a str, pub args: Vec<&'a str>, pub nodes: Vec>, pub ws2: Ws, } +impl<'a> Macro<'a> { + fn parse(i: &'a str, s: &State<'_>) -> IResult<&'a str, Self> { + fn parameters(i: &str) -> IResult<&str, Vec<&str>> { + delimited( + ws(char('(')), + separated_list0(char(','), ws(identifier)), + ws(char(')')), + )(i) + } + + let mut start = tuple(( + opt(Whitespace::parse), + ws(keyword("macro")), + cut(tuple(( + ws(identifier), + opt(ws(parameters)), + opt(Whitespace::parse), + |i| s.tag_block_end(i), + ))), + )); + let (i, (pws1, _, (name, params, nws1, _))) = start(i)?; + + let mut end = cut(tuple(( + |i| Node::many(i, s), + cut(tuple(( + |i| s.tag_block_start(i), + opt(Whitespace::parse), + ws(keyword("endmacro")), + cut(tuple((opt(ws(keyword(name))), opt(Whitespace::parse)))), + ))), + ))); + let (i, (contents, (_, pws2, _, (_, nws2)))) = end(i)?; + + assert_ne!(name, "super", "invalid macro name 'super'"); + + let params = params.unwrap_or_default(); + + Ok(( + i, + Self { + ws1: Ws(pws1, nws1), + name, + args: params, + nodes: contents, + ws2: Ws(pws2, nws2), + }, + )) + } +} + /// First field is "minus/plus sign was used on the left part of the item". /// /// Second field is "minus/plus sign was used on the right part of the item". -- cgit