diff --git a/rinja_parser/src/expr.rs b/rinja_parser/src/expr.rs index ac3f683e..f085f192 100644 --- a/rinja_parser/src/expr.rs +++ b/rinja_parser/src/expr.rs @@ -215,18 +215,10 @@ impl<'a> Expr<'a> { tuple((|i| Self::prefix(i, level), many0(|i| filter(i, level))))(i)?; let mut res = obj; - for (fname, args) in filters { - res = WithSpan::new( - Self::Filter(Filter { - name: fname, - arguments: { - let mut args = args.unwrap_or_default(); - args.insert(0, res); - args - }, - }), - start, - ); + for (name, args) in filters { + let mut arguments = args.unwrap_or_default(); + arguments.insert(0, res); + res = WithSpan::new(Self::Filter(Filter { name, arguments }), start); } Ok((i, res)) diff --git a/rinja_parser/src/lib.rs b/rinja_parser/src/lib.rs index 8fa68162..6452d271 100644 --- a/rinja_parser/src/lib.rs +++ b/rinja_parser/src/lib.rs @@ -730,7 +730,7 @@ impl Level { #[allow(clippy::type_complexity)] fn filter(i: &str, level: Level) -> ParseResult<'_, (&str, Option>>>)> { let (i, (_, fname, args)) = tuple(( - char('|'), + ws(char('|')), ws(identifier), opt(|i| Expr::arguments(i, level, false)), ))(i)?; diff --git a/rinja_parser/src/tests.rs b/rinja_parser/src/tests.rs index bb72cab2..058660c7 100644 --- a/rinja_parser/src/tests.rs +++ b/rinja_parser/src/tests.rs @@ -684,6 +684,19 @@ fn test_odd_calls() { })), )] ); + assert_eq!( + Ast::from_str("{{ a(b) |c }}", None, &syntax).unwrap().nodes, + vec![Node::Expr( + Ws(None, None), + WithSpan::no_span(Expr::Filter(Filter { + name: "c", + arguments: vec![WithSpan::no_span(Expr::Call( + Box::new(WithSpan::no_span(Expr::Var("a"))), + vec![WithSpan::no_span(Expr::Var("b"))] + ))] + })), + )] + ); } #[test] @@ -833,6 +846,18 @@ fn test_parse_tuple() { })), )], ); + assert_eq!( + Ast::from_str("{{ () | abs }}", None, &syntax) + .unwrap() + .nodes, + vec![Node::Expr( + Ws(None, None), + WithSpan::no_span(Expr::Filter(Filter { + name: "abs", + arguments: vec![WithSpan::no_span(Expr::Tuple(vec![]))] + })), + )], + ); assert_eq!( Ast::from_str("{{ (1)|abs }}", None, &syntax).unwrap().nodes, vec![Node::Expr( @@ -845,6 +870,20 @@ fn test_parse_tuple() { })), )], ); + assert_eq!( + Ast::from_str("{{ (1) | abs }}", None, &syntax) + .unwrap() + .nodes, + vec![Node::Expr( + Ws(None, None), + WithSpan::no_span(Expr::Filter(Filter { + name: "abs", + arguments: vec![WithSpan::no_span(Expr::Group(Box::new(WithSpan::no_span( + Expr::NumLit("1") + ))))] + })), + )], + ); assert_eq!( Ast::from_str("{{ (1,)|abs }}", None, &syntax) .unwrap() @@ -859,6 +898,20 @@ fn test_parse_tuple() { })), )], ); + assert_eq!( + Ast::from_str("{{ (1,) | abs }}", None, &syntax) + .unwrap() + .nodes, + vec![Node::Expr( + Ws(None, None), + WithSpan::no_span(Expr::Filter(Filter { + name: "abs", + arguments: vec![WithSpan::no_span(Expr::Tuple(vec![WithSpan::no_span( + Expr::NumLit("1") + )]))] + })), + )], + ); assert_eq!( Ast::from_str("{{ (1, 2)|abs }}", None, &syntax) .unwrap() @@ -874,6 +927,21 @@ fn test_parse_tuple() { })), )], ); + assert_eq!( + Ast::from_str("{{ (1, 2) | abs }}", None, &syntax) + .unwrap() + .nodes, + vec![Node::Expr( + Ws(None, None), + WithSpan::no_span(Expr::Filter(Filter { + name: "abs", + arguments: vec![WithSpan::no_span(Expr::Tuple(vec![ + WithSpan::no_span(Expr::NumLit("1")), + WithSpan::no_span(Expr::NumLit("2")) + ]))] + })), + )], + ); } #[test] @@ -977,6 +1045,16 @@ fn test_parse_array() { })) )], ); + assert_eq!( + Ast::from_str("{{ [] |foo }}", None, &syntax).unwrap().nodes, + vec![Node::Expr( + Ws(None, None), + WithSpan::no_span(Expr::Filter(Filter { + name: "foo", + arguments: vec![WithSpan::no_span(Expr::Array(vec![]))] + })) + )], + ); } #[test]