Skip to content

Commit

Permalink
Add extra rule to properly parse semicolons so that the ASTs remain e…
Browse files Browse the repository at this point in the history
…qual. Add test corroborating equality of ASTs
  • Loading branch information
Ignacio Tiraboschi committed Jan 8, 2025
1 parent 530b2c4 commit 2425174
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
4 changes: 4 additions & 0 deletions pycparser/c_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -1581,6 +1581,10 @@ def p_labeled_statement_3(self, p):
p[0] = c_ast.Default([p[3]], self._token_coord(p, 1))

def p_labeled_statement_4(self, p):
""" labeled_statement : ID COLON SEMI"""
p[0] = c_ast.Label(p[1], c_ast.EmptyStatement(self._token_coord(p, 1)), self._token_coord(p, 1))

def p_labeled_statement_5(self, p):
""" labeled_statement : ID COLON """
p[0] = c_ast.Label(p[1], c_ast.EmptyStatement(self._token_coord(p, 1)), self._token_coord(p, 1))

Expand Down
23 changes: 23 additions & 0 deletions tests/test_c_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -2489,6 +2489,29 @@ def test_samescope_reuse_name(self):
'''
self.assertRaises(ParseError, self.parse, s2)

class TestCParser_labels(TestCParser_base):
""" Test issues related to the labels.
"""
def test_label_empty_statement(self):
# Parse the statements
s1 = r'''
int main() {
label:
}
'''
s2 = r'''
int main() {
label:;
}
'''
ast1 = self.parse(s1)
ast2 = self.parse(s2)

buf1 = io.StringIO()
buf2 = io.StringIO()
ast1.show(buf=buf1)
ast2.show(buf=buf2)
self.assertEqual(buf1.getvalue(), buf2.getvalue())

if __name__ == '__main__':
#~ suite = unittest.TestLoader().loadTestsFromNames(
Expand Down

0 comments on commit 2425174

Please sign in to comment.