aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSimon Lindholm <simon.lindholm10@gmail.com>2019-03-06 14:52:00 +0100
committerEli Bendersky <eliben@users.noreply.github.com>2019-03-06 05:52:00 -0800
commit992715f12aef69eb1351308f14fb5f1ff972ce57 (patch)
tree8528b62e2c635ae935022660af88851fd8a0027d
parent139fc1ab7c9e6a6f310f5e7f723b6680cd029f6c (diff)
downloadpycparser-992715f12aef69eb1351308f14fb5f1ff972ce57.tar.gz
Fix crash when file starts with a semicolon (#310)
-rw-r--r--pycparser/c_parser.py5
-rwxr-xr-xtests/test_c_parser.py22
2 files changed, 24 insertions, 3 deletions
diff --git a/pycparser/c_parser.py b/pycparser/c_parser.py
index 0e6e755..4e1889d 100644
--- a/pycparser/c_parser.py
+++ b/pycparser/c_parser.py
@@ -529,8 +529,7 @@ class CParser(PLYParser):
def p_translation_unit_2(self, p):
""" translation_unit : translation_unit external_declaration
"""
- if p[2] is not None:
- p[1].extend(p[2])
+ p[1].extend(p[2])
p[0] = p[1]
# Declarations always come as lists (because they can be
@@ -557,7 +556,7 @@ class CParser(PLYParser):
def p_external_declaration_4(self, p):
""" external_declaration : SEMI
"""
- p[0] = None
+ p[0] = []
def p_pp_directive(self, p):
""" pp_directive : PPHASH
diff --git a/tests/test_c_parser.py b/tests/test_c_parser.py
index 34c1f1e..c09e04d 100755
--- a/tests/test_c_parser.py
+++ b/tests/test_c_parser.py
@@ -136,6 +136,15 @@ class TestCParser_fundamentals(TestCParser_base):
['Decl', 'foo',
['TypeDecl', ['IdentifierType', ['int']]]])
+ def test_initial_semi(self):
+ t = self.parse(';')
+ self.assertEqual(len(t.ext), 0)
+ t = self.parse(';int foo;')
+ self.assertEqual(len(t.ext), 1)
+ self.assertEqual(expand_decl(t.ext[0]),
+ ['Decl', 'foo',
+ ['TypeDecl', ['IdentifierType', ['int']]]])
+
def test_coords(self):
""" Tests the "coordinates" of parsed elements - file
name, line and column numbers, with modification
@@ -828,6 +837,19 @@ class TestCParser_fundamentals(TestCParser_base):
['Decl', 'd',
['TypeDecl', ['IdentifierType', ['char']]]]]]]])
+ def test_struct_with_initial_semi(self):
+ s1 = """
+ struct {
+ ;int a;
+ } foo;
+ """
+ s1_ast = self.parse(s1)
+ self.assertEqual(expand_decl(s1_ast.ext[0]),
+ ['Decl', 'foo',
+ ['TypeDecl', ['Struct', None,
+ [['Decl', 'a',
+ ['TypeDecl', ['IdentifierType', ['int']]]]]]]])
+
def test_anonymous_struct_union(self):
s1 = """
union