Skip to content

Commit 0960623

Browse files
committed
Fix support of augmented assignment with no rvalue. Fixes GH-1.
1 parent 935227f commit 0960623

File tree

3 files changed

+27
-4
lines changed

3 files changed

+27
-4
lines changed

src/ast.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,8 @@ pub enum Statement {
327327
Expressions(Vec<Expression>),
328328
// `lhs = rhs1 = rhs2` -> `lhs, vec![rhs1, rhs2]`
329329
Assignment(Vec<Expression>, Vec<Vec<Expression>>),
330+
// `lhs: type` -> `lhs, type`
331+
TypeAnnotation(Vec<Expression>, Expression),
330332
// `lhs: type = rhs` -> `lhs, type, rhs`
331333
TypedAssignment(Vec<Expression>, Expression, Vec<Expression>),
332334
// `lhs += rhs` -> `lhs, AugAssignOp::Add, rhs`

src/statements.rs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,17 @@ named!(expr_stmt<StrSpan, Statement>,
6464
do_parse!(
6565
lhs: call!(ExpressionParser::<NewlinesAreNotSpaces>::testlist_star_expr) >>
6666
r: ws_nonl!(alt!(
67-
// Case 1: "foo: bar = baz"
67+
// Case 1: "foo: bar = baz" and "foo: bar"
6868
do_parse!(
6969
char!(':') >>
7070
typed: call!(ExpressionParser::<NewlinesAreNotSpaces>::test) >>
71-
char!('=') >>
72-
rhs: call!(ExpressionParser::<NewlinesAreNotSpaces>::test) >> (
73-
Statement::TypedAssignment(lhs.clone(), *typed, vec![*rhs])
71+
rhs: opt!(ws_nonl!(preceded!(char!('='),
72+
call!(ExpressionParser::<NewlinesAreNotSpaces>::test)
73+
))) >> (
74+
match rhs {
75+
None => Statement::TypeAnnotation(lhs.clone(), *typed),
76+
Some(rhs) => Statement::TypedAssignment(lhs.clone(), *typed, vec![*rhs]),
77+
}
7478
)
7579
)
7680

@@ -974,6 +978,18 @@ mod tests {
974978
)));
975979
}
976980

981+
#[test]
982+
fn test_typeannotation() {
983+
assert_parse_eq(small_stmt(make_strspan("foo: bar")), Ok((make_strspan(""),
984+
Statement::TypeAnnotation(
985+
vec![
986+
Expression::Name("foo".to_string()),
987+
],
988+
Expression::Name("bar".to_string()),
989+
)
990+
)));
991+
}
992+
977993
#[test]
978994
fn test_augassign() {
979995
assert_parse_eq(small_stmt(make_strspan("foo:bar = baz")), Ok((make_strspan(""),

src/visitors/printer.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,11 @@ fn format_statement(indent: usize, stmt: &Statement) -> String {
113113
}
114114
s.push_str("\n");
115115
},
116+
Statement::TypeAnnotation(ref lhs, ref typed) => {
117+
s.push_str(&format!("{}: {}\n",
118+
comma_join(lhs.iter().map(format_expr)),
119+
format_expr(typed)));
120+
},
116121
Statement::TypedAssignment(ref lhs, ref typed, ref rhs) => {
117122
s.push_str(&format!("{}:{} = {}\n",
118123
comma_join(lhs.iter().map(format_expr)),

0 commit comments

Comments
 (0)