-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparser.go
93 lines (80 loc) · 2.17 KB
/
parser.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
84
85
86
87
88
89
90
91
92
93
// Copyright 2020 murosan. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package gollect
import (
"fmt"
"go/parser"
"go/token"
"os"
"strconv"
"golang.org/x/tools/go/packages"
)
// ParseAll parses all ast files and sets to Program's map.
// This also parses external imported package's ast.
func ParseAll(
program *Program,
initialPackage string,
initialFilePaths []string,
) {
find := func(path string) []string {
if path == initialPackage {
return initialFilePaths
}
return FindFilePaths(path)
}
for paths := []string{initialPackage}; len(paths) > 0; paths = paths[1:] {
path := paths[0]
if _, ok := program.PackageSet().Get(path); ok {
continue
}
pkg := NewPackage(path)
program.PackageSet().Add(path, pkg)
fp := find(path)
ParseAst(program.FileSet(), pkg, fp...)
if len(pkg.files) == 0 {
panic(fmt.Sprintf("there are no files. paths=%v", fp))
}
paths = append(paths, NextPackagePaths(pkg)...)
}
}
// ParseAst parses ast and pushes to files slice.
func ParseAst(fset *token.FileSet, p *Package, paths ...string) {
for _, path := range paths {
f, err := parser.ParseFile(fset, path, nil, parser.ParseComments)
if err != nil {
panic(fmt.Errorf("parse file (path = %s): %w", path, err))
}
p.PushAstFile(f)
}
}
// FindFilePaths finds filepaths from package path.
// https://pkg.go.dev/golang.org/x/tools/go/packages?tab=doc#example-package
func FindFilePaths(path string) (paths []string) {
cfg := &packages.Config{Mode: packages.NeedFiles | packages.NeedSyntax}
pkgs, err := packages.Load(cfg, path)
if err != nil {
panic(fmt.Errorf("load: %w", err))
}
if packages.PrintErrors(pkgs) > 0 {
os.Exit(1)
}
for _, pkg := range pkgs {
paths = append(paths, pkg.GoFiles...)
}
return
}
// NextPackagePaths returns list of imported package paths.
func NextPackagePaths(p *Package) (paths []string) {
m := make(map[string]interface{})
for _, f := range p.files {
for _, i := range f.Imports {
p, _ := strconv.Unquote(i.Path.Value)
if _, ok := m[p]; !ok && !isBuiltinPackage(p) {
m[p] = struct{}{}
paths = append(paths, p)
}
}
}
return
}