-
Notifications
You must be signed in to change notification settings - Fork 27
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
Python的支持问题,以及 coref 的打印问题 #95
Comments
我们这里有个样例可能能帮到你 // script
use coref::python::*
fn default_db() -> PythonDB {
return PythonDB::load("coref_python_src.db")
}
// 递归查找以a为起点的语法树边,b 和 c 组成一条有向边,b 为父节点,c 为子节点,index 为这条边在树中距离 root 的层级
fn searchEdge(a: CombineElement, b: CombineElement, c: CombineElement, index: int) -> bool {
if (a = b.getAnAncestorForIndex(index) || (b = a && index = 0)) {
if (b = c.getParent()) {
return true
}
}
}
// 获取位置信息,如果该节点在 ast parser 中不存在位置信息,则会递归展示该节点的父节点的位置信息,
// 例如 Comprehension,Arguments,Withitem, DocstringComment 等类型
fn getLoc(p: CombineElement, line: int, col: int) -> bool {
return line = p.getLocation().getStartLineNumber() &&
col = p.getLocation().getStartColumnNumber()
}
// 输出AST语法树的有向边,以及点的代码片段
// 第一列是层数,从0开始
// 第二列是当前边的父节点
// 第三列是父节点的节点类型
// 第四列是父节点的代码片段
// 第五列是父节点起始行号
// 第六列是父节点起始列号
// 第七列是当前边的子节点
// 第八列是子节点的节点类型
// 第九列是子节点的代码片段
// 第十列是子节点起始行号
// 第十一列是子节点起始列号
@output
fn out(filePath: string,
depth: int,
parent: CombineElement,
parentKind: string,
parentContent: string,
parentLine: int,
parentColumn: int,
child: CombineElement,
childKind: string,
childContent: string,
childLine: int,
childColumn: int) -> bool {
for (a in File(default_db())) {
if (filePath = a.getRelativePath() &&
searchEdge(CombineElement(default_db()).find(a), parent, child, depth) &&
childContent = child.print() && // 输出子节点的内容
shortPrint(parent, parentContent) && // 优化输出父节点内容
parentKind = parent.getType() &&
childKind = child.getType() &&
getLoc(parent, parentLine, parentColumn) &&
getLoc(child, childLine, childColumn)) {
return true
}
}
}
// 找到长度在 5 行以上的Expression, 可以调整
fn isLongExpression(s: CombineElement) -> bool {
return Expression(default_db()).find(s).getSize().getNumberOfTotalLines() > 4
}
// 优化输出父节点
fn shortPrint(p: CombineElement, n: string) -> bool {
if (isStatement(p)) {
return n = p.getType()
}
if (!isStatement(p)) {
if (isLongExpression(p)) {
return n = p.getType()
}
if (!isLongExpression(p)) {
return n = p.print()
}
}
}
// 找到属于Statement的节点
fn isStatement(s: CombineElement) -> bool {
for (b in Statement(default_db())) {
if (b.key_eq(s)) {
return true
}
}
} |
您好!非常感谢您及时提供的脚本和热情的回复! 关于您分享的Python AST打印示例,我已经仿照4_godelscript_language.md文件中JavaScript 的AST Print章节,已经可以成功解析并且打印 AST。目前仍有两个问题想向您进一步请教:
若团队能提供更多技术细节或方向指引,我将深感荣幸!再次感谢您的耐心解答! 🌟 |
Python 库的支持范围可能需要咨询一下@xiexie。coref 图结构目前没有工具可以提供图数据格式生成,你可以利用 GödelScript 先拿到初步数据,再使用其他脚本来辅助生成图数据。 |
你好,目前 Python 支持的 COREF 信息包括 AST、类继承关系和文档信息,并已支持部分 ASG 信息。 如果你有 ASG 或 CFG 相关的需求,欢迎尝试基于我们的 抽取器 进行修改 或 Godel lib 进行建模,以补充所需的信息。 |
非常感谢两位开发人员的热心回复! 1. 关于 @ValKmjolnir 的建议:“利用 GödelScript 先拿到初步数据,再使用其他脚本来辅助生成图数据。”。我现在遇到了一些问题: 2. 关于@xiexie 建议:“欢迎尝试基于我们的 抽取器 进行修改 或 Godel lib 进行建模,以补充所需的信息”。我现在已经阅读过了源码,大概读懂了文件结构,“抽取器”的源码位于:cli/extractor/extractor.py 以及language/语言/extractor 中。 Godel lib 包含了各种Godel 各种函数的实现。非常感谢您的建议! |
您好,我是浙江大学的在读博士生。我对贵团队提出的CodeFuse-Query项目非常感兴趣,目前想根据该项目进行后续的研究。我仔细阅读了贵团队在 github 上的 CodeFuse-Query 的仓库,以及,并且使用python 项目进行了实验。有以下的问题想要咨询一下:
CodeFuse-Query 对于 python 支持的情况:目前 github 上写的是:python 版本还在 beta,而且 "Except for OC/C++, all languages support complete AST information and Documentation"。我目前使用一个 python 的 flask 的项目进行实验,在生成的数据库中,看到了一些包含slice、class、variable等关键字的表,请问现在CodeFuse-Query 对于 python 还是“只支持 AST+文档信息”吗?是否存储了ASG、CFG 等图结构?
关于Gödel语法的问题:我想实现:“查询整个 coref 图结构,并且转换成 networkx 这种通用的图结构”。我仔细阅读了 CodeFuse-Query-main/godel-script/docs 中的所有文档,并且仔细阅读了API 文档:https://codefuse.ai/CodeFuse-Query/,然后我尝试编写了 python 的查询脚本:query_allgraph.gdl,多次尝试并未成功(仿照4_godelscript_language.md文件中,JavaScript 的AST Print章节,已经可以成功解析并且打印 AST)。请问有什么方式可以便捷实现我的需求吗?或者有没有一些更详细的 API 文档可以参考一下?
(github不允许上传 gdl以及 db 格式的文件,我加了 txt 的后缀)
coref_python_src.db.txt
query_allgraph.gdl.txt
The text was updated successfully, but these errors were encountered: