From 26a000cd9e6ab48325fbd4990bf2a3d9e2100a67 Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Tue, 22 Aug 2017 20:22:33 +0200 Subject: Add support for calling macros --- askama_derive/src/parser.rs | 37 ++++++++++++++++++++++++------------- 1 file changed, 24 insertions(+), 13 deletions(-) (limited to 'askama_derive/src/parser.rs') diff --git a/askama_derive/src/parser.rs b/askama_derive/src/parser.rs index dad7e84..491a5d7 100644 --- a/askama_derive/src/parser.rs +++ b/askama_derive/src/parser.rs @@ -26,6 +26,7 @@ pub enum Node<'a> { Lit(&'a str, &'a str, &'a str), Comment(), Expr(WS, Expr<'a>), + Call(WS, &'a str, Vec>), LetDecl(WS, Target<'a>), Let(WS, Target<'a>, Expr<'a>), Cond(Vec<(WS, Option>, Vec>)>, WS), @@ -129,21 +130,21 @@ named!(target_single, map!(identifier, named!(arguments>, do_parse!( tag_s!("(") >> - arg0: ws!(opt!(expr_any)) >> - args: many0!(do_parse!( - tag_s!(",") >> - argn: ws!(expr_any) >> - (argn) + args: opt!(do_parse!( + arg0: ws!(expr_any) >> + args: many0!(do_parse!( + tag_s!(",") >> + argn: ws!(expr_any) >> + (argn) + )) >> + ({ + let mut res = vec![arg0]; + res.extend(args); + res + }) )) >> tag_s!(")") >> - ({ - let mut res = Vec::new(); - if arg0.is_some() { - res.push(arg0.unwrap()); - } - res.extend(args); - res - }) + (args.unwrap_or(Vec::new())) )); named!(parameters>, do_parse!( @@ -261,6 +262,15 @@ named!(expr_node, do_parse!( (Node::Expr(WS(pws.is_some(), nws.is_some()), expr)) )); +named!(block_call, do_parse!( + pws: opt!(tag_s!("-")) >> + ws!(tag_s!("call")) >> + name: ws!(identifier) >> + args: ws!(arguments) >> + nws: opt!(tag_s!("-")) >> + (Node::Call(WS(pws.is_some(), nws.is_some()), name, args)) +)); + named!(cond_if, do_parse!( ws!(tag_s!("if")) >> cond: ws!(expr_any) >> @@ -389,6 +399,7 @@ named!(block_macro, do_parse!( named!(block_node, do_parse!( tag_s!("{%") >> contents: alt!( + block_call | block_let | block_if | block_for | -- cgit