From d1d34ec6080697c47b6b548de15a0cbd55d68ead Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Sat, 4 Feb 2017 21:36:32 +0100 Subject: Add support for == operator --- askama/src/generator.rs | 7 +++++++ askama/src/parser.rs | 17 +++++++++++++---- 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>), + Eq(Box>, Box>), } pub enum Target<'a> { @@ -65,13 +66,21 @@ fn expr_filtered(i: &[u8]) -> IResult<&[u8], Expr> { return IResult::Done(left, expr); } +named!(expr_eq, do_parse!( + left: expr_filtered >> + ws!(tag_s!("==")) >> + right: expr_filtered >> + (Expr::Eq(Box::new(left), Box::new(right))))); + +named!(expr_any, alt!(expr_eq | expr_filtered)); + named!(expr_node, map!( - delimited!(tag_s!("{{"), ws!(expr_filtered), tag_s!("}}")), + delimited!(tag_s!("{{"), ws!(expr_any), tag_s!("}}")), Node::Expr)); named!(cond_if, do_parse!( ws!(tag_s!("if")) >> - cond: ws!(expr_filtered) >> + cond: ws!(expr_any) >> (cond))); named!(cond_block<(Option, Nodes)>, do_parse!( @@ -85,7 +94,7 @@ named!(cond_block<(Option, Nodes)>, do_parse!( named!(block_if, 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, 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!("{%") >> -- cgit