aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLibravatar Dirkjan Ochtman <dirkjan@ochtman.nl>2017-02-13 22:00:35 +0100
committerLibravatar Dirkjan Ochtman <dirkjan@ochtman.nl>2017-02-13 22:00:35 +0100
commitca5bfc111616ac29eb18165d60bf309c2f18f419 (patch)
treef57e85c5ffc2b1e0856ed827583d9b7978e6bd9e
parenta6b4f305e948cdf327b45abe52b27a4d81013d5d (diff)
downloadaskama-ca5bfc111616ac29eb18165d60bf309c2f18f419.tar.gz
askama-ca5bfc111616ac29eb18165d60bf309c2f18f419.tar.bz2
askama-ca5bfc111616ac29eb18165d60bf309c2f18f419.zip
Implement support for mul, div, mod operators
Diffstat (limited to '')
-rw-r--r--askama/src/parser.rs16
-rw-r--r--testing/templates/operators.html9
-rw-r--r--testing/tests/operators.rs2
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<Expr>, alt!(
expr_str_lit
));
-named!(expr_any<Expr>, alt!(
+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
+));
+
+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_single >>
+ right: expr_muldivmod >>
(Expr::BinOp(str::from_utf8(op).unwrap(),
Box::new(left), Box::new(right)))
- ) | expr_single
+ ) | expr_muldivmod
));
named!(expr_node<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");
}