Skip to content

Commit

Permalink
Add namedexpr (aka walrus operator) in argument position.
Browse files Browse the repository at this point in the history
cpython commit: 8f59ee01be3d83d5513a9a3f654a237d77d80d9a
  • Loading branch information
progval committed Sep 9, 2020
1 parent 338dede commit e92da74
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/expressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,7 @@ impl<ANS: AreNewlinesSpaces> ExpressionParser<ANS> {
// arglist: argument (',' argument)* [',']

// argument: ( test [comp_for] |
// test ':=' test |
// test '=' test |
// '**' test |
// '*' test )
Expand All @@ -563,6 +564,12 @@ impl<ANS: AreNewlinesSpaces> ExpressionParser<ANS> {
alt!(
preceded!(tag!("**"), call!(Self::test)) => { |kwargs: Box<_>| Argument::Kwargs(*kwargs) }
| preceded!(char!('*'), call!(Self::test)) => { |args: Box<_>| Argument::Starargs(*args) }
| do_parse!(
name: call!(Self::test) >>
value: preceded!(tag!(":="), call!(Self::test)) >> (
Argument::Positional(Expression::Named(name, value))
)
)
| do_parse!(
name: name >> // According to the grammar, this should be a 'test', but cpython actually refuses it (for good reasons)
value: preceded!(char!('='), call!(Self::test)) >> (
Expand Down Expand Up @@ -1175,6 +1182,24 @@ mod tests {
);
}

#[test]
fn test_call_positional_namedexpr() {
let atom_expr = ExpressionParser::<NewlinesAreNotSpaces>::atom_expr;
assert_parse_eq(
atom_expr(make_strspan("foo(bar := baz)")),
Ok((
make_strspan(""),
Box::new(Expression::Call(
Box::new(Expression::Name("foo".to_string())),
vec![Argument::Positional(Expression::Named(
Box::new(Expression::Name("bar".to_string())),
Box::new(Expression::Name("baz".to_string())),
))],
)),
)),
);
}

#[test]
fn test_call_positional() {
let atom_expr = ExpressionParser::<NewlinesAreNotSpaces>::atom_expr;
Expand Down

0 comments on commit e92da74

Please sign in to comment.