diff options
author | Dirkjan Ochtman <dirkjan@ochtman.nl> | 2017-02-17 14:24:50 +0100 |
---|---|---|
committer | Dirkjan Ochtman <dirkjan@ochtman.nl> | 2017-02-17 14:24:50 +0100 |
commit | 22cd667f4ea414884217d8fb37ccd4e3e1369f5a (patch) | |
tree | 787c221075ad0d1824b5641562bbcfdfef882bbe | |
parent | 6d8364afcf62d3b8add50e0935da08cf968804c2 (diff) | |
download | askama-22cd667f4ea414884217d8fb37ccd4e3e1369f5a.tar.gz askama-22cd667f4ea414884217d8fb37ccd4e3e1369f5a.tar.bz2 askama-22cd667f4ea414884217d8fb37ccd4e3e1369f5a.zip |
Use macro to prevent repetitive parser definitions
-rw-r--r-- | askama/src/parser.rs | 39 |
1 files changed, 17 insertions, 22 deletions
diff --git a/askama/src/parser.rs b/askama/src/parser.rs index 127d8d2..7d67f86 100644 --- a/askama/src/parser.rs +++ b/askama/src/parser.rs @@ -110,29 +110,24 @@ named!(expr_single<Expr>, alt!( expr_str_lit )); -named!(expr_muldivmod<Expr>, alt!( - do_parse!( - left: expr_single >> - op: ws!(alt!(tag_s!("*") | tag_s!("/") | tag_s!("%"))) >> - right: expr_single >> - (Expr::BinOp(str::from_utf8(op).unwrap(), - Box::new(left), Box::new(right))) - ) | expr_single -)); +macro_rules! expr_prec_layer { + ( $name:ident, $inner:ident, $( $op:expr ),* ) => { + named!($name<Expr>, alt!( + do_parse!( + left: $inner >> + op: ws!(alt!($( tag_s!($op) )|*)) >> + right: $inner >> + (Expr::BinOp(str::from_utf8(op).unwrap(), + Box::new(left), Box::new(right))) + ) | $inner + )); + } +} -named!(expr_any<Expr>, alt!( - do_parse!( - left: expr_muldivmod >> - op: ws!(alt!( - tag_s!("==") | tag_s!("!=") | - tag_s!(">=") | tag_s!(">") | - tag_s!("<=") | tag_s!("<") - )) >> - right: expr_muldivmod >> - (Expr::BinOp(str::from_utf8(op).unwrap(), - Box::new(left), Box::new(right))) - ) | expr_muldivmod -)); +expr_prec_layer!(expr_muldivmod, expr_single, "*", "/", "%"); +expr_prec_layer!(expr_any, expr_muldivmod, + "==", "!=", ">=", ">", "<=", "<" +); named!(expr_node<Node>, do_parse!( tag_s!("{{") >> |