aboutsummaryrefslogtreecommitdiffstats
path: root/askama_derive/src/parser.rs
diff options
context:
space:
mode:
authorLibravatar Dirkjan Ochtman <dirkjan@ochtman.nl>2017-08-22 20:22:33 +0200
committerLibravatar Dirkjan Ochtman <dirkjan@ochtman.nl>2017-08-22 20:22:33 +0200
commit3652708020cf86c0cd075d0f827c51500a5aabaa (patch)
tree391de692bfbc2041078ef0e4171fbde5580fc9f2 /askama_derive/src/parser.rs
parent770b12cc2274df8e86ce89b9047bfd3a51954a7f (diff)
downloadaskama-3652708020cf86c0cd075d0f827c51500a5aabaa.tar.gz
askama-3652708020cf86c0cd075d0f827c51500a5aabaa.tar.bz2
askama-3652708020cf86c0cd075d0f827c51500a5aabaa.zip
Implement support for macro definition handling
Diffstat (limited to 'askama_derive/src/parser.rs')
-rw-r--r--askama_derive/src/parser.rs44
1 files changed, 43 insertions, 1 deletions
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<Node<'a>>, WS),
Block(WS, &'a str, WS),
Include(WS, &'a str),
+ Macro(WS, &'a str, Vec<&'a str>, Vec<Node<'a>>, WS),
}
pub type Cond<'a> = (WS, Option<Expr<'a>>, Vec<Node<'a>>);
@@ -147,6 +148,25 @@ named!(arguments<Option<Vec<Expr>>>, opt!(
)
));
+named!(parameters<Vec<&'a str>>, 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<Expr>, map!(
delimited!(char!('('), expr_any, char!(')')),
|s| Expr::Group(Box::new(s))
@@ -347,6 +367,27 @@ named!(block_include<Node>, do_parse!(
}))
));
+named!(block_macro<Node>, 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<Node>, do_parse!(
tag_s!("{%") >>
contents: alt!(
@@ -355,7 +396,8 @@ named!(block_node<Node>, do_parse!(
block_for |
block_extends |
block_include |
- block_block
+ block_block |
+ block_macro
) >>
tag_s!("%}") >>
(contents)