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, format
subject 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 paths
Available 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, format
subject 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 module
Available options:
--imports Compute an import graph (default)
--calls Compute a call graph
--packages Include a vertex for the package, with edges from it
to each module
--dot Output in DOT graph format (default)
--json Output JSON graph
--show Output using the Show instance (debug only, format
subject to change without notice)
--root DIR Root directory of project. Optional, defaults to
entry 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 analyze
from 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 团结就是力量 合作才能共赢 7903987
- 2 正部级蓝天立被查 5天前曾公开露面 7808300
- 3 女演员戴230万耳环引质疑 官方介入 7712353
- 4 消费“挖潜” 发展“提速” 7615824
- 5 单依纯《歌手2025》首期第一名 7520995
- 6 新冠感染又抬头?专家回应 7428155
- 7 俄乌直接谈判结束 乌称“毫无成果” 7331332
- 8 《歌手2025》首期排名 7234084
- 9 林志炫穿得像金刚鹦鹉 7136108
- 10 龙门石窟千年菩提已开出满树繁花 7039504