剛剛,GitHub Semantic Code 團隊宣佈開源 semantic 庫。這是一個 Haskell 庫,用於解析、對比、解釋多種程式語言,目前該庫支援 JavaScript、TypeScript、Python、Ruby 和 Go 等多種語言。

GitHub趨勢榜第二名:解析、對比不同程式語言的Semantic庫

semantic 庫地址:

https://

github。com/github/seman

tic

今天,該庫獲得了 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 庫支援的語言很多,如下圖所示:

GitHub趨勢榜第二名:解析、對比不同程式語言的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 表示:「很期待看到該專案開源後的變化。」

GitHub趨勢榜第二名:解析、對比不同程式語言的Semantic庫

GitHub趨勢榜第二名:解析、對比不同程式語言的Semantic庫