49

GitHub趋势榜第二名:解析、对比不同编程语言的Semantic库

 4 years ago
source link: https://www.tuicool.com/articles/ram6Bn2
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

刚刚,GitHub Semantic Code 团队宣布开源 semantic 库。这是一个 Haskell 库,用于解析、对比、解释多种编程语言,目前该库支持 JavaScript、TypeScript、Python、Ruby 和 Go 等多种语言。

3QVFbqn.jpg!web

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 符号表等。

<code>Usage: semantic parse ([--sexpression] | [--json] | [--json-graph] | [--symbols]</code><code>                      | [--dot] | [--show] | [--quiet]) [FILES...]</code><code>  Generate parse trees for path(s)</code><code>Available options:</code><code>  --sexpression            Output s-expression parse trees (default)</code><code>  --json                   Output JSON parse trees</code><code>  --json-graph             Output JSON adjacency list</code><code>  --symbols                Output JSON symbol list</code><code>  --dot                    Output DOT graph parse trees</code><code>  --show                   Output using the Show instance (debug only, format</code><code>                           subject to change without notice)</code><code>  --quiet                  Don't produce output, but show timing stats</code>

对比

该库通过语义对比计算路径之间的变化,支持生成 s-expression diff tree、JSON diff tree 等。

<code>Usage: semantic diff ([--sexpression] | [--json] | [--json-graph] | [--toc] |</code><code>                     [--dot] | [--show]) [FILE_A] [FILE_B]</code><code>  Compute changes between paths</code><code>Available options:</code><code>  --sexpression            Output s-expression diff tree (default)</code><code>  --json                   Output JSON diff trees</code><code>  --json-graph             Output JSON diff trees</code><code>  --toc                    Output JSON table of contents diff summary</code><code>  --dot                    Output the diff as a DOT graph</code><code>  --show                   Output using the Show instance (debug only, format</code><code>                           subject to change without notice)</code>

Graph

该库可以通过语义图计算目录或顶级入口点模块的图,支持 import graph、call graph、JSON graph 等。

<code>Usage: semantic graph ([--imports] | [--calls]) [--packages] ([--dot] | [--json]</code><code>                      | [--show]) ([--root DIR] [--exclude-dir DIR]</code><code>                      DIR:LANGUAGE | FILE | --language ARG (FILES... | --stdin))</code><code>  Compute a graph for a directory or from a top-level entry point module</code><code>Available options:</code><code>  --imports                Compute an import graph (default)</code><code>  --calls                  Compute a call graph</code><code>  --packages               Include a vertex for the package, with edges from it</code><code>                           to each module</code><code>  --dot                    Output in DOT graph format (default)</code><code>  --json                   Output JSON graph</code><code>  --show                   Output using the Show instance (debug only, format</code><code>                           subject to change without notice)</code><code>  --root DIR               Root directory of project. Optional, defaults to</code><code>                           entry file/directory.</code><code>  --exclude-dir DIR        Exclude a directory (e.g. vendor)</code><code>  --language ARG           The language for the analysis.</code><code>  --stdin                  Read a list of newline-separated paths to analyze</code><code>                           from stdin.</code>

语言支持

semantic 库支持的语言很多,如下图所示:

AJ3Abur.jpg!web

技术和架构

从架构上来说,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 表示:「很期待看到该项目开源后的变化。」

V3EJZvv.jpg!web


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK