tree-sitter / tree-sitter-c

C grammar for tree-sitter
MIT License
225 stars 100 forks source link

bug: Parsing error for structure initialisation #180

Closed Raghava-Ch closed 7 months ago

Raghava-Ch commented 8 months ago

Did you check existing issues?

Describe the bug

Ex:struct Point p1 = { x: 10, y: 20 };

AST:

translation_unit [0, 0] - [2, 0] declaration [0, 0] - [0, 35] type: struct_specifier [0, 0] - [0, 12] name: type_identifier [0, 7] - [0, 12] declarator: init_declarator [0, 13] - [0, 34] declarator: identifier [0, 13] - [0, 15] value: initializer_list [0, 18] - [0, 34] ERROR [0, 20] - [0, 22] identifier [0, 20] - [0, 21] number_literal [0, 23] - [0, 25] ERROR [0, 27] - [0, 29] identifier [0, 27] - [0, 28] number_literal [0, 30] - [0, 32]

while initialising the structure with the member names is parsing as errors.

Steps To Reproduce/Bad Parse Tree

struct Point p1 = { .x = 10, .y = 20 }; //this is parsing fine struct Point p1 = { x: 10, y: 20 }; // this is parsing with errors check on Tree-sitter Syntax Tree Playground

Expected Behavior/Parse Tree

parse without error, considering labels of the struct members

Repro

Raghava-Ch commented 7 months ago

If some one having the same issue, I have done some workaround the in the grammar.js, though it doesn't break any existing test case I am not full confident on the work around so use it with caution.

updated grammar rules: initializer_pair: $ => seq( field('designator', repeat(choice($.subscript_designator, $.field_designator))), choice( '=', ':', ), field('value', choice($._expression, $.initializer_list)), ),
field_designator: $ => seq(optional('.'), $._field_identifier),

And the conflict entry [$._expression_not_binary, $.field_designator],

amaanq commented 7 months ago

Thanks for the bug report, fixed on master