tree-sitter / tree-sitter-c

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

bug: Flat macro instances cause syntax error #209

Closed leath-dub closed 3 months ago

leath-dub commented 3 months ago

Did you check existing issues?

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

0.22.1

Describe the bug

On my system (Fedora 38), when you run tree-sitter parse on a basic c standard library header, for example stdio.h. You will find quite a lot of syntax errors related to macros like this:

#define FOO static int x = 10;

FOO

This causes a syntax error in the c parser when parsing the usage of FOO. This is problematic for my project as I extract any syntax errors and raise them generically if not handled in my program. With this I can't even parse stdio.h without needed to add weird edge cases on where to ignore syntax errors.

Steps To Reproduce/Bad Parse Tree

  1. run tree-sitter parse on the following minimal source:
#define FOO static int x = 10;

FOO
  1. Observe the error
(translation_unit [0, 0] - [3, 0]                                    
  (preproc_def [0, 0] - [1, 0]                                       
    name: (identifier [0, 8] - [0, 11])                              
    value: (preproc_arg [0, 12] - [0, 30]))                          
  (type_identifier [2, 0] - [2, 3]))                                 
fail.h     0.12 ms         311 bytes/ms (MISSING ";" [2, 3] - [2, 3])

Expected Behavior/Parse Tree

I would expect it to have no errors

Repro

NA
amaanq commented 3 months ago

this is a known limitation of the C parser - unfortunately macros require too much contextual info for them to be parsable

leath-dub commented 3 months ago

this is a known limitation of the C parser - unfortunately macros require too much contextual info for them to be parsable

Yeah as I was writing this, I realised it is not really feasibly possible to account for arbitrary string replacement in a parser. Very understandable