diff options
Diffstat (limited to '')
-rw-r--r-- | askama_derive/src/generator.rs | 17 | ||||
-rw-r--r-- | askama_parser/src/lib.rs | 4 | ||||
-rw-r--r-- | askama_parser/src/node.rs | 56 |
3 files changed, 50 insertions, 27 deletions
diff --git a/askama_derive/src/generator.rs b/askama_derive/src/generator.rs index 8a2c0e3..d29c4c3 100644 --- a/askama_derive/src/generator.rs +++ b/askama_derive/src/generator.rs @@ -2,7 +2,7 @@ use crate::config::{get_template_source, read_config_file, Config, WhitespaceHan use crate::heritage::{Context, Heritage}; use crate::input::{Print, Source, TemplateInput}; use crate::CompileError; -use parser::{Cond, CondTest, Expr, Loop, Node, Parsed, Target, When, Whitespace, Ws}; +use parser::{Call, Cond, CondTest, Expr, Loop, Node, Parsed, Target, When, Whitespace, Ws}; use proc_macro::TokenStream; use quote::{quote, ToTokens}; @@ -656,8 +656,8 @@ impl<'a> Generator<'a> { Node::Include(ws, path) => { size_hint += self.handle_include(ctx, buf, ws, path)?; } - Node::Call(ws, scope, name, ref args) => { - size_hint += self.write_call(ctx, buf, ws, scope, name, args)?; + Node::Call(ref call) => { + size_hint += self.write_call(ctx, buf, call)?; } Node::Macro(ref m) => { if level != AstLevel::Top { @@ -896,11 +896,14 @@ impl<'a> Generator<'a> { &mut self, ctx: &'a Context<'_>, buf: &mut Buffer, - ws: Ws, - scope: Option<&str>, - name: &str, - args: &[Expr<'_>], + call: &'a Call<'_>, ) -> Result<usize, CompileError> { + let Call { + ws, + scope, + name, + ref args, + } = *call; if name == "super" { return self.write_block(buf, None, ws); } 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<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". |