From 19c9db23756b0e2eff310b93869532ad79dcebdd Mon Sep 17 00:00:00 2001 From: Daniel Watkins Date: Sat, 11 Aug 2018 23:37:01 -0400 Subject: [PATCH] Parse trailing commas in lists/dicts --- parser/src/python.lalrpop | 4 ++-- tests/snippets/commas.py | 12 ++++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) create mode 100644 tests/snippets/commas.py diff --git a/parser/src/python.lalrpop b/parser/src/python.lalrpop index 8a252c8acc5..c75cdc294cf 100644 --- a/parser/src/python.lalrpop +++ b/parser/src/python.lalrpop @@ -336,7 +336,7 @@ Atom: ast::Expression = { => ast::Expression::String { value: s }, => ast::Expression::Number { value: n }, => ast::Expression::Identifier { name: i }, - "[" "]" => { + "[" <_trailing_comma:","?> "]" => { match e { None => ast::Expression::List { elements: Vec::new() }, Some(elements) => ast::Expression::List { elements }, @@ -362,7 +362,7 @@ Atom: ast::Expression = { }; TestDict: Vec<(ast::Expression, ast::Expression)> = { - => { + <_trailing_comma:","?> => { let mut d = vec![e1]; d.extend(e2.into_iter().map(|x| x.1)); d diff --git a/tests/snippets/commas.py b/tests/snippets/commas.py new file mode 100644 index 00000000000..ccf9d5857c0 --- /dev/null +++ b/tests/snippets/commas.py @@ -0,0 +1,12 @@ +list1 = ["a", "b",] +list2 = [ + "a", + "b", +] +assert list1 == list2 + +dict1 = {"a": "b",} +dict2 = { + "a": "b", +} +#assert dict1 == dict2