-
Notifications
You must be signed in to change notification settings - Fork 5
/
gogrep.go
187 lines (157 loc) · 4.06 KB
/
gogrep.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package gogrep
import (
"errors"
"go/ast"
"go/token"
"go/types"
"strings"
"github.com/quasilyte/gogrep/nodetag"
)
func IsEmptyNodeSlice(n ast.Node) bool {
if list, ok := n.(*NodeSlice); ok {
return list.Len() == 0
}
return false
}
// MatchData describes a successful pattern match.
type MatchData struct {
Node ast.Node
Capture []CapturedNode
}
type CapturedNode struct {
Name string
Node ast.Node
}
func (data MatchData) CapturedByName(name string) (ast.Node, bool) {
if name == "$$" {
return data.Node, true
}
return findNamed(data.Capture, name)
}
type PartialNode struct {
X ast.Node
from token.Pos
to token.Pos
}
func (p *PartialNode) Pos() token.Pos { return p.from }
func (p *PartialNode) End() token.Pos { return p.to }
type MatcherState struct {
Types *types.Info
// CapturePreset is a key-value pairs to use in the next match calls
// as predefined variables.
// For example, if the pattern is `$x = f()` and CapturePreset contains
// a pair with Name=x and value of `obj.x`, then the above mentioned
// pattern will only match `obj.x = f()` statements.
//
// If nil, the default behavior will be used. A first syntax element
// matching the matcher var will be captured.
CapturePreset []CapturedNode
// node values recorded by name, excluding "_" (used only by the
// actual matching phase)
capture []CapturedNode
nodeSlices []NodeSlice
nodeSlicesUsed int
pc int
partial PartialNode
}
func NewMatcherState() MatcherState {
return MatcherState{
capture: make([]CapturedNode, 0, 8),
nodeSlices: make([]NodeSlice, 16),
}
}
type Pattern struct {
m *matcher
}
type PatternInfo struct {
Vars map[string]struct{}
}
func (p *Pattern) NodeTag() nodetag.Value {
return operationInfoTable[p.m.prog.insts[0].op].Tag
}
// MatchNode calls cb if n matches a pattern.
func (p *Pattern) MatchNode(state *MatcherState, n ast.Node, cb func(MatchData)) {
p.m.MatchNode(state, n, cb)
}
// Clone creates a pattern copy.
func (p *Pattern) Clone() *Pattern {
clone := *p
clone.m = &matcher{}
*clone.m = *p.m
return &clone
}
type CompileConfig struct {
Fset *token.FileSet
// Src is a gogrep pattern expression string.
Src string
// When strict is false, gogrep may consider 0xA and 10 to be identical.
// If true, a compiled pattern will require a full syntax match.
Strict bool
// WithTypes controls whether gogrep would have types.Info during the pattern execution.
// If set to true, it will compile a pattern to a potentially more precise form, where
// fmt.Printf maps to the stdlib function call but not Printf method call on some
// random fmt variable.
WithTypes bool
// Imports specifies packages that should be recognized for the type-aware matching.
// It maps a package name to a package path.
// Only used if WithTypes is true.
Imports map[string]string
}
func Compile(config CompileConfig) (*Pattern, PatternInfo, error) {
if strings.HasPrefix(config.Src, "import $") {
return compileImportPattern(config)
}
info := newPatternInfo()
n, err := parseExpr(config.Fset, config.Src)
if err != nil {
return nil, info, err
}
if n == nil {
return nil, info, errors.New("invalid pattern syntax")
}
var c compiler
c.config = config
prog, err := c.Compile(n, &info)
if err != nil {
return nil, info, err
}
m := newMatcher(prog)
return &Pattern{m: m}, info, nil
}
func Walk(root ast.Node, fn func(n ast.Node) bool) {
if root, ok := root.(*NodeSlice); ok {
switch root.Kind {
case ExprNodeSlice:
for _, e := range root.exprSlice {
ast.Inspect(e, fn)
}
case StmtNodeSlice:
for _, e := range root.stmtSlice {
ast.Inspect(e, fn)
}
case FieldNodeSlice:
for _, e := range root.fieldSlice {
ast.Inspect(e, fn)
}
case IdentNodeSlice:
for _, e := range root.identSlice {
ast.Inspect(e, fn)
}
case SpecNodeSlice:
for _, e := range root.specSlice {
ast.Inspect(e, fn)
}
default:
for _, e := range root.declSlice {
ast.Inspect(e, fn)
}
}
return
}
ast.Inspect(root, fn)
}
func newPatternInfo() PatternInfo {
return PatternInfo{
Vars: make(map[string]struct{}),
}
}