Fix some more parsing issues

pull/104/head
Anthony Johnson 8 years ago
parent 0faeb500ee
commit 2603729925
No known key found for this signature in database
GPG Key ID: C5C79D82F922D604

@ -318,26 +318,33 @@ class ParserExtra(parser.Parser):
as ``__all__`` and class parent classes on class definition.
"""
content = None
is_list = True
while self.current is not None:
if self.current.kind == tk.OP and self.current.value in '[(':
if self.current.kind == tk.STRING:
content.append(self.parse_string())
elif self.current.kind == tk.NUMBER:
content.append(self.parse_number())
elif self.current.kind == tk.NAME:
# Handle generators
if self.current.value == 'for' and not content:
is_list = False
# TODO this is dropped for now, but will can be handled with an
# object lookup in the future, if we decide to track assignment.
# content.append(self.parse_object_identifier())
self.stream.move()
elif self.current.kind == tk.OP and self.current.value in '[(':
if content is None:
content = []
self.stream.move()
else:
content.append(self.parse_iterable())
continue
elif self.current.kind == tk.OP and self.current.value in '])':
self.stream.move()
return content
elif self.current.kind == tk.STRING:
content.append(self.parse_string())
elif self.current.kind == tk.NUMBER:
content.append(self.parse_number())
elif self.current.kind == tk.NAME:
# TODO this is dropped for now, but will can be handled with an
# object lookup in the future, if we decide to track assignment.
# content.append(self.parse_object_identifier())
pass
if is_list:
return content
# Discard generator because we can't do anything with them
return []
else:
self.stream.move()

@ -66,3 +66,18 @@ class PythonParserTests(unittest.TestCase):
]
"""
self.assertEqual(self.parse(source).all, ['foo', 'bar'])
def test_parses_all_generator(self):
source = """
__all__ = [x for x in dir(token) if x[0] != '_'] + ['foo', 'bar']
"""
out = self.parse(source)
self.assertEqual(self.parse(source).all, ['foo', 'bar'])
def test_parses_name(self):
source = "foo.bar"
self.assertEqual(self.parse(source).children, [])
def test_parses_list(self):
source = "__all__ = [[1, 2], [3, 4]]"
self.assertEqual(self.parse(source).all, [[1, 2], [3, 4]])

Loading…
Cancel
Save