/
parser.go
168 lines (151 loc) · 4.52 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package parser
import (
"fmt"
"go/ast"
"go/build"
"go/parser"
"go/token"
"io"
"io/fs"
"path"
"path/filepath"
"unicode"
"github.com/livebud/bud/package/gomod"
)
// New Go parser.
func New(fsys fs.FS, module *gomod.Module) *Parser {
return &Parser{
fsys: fsys,
module: module,
}
}
// Parser for parsing Go code.
type Parser struct {
fsys fs.FS
module *gomod.Module
}
// Parse a dir containing Go files.
func (p *Parser) Parse(dir string) (*Package, error) {
imported, err := p.Import(dir)
if err != nil {
return nil, err
}
parsedPackage := &ast.Package{
Name: imported.Name,
Files: make(map[string]*ast.File),
}
fset := token.NewFileSet()
// Parse each valid Go file
for _, filename := range imported.GoFiles {
filename = path.Join(dir, filename)
code, err := fs.ReadFile(p.fsys, filename)
if err != nil {
return nil, err
}
parsedFile, err := parser.ParseFile(fset, filename, code, parser.DeclarationErrors)
if err != nil {
return nil, err
}
parsedPackage.Files[filename] = parsedFile
}
pkg := newPackage(dir, p, p.module, parsedPackage)
return pkg, nil
}
// Import the package, taking into account build tags and file name conventions
func (p *Parser) Import(dir string) (*build.Package, error) {
return Import(p.fsys, dir)
}
func Import(fsys fs.FS, dir string) (*build.Package, error) {
// TODO: figure out how to set the import path correctly to have better error
// messages
imported, err := buildContext(fsys).Import(".", dir, build.ImportMode(0))
if err != nil {
return nil, fmt.Errorf("parser: unable to import package %q. %w", dir, err)
}
return imported, nil
}
// Check is a convenience function for tests to check Go code for syntax errors.
func Check(code []byte) error {
fset := token.NewFileSet()
_, err := parser.ParseFile(fset, "", code, parser.DeclarationErrors)
if err != nil {
return fmt.Errorf("parser: unable to parse code the following:\n\n%s\n%w", string(code), err)
}
return nil
}
// A Context specifies the supporting context for a build. We mostly use the
// default context, but we want to override some of the values. This should be
// kept in sync with the keys in *build.Context
func buildContext(fsys fs.FS) *build.Context {
context := build.Default
return &build.Context{
GOARCH: context.GOARCH,
GOOS: context.GOOS,
GOROOT: context.GOROOT,
GOPATH: context.GOPATH,
Dir: context.Dir,
CgoEnabled: context.CgoEnabled,
UseAllFiles: context.UseAllFiles,
Compiler: context.Compiler,
BuildTags: context.BuildTags,
ToolTags: context.ToolTags,
ReleaseTags: context.ReleaseTags,
InstallSuffix: context.InstallSuffix,
// TODO: I'm not sure how to turn this into a call that uses the virtual
// filesystem. It does rely on some os-specific filepath calls, but hasn't
// seemed to affect the output.
HasSubdir: context.HasSubdir,
// // JoinPath joins the sequence of path fragments into a single path.
// // If JoinPath is nil, Import uses filepath.Join.
JoinPath: func(elem ...string) string {
return path.Join(elem...)
},
// // SplitPathList splits the path list into a slice of individual paths.
// // If SplitPathList is nil, Import uses filepath.SplitList.
SplitPathList: filepath.SplitList,
// // IsAbsPath reports whether path is an absolute path.
// // If IsAbsPath is nil, Import uses filepath.IsAbs.
IsAbsPath: func(elem string) bool {
return path.IsAbs(elem)
},
// IsDir reports whether the path names a directory.
// If IsDir is nil, Import calls os.Stat and uses the result's IsDir method.
IsDir: func(path string) bool {
fi, err := fs.Stat(fsys, path)
if err != nil {
// Error handling follows what build.Default does
return false
}
return fi.IsDir()
},
// ReadDir returns a slice of fs.FileInfo, sorted by Name,
// describing the content of the named directory.
// If ReadDir is nil, Import uses ioutil.ReadDir.
ReadDir: func(dir string) (fis []fs.FileInfo, err error) {
des, err := fs.ReadDir(fsys, dir)
if err != nil {
return nil, err
}
for _, de := range des {
fi, err := de.Info()
if err != nil {
return nil, err
}
fis = append(fis, fi)
}
return fis, nil
},
// OpenFile opens a file (not a directory) for reading.
// If OpenFile is nil, Import uses os.Open.
OpenFile: func(path string) (io.ReadCloser, error) {
file, err := fsys.Open(path)
if err != nil {
return nil, err
}
return file, nil
},
}
}
func isPrivate(name string) bool {
return unicode.IsLower(rune(name[0]))
}