Skip to content

bug: Error parsing designated aggregate initialization without = #331

@MarcelRobitaille

Description

@MarcelRobitaille

Did you check existing issues?

  • I have read all the tree-sitter docs if it relates to using the parser
  • I have searched the existing issues of tree-sitter-cpp

Tree-Sitter CLI Version, if relevant (output of tree-sitter --version)

tree-sitter 0.25.4 (726dcd1e872149d95de581589fc408fb8ea9cb0b)

Describe the bug

There is a parsing error in case of aggregate initialization with designated initializers without using the equals sign.

This would be a 1-line change in tree-sitter-c, but as far as I can tell it's not valid C syntax.

     initializer_pair: $ => choice(
       seq(
         field('designator', repeat1(choice(
           $.subscript_designator,
           $.field_designator,
           $.subscript_range_designator,
         ))),
-        '=',
+        optional('='),
         field('value', choice($.expression, $.initializer_list)),
       ),
       seq(

I can't find this in the C++ spec, but it compiles in gcc, clang, and msvc.

Some background: I want to write a query to detect this, but since there's an error, I can't. I tried making a query that includes the ERROR node, but because of this bug, that's not working either.

Steps To Reproduce/Bad Parse Tree

Code:

SomeStruct
{
   .foo{"bar"},
};

Parse tree:

(translation_unit ; [0, 0] - [4, 0]
  (expression_statement ; [0, 0] - [3, 2]
    (compound_literal_expression ; [0, 0] - [3, 1]
      type: (type_identifier) ; [0, 0] - [0, 10]
      value: (initializer_list ; [1, 0] - [3, 1]
        (ERROR ; [2, 3] - [2, 7]
          (field_designator ; [2, 3] - [2, 7]
            (field_identifier))) ; [2, 4] - [2, 7]
        (initializer_list ; [2, 7] - [2, 14]
          (string_literal ; [2, 8] - [2, 13]
            (string_content))))))) ; [2, 9] - [2, 12]

Expected Behavior/Parse Tree

This is the parse tree if I add the = sign like

SomeStruct
{
   .foo = {"bar"},
};
(translation_unit ; [0, 0] - [4, 0]
  (expression_statement ; [0, 0] - [3, 2]
    (compound_literal_expression ; [0, 0] - [3, 1]
      type: (type_identifier) ; [0, 0] - [0, 10]
      value: (initializer_list ; [1, 0] - [3, 1]
        (initializer_pair ; [2, 3] - [2, 17]
          designator: (field_designator ; [2, 3] - [2, 7]
            (field_identifier)) ; [2, 4] - [2, 7]
          value: (initializer_list ; [2, 10] - [2, 17]
            (string_literal ; [2, 11] - [2, 16]
              (string_content)))))))) ; [2, 12] - [2, 15]

Repro

SomeStruct
{
   .foo{"bar"},
};

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions