Skip to content

Commit

Permalink
Fix parsing of extra semi-colons inside structure declarations.
Browse files Browse the repository at this point in the history
  • Loading branch information
eliben committed Mar 19, 2016
1 parent ad99655 commit 95e3b76
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
10 changes: 9 additions & 1 deletion pycparser/c_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -836,7 +836,10 @@ def p_struct_declaration_list(self, p):
""" struct_declaration_list : struct_declaration
| struct_declaration_list struct_declaration
"""
p[0] = p[1] if len(p) == 2 else p[1] + p[2]
if len(p) == 2:
p[0] = p[1] or []
else:
p[0] = p[1] + (p[2] or [])

def p_struct_declaration_1(self, p):
""" struct_declaration : specifier_qualifier_list struct_declarator_list_opt SEMI
Expand Down Expand Up @@ -890,6 +893,11 @@ def p_struct_declaration_2(self, p):
spec=p[1],
decls=[dict(decl=p[2], init=None)])

def p_struct_declaration_3(self, p):
""" struct_declaration : SEMI
"""
p[0] = None

def p_struct_declarator_list(self, p):
""" struct_declarator_list : struct_declarator
| struct_declarator_list COMMA struct_declarator
Expand Down
31 changes: 31 additions & 0 deletions tests/test_c_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -763,6 +763,37 @@ def test_struct_union(self):
['Decl', 'heads',
['PtrDecl', ['PtrDecl', ['TypeDecl', ['IdentifierType', ['Node']]]]]]]]]])

def test_struct_with_extra_semis_inside(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']]]]]]]])

s2 = """
struct {
int a;;;;
float b, c;
;;
char d;
} foo;
"""
s2_ast = self.parse(s2)
self.assertEqual(expand_decl(s2_ast.ext[0]),
['Decl', 'foo',
['TypeDecl', ['Struct', None,
[['Decl', 'a', ['TypeDecl', ['IdentifierType', ['int']]]],
['Decl', 'b', ['TypeDecl', ['IdentifierType', ['float']]]],
['Decl', 'c', ['TypeDecl', ['IdentifierType', ['float']]]],
['Decl', 'd',
['TypeDecl', ['IdentifierType', ['char']]]]]]]])

def test_anonymous_struct_union(self):
s1 = """
union
Expand Down

0 comments on commit 95e3b76

Please sign in to comment.