From 08dbe0a466cdfb3a35e19911ee364e6a0ed0f1d3 Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Fri, 27 Apr 2018 13:44:19 +0200 Subject: Add support for unary operators (fixes #83) --- askama_derive/src/parser.rs | 12 +++++++++++- 1 file changed, 11 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 f1e3c72..d15fc1a 100644 --- a/askama_derive/src/parser.rs +++ b/askama_derive/src/parser.rs @@ -13,6 +13,7 @@ pub enum Expr<'a> { Array(Vec>), Attr(Box>, &'a str), Filter(&'a str, Vec>), + Unary(&'a str, Box>), BinOp(&'a str, Box>, Box>), Group(Box>), MethodCall(Box>, &'a str, Vec>), @@ -366,6 +367,15 @@ named!(expr_filtered, do_parse!( }) )); +named!(expr_unary, do_parse!( + op: opt!(alt!(tag_s!("!") | tag_s!("-"))) >> + expr: expr_filtered >> + (match op { + Some(op) => Expr::Unary(str::from_utf8(op).unwrap(), Box::new(expr)), + None => expr, + }) +)); + macro_rules! expr_prec_layer { ( $name:ident, $inner:ident, $( $op:expr ),* ) => { named!($name, do_parse!( @@ -381,7 +391,7 @@ macro_rules! expr_prec_layer { } } -expr_prec_layer!(expr_muldivmod, expr_filtered, "*", "/", "%"); +expr_prec_layer!(expr_muldivmod, expr_unary, "*", "/", "%"); expr_prec_layer!(expr_addsub, expr_muldivmod, "+", "-"); expr_prec_layer!(expr_shifts, expr_addsub, ">>", "<<"); expr_prec_layer!(expr_band, expr_shifts, "&"); -- cgit