diff options
-rw-r--r-- | askama/src/generator.rs | 7 | ||||
-rw-r--r-- | askama/src/parser.rs | 9 |
2 files changed, 15 insertions, 1 deletions
diff --git a/askama/src/generator.rs b/askama/src/generator.rs index 72c5ecd..6975f25 100644 --- a/askama/src/generator.rs +++ b/askama/src/generator.rs @@ -163,6 +163,12 @@ impl<'a> Generator<'a> { self.visit_expr(right); } + fn visit_group(&mut self, inner: &Expr) { + self.write("("); + self.visit_expr(inner); + self.write(")"); + } + fn visit_call(&mut self, obj: &Expr, method: &str, args: &[Expr]) { self.visit_expr(obj); self.write(&format!(".{}(", method)); @@ -184,6 +190,7 @@ impl<'a> Generator<'a> { Expr::Filter(name, ref args) => self.visit_filter(name, args), Expr::BinOp(op, ref left, ref right) => self.visit_binop(op, left, right), + Expr::Group(ref inner) => self.visit_group(inner), Expr::Call(ref obj, method, ref args) => self.visit_call(obj, method, args), } diff --git a/askama/src/parser.rs b/askama/src/parser.rs index cad6468..b955f0a 100644 --- a/askama/src/parser.rs +++ b/askama/src/parser.rs @@ -9,6 +9,7 @@ pub enum Expr<'a> { Attr(Box<Expr<'a>>, &'a str), Filter(&'a str, Vec<Expr<'a>>), BinOp(&'a str, Box<Expr<'a>>, Box<Expr<'a>>), + Group(Box<Expr<'a>>), Call(Box<Expr<'a>>, &'a str, Vec<Expr<'a>>), } @@ -127,10 +128,16 @@ named!(arguments<Option<Vec<Expr>>>, opt!( ) )); +named!(expr_group<Expr>, map!( + delimited!(char!('('), expr_any, char!(')')), + |s| Expr::Group(Box::new(s)) +)); + named!(expr_single<Expr>, alt!( expr_num_lit | expr_str_lit | - expr_var + expr_var | + expr_group )); named!(expr_attr<Expr>, alt!( |