diff options
-rw-r--r-- | pycparser/_c_ast.yaml | 2 | ||||
-rw-r--r-- | pycparser/c_parser.py | 3 | ||||
-rw-r--r-- | tests/test_c_parser.py | 26 |
3 files changed, 23 insertions, 8 deletions
diff --git a/pycparser/_c_ast.yaml b/pycparser/_c_ast.yaml index 6f0f8a3..d225102 100644 --- a/pycparser/_c_ast.yaml +++ b/pycparser/_c_ast.yaml @@ -37,7 +37,7 @@ Compound: [block_items**] # Compound literal (anonymous aggregate) for C99.
# (type-name) {initializer_list}
-# type: the type decl
+# type: the typename
# init: ExprList for the initializer list
#
CompoundLiteral: [type*, init*]
diff --git a/pycparser/c_parser.py b/pycparser/c_parser.py index 0480447..e9d0403 100644 --- a/pycparser/c_parser.py +++ b/pycparser/c_parser.py @@ -1239,7 +1239,8 @@ class CParser(PLYParser): p[0] = c_ast.UnaryOp('p' + p[2], p[1], p[1].coord)
def p_postfix_expression_6(self, p): - """ postfix_expression : LPAREN type_name RPAREN LBRACE initializer_list LBRACE
+ """ postfix_expression : LPAREN type_name RPAREN LBRACE initializer_list RBRACE
+ | LPAREN type_name RPAREN LBRACE initializer_list COMMA RBRACE
"""
p[0] = c_ast.CompoundLiteral(p[2], p[5])
diff --git a/tests/test_c_parser.py b/tests/test_c_parser.py index 1b021d5..96a6a02 100644 --- a/tests/test_c_parser.py +++ b/tests/test_c_parser.py @@ -376,14 +376,28 @@ class TestCParser_fundamentals(unittest.TestCase): ['TypeDecl', ['IdentifierType', ['int']]]]]]) + # The C99 compound literal feature + # def test_compound_literals(self): - s1 = r''' + ps1 = self.parse(r''' void foo() { - int p = (int []){.kwa = 4}; - }''' - - self.parse(s1).show() - + p = (long long){k}; + tc = (struct jk){.a = {1, 2}, .b[0] = t}; + }''') + + compound = ps1.ext[0].body.block_items[0].rvalue + self.assertEqual(expand_decl(compound.type), + ['Typename', ['TypeDecl', ['IdentifierType', ['long', 'long']]]]) + self.assertEqual(expand_init(compound.init), + [['ID', 'k']]) + + compound = ps1.ext[0].body.block_items[1].rvalue + self.assertEqual(expand_decl(compound.type), + ['Typename', ['TypeDecl', ['Struct', 'jk', []]]]) + self.assertEqual(expand_init(compound.init), + [ + ([['ID', 'a']], [['Constant', 'int', '1'], ['Constant', 'int', '2']]), + ([['ID', 'b'], ['Constant', 'int', '0']], ['ID', 't'])]) def test_enums(self): e1 = "enum mycolor op;" |