I want to extract all function names from some C++ source code via a tree_sitter query. (link to playground
Here is my query:
(function_definition
declarator: [
(reference_declarator (function_declarator declarator: (_) @name))
(function_declarator declarator: (_) @name)
]
) @func
And here is the source code that I'm testing it on:
void sayHello() {
cout << "Hello, World!" << endl;
}
blah::mlah cram::greet(const string& name) {
cout << "Hello, " << name << "!" << endl;
}
blah::mlah& greet(const string& name) {
cout << "Hello, " << name << "!" << endl;
}
blah::mlah& cram::greet(const string& name) {
cout << "Hello, " << name << "!" << endl;
}
It seems like a bug to me that I must go through the
reference_declarator
node to get to thefunction_declarator
node when the function returns a reference to an object.Imperatively, I could optionally iterate into the
reference_declarator
. But queries do not traverse, they match.
I read through the tree sitter querying spec, and I couldn't figure out a way to shorten my query. I want to say "Given a function declaration, find the name of the declarator", but I do not know how to express it with the existing grammar.