Skip to content

Commit

Permalink
Fix support of augmented assignment with no rvalue. Fixes GH-1.
Browse files Browse the repository at this point in the history
  • Loading branch information
progval committed Oct 28, 2018
1 parent 935227f commit 0960623
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,8 @@ pub enum Statement {
Expressions(Vec<Expression>),
// `lhs = rhs1 = rhs2` -> `lhs, vec![rhs1, rhs2]`
Assignment(Vec<Expression>, Vec<Vec<Expression>>),
// `lhs: type` -> `lhs, type`
TypeAnnotation(Vec<Expression>, Expression),
// `lhs: type = rhs` -> `lhs, type, rhs`
TypedAssignment(Vec<Expression>, Expression, Vec<Expression>),
// `lhs += rhs` -> `lhs, AugAssignOp::Add, rhs`
Expand Down
24 changes: 20 additions & 4 deletions src/statements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,17 @@ named!(expr_stmt<StrSpan, Statement>,
do_parse!(
lhs: call!(ExpressionParser::<NewlinesAreNotSpaces>::testlist_star_expr) >>
r: ws_nonl!(alt!(
// Case 1: "foo: bar = baz"
// Case 1: "foo: bar = baz" and "foo: bar"
do_parse!(
char!(':') >>
typed: call!(ExpressionParser::<NewlinesAreNotSpaces>::test) >>
char!('=') >>
rhs: call!(ExpressionParser::<NewlinesAreNotSpaces>::test) >> (
Statement::TypedAssignment(lhs.clone(), *typed, vec![*rhs])
rhs: opt!(ws_nonl!(preceded!(char!('='),
call!(ExpressionParser::<NewlinesAreNotSpaces>::test)
))) >> (
match rhs {
None => Statement::TypeAnnotation(lhs.clone(), *typed),
Some(rhs) => Statement::TypedAssignment(lhs.clone(), *typed, vec![*rhs]),
}
)
)

Expand Down Expand Up @@ -974,6 +978,18 @@ mod tests {
)));
}

#[test]
fn test_typeannotation() {
assert_parse_eq(small_stmt(make_strspan("foo: bar")), Ok((make_strspan(""),
Statement::TypeAnnotation(
vec![
Expression::Name("foo".to_string()),
],
Expression::Name("bar".to_string()),
)
)));
}

#[test]
fn test_augassign() {
assert_parse_eq(small_stmt(make_strspan("foo:bar = baz")), Ok((make_strspan(""),
Expand Down
5 changes: 5 additions & 0 deletions src/visitors/printer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,11 @@ fn format_statement(indent: usize, stmt: &Statement) -> String {
}
s.push_str("\n");
},
Statement::TypeAnnotation(ref lhs, ref typed) => {
s.push_str(&format!("{}: {}\n",
comma_join(lhs.iter().map(format_expr)),
format_expr(typed)));
},
Statement::TypedAssignment(ref lhs, ref typed, ref rhs) => {
s.push_str(&format!("{}:{} = {}\n",
comma_join(lhs.iter().map(format_expr)),
Expand Down

0 comments on commit 0960623

Please sign in to comment.