diff options
author | René Kijewski <rene.kijewski@fu-berlin.de> | 2023-08-01 03:22:08 +0200 |
---|---|---|
committer | Dirkjan Ochtman <dirkjan@ochtman.nl> | 2023-08-01 13:04:41 +0200 |
commit | 4f52dbe8be9746d1f1a2eab03e135c9003ef6dd3 (patch) | |
tree | 8e3469529aff9e0538c24e854c56f1f6e643368e /askama_parser/src/node.rs | |
parent | cd075630eab578a66afec6000ce0f266e6b281e8 (diff) | |
download | askama-4f52dbe8be9746d1f1a2eab03e135c9003ef6dd3.tar.gz askama-4f52dbe8be9746d1f1a2eab03e135c9003ef6dd3.tar.bz2 askama-4f52dbe8be9746d1f1a2eab03e135c9003ef6dd3.zip |
parser: add type for `Node::Call`
Diffstat (limited to 'askama_parser/src/node.rs')
-rw-r--r-- | askama_parser/src/node.rs | 56 |
1 files changed, 37 insertions, 19 deletions
diff --git a/askama_parser/src/node.rs b/askama_parser/src/node.rs index 972314e..9cdd451 100644 --- a/askama_parser/src/node.rs +++ b/askama_parser/src/node.rs @@ -19,7 +19,7 @@ pub enum Node<'a> { Lit(&'a str, &'a str, &'a str), Comment(Ws), Expr(Ws, Expr<'a>), - Call(Ws, Option<&'a str>, &'a str, Vec<Expr<'a>>), + Call(Call<'a>), LetDecl(Ws, Target<'a>), Let(Ws, Target<'a>, Expr<'a>), Cond(Vec<Cond<'a>>, Ws), @@ -69,7 +69,7 @@ impl<'a> Node<'a> { let mut p = tuple(( |i| s.tag_block_start(i), alt(( - Self::call, + map(Call::parse, Self::Call), Self::r#let, |i| Self::r#if(i, s), |i| Self::r#for(i, s), @@ -89,23 +89,6 @@ impl<'a> Node<'a> { Ok((i, contents)) } - fn call(i: &'a str) -> IResult<&'a str, Self> { - let mut p = tuple(( - opt(Whitespace::parse), - ws(keyword("call")), - cut(tuple(( - opt(tuple((ws(identifier), ws(tag("::"))))), - ws(identifier), - opt(ws(Expr::arguments)), - opt(Whitespace::parse), - ))), - )); - let (i, (pws, _, (scope, name, args, nws))) = p(i)?; - let scope = scope.map(|(scope, _)| scope); - let args = args.unwrap_or_default(); - Ok((i, Self::Call(Ws(pws, nws), scope, name, args))) - } - fn r#let(i: &'a str) -> IResult<&'a str, Self> { let mut p = tuple(( opt(Whitespace::parse), @@ -744,6 +727,41 @@ impl<'a> Import<'a> { } } +#[derive(Debug, PartialEq)] +pub struct Call<'a> { + pub ws: Ws, + pub scope: Option<&'a str>, + pub name: &'a str, + pub args: Vec<Expr<'a>>, +} + +impl<'a> Call<'a> { + fn parse(i: &'a str) -> IResult<&'a str, Self> { + let mut p = tuple(( + opt(Whitespace::parse), + ws(keyword("call")), + cut(tuple(( + opt(tuple((ws(identifier), ws(tag("::"))))), + ws(identifier), + opt(ws(Expr::arguments)), + opt(Whitespace::parse), + ))), + )); + let (i, (pws, _, (scope, name, args, nws))) = p(i)?; + let scope = scope.map(|(scope, _)| scope); + let args = args.unwrap_or_default(); + Ok(( + i, + Self { + ws: Ws(pws, nws), + scope, + name, + args, + }, + )) + } +} + /// 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". |