Skip to content

Commit

Permalink
Allow spaces around the |filter operator
Browse files Browse the repository at this point in the history
  • Loading branch information
Kijewski committed Jun 19, 2024
1 parent f07c724 commit 759c605
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 13 deletions.
16 changes: 4 additions & 12 deletions rinja_parser/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion rinja_parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -730,7 +730,7 @@ impl Level {
#[allow(clippy::type_complexity)]
fn filter(i: &str, level: Level) -> ParseResult<'_, (&str, Option<Vec<WithSpan<'_, Expr<'_>>>>)> {
let (i, (_, fname, args)) = tuple((
char('|'),
ws(char('|')),
ws(identifier),
opt(|i| Expr::arguments(i, level, false)),
))(i)?;
Expand Down
78 changes: 78 additions & 0 deletions rinja_parser/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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(
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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]
Expand Down Expand Up @@ -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]
Expand Down

0 comments on commit 759c605

Please sign in to comment.