-
-
Notifications
You must be signed in to change notification settings - Fork 15
API for gogrep matching engine #55
Description
In writing linters (in my case, go/analysis
passes to be run as golangci-lint plugins) I find myself wanting some sort of AST-matching engine. This tool seems like a really great one! But I want to use it inside my own tool, rather than as a simple global search, like this package or go-ruleguard is designed to do. To do that, I need to use the gogrep
matcher, but then refer back to the AST or types in an arbitrary way.
A sample API that I think would be sufficient, and which looks to my quick glance similar to what you're already using internally:
type Matcher
// Compile compiles a gogrep pattern into a Matcher object.
func Compile(pattern string) (*Matcher, error)
// FindAll returns all matches to the given pattern within the given AST node.
func (m *Matcher) FindAll(node ast.Node) []Match
// A single match of a pattern.
type Match struct {
// The top-level node that matched the pattern.
Node ast.Node
// The nodes that matched each variable; for example if $x + $_ matched 2 + 3,
// captures would be {"$x": <node for 2>}
Captures map[string]ast.Node
}
// optionally other regexp-style APIs like MustCompile, Matcher.Find, Matcher.Match.
Related to #32, but it seems like that won't actually do what we would want, for the same reason go-ruleguard isn't quite enough for us: we would still have no direct access to the underlying AST/types.