diff options
Diffstat (limited to '')
-rw-r--r-- | askama_parser/src/lib.rs | 2 | ||||
-rw-r--r-- | askama_parser/src/node.rs | 73 |
2 files changed, 45 insertions, 30 deletions
diff --git a/askama_parser/src/lib.rs b/askama_parser/src/lib.rs index 74004cb..d095f65 100644 --- a/askama_parser/src/lib.rs +++ b/askama_parser/src/lib.rs @@ -16,7 +16,7 @@ use nom::{error_position, AsChar, IResult, InputTakeAtPosition}; pub use self::expr::Expr; pub use self::node::{ - Call, Cond, CondTest, Import, Loop, Macro, Match, Node, Target, When, Whitespace, Ws, + BlockDef, Call, Cond, CondTest, Import, Loop, Macro, Match, Node, Target, When, Whitespace, Ws, }; mod expr; 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". |