前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >GitHub趋势榜第二名:解析、对比不同编程语言的Semantic库

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

作者头像
机器之心
发布2019-06-03 18:15:42
8930
发布2019-06-03 18:15:42
举报
文章被收录于专栏:机器之心机器之心

机器之心报道

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

代码语言:javascript
复制
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 等。

代码语言:javascript
复制
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 等。

代码语言:javascript
复制
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 表示:「很期待看到该项目开源后的变化。」

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2019-06-01,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 机器之心 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
命令行工具
腾讯云命令行工具 TCCLI 是管理腾讯云资源的统一工具。使用腾讯云命令行工具,您可以快速调用腾讯云 API 来管理您的腾讯云资源。此外,您还可以基于腾讯云的命令行工具来做自动化和脚本处理,以更多样的方式进行组合和重用。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档