如何简化Tree-sitter查询以提取C++所有函数名?
简化Tree-sitter查询提取C++函数名
我需要用Tree-sitter query提取C++源码中的所有函数名,当前使用的查询语句如下:
(function_definition declarator: [ (reference_declarator (function_declarator declarator: (_) @name)) (function_declarator declarator: (_) @name) ] ) @func
测试用的C++源码:
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; }
我遇到两个问题:
- 当函数返回对象引用时,必须通过
reference_declarator节点才能访问function_declarator节点的设计过于繁琐; - 命令式编程中可以可选遍历
reference_declarator,但Tree-sitter查询是匹配而非遍历逻辑,我翻遍了Tree-sitter查询规范也找不到简化方式,不知道如何用现有语法实现“给定函数声明,找到declarator的名称”的逻辑。
内容的提问来源于stack exchange,提问作者Ari Sweedler
相关产品推荐
相关产品推荐

