tree-sitter / tree-sitter-c

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

bug: Query Error in Parsing Else Clauses #216

Closed in-ch closed 1 month ago

in-ch commented 1 month ago

Did you check existing issues?

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

tree-sitter 0.21.1

Describe the bug

There appears to be a bug related to the query for parsing else clauses in tree-sitter. When using the following query to parse the provided code snippet, a TSQueryErrorStructure error occurs.

The query works correctly in the Syntax Tree Playground, suggesting that the issue might be specific to the TypeScript implementation or usage of the tree-sitter library.

스크린샷 2024-08-07 14 10 26

This issue arises when trying to ensure compliance with a rule that mandates specific handling of else clauses.

Steps To Reproduce/Bad Parse Tree

  1. Use the provided query in a TypeScript environment with tree-sitter for C.
  2. Parse the given code snippet.
  3. Observe the Query error of type TSQueryErrorNodeType error.

Expected Behavior/Parse Tree

The query should correctly identify the else_clause without any errors, as it does in the Syntax Tree Playground.

Repro

Query

(if_statement
      (else_clause
            (if_statement)
     ) @else_clause
)

Code Snippet

if( condition ) {
      printf("Hello 1");
} 
else if( condition ) {
      printf("Hello 2");
} else {
      printf("Hello 3");
}

Typescript

const query = `
  (if_statement
        (else_clause
             (statement_block)
        ) @else_clause
   )
`;
const parserQuery = new Query(C, query);
const matches = parserQuery.matches(this.tree.rootNode);
in-ch commented 1 month ago

I realized that I set the language incorrectly. I will close the question.