专栏名称: 开源前线
推荐热门开源软件,播报最新开源项目和开源资讯!
目录
相关文章推荐
最爱大北京  ·  多地气温超25℃!气温火箭式飙升!迎春、红梅 ... ·  21 小时前  
最爱大北京  ·  北京一男子独自登山失联8天!遗体被找到→ ·  21 小时前  
北京本地宝  ·  北京领证结婚指南和免费福利! ·  2 天前  
最爱大北京  ·  知名专家修波,突遇交通意外去世 ·  昨天  
51好读  ›  专栏  ›  开源前线

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

开源前线  · 公众号  ·  · 2021-05-17 10:25

正文


开源最前线(ID:OpenSourceTop) 猿妹编译

编译自:https://github.com/github/semantic


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


平常解析源代码也算是程序员的家常便饭了,如果有个工具帮你一把那是最好不过的了, 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


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


def Foo(x):
    return x
print Foo("hi")


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


$ semantic parse test.A.py
(Statements
  (Annotation
    (Function
      (Identifier)
      (Identifier)
      (Return
        (Identifier))
)
    (Empty))
  (Call
    (Identifier)
    (Call
      (Identifier)
      (TextElement)
      (Empty))

    (Empty))
)


默认的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)



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






请到「今天看啥」查看全文