diff options
author | René Kijewski <rene.kijewski@fu-berlin.de> | 2023-08-01 03:38:56 +0200 |
---|---|---|
committer | Dirkjan Ochtman <dirkjan@ochtman.nl> | 2023-08-01 13:04:41 +0200 |
commit | 26f598c0d189769952b11ea9fe6168718f403590 (patch) | |
tree | b8cab7749e993fd24c8d7abc7a126a1862a87c76 /askama_parser/src/node.rs | |
parent | 108c4a6a33a9eeda3ecf76e5e09da5ccb9b188e8 (diff) | |
download | askama-26f598c0d189769952b11ea9fe6168718f403590.tar.gz askama-26f598c0d189769952b11ea9fe6168718f403590.tar.bz2 askama-26f598c0d189769952b11ea9fe6168718f403590.zip |
parser: add type for `Node::BlockDef`
Diffstat (limited to 'askama_parser/src/node.rs')
-rw-r--r-- | askama_parser/src/node.rs | 73 |
1 files changed, 44 insertions, 29 deletions
diff --git a/askama_parser/src/node.rs b/askama_parser/src/node.rs index 277d60f..a8325ef 100644 --- a/askama_parser/src/node.rs +++ b/askama_parser/src/node.rs @@ -26,7 +26,7 @@ pub enum Node<'a> { Match(Match<'a>), Loop(Loop<'a>), Extends(&'a str), - BlockDef(Ws, &'a str, Vec<Node<'a>>, Ws), + BlockDef(BlockDef<'a>), Include(Ws, &'a str), Import(Import<'a>), Macro(Macro<'a>), @@ -77,7 +77,7 @@ impl<'a> Node<'a> { Self::extends, Self::include, map(Import::parse, Self::Import), - |i| Self::block(i, s), + map(|i| BlockDef::parse(i, s), Self::BlockDef), map(|i| Macro::parse(i, s), Self::Macro), |i| Self::raw(i, s), |i| Self::r#break(i, s), @@ -224,33 +224,6 @@ impl<'a> Node<'a> { Ok((i, Self::Include(Ws(pws, nws), name))) } - fn block(i: &'a str, s: &State<'_>) -> IResult<&'a str, Self> { - let mut start = tuple(( - opt(Whitespace::parse), - ws(keyword("block")), - cut(tuple((ws(identifier), opt(Whitespace::parse), |i| { - s.tag_block_end(i) - }))), - )); - let (i, (pws1, _, (name, 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("endblock")), - cut(tuple((opt(ws(keyword(name))), opt(Whitespace::parse)))), - ))), - ))); - let (i, (contents, (_, pws2, _, (_, nws2)))) = end(i)?; - - Ok(( - i, - Self::BlockDef(Ws(pws1, nws1), name, contents, Ws(pws2, nws2)), - )) - } - fn raw(i: &'a str, s: &State<'_>) -> IResult<&'a str, Self> { let endraw = tuple(( |i| s.tag_block_start(i), @@ -780,6 +753,48 @@ impl<'a> Match<'a> { } } +#[derive(Debug, PartialEq)] +pub struct BlockDef<'a> { + pub ws1: Ws, + pub name: &'a str, + pub nodes: Vec<Node<'a>>, + pub ws2: Ws, +} + +impl<'a> BlockDef<'a> { + fn parse(i: &'a str, s: &State<'_>) -> IResult<&'a str, Self> { + let mut start = tuple(( + opt(Whitespace::parse), + ws(keyword("block")), + cut(tuple((ws(identifier), opt(Whitespace::parse), |i| { + s.tag_block_end(i) + }))), + )); + let (i, (pws1, _, (name, 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("endblock")), + cut(tuple((opt(ws(keyword(name))), opt(Whitespace::parse)))), + ))), + ))); + let (i, (nodes, (_, pws2, _, (_, nws2)))) = end(i)?; + + Ok(( + i, + BlockDef { + ws1: Ws(pws1, nws1), + name, + nodes, + 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". |