我在GitHub上又找到一个堪称神器的命令行工具

开发 开发工具
GitHub上的实用工具还是挺多的,这不,又让我找到了一个堪称神器的支持多语言的命令行工具——Semantic,Semantic是一个解析,分析和比较多种语言源代码的命令行工具,也是一个Haskell库。

GitHub上的实用工具还是挺多的,这不,又让我找到了一个堪称神器的支持多语言的命令行工具——Semantic,Semantic是一个解析,分析和比较多种语言源代码的命令行工具,也是一个Haskell库。

[[399817]]

平常解析源代码也算是程序员的家常便饭了,如果有个工具帮你一把那是最好不过的了,Semantic具体如何使用,猿妹下面一说你就明白了:

首先呢,运行semantic --help获取最新的完整选项列表:

解析

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 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.

Semantic使用树形图来生成解析树,现在我们拿一个简单的程序来解析你会看的更明了,打开test.A.py文件,粘贴如下:

def Foo(x): 
    return x 
print Foo("hi") 
  • 1.
  • 2.
  • 3.

现在,让我们生成一个抽象语法树(AST)

$ semantic parse test.A.py 
(Statements 
  (Annotation 
    (Function 
      (Identifier) 
      (Identifier) 
      (Return 
        (Identifier))) 
    (Empty)) 
  (Call 
    (Identifier) 
    (Call 
      (Identifier) 
      (TextElement) 
      (Empty)) 
    (Empty))) 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.

默认的s-expression输出是一种很好的格式,可以快速可视化代码结构。我们可以看到有一个声明的函数,然后有一个调用表达式,嵌套在另一个调用表达式中,它与函数调用print和Foo。你还可以使用其他的输出格式。

DIFF(比较)

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) 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.

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. 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.

语言支持

多语言支持是Semantic的一大优势,目前支持Ruby、JavaScript、typescript、Python、Go、PHP、Java等主流编程语言都支持

Semantic最低要求GHC 8.6.4和Cabal 2.4,建议使用ghcup沙箱GHC版本,为通过操作系统的软件包管理器安装的GHC软件包可能无法安装静态链接版本的GHC启动库。

git clone git@github.com:github/semantic.git 
cd semantic 
script/bootstrap 
cabal new-build 
cabal new-test 
cabal new-run semantic -- --help 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.

创建者使用cabal的Nix风格的本地版本进行开发。要快速入门,可以按照以上操作。

目前,semantic已经在GitHub上获得 8000 个Star,450 个Fork,感兴趣的可以到GitHub上查阅更多详情

(GitHub地址:https://github.com/github/semantic)。

 

责任编辑:赵宁宁 来源: 开源最前线
相关推荐

2020-12-08 10:33:56

DDoS攻击开源安全安全工具

2021-02-04 11:46:49

GithubSQL工具Franchise

2021-02-04 11:11:08

开发技能工具

2022-01-26 18:59:08

Python工具

2011-06-17 16:49:05

Cocoa苹果

2021-06-03 10:34:43

GitHub停车系统项目

2022-01-11 09:05:07

工具Python 命令行

2019-06-10 15:00:27

node命令行前端

2016-08-10 12:41:00

Linux工具bcShell

2023-03-08 15:38:56

Linux命令dict

2019-09-09 15:43:29

UnixLinux命令行

2019-02-27 09:24:48

命令行文件Linux

2012-07-11 13:35:25

UnixLinux

2014-06-17 09:49:07

Ngxtop实时监控Nginx

2022-02-17 18:21:47

工具HTTPie客户端

2019-05-30 10:40:04

ddgrLinuxDuckDuckGo

2020-12-08 08:46:07

GoJava工具

2017-05-27 14:45:04

Linux命令进程

2015-07-13 11:05:31

Linuxlolcat

2016-09-23 20:16:23

TaskwarriorLinux命令行工具
点赞
收藏

51CTO技术栈公众号