才刚,GitHub Semantic Code 项目组正式宣布开放源码 semantic 库。这是两个 Haskell 库,用作导出、对照、说明多种相同C词汇,现阶段该库全力支持 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 Dont 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 表示:「很期待看到该项目开放源码后的变化。」