From dbc0c8998b904855ba61ed0c47afc9b5d8ab6afa Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Sat, 18 Feb 2017 16:04:12 +0100 Subject: Add support for method calls --- askama/src/generator.rs | 14 ++++++++++++++ askama/src/parser.rs | 8 +++++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/askama/src/generator.rs b/askama/src/generator.rs index 40b6523..895ec25 100644 --- a/askama/src/generator.rs +++ b/askama/src/generator.rs @@ -146,6 +146,18 @@ impl<'a> Generator<'a> { self.visit_expr(right); } + fn visit_call(&mut self, obj: &Expr, method: &str, args: &[Expr]) { + self.visit_expr(obj); + self.write(&format!(".{}(", method)); + for (i, arg) in args.iter().enumerate() { + if i > 0 { + self.write(", "); + } + self.visit_expr(arg); + } + self.write(")"); + } + fn visit_expr(&mut self, expr: &Expr) { match *expr { Expr::NumLit(s) => self.visit_num_lit(s), @@ -155,6 +167,8 @@ 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::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 c09eeb3..cad6468 100644 --- a/askama/src/parser.rs +++ b/askama/src/parser.rs @@ -9,6 +9,7 @@ pub enum Expr<'a> { Attr(Box>, &'a str), Filter(&'a str, Vec>), BinOp(&'a str, Box>, Box>), + Call(Box>, &'a str, Vec>), } #[derive(Debug)] @@ -137,7 +138,12 @@ named!(expr_attr, alt!( obj: expr_single >> tag_s!(".") >> attr: identifier >> - (Expr::Attr(Box::new(obj), attr)) + args: arguments >> + (if args.is_some() { + Expr::Call(Box::new(obj), attr, args.unwrap()) + } else { + Expr::Attr(Box::new(obj), attr) + }) ) | expr_single )); -- cgit