GitHub趋势榜第二名:解析、对比不同编程语言的Semantic库
机器之心报道
参与:路、Shooting
刚刚,GitHub Semantic Code 团队宣布开源 semantic 库。这是一个 Haskell 库,用于解析、对比、解释多种编程语言,目前该库支持 JavaScript、TypeScript、Python、Ruby 和 Go 等多种语言。

semantic 库地址:https://github.com/github/semantic
今天,该库获得了 608 个 star,位列 GitHub 趋势榜第二名。
semantic 库简介
semantic 是一个用于解析、分析和对比不同语言源代码的 Haskell 库和命令行工具,目前支持 JavaScript、TypeScript、Python、Ruby 和 Go 语言。根据 GitHub Semantic Code 团队成员 patrick thomson 的 twitter 评论,未来该库可能会增加对 C 语言的支持。
该库由 GitHub Semantic Code team 完成。这支团队主要负责解决分析、解释 GitHub 上公开代码的任务,使用 Haskell 语言。
功能
semantic 库可用于解析、对比、解释多种编程语言的源代码。
解析
该库通过语义解析为路径生成解析树,支持生成 s-expression 解析树、JSON 解析树、JSON 邻接表、JSON 符号表等。
Usage: semantic parse ([--sexpression] | [--json] | [--json-graph] | [--symbols]| [--dot] | [--show] | [--quiet]) [FILES...]Generate parse trees for path(s)Available options:--sexpression Output s-expression parse trees (default)--json Output JSON parse trees--json-graph Output JSON adjacency list--symbols Output JSON symbol list--dot Output DOT graph parse trees--show Output using the Show instance (debug only, formatsubject to change without notice)--quiet Don't produce output, but show timing stats
对比
该库通过语义对比计算路径之间的变化,支持生成 s-expression diff tree、JSON diff tree 等。
Usage: semantic diff ([--sexpression] | [--json] | [--json-graph] | [--toc] |[--dot] | [--show]) [FILE_A] [FILE_B]Compute changes between pathsAvailable options:--sexpression Output s-expression diff tree (default)--json Output JSON diff trees--json-graph Output JSON diff trees--toc Output JSON table of contents diff summary--dot Output the diff as a DOT graph--show Output using the Show instance (debug only, formatsubject to change without notice)
Graph
该库可以通过语义图计算目录或顶级入口点模块的图,支持 import graph、call graph、JSON graph 等。
Usage: semantic graph ([--imports] | [--calls]) [--packages] ([--dot] | [--json]| [--show]) ([--root DIR] [--exclude-dir DIR]DIR:LANGUAGE | FILE | --language ARG (FILES... | --stdin))Compute a graph for a directory or from a top-level entry point moduleAvailable options:--imports Compute an import graph (default)--calls Compute a call graph--packages Include a vertex for the package, with edges from itto each module--dot Output in DOT graph format (default)--json Output JSON graph--show Output using the Show instance (debug only, formatsubject to change without notice)--root DIR Root directory of project. Optional, defaults toentry file/directory.--exclude-dir DIR Exclude a directory (e.g. vendor)--language ARG The language for the analysis.--stdin Read a list of newline-separated paths to analyzefrom stdin.
语言支持
semantic 库支持的语言很多,如下图所示:

技术和架构
从架构上来说,semantic 库可以:
读 blob;
用 tree-sitter 生成这些 blob 的解析树,tree-sitter 是一种用于编程工具的增量解析系统;
将这些树分配至语法的泛型表示中;
执行分析、计算差异或仅返回解析树;
以支持的格式渲染输出。
semantic 库利用了一系列有趣的算法和技术:
Myers 的算法 (SES),详见论文:An O(ND) Difference Algorithm and Its Variations;
RWS,详见论文:RWS-Diff: Flexible and Efficient Change Detection in Hierarchical Data;
open-union 包和 data types à la carte;
将 Abstracting Definitional Interpreters 的实现扩展,使其可以处理语法项的à la carte 表示。
该项目欢迎社区的参与,该项目参与者之一 patrick thomson 表示:「很期待看到该项目开源后的变化。」

本文为机器之心报道,转载请联系本公众号获得授权。
?------------------------------------------------
加入机器之心(全职记者 / 实习生):hr@jiqizhixin.com
投稿或寻求报道:content@jiqizhixin.com
广告 & 商务合作:bd@jiqizhixin.com
关注公众号:拾黑(shiheibook)了解更多
[广告]赞助链接:
四季很好,只要有你,文娱排行榜:https://www.yaopaiming.com/
让资讯触达的更精准有趣:https://www.0xu.cn/
关注网络尖刀微信公众号随时掌握互联网精彩
- 1 绘就共同富裕新画卷 7904133
- 2 芬兰总理正式向中韩日民众道歉 7808956
- 3 最能生娃省份排名来了 7713382
- 4 这一年 一个个暖心瞬间令人难忘 7617771
- 5 男子入伍火箭军后拒服兵役 官方通报 7521054
- 6 外交部:中方将再赴柬泰穿梭调停 7427042
- 7 30岁健美冠军去世前几天刚体检完 7328477
- 8 王毅同委内瑞拉外长希尔通电话 7234549
- 9 天津一医院多名女护士换衣时被偷拍 7136185
- 10 海南封关护港 海警全天候执法演练 7045666

机器之心
