forked from zimmski/go-mutesting
-
Notifications
You must be signed in to change notification settings - Fork 2
/
parse.go
83 lines (68 loc) · 2.48 KB
/
parse.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package mutesting
import (
"fmt"
"go/ast"
"go/build"
"go/parser"
"go/token"
"go/types"
"io/ioutil"
"path/filepath"
"golang.org/x/tools/go/packages"
)
// ParseFile parses the content of the given file and returns the corresponding ast.File node and its file set for positional information.
// If a fatal error is encountered the error return argument is not nil.
func ParseFile(file string) (*ast.File, *token.FileSet, error) {
data, err := ioutil.ReadFile(file)
if err != nil {
return nil, nil, err
}
return ParseSource(data)
}
// ParseSource parses the given source and returns the corresponding ast.File node and its file set for positional information.
// If a fatal error is encountered the error return argument is not nil.
func ParseSource(data interface{}) (*ast.File, *token.FileSet, error) {
fset := token.NewFileSet()
src, err := parser.ParseFile(fset, "", data, parser.ParseComments|parser.AllErrors)
if err != nil {
return nil, nil, err
}
return src, fset, err
}
// ParseAndTypeCheckFile parses and type-checks the given file, and returns everything interesting about the file.
// If a fatal error is encountered the error return argument is not nil.
func ParseAndTypeCheckFile(file string, flags ...string) (*ast.File, *token.FileSet, *types.Package, *types.Info, error) {
fileAbs, err := filepath.Abs(file)
if err != nil {
return nil, nil, nil, nil, fmt.Errorf("could not absolute the file path of %q: %v", file, err)
}
dir := filepath.Dir(fileAbs)
buildPkg, err := build.ImportDir(dir, build.FindOnly)
if err != nil {
return nil, nil, nil, nil, fmt.Errorf("could not create build package of %q: %v", file, err)
}
pkgPath := buildPkg.ImportPath
if buildPkg.ImportPath == "." {
pkgPath = dir
}
prog, err := packages.Load(&packages.Config{
ParseFile: func(fset *token.FileSet, filename string, src []byte) (*ast.File, error) {
return parser.ParseFile(fset, filename, src, parser.ParseComments|parser.AllErrors)
},
BuildFlags: flags,
Mode: packages.NeedTypes | packages.NeedSyntax | packages.NeedDeps | packages.NeedName | packages.NeedImports | packages.NeedTypesInfo | packages.NeedFiles,
}, pkgPath)
if err != nil {
fmt.Println(err)
return nil, nil, nil, nil, fmt.Errorf("could not load package of file %q: %v", file, err)
}
pkgInfo := prog[0]
var src *ast.File
for _, f := range pkgInfo.Syntax {
if pkgInfo.Fset.Position(f.Pos()).Filename == fileAbs {
src = f
break
}
}
return src, pkgInfo.Fset, pkgInfo.Types, pkgInfo.TypesInfo, nil
}