Go之底层利器-AST遍历

原文出处:dpjeep.com/gozhi-di-ce…node

背景

最近须要基于AST来作一些自动化工具,遂也须要针对这个神兵利器进行一下了解研究。本篇文章也准备只是简单的讲解一下如下两个部分:git

  • 经过AST解析一个Go程序
  • 而后经过Go的标准库来对这个AST进行分析

AST

什么是AST,其实就是抽象语法树Abstract Syntax Tree的简称。它以树状的形式表现编程语言的语法结构,树上的每一个节点都表示源代码中的一种结构。之因此说语法是“抽象”的,是由于这里的语法并不会表示出真实语法中出现的每一个细节。github

主菜

开胃提示语

如下内容有点长,要不先去买点瓜子,边磕边看?golang

编译过程

要讲解相关AST部分,先简单说一下咱们知道的编译过程:编程

  • 词法分析
  • 语法分析
  • 语义分析和中间代码产生
  • 编译器优化
  • 目标代码生成 而咱们如今要利用的正是Google所为咱们准备的一套很是友好的词法分析和语法分析工具链,有了它咱们就能够造车了。

代码示例

在Golang官方文档中已经提供实例,本处就不把文档源码贴出来了,只放出部分用例json

// This example shows what an AST looks like when printed for debugging.
func ExamplePrint() {
	// src is the input for which we want to print the AST.
	src := ` package main func main() { println("Hello, World!") } `

	// Create the AST by parsing src.
	fset := token.NewFileSet() // positions are relative to fset
	f, err := parser.ParseFile(fset, "", src, 0)
	if err != nil {
		panic(err)
	}

	// Print the AST.
	ast.Print(fset, f)

	// Output:
	// 0 *ast.File {
	// 1 . Package: 2:1
	// 2 . Name: *ast.Ident {
	// 3 . . NamePos: 2:9
	// 4 . . Name: "main"
	// 5 . }
	// 6 . Decls: []ast.Decl (len = 1) {
	// 7 . . 0: *ast.FuncDecl {
	// 8 . . . Name: *ast.Ident {
	// 9 . . . . NamePos: 3:6
	// 10 . . . . Name: "main"
	// 11 . . . . Obj: *ast.Object {
	// 12 . . . . . Kind: func
	// 13 . . . . . Name: "main"
	// 14 . . . . . Decl: *(obj @ 7)
	// 15 . . . . }
	// 16 . . . }
	// 17 . . . Type: *ast.FuncType {
	// 18 . . . . Func: 3:1
	// 19 . . . . Params: *ast.FieldList {
	// 20 . . . . . Opening: 3:10
	// 21 . . . . . Closing: 3:11
	// 22 . . . . }
	// 23 . . . }
	// 24 . . . Body: *ast.BlockStmt {
	// 25 . . . . Lbrace: 3:13
	// 26 . . . . List: []ast.Stmt (len = 1) {
	// 27 . . . . . 0: *ast.ExprStmt {
	// 28 . . . . . . X: *ast.CallExpr {
	// 29 . . . . . . . Fun: *ast.Ident {
	// 30 . . . . . . . . NamePos: 4:2
	// 31 . . . . . . . . Name: "println"
	// 32 . . . . . . . }
	// 33 . . . . . . . Lparen: 4:9
	// 34 . . . . . . . Args: []ast.Expr (len = 1) {
	// 35 . . . . . . . . 0: *ast.BasicLit {
	// 36 . . . . . . . . . ValuePos: 4:10
	// 37 . . . . . . . . . Kind: STRING
	// 38 . . . . . . . . . Value: "\"Hello, World!\""
	// 39 . . . . . . . . }
	// 40 . . . . . . . }
	// 41 . . . . . . . Ellipsis: -
	// 42 . . . . . . . Rparen: 4:25
	// 43 . . . . . . }
	// 44 . . . . . }
	// 45 . . . . }
	// 46 . . . . Rbrace: 5:1
	// 47 . . . }
	// 48 . . }
	// 49 . }
	// 50 . Scope: *ast.Scope {
	// 51 . . Objects: map[string]*ast.Object (len = 1) {
	// 52 . . . "main": *(obj @ 11)
	// 53 . . }
	// 54 . }
	// 55 . Unresolved: []*ast.Ident (len = 1) {
	// 56 . . 0: *(obj @ 29)
	// 57 . }
	// 58 }
}
复制代码

一看到上面的打印是否是有点头晕?哈哈,我也是。没想到一个简单的hello world就能打印出这么多东西,里面其实隐藏了不少有趣的元素,好比函数、变量、评论、imports等等,那咱们要如何才能从中提取出咱们想要的数据呢?为达这个目的,咱们须要用到Golang所为咱们提供的go/parser包:数组

// Create the AST by parsing src.
fset := token.NewFileSet() // positions are relative to fset
f, err := parser.ParseFile(fset, "", src, 0)
if err != nil {
    panic(err)
}
复制代码

第一行引用了go/token包,用来建立一个新的用于解析的源文件FileSet。
而后咱们使用的parser.ParseFile返回的是一个ast.File类型结构体(原始文档),而后回头查看上面的日志打印,每一个字段元素的含义你也许已经霍然开朗了,结构体定义以下:编程语言

type File struct {
        Doc        *CommentGroup   // associated documentation; or nil
        Package    token.Pos       // position of "package" keyword
        Name       *Ident          // package name
        Decls      []Decl          // top-level declarations; or nil
        Scope      *Scope          // package scope (this file only)
        Imports    []*ImportSpec   // imports in this file
        Unresolved []*Ident        // unresolved identifiers in this file
        Comments   []*CommentGroup // list of all comments in the source file
}
复制代码

好了,目前咱们就是要利用这个结构体作一下小的代码示例,咱们就来解析下面的这个文件ast_traversal.goide

package ast_demo

import "fmt"

type Example1 struct {
	// Foo Comments
	Foo string `json:"foo"`
}

type Example2 struct {
	// Aoo Comments
	Aoo int `json:"aoo"`
}

// print Hello World
func PrintHello(){
	fmt.Println("Hello World")
}
复制代码

咱们已经能够利用上面说到的ast.File结构体去解析这个文件了,好比利用f.Imports列出所引用的包:函数

for _, i := range f.Imports {
	t.Logf("import: %s", i.Path.Value)
}
复制代码

一样的,咱们能够过滤出其中的评论、函数等,如:

for _, i := range f.Comments {
	t.Logf("comment: %s", i.Text())
}

for _, i := range f.Decls {
	fn, ok := i.(*ast.FuncDecl)
	if !ok {
		continue
	}
	t.Logf("function: %s", fn.Name.Name)
}
复制代码

上面,获取comment的方式和import相似,直接就能使用,而对于函数,则采用了*ast.FucDecl的方式,此时,移步至本文最上层,查看AST树的打印,你就发现了Decls: []ast.Decl是以数组形式存放,且其中存放了多种类型的node,此处经过强制类型转换的方式,检测某个类型是否存在,存在的话则按照该类型中的结构进行打印。上面的方式已能知足咱们的基本需求,针对某种类型能够进行具体解析。
可是,凡是仍是有个可是,哈哈,经过上面的方式来一个一个解析是否是有点麻烦?没事,谷歌老爹经过go/ast包给咱们又提供了一个方便快捷的方法:

// Inspect traverses an AST in depth-first order: It starts by calling
// f(node); node must not be nil. If f returns true, Inspect invokes f
// recursively for each of the non-nil children of node, followed by a
// call of f(nil).
//
func Inspect(node Node, f func(Node) bool) {
	Walk(inspector(f), node)
}
复制代码

这个方法的大概用法就是:经过深度优先的方式,把整个传递进去的AST进行了解析,它经过调用f(node) 开始;节点不能为零。若是 f 返回 true,Inspect 会为节点的每一个非零子节点递归调用f,而后调用 f(nil)。相关用例以下:

ast.Inspect(f, func(n ast.Node) bool {
	// Find Return Statements
	ret, ok := n.(*ast.ReturnStmt)
	if ok {
		t.Logf("return statement found on line %d:\n\t", fset.Position(ret.Pos()).Line)
		printer.Fprint(os.Stdout, fset, ret)
		return true
	}

	// Find Functions
	fn, ok := n.(*ast.FuncDecl)
	if ok {
		var exported string
		if fn.Name.IsExported() {
			exported = "exported "
		}
		t.Logf("%sfunction declaration found on line %d: %s", exported, fset.Position(fn.Pos()).Line, fn.Name.Name)
		return true
	}

	return true
})
复制代码

后记

至此,你手中的瓜子可能已经嗑完了,AST用处颇多,上面咱们所讲到的也只是AST其中的一小部分,不少底层相关分析工具都是基于它来进行语法分析进行,工具在手,而后要制造什么艺术品就得看各位手艺人了。后续会陆续更新部分基于Go AST的小工具出来,但愿本身能早日实现吧,哈哈😆。
如下为上文中所用到的测试用例及使用AST针对结构体进行字段解析的源码,我已提交至Github,若有兴趣能够去看看

相关文章
相关标签/搜索