Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug: tree-sitter-c ignores line comment after the macro defined on the same line #235

Open
2 tasks done
Linerre opened this issue Oct 7, 2024 · 0 comments
Open
2 tasks done
Labels

Comments

@Linerre
Copy link

Linerre commented Oct 7, 2024

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)

tree-sitter 0.23.2 (d97db6d63507eb62c536bcb2c4ac7d70c8ec665e)

Describe the bug

In Emacs (v29+ with built-in tree-sitter support), when editing C files, I found tree-sitter-c does not recognize the line comment like below

#define CR4_PSE         0x00000010      // Page size extension

The parse tree for the above line is

(preproc_def #define name: (identifier) value: (preproc_arg))

In the screenshot below, the comment // Page size extension is recognized as part of preproc_arc, not a comment node

tree-sitter-c

Steps To Reproduce/Bad Parse Tree

  1. install tree-sitter and tree-sitter-cli (I'm using Arch Linux so sudo pacman -S tree-sitter tree-sitter-cli)
  2. clone tree-sitter/tree-sitter-c repository and cd into it
  3. run tree-sitter init-config and tree-sitter generate to set up the environment properly
  4. create a simple test file (see Repro below) that has a line of macro mentioned above and run tree-sitter parse test.c I got the the following parse tree
(translation_unit [0, 0] - [5, 0]
  (preproc_def [0, 0] - [1, 0]
    name: (identifier [0, 8] - [0, 14])
    value: (preproc_arg [0, 24] - [0, 57]))
  (function_definition [2, 0] - [4, 1]
    type: (primitive_type [2, 0] - [2, 3])
    declarator: (function_declarator [2, 4] - [2, 10]
      declarator: (identifier [2, 4] - [2, 8])
      parameters: (parameter_list [2, 8] - [2, 10]))
    body: (compound_statement [2, 11] - [4, 1]
      (return_statement [3, 2] - [3, 11]
        (number_literal [3, 9] - [3, 10])))))

Expected Behavior/Parse Tree

Something similar to the below if possible

(translation_unit [0, 0] - [5, 0]
  (preproc_def [0, 0] - [1, 0]
    name: (identifier [0, 8] - [0, 14])
    value: (preproc_arg [0, 24] - [0, 57]))
   (comment)                                                          <====== expect a comment node here
  (function_definition [2, 0] - [4, 1]
    type: (primitive_type [2, 0] - [2, 3])
    declarator: (function_declarator [2, 4] - [2, 10]
      declarator: (identifier [2, 4] - [2, 8])
      parameters: (parameter_list [2, 8] - [2, 10]))
    body: (compound_statement [2, 11] - [4, 1]
      (return_statement [3, 2] - [3, 11]
        (number_literal [3, 9] - [3, 10])))))

Repro

#define PGSIZE          4096    // bytes mapped by a page

int main() {
  return 0;
}
@Linerre Linerre added the bug label Oct 7, 2024
@Linerre Linerre changed the title bug: tree-sitter-c does ignores line comment after the macro defined on the same line bug: tree-sitter-c ignores line comment after the macro defined on the same line Oct 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant