From 3652708020cf86c0cd075d0f827c51500a5aabaa Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Tue, 22 Aug 2017 20:22:33 +0200 Subject: Implement support for macro definition handling --- askama_derive/src/parser.rs | 44 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) (limited to 'askama_derive/src/parser.rs') diff --git a/askama_derive/src/parser.rs b/askama_derive/src/parser.rs index 9e8f087..9a49715 100644 --- a/askama_derive/src/parser.rs +++ b/askama_derive/src/parser.rs @@ -34,6 +34,7 @@ pub enum Node<'a> { BlockDef(WS, &'a str, Vec>, WS), Block(WS, &'a str, WS), Include(WS, &'a str), + Macro(WS, &'a str, Vec<&'a str>, Vec>, WS), } pub type Cond<'a> = (WS, Option>, Vec>); @@ -147,6 +148,25 @@ named!(arguments>>, opt!( ) )); +named!(parameters>, do_parse!( + tag_s!("(") >> + vals: opt!(do_parse!( + arg0: ws!(identifier) >> + args: many0!(do_parse!( + tag_s!(",") >> + argn: ws!(identifier) >> + (argn) + )) >> + ({ + let mut res = vec![arg0]; + res.extend(args); + res + }) + )) >> + tag_s!(")") >> + (vals.unwrap_or(Vec::new())) +)); + named!(expr_group, map!( delimited!(char!('('), expr_any, char!(')')), |s| Expr::Group(Box::new(s)) @@ -347,6 +367,27 @@ named!(block_include, do_parse!( })) )); +named!(block_macro, do_parse!( + pws1: opt!(tag_s!("-")) >> + ws!(tag_s!("macro")) >> + name: ws!(identifier) >> + params: ws!(parameters) >> + nws1: opt!(tag_s!("-")) >> + tag_s!("%}") >> + contents: parse_template >> + tag_s!("{%") >> + pws2: opt!(tag_s!("-")) >> + ws!(tag_s!("endmacro")) >> + nws2: opt!(tag_s!("-")) >> + (Node::Macro( + WS(pws1.is_some(), nws1.is_some()), + name, + params, + contents, + WS(pws2.is_some(), nws2.is_some()) + )) +)); + named!(block_node, do_parse!( tag_s!("{%") >> contents: alt!( @@ -355,7 +396,8 @@ named!(block_node, do_parse!( block_for | block_extends | block_include | - block_block + block_block | + block_macro ) >> tag_s!("%}") >> (contents) -- cgit