From c5c46ada406aea830293a22bcef64a0d6205b3f4 Mon Sep 17 00:00:00 2001 From: René Kijewski Date: Tue, 1 Aug 2023 04:04:27 +0200 Subject: parser: rename `Node::Cond` into `If` and add type --- askama_parser/src/lib.rs | 4 +-- askama_parser/src/node.rs | 80 ++++++++++++++++++++++++++++------------------- 2 files changed, 49 insertions(+), 35 deletions(-) (limited to 'askama_parser/src') diff --git a/askama_parser/src/lib.rs b/askama_parser/src/lib.rs index 560507a..8e90802 100644 --- a/askama_parser/src/lib.rs +++ b/askama_parser/src/lib.rs @@ -16,8 +16,8 @@ use nom::{error_position, AsChar, IResult, InputTakeAtPosition}; pub use self::expr::Expr; pub use self::node::{ - BlockDef, Call, Cond, CondTest, Import, Let, Lit, Loop, Macro, Match, Node, Raw, Target, When, - Whitespace, Ws, + BlockDef, Call, Cond, CondTest, If, Import, Let, Lit, Loop, Macro, Match, Node, Raw, Target, + When, Whitespace, Ws, }; mod expr; diff --git a/askama_parser/src/node.rs b/askama_parser/src/node.rs index 9055d99..f7e51de 100644 --- a/askama_parser/src/node.rs +++ b/askama_parser/src/node.rs @@ -21,7 +21,7 @@ pub enum Node<'a> { Expr(Ws, Expr<'a>), Call(Call<'a>), Let(Let<'a>), - Cond(Vec>, Ws), + If(If<'a>), Match(Match<'a>), Loop(Loop<'a>), Extends(&'a str), @@ -50,7 +50,7 @@ impl<'a> Node<'a> { alt(( map(Call::parse, Self::Call), map(Let::parse, Self::Let), - |i| Self::r#if(i, s), + map(|i| If::parse(i, s), Self::If), |i| Self::r#for(i, s), map(|i| Match::parse(i, s), Self::Match), Self::extends, @@ -68,37 +68,6 @@ impl<'a> Node<'a> { Ok((i, contents)) } - fn r#if(i: &'a str, s: &State<'_>) -> IResult<&'a str, Self> { - let mut p = tuple(( - opt(Whitespace::parse), - CondTest::parse, - cut(tuple(( - opt(Whitespace::parse), - |i| s.tag_block_end(i), - cut(tuple(( - |i| Node::many(i, s), - many0(|i| Cond::parse(i, s)), - cut(tuple(( - |i| s.tag_block_start(i), - opt(Whitespace::parse), - ws(keyword("endif")), - opt(Whitespace::parse), - ))), - ))), - ))), - )); - - let (i, (pws1, cond, (nws1, _, (nodes, elifs, (_, pws2, _, nws2))))) = p(i)?; - let mut res = vec![Cond { - ws: Ws(pws1, nws1), - cond: Some(cond), - nodes, - }]; - res.extend(elifs); - - Ok((i, Self::Cond(res, Ws(pws2, nws2)))) - } - fn r#for(i: &'a str, s: &State<'_>) -> IResult<&'a str, Self> { fn content<'a>(i: &'a str, s: &State<'_>) -> IResult<&'a str, Vec>> { s.enter_loop(); @@ -829,6 +798,51 @@ impl<'a> Let<'a> { } } +#[derive(Debug, PartialEq)] +pub struct If<'a> { + pub ws: Ws, + pub branches: Vec>, +} + +impl<'a> If<'a> { + fn parse(i: &'a str, s: &State<'_>) -> IResult<&'a str, Self> { + let mut p = tuple(( + opt(Whitespace::parse), + CondTest::parse, + cut(tuple(( + opt(Whitespace::parse), + |i| s.tag_block_end(i), + cut(tuple(( + |i| Node::many(i, s), + many0(|i| Cond::parse(i, s)), + cut(tuple(( + |i| s.tag_block_start(i), + opt(Whitespace::parse), + ws(keyword("endif")), + opt(Whitespace::parse), + ))), + ))), + ))), + )); + + let (i, (pws1, cond, (nws1, _, (nodes, elifs, (_, pws2, _, nws2))))) = p(i)?; + let mut branches = vec![Cond { + ws: Ws(pws1, nws1), + cond: Some(cond), + nodes, + }]; + branches.extend(elifs); + + Ok(( + i, + Self { + ws: Ws(pws2, nws2), + branches, + }, + )) + } +} + /// 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