From 4f52dbe8be9746d1f1a2eab03e135c9003ef6dd3 Mon Sep 17 00:00:00 2001 From: René Kijewski Date: Tue, 1 Aug 2023 03:22:08 +0200 Subject: parser: add type for `Node::Call` --- askama_parser/src/lib.rs | 4 +++- askama_parser/src/node.rs | 56 +++++++++++++++++++++++++++++++---------------- 2 files changed, 40 insertions(+), 20 deletions(-) (limited to 'askama_parser/src') diff --git a/askama_parser/src/lib.rs b/askama_parser/src/lib.rs index 0f796ae..c92adbe 100644 --- a/askama_parser/src/lib.rs +++ b/askama_parser/src/lib.rs @@ -15,7 +15,9 @@ use nom::sequence::{delimited, pair, tuple}; use nom::{error_position, AsChar, IResult, InputTakeAtPosition}; pub use self::expr::Expr; -pub use self::node::{Cond, CondTest, Import, Loop, Macro, Node, Target, When, Whitespace, Ws}; +pub use self::node::{ + Call, Cond, CondTest, Import, Loop, Macro, Node, Target, When, Whitespace, Ws, +}; mod expr; mod node; 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>), + Call(Call<'a>), LetDecl(Ws, Target<'a>), Let(Ws, Target<'a>, Expr<'a>), Cond(Vec>, 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>, +} + +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". -- cgit