如何解析方法声明?

时间:2015-01-31 00:51:48

标签: parsing go

我正在尝试解析方法声明。基本上我需要获取接收器基类型(type hello)的语法节点和返回类型(notypeerror)。 ast包似乎很简单但由于某种原因我没有得到我需要的数据(即字段报告为零)。

唯一有用的数据似乎仅在Object -> Decl字段interface{}中提供,所以我认为我不能将其序列化。

任何帮助将不胜感激。代码如下:

package main

import (
    "fmt"
    "go/ast"
    "go/parser"
    "go/token"
)

func main() {
    // src is the input for which we want to inspect the AST.
    src := `
package mypack
// type hello is a cool type
type hello string

// type notype is not that cool
type notype int

// func printme is like nothing else.
func (x *hello)printme(s string)(notype, error){
    return 0, nil
}
`
    // Create the AST by parsing src.
    fset := token.NewFileSet() // positions are relative to fset
    f, err := parser.ParseFile(fset, "src.go", src, 0)
    if err != nil {
        panic(err)
    }

    // Inspect the AST and find our function
    var mf ast.FuncDecl
    ast.Inspect(f, func(n ast.Node) bool {
        switch x := n.(type) {
        case *ast.FuncDecl:
            mf = *x
        }
        return true
    })

    if mf.Recv != nil {
        fmt.Printf("\n receivers:")
        for _, v := range mf.Recv.List {
            fmt.Printf(",tag %v", v.Tag)
            for _, xv := range v.Names {
                fmt.Printf("name %v, decl %v, data %v, type %v",
                    xv.Name, xv.Obj.Decl, xv.Obj.Data, xv.Obj.Type)
            }
        }
    }
}

Playground

1 个答案:

答案 0 :(得分:4)

要获取您需要查看Type属性的类型,该属性可以是ast.StarExprast.Ident

以下是this

package main

import (
    "fmt"
    "go/ast"
    "go/parser"
    "go/token"
)

func main() {
    // src is the input for which we want to inspect the AST.
    src := `
package mypack
// type hello is a cool type
type hello string

// type notype is not that cool
type notype int

// printme is like nothing else.
func (x *hello)printme(s string)(notype, error){
    return 0, nil
}
`
    // Create the AST by parsing src.
    fset := token.NewFileSet() // positions are relative to fset
    f, err := parser.ParseFile(fset, "src.go", src, 0)
    if err != nil {
        panic(err)
    }

    // Inspect the AST and find our function
    var mf ast.FuncDecl
    ast.Inspect(f, func(n ast.Node) bool {
        switch x := n.(type) {
        case *ast.FuncDecl:
            mf = *x
        }
        return true
    })

    if mf.Recv != nil {
        for _, v := range mf.Recv.List {
            fmt.Print("recv type : ")
            switch xv := v.Type.(type) {
            case *ast.StarExpr:
                if si, ok := xv.X.(*ast.Ident); ok {
                    fmt.Println(si.Name)
                }
            case *ast.Ident:
                fmt.Println(xv.Name)
            }
        }
    }
}