Skip to content

Commit 19c9db2

Browse files
committed
Parse trailing commas in lists/dicts
1 parent 37c1377 commit 19c9db2

2 files changed

Lines changed: 14 additions & 2 deletions

File tree

parser/src/python.lalrpop

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ Atom: ast::Expression = {
336336
<s:String> => ast::Expression::String { value: s },
337337
<n:Number> => ast::Expression::Number { value: n },
338338
<i:Identifier> => ast::Expression::Identifier { name: i },
339-
"[" <e:TestList?> "]" => {
339+
"[" <e:TestList?> <_trailing_comma:","?> "]" => {
340340
match e {
341341
None => ast::Expression::List { elements: Vec::new() },
342342
Some(elements) => ast::Expression::List { elements },
@@ -362,7 +362,7 @@ Atom: ast::Expression = {
362362
};
363363

364364
TestDict: Vec<(ast::Expression, ast::Expression)> = {
365-
<e1:DictEntry> <e2:("," DictEntry)*> => {
365+
<e1:DictEntry> <e2:("," DictEntry)*> <_trailing_comma:","?> => {
366366
let mut d = vec![e1];
367367
d.extend(e2.into_iter().map(|x| x.1));
368368
d

tests/snippets/commas.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
list1 = ["a", "b",]
2+
list2 = [
3+
"a",
4+
"b",
5+
]
6+
assert list1 == list2
7+
8+
dict1 = {"a": "b",}
9+
dict2 = {
10+
"a": "b",
11+
}
12+
#assert dict1 == dict2

0 commit comments

Comments
 (0)