From 108c4a6a33a9eeda3ecf76e5e09da5ccb9b188e8 Mon Sep 17 00:00:00 2001 From: René Kijewski Date: Tue, 1 Aug 2023 03:27:45 +0200 Subject: parser: add type for `Node::Match` --- askama_parser/src/lib.rs | 2 +- askama_parser/src/node.rs | 88 ++++++++++++++++++++++++++++------------------- 2 files changed, 54 insertions(+), 36 deletions(-) (limited to 'askama_parser/src') diff --git a/askama_parser/src/lib.rs b/askama_parser/src/lib.rs index c92adbe..74004cb 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, Node, Target, When, Whitespace, Ws, + 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 9cdd451..277d60f 100644 --- a/askama_parser/src/node.rs +++ b/askama_parser/src/node.rs @@ -23,7 +23,7 @@ pub enum Node<'a> { LetDecl(Ws, Target<'a>), Let(Ws, Target<'a>, Expr<'a>), Cond(Vec>, Ws), - Match(Ws, Expr<'a>, Vec>, Ws), + Match(Match<'a>), Loop(Loop<'a>), Extends(&'a str), BlockDef(Ws, &'a str, Vec>, Ws), @@ -73,7 +73,7 @@ impl<'a> Node<'a> { Self::r#let, |i| Self::r#if(i, s), |i| Self::r#for(i, s), - |i| Self::r#match(i, s), + map(|i| Match::parse(i, s), Self::Match), Self::extends, Self::include, map(Import::parse, Self::Import), @@ -209,39 +209,6 @@ impl<'a> Node<'a> { )) } - fn r#match(i: &'a str, s: &State<'_>) -> IResult<&'a str, Self> { - let mut p = tuple(( - opt(Whitespace::parse), - ws(keyword("match")), - cut(tuple(( - ws(Expr::parse), - opt(Whitespace::parse), - |i| s.tag_block_end(i), - cut(tuple(( - ws(many0(ws(value((), |i| Self::comment(i, s))))), - many1(|i| When::when(i, s)), - cut(tuple(( - opt(|i| When::r#match(i, s)), - cut(tuple(( - ws(|i| s.tag_block_start(i)), - opt(Whitespace::parse), - ws(keyword("endmatch")), - opt(Whitespace::parse), - ))), - ))), - ))), - ))), - )); - let (i, (pws1, _, (expr, nws1, _, (_, arms, (else_arm, (_, pws2, _, nws2)))))) = p(i)?; - - let mut arms = arms; - if let Some(arm) = else_arm { - arms.push(arm); - } - - Ok((i, Self::Match(Ws(pws1, nws1), expr, arms, Ws(pws2, nws2)))) - } - fn extends(i: &'a str) -> IResult<&'a str, Self> { let (i, (_, name)) = tuple((ws(keyword("extends")), ws(str_lit)))(i)?; Ok((i, Self::Extends(name))) @@ -762,6 +729,57 @@ impl<'a> Call<'a> { } } +#[derive(Debug, PartialEq)] +pub struct Match<'a> { + pub ws1: Ws, + pub expr: Expr<'a>, + pub arms: Vec>, + pub ws2: Ws, +} + +impl<'a> Match<'a> { + fn parse(i: &'a str, s: &State<'_>) -> IResult<&'a str, Self> { + let mut p = tuple(( + opt(Whitespace::parse), + ws(keyword("match")), + cut(tuple(( + ws(Expr::parse), + opt(Whitespace::parse), + |i| s.tag_block_end(i), + cut(tuple(( + ws(many0(ws(value((), |i| Node::comment(i, s))))), + many1(|i| When::when(i, s)), + cut(tuple(( + opt(|i| When::r#match(i, s)), + cut(tuple(( + ws(|i| s.tag_block_start(i)), + opt(Whitespace::parse), + ws(keyword("endmatch")), + opt(Whitespace::parse), + ))), + ))), + ))), + ))), + )); + let (i, (pws1, _, (expr, nws1, _, (_, arms, (else_arm, (_, pws2, _, nws2)))))) = p(i)?; + + let mut arms = arms; + if let Some(arm) = else_arm { + arms.push(arm); + } + + Ok(( + i, + Self { + ws1: Ws(pws1, nws1), + expr, + arms, + 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