Merge pull request #58 from OddBloke/trailing_commas

Parse trailing commas in lists/dicts
This commit is contained in:
Windel Bouwman
2018-08-12 12:20:00 +02:00
committed by GitHub
2 changed files with 14 additions and 2 deletions

View File

@@ -336,7 +336,7 @@ Atom: ast::Expression = {
<s:String> => ast::Expression::String { value: s },
<n:Number> => ast::Expression::Number { value: n },
<i:Identifier> => ast::Expression::Identifier { name: i },
"[" <e:TestList?> "]" => {
"[" <e:TestList?> <_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)> = {
<e1:DictEntry> <e2:("," DictEntry)*> => {
<e1:DictEntry> <e2:("," DictEntry)*> <_trailing_comma:","?> => {
let mut d = vec![e1];
d.extend(e2.into_iter().map(|x| x.1));
d

12
tests/snippets/commas.py Normal file
View File

@@ -0,0 +1,12 @@
list1 = ["a", "b",]
list2 = [
"a",
"b",
]
assert list1 == list2
dict1 = {"a": "b",}
dict2 = {
"a": "b",
}
#assert dict1 == dict2