diff options
author | Dirkjan Ochtman <dirkjan@ochtman.nl> | 2018-04-27 13:44:19 +0200 |
---|---|---|
committer | Dirkjan Ochtman <dirkjan@ochtman.nl> | 2018-04-27 13:44:19 +0200 |
commit | 08dbe0a466cdfb3a35e19911ee364e6a0ed0f1d3 (patch) | |
tree | d5c707c0d006bfae98bf460fc0e6a41f83b21bda | |
parent | b83106c8e083cb49aef92ec891387c20f0494ecd (diff) | |
download | askama-08dbe0a466cdfb3a35e19911ee364e6a0ed0f1d3.tar.gz askama-08dbe0a466cdfb3a35e19911ee364e6a0ed0f1d3.tar.bz2 askama-08dbe0a466cdfb3a35e19911ee364e6a0ed0f1d3.zip |
Add support for unary operators (fixes #83)
Diffstat (limited to '')
-rw-r--r-- | askama_derive/src/generator.rs | 7 | ||||
-rw-r--r-- | askama_derive/src/parser.rs | 12 |
2 files changed, 18 insertions, 1 deletions
diff --git a/askama_derive/src/generator.rs b/askama_derive/src/generator.rs index b110a89..0050d35 100644 --- a/askama_derive/src/generator.rs +++ b/askama_derive/src/generator.rs @@ -620,6 +620,7 @@ impl<'a> Generator<'a> { Expr::Array(ref elements) => self.visit_array(elements, code), Expr::Attr(ref obj, name) => self.visit_attr(obj, name, code), Expr::Filter(name, ref args) => self.visit_filter(name, args, code), + Expr::Unary(op, ref inner) => self.visit_unary(op, inner, code), Expr::BinOp(op, ref left, ref right) => self.visit_binop(op, left, right, code), Expr::Group(ref inner) => self.visit_group(inner, code), Expr::MethodCall(ref obj, method, ref args) => { @@ -766,6 +767,12 @@ impl<'a> Generator<'a> { DisplayWrap::Unwrapped } + fn visit_unary(&mut self, op: &str, inner: &Expr, code: &mut String) -> DisplayWrap { + code.push_str(op); + self.visit_expr(inner, code); + DisplayWrap::Unwrapped + } + fn visit_binop(&mut self, op: &str, left: &Expr, right: &Expr, code: &mut String) -> DisplayWrap { self.visit_expr(left, code); 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<Expr<'a>>), Attr(Box<Expr<'a>>, &'a str), Filter(&'a str, Vec<Expr<'a>>), + Unary(&'a str, Box<Expr<'a>>), BinOp(&'a str, Box<Expr<'a>>, Box<Expr<'a>>), Group(Box<Expr<'a>>), MethodCall(Box<Expr<'a>>, &'a str, Vec<Expr<'a>>), @@ -366,6 +367,15 @@ named!(expr_filtered<Expr>, do_parse!( }) )); +named!(expr_unary<Expr>, 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<Expr>, 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, "&"); |