aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLibravatar Dirkjan Ochtman <dirkjan@ochtman.nl>2017-02-04 21:36:32 +0100
committerLibravatar Dirkjan Ochtman <dirkjan@ochtman.nl>2017-02-04 21:37:34 +0100
commitd1d34ec6080697c47b6b548de15a0cbd55d68ead (patch)
treead27b8b4836399c4df0b6e4a0d1f5a13c943a558
parent8041160766f799f6c474d393568e5a15e1a551e3 (diff)
downloadaskama-d1d34ec6080697c47b6b548de15a0cbd55d68ead.tar.gz
askama-d1d34ec6080697c47b6b548de15a0cbd55d68ead.tar.bz2
askama-d1d34ec6080697c47b6b548de15a0cbd55d68ead.zip
Add support for == operator
-rw-r--r--askama/src/generator.rs7
-rw-r--r--askama/src/parser.rs17
2 files changed, 20 insertions, 4 deletions
diff --git a/askama/src/generator.rs b/askama/src/generator.rs
index cabe381..7ad1313 100644
--- a/askama/src/generator.rs
+++ b/askama/src/generator.rs
@@ -91,10 +91,17 @@ impl Generator {
self.write(")");
}
+ fn visit_eq(&mut self, left: &Expr, right: &Expr) {
+ self.visit_expr(left);
+ self.write(" == ");
+ self.visit_expr(right);
+ }
+
fn visit_expr(&mut self, expr: &Expr) {
match expr {
&Expr::Var(s) => self.visit_var(s),
&Expr::Filter(name, ref val) => self.visit_filter(name, &val),
+ &Expr::Eq(ref left, ref right) => self.visit_eq(&left, &right),
}
}
diff --git a/askama/src/parser.rs b/askama/src/parser.rs
index 2e94ef4..428ac7c 100644
--- a/askama/src/parser.rs
+++ b/askama/src/parser.rs
@@ -4,6 +4,7 @@ use std::str;
pub enum Expr<'a> {
Var(&'a [u8]),
Filter(&'a str, Box<Expr<'a>>),
+ Eq(Box<Expr<'a>>, Box<Expr<'a>>),
}
pub enum Target<'a> {
@@ -65,13 +66,21 @@ fn expr_filtered(i: &[u8]) -> IResult<&[u8], Expr> {
return IResult::Done(left, expr);
}
+named!(expr_eq<Expr>, do_parse!(
+ left: expr_filtered >>
+ ws!(tag_s!("==")) >>
+ right: expr_filtered >>
+ (Expr::Eq(Box::new(left), Box::new(right)))));
+
+named!(expr_any<Expr>, alt!(expr_eq | expr_filtered));
+
named!(expr_node<Node>, map!(
- delimited!(tag_s!("{{"), ws!(expr_filtered), tag_s!("}}")),
+ delimited!(tag_s!("{{"), ws!(expr_any), tag_s!("}}")),
Node::Expr));
named!(cond_if<Expr>, do_parse!(
ws!(tag_s!("if")) >>
- cond: ws!(expr_filtered) >>
+ cond: ws!(expr_any) >>
(cond)));
named!(cond_block<(Option<Expr>, Nodes)>, do_parse!(
@@ -85,7 +94,7 @@ named!(cond_block<(Option<Expr>, Nodes)>, do_parse!(
named!(block_if<Node>, do_parse!(
tag_s!("{%") >>
ws!(tag_s!("if")) >>
- cond: ws!(expr_filtered) >>
+ cond: ws!(expr_any) >>
tag_s!("%}") >>
block: parse_template >>
elifs: many0!(cond_block) >>
@@ -104,7 +113,7 @@ named!(block_for<Node>, do_parse!(
ws!(tag_s!("for")) >>
var: ws!(target_single) >>
ws!(tag_s!("in")) >>
- iter: ws!(expr_filtered) >>
+ iter: ws!(expr_any) >>
tag_s!("%}") >>
block: parse_template >>
tag_s!("{%") >>