tree-sitter-c icon indicating copy to clipboard operation
tree-sitter-c copied to clipboard

bug: Incorrect parsing of macro with whitespace at end of line

Open mariusgreuel opened this issue 10 months ago • 0 comments

Did you check existing issues?

  • [ ] I have read all the tree-sitter docs if it relates to using the parser
  • [x] 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

When a macro without an argument contains a whitespace at the end of the line, the parser incorrectly parses the following line as an argument.

Steps To Reproduce/Bad Parse Tree

For instance, the code (note that there is a whitespace after foo, i.e. #define foo \n#define bar\n)

#define foo 
#define bar

is parsed by https://tree-sitter.github.io/tree-sitter/7-playground.html as

translation_unit [0, 0] - [3, 0]
  preproc_def [0, 0] - [2, 0]
    name: identifier [0, 8] - [0, 11]
    value: preproc_arg [1, 0] - [1, 11]

The GitHub markdown renderer has the same issue, as #define bar is not rendered in red.

Expected Behavior/Parse Tree

translation_unit [0, 0] - [3, 0]
  preproc_def [0, 0] - [1, 0]
    name: identifier [0, 8] - [0, 11]
  preproc_def [1, 0] - [2, 0]
    name: identifier [1, 8] - [1, 11]

Repro

#define foo 
#define bar

mariusgreuel avatar May 22 '25 13:05 mariusgreuel