-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnew.go
More file actions
79 lines (71 loc) · 2.42 KB
/
new.go
File metadata and controls
79 lines (71 loc) · 2.42 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
69
70
71
72
73
74
75
76
77
78
79
package parser
import (
"github.com/mathbalduino/go-log/loggerCLI"
"go/token"
"golang.org/x/tools/go/packages"
)
// GoParser is a type that can parse
// GO source code and iterate over the
// parsed code
type GoParser struct {
// pkgs is a slice that contains all the
// parsed packages
pkgs []*packages.Package
// focus tell the parser what kind of thing
// it must focus on. It can be a type, a file,
// an entire package, etc
focus *Focus
// logger is used to print information to stdout
// about each step
logger LoggerCLI
// fileSet is the one forwarded to the underlying
// packages.Load function
fileSet *token.FileSet
}
// NewGoParser creates a new parser for GO source files
//
// The pattern argument will be forwarded directly to the
// packages.Load function. Take a look at the docs:
// Load passes most patterns directly to the underlying build tool,
// but all patterns with the prefix "query=", where query is a non-empty
// string of letters from [a-z], are reserved and may be interpreted
// as query operators
//
// Two query operators are currently supported: "file" and "pattern"
//
// The query "file=path/to/file.go" matches the package or packages
// enclosing the Go source file path/to/file.go. For example
// "file=~/go/src/fmt/print.go" might return the packages "fmt" and
// "fmt [fmt.test]"
//
// The query "pattern=string" causes "string" to be passed directly to
// the underlying build tool. In most cases this is unnecessary, but an
// application can use Load("pattern=" + x) as an escaping mechanism to
// ensure that x is not interpreted as a query operator if it contains
// '='
//
// All other query operators are reserved for future use and currently
// cause Load to report an error
//
// Note that one pattern can match multiple packages and that a package
// might be matched by multiple patterns: in general it is not possible
// to determine which packages correspond to which patterns.
func NewGoParser(pattern string, config Config) (*GoParser, error) {
logger := config.Logger
if logger == nil {
logger = loggerCLI.New(config.LogFlags&LogJSON != 0, config.LogFlags&(^LogJSON))
}
printFinalConfig(pattern, config, logger)
config.Logger = logger
packagesLoadConfig := packagesLoadConfig(config)
pkgs, e := packages.Load(packagesLoadConfig, pattern)
if e != nil {
return nil, e
}
return &GoParser{
pkgs,
config.Focus,
logger,
packagesLoadConfig.Fset,
}, nil
}