From ca5bfc111616ac29eb18165d60bf309c2f18f419 Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Mon, 13 Feb 2017 22:00:35 +0100 Subject: Implement support for mul, div, mod operators --- askama/src/parser.rs | 16 +++++++++++++--- testing/templates/operators.html | 9 +++++++++ testing/tests/operators.rs | 2 +- 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/askama/src/parser.rs b/askama/src/parser.rs index 2654c9b..127d8d2 100644 --- a/askama/src/parser.rs +++ b/askama/src/parser.rs @@ -110,18 +110,28 @@ named!(expr_single, alt!( expr_str_lit )); -named!(expr_any, alt!( +named!(expr_muldivmod, 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 +)); + +named!(expr_any, alt!( + do_parse!( + left: expr_muldivmod >> op: ws!(alt!( tag_s!("==") | tag_s!("!=") | tag_s!(">=") | tag_s!(">") | tag_s!("<=") | tag_s!("<") )) >> - right: expr_single >> + right: expr_muldivmod >> (Expr::BinOp(str::from_utf8(op).unwrap(), Box::new(left), Box::new(right))) - ) | expr_single + ) | expr_muldivmod )); named!(expr_node, do_parse!( diff --git a/testing/templates/operators.html b/testing/templates/operators.html index ea22546..020126b 100644 --- a/testing/templates/operators.html +++ b/testing/templates/operators.html @@ -46,3 +46,12 @@ {%- else -%} f {%- endif %} +{% if a * c > b -%} + mul +{%- endif %} +{% if c / c == a -%} + div +{%- endif %} +{% if a % c == b -%} + mod +{%- endif %} diff --git a/testing/tests/operators.rs b/testing/tests/operators.rs index 0154118..240fd52 100644 --- a/testing/tests/operators.rs +++ b/testing/tests/operators.rs @@ -15,5 +15,5 @@ struct OperatorsTemplate { #[test] fn test_operators() { let t = OperatorsTemplate { a: 1, b: 1, c: 2 }; - assert_eq!(t.render(), "tf\ntf\ntf\ntf\ntf\ntf\n"); + assert_eq!(t.render(), "tf\ntf\ntf\ntf\ntf\ntf\nmul\ndiv\nmod\n"); } -- cgit