From 107bdfdd7658919691948f629adf2254cd6aa367 Mon Sep 17 00:00:00 2001 From: René Kijewski Date: Mon, 31 Jul 2023 20:54:47 +0200 Subject: Fix parsing calls This change: * adds a cut when the leading `(` was encountered, and * fixed the interaction between call expressions and boolean OR. --- askama_parser/src/expr.rs | 8 +++++--- askama_parser/src/tests.rs | 27 ++++++++++++++++++++++++++- 2 files changed, 31 insertions(+), 4 deletions(-) (limited to 'askama_parser') diff --git a/askama_parser/src/expr.rs b/askama_parser/src/expr.rs index ef5c5e2..ab7caff 100644 --- a/askama_parser/src/expr.rs +++ b/askama_parser/src/expr.rs @@ -68,10 +68,12 @@ pub enum Expr<'a> { impl<'a> Expr<'a> { pub(super) fn arguments(i: &'a str) -> IResult<&'a str, Vec> { - delimited( + preceded( ws(char('(')), - separated_list0(char(','), ws(Self::parse)), - ws(char(')')), + cut(terminated( + separated_list0(char(','), ws(Self::parse)), + char(')'), + )), )(i) } diff --git a/askama_parser/src/tests.rs b/askama_parser/src/tests.rs index 5b3b66e..2beeb38 100644 --- a/askama_parser/src/tests.rs +++ b/askama_parser/src/tests.rs @@ -493,9 +493,34 @@ fn test_odd_calls() { Ast::from_str("{{ -a(b) }}", &syntax).unwrap().nodes, vec![Node::Expr( Ws(None, None), - Unary("-", Box::new(Call(Box::new(Var("a")), vec![Var("b")])),), + Unary("-", Box::new(Call(Box::new(Var("a")), vec![Var("b")]))), )], ); + assert_eq!( + Ast::from_str("{{ a(b)|c }}", &syntax).unwrap().nodes, + vec![Node::Expr( + Ws(None, None), + Filter("c", vec![Call(Box::new(Var("a")), vec![Var("b")])]), + )] + ); + assert_eq!( + Ast::from_str("{{ a(b)| c }}", &syntax).unwrap().nodes, + vec![Node::Expr( + Ws(None, None), + Filter("c", vec![Call(Box::new(Var("a")), vec![Var("b")])]), + )] + ); + assert_eq!( + Ast::from_str("{{ a(b) |c }}", &syntax).unwrap().nodes, + vec![Node::Expr( + Ws(None, None), + BinOp( + "|", + Box::new(Call(Box::new(Var("a")), vec![Var("b")])), + Box::new(Var("c")) + ), + )] + ); } #[test] -- cgit