当前位置: 首页 > 科技观察

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

时间:2023-03-14 21:46:52 科技观察

GitHub上还有很多实用的工具,不,我再找一个支持多语言的堪称神器的命令行工具———Semantic,Semantic是一个命令行工具,用于解析、分析和比较多种语言的源代码,是一个Haskell库。解析源代码通常是程序员的家常便饭。如果有一个工具可以帮助你,那将是最好的。Semantic的使用方法,下面我讲的时候你就明白了:首先,运行semantic--help获取最新的完整选项列表:parse用法:semanticparse([--sexpression]|[--json]|[--json-graph]|[--symbols]|[--dot]|[--show]|[--quiet])[FILES...]Generateparsetreesforpath(s)可用选项:--sexpressionOutputs-expressionparsetrees(默认)--jsonOutputJSONparsetrees--json-graphOutputJSONadjacencylist--symbolsOutputJSONsymbollist--dotOutputDOTgraphparsetrees--showOutputusingtheformewinstance,angatjebugonout-quiet不产生输出,但showtimingstatsSemantic使用树状图生成解析树。现在我们拿一个简单的程序来解析一下,你会看得更清楚。打开test.A.py文件并粘贴如下:defFoo(x):returnxprintFoo("hi")现在,让我们生成一个抽象语法树(AST)$semanticparsetest.A.py(Statements(Annotation(Function(Identifier))(Identifier)(Return(Identifier)))(Empty))(Call(Identifier)(Call(Identifier)(TextElement)(Empty))(Empty)))默认的s表达式输出是一种很好的格式,可以快速可视化代码结构。我们可以看到有一个声明的函数,然后有一个调用表达式,嵌套在另一个调用表达式中,调用函数调用print和Foo。您还可以使用其他输出格式。DIFF(比较)用法:semanticdiff([--sexpression]|[--json]|[--json-graph]|[--toc]|[--dot]|[--show])[FILE_A][FILE_B]ComputechangesbetweenpathsAvailableoptions:--sexpressionOutputs-expressiondifftree(default)--jsonOutputJSONdifftrees--json-graphOutputJSONdifftrees--tocOutputJSONtableofcontentsdiffsummary--dotOutputthediffasaDOTgraph--showOutputusingtheShowinstance(debugonly,formatsubjecttochangewithoutnotice)图表(debugonly,formatsubjecttochangewithoutnotice)图表(se-man)[se-man][se-man]-calls])[--packages]([--dot]|[--json]|[--show])([--rootDIR][--exclude-dirDIR]DIR:LANGUAGE|FILE|--languageARG(FILES...|--stdin))Computeagraphforadirectoryorfromatop-levelentrypointmoduleAvailableoptions:--importsComputeanimportgraph(default)--callsComputeacallgraph--packagesIncludeavertexforthepackage,withedgesfromittoeachmodule--dotOutputinDOTgraphformat(default)--jsonOutputJSONgraph--showOutputusingtheShoworyinstance(debuggoned-subjecttoroot-projectwithoutRR.format格式)可选,默认为入口文件/目录。--exclude-dirDIRE排除目录(例如供应商)--languageARGThelanguagefortheanalysis.--stdinReadalistofnewline-separatedpathstoanalyzefromstdin。语言支持多语言支持是Semantic的一大优势,目前支持Ruby、JavaScript、typescript、Python、Go、PHP、Java等主流编程语言Semantic支持GHC8.6.4和Cabal2.4对Semantic的最低要求。推荐使用ghcup沙箱GHC版本。通过操作系统的包管理器安装的GHC包可能无法安装静态链接版本的GHC启动库gitclonegit@github.com:github/semantic.gitcdsemanticscript/bootstrapcabalnew-buildcabalnew-testcabalnew-runsemantic----求助创作者使用Nix风格的cabal本地构建进行开发。要快速入门,您可以按照上述步骤操作。目前,Semantic在GitHub上获得了8000个Stars和450个Fork。如果你有兴趣,可以在GitHub上查看更多细节(GitHub地址:https://github.com/github/semantic)。