-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse.go
More file actions
68 lines (52 loc) · 1.34 KB
/
parse.go
File metadata and controls
68 lines (52 loc) · 1.34 KB
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
package play
import (
"go/ast"
"go/parser"
"path"
)
func ParseDir(config *Config, dir string, mode parser.Mode) (map[string][]*ast.File, error) {
return parseDir(config, dir, mode)
}
func parseDir(config *Config, dir string, mode parser.Mode) (_ map[string][]*ast.File, rerr error) {
defer derr(&rerr, "ParseDir")
fis, err := config.Context.ReadDir(dir)
if err != nil {
return nil, err
}
pkgs := make(map[string][]*ast.File)
for _, fi := range fis {
if path.Ext(fi.Name()) != ".go" {
continue
}
match, err := config.Context.MatchFile(dir, fi.Name())
if err != nil {
return nil, err
}
if !match {
continue
}
filename := config.Context.JoinPath(dir, fi.Name())
file, err := ParseFile(config, filename, mode)
if err != nil {
return nil, err
}
pkgs[file.Name.Name] = append(pkgs[file.Name.Name], file)
}
return pkgs, nil
}
func ParseFile(config *Config, filename string, mode parser.Mode) (*ast.File, error) {
return parseFile(config, filename, mode)
}
func parseFile(config *Config, filename string, mode parser.Mode) (_ *ast.File, rerr error) {
defer derr(&rerr, "ParseFile")
src, err := config.Context.OpenFile(filename)
if err != nil {
return nil, err
}
defer src.Close()
file, err := parser.ParseFile(config.Fset, filename, src, mode)
if err != nil {
return nil, err
}
return file, nil
}