Skip to content

bug: macros produce incorrect parse trees #268

@nickav

Description

@nickav

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-c

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

No response

Describe the bug

The following code snippet produces a parse tree that seems to suggest the if is inside the preproc_if:

#if 0
if (false) {
#endif
}

Steps To Reproduce/Bad Parse Tree

Produces the following tree (copied from Playground):

translation_unit [0, 0] - [4, 0]
  preproc_if [0, 0] - [3, 1]
    condition: number_literal [0, 4] - [0, 5]
    if_statement [1, 0] - [3, 1]
      condition: parenthesized_expression [1, 3] - [1, 10]
        false [1, 4] - [1, 9]
      consequence: compound_statement [1, 11] - [3, 1]
        preproc_call [2, 0] - [3, 0]
          directive: preproc_directive [2, 0] - [2, 6]
    MISSING "#endif" [3, 1] - [3, 1]

Expected Behavior/Parse Tree

However, I would expect the macro to take the highest precedence. Meaning the parse tree I would expect would include everything inside the #if 0 / #endif and then have a single dangling } brace as an error at the end.

Example parse tree:

translation_unit [0, 0] - [4, 0]
  ERROR [0, 0] - [3, 0]
    number_literal [0, 4] - [0, 5]
    parenthesized_expression [1, 3] - [1, 10]
      false [1, 4] - [1, 9]
    preproc_call [2, 0] - [3, 0]
      directive: preproc_directive [2, 0] - [2, 6]
ERROR [3, 0] - [3, 1]

Repro

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