-
Notifications
You must be signed in to change notification settings - Fork 17
/
parse.go
83 lines (67 loc) · 2.29 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"
"os"
"path/filepath"
"golang.org/x/tools/go/loader" //nolint:staticcheck
)
// 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 := os.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) (*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)
}
var conf = loader.Config{
ParserMode: parser.AllErrors | parser.ParseComments,
}
if buildPkg.ImportPath != "." {
conf.Import(buildPkg.ImportPath)
} else {
// This is most definitely the case for files inside a "testdata" package
conf.CreateFromFilenames(dir, fileAbs)
}
conf.AllowErrors = true
prog, err := conf.Load()
if err != nil {
return nil, nil, nil, nil, fmt.Errorf("Could not load package of file %q: %v", file, err)
}
pkgInfo := prog.InitialPackages()[0]
var src *ast.File
for _, f := range pkgInfo.Files {
if prog.Fset.Position(f.Pos()).Filename == fileAbs {
src = f
break
}
}
return src, prog.Fset, pkgInfo.Pkg, &pkgInfo.Info, nil
}