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

Query for function definitions

Open dan1338 opened this issue 2 years ago • 1 comments

I tried to construct a query that would match a function_definition with a capture for the identifier, but I wasn't able to make a one that would reliably match the name of a defined function regardless of it's return type (mostly to do with the way pointers are handled, #14)

The code to query

void func1(void)
{
}

void* func2(void)
{
}

void** func3(void)
{
}

The query

(function_definition (_ (identifier) @name)) @func

The resulting tree structure is something like

function_definition
    declarator: pointer_declarator
        declarator: function_declarator
            declarator: identifier

Without the ability to recursively match an arbitrary number of pointer_declarators I don't see how one could use the query language for any useful purpose with this grammar.

dan1338 avatar May 18 '22 07:05 dan1338

I have started playing with tree-sitter for a short time and am trying to parse function declarations; however, I have not found a way to parse the function's return type. Therefore, the query I have constructed at the moment is:

(function_declarator) @func

But it lacks the part of the return type, which I don't know how to fix yet. I don't think there should be a special case if the return type is a pointer or any other type. I guess the type_identifier rule would be the one that will help us. Still, I may be wrong since I am entirely new to tree-sitter queries.

innerout avatar Jun 25 '22 15:06 innerout