gogrep

package
v0.3.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 15, 2021 License: BSD-3-Clause, BSD-3-Clause Imports: 9 Imported by: 0

README

gogrep

go get mvdan.cc/gogrep

Search for Go code using syntax trees. Work in progress.

gogrep -x 'if $x != nil { return $x, $*_ }'
Instructions
usage: gogrep commands [packages]

A command is of the form "-A pattern", where -A is one of:

   -x  find all nodes matching a pattern
   -g  discard nodes not matching a pattern
   -v  discard nodes matching a pattern
   -a  filter nodes by certain attributes
   -s  substitute with a given syntax tree
   -w  write source back to disk or stdout

A pattern is a piece of Go code which may include wildcards. It can be:

   a statement (many if split by semicolonss)
   an expression (many if split by commas)
   a type expression
   a top-level declaration (var, func, const)
   an entire file

Wildcards consist of $ and a name. All wildcards with the same name within an expression must match the same node, excluding "_". Example:

   $x.$_ = $x // assignment of self to a field in self

If * is before the name, it will match any number of nodes. Example:

   fmt.Fprintf(os.Stdout, $*_) // all Fprintfs on stdout

* can also be used to match optional nodes, like:

for $*_ { $*_ }    // will match all for loops
if $*_; $b { $*_ } // will match all ifs with condition $b

Regexes can also be used to match certain identifier names only. The .* pattern can be used to match all identifiers. Example:

   fmt.$(_ /Fprint.*/)(os.Stdout, $*_) // all Fprint* on stdout

The nodes resulting from applying the commands will be printed line by line to standard output.

Here are two simple examples of the -a operand:

   gogrep -x '$x + $y'                   // will match both numerical and string "+" operations
   gogrep -x '$x + $y' -a 'type(string)' // matches only string concatenations

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsEmptyList added in v0.3.1

func IsEmptyList(n ast.Node) bool

Types

type CapturedNode added in v0.3.1

type CapturedNode struct {
	Name string
	Node ast.Node
}

type ExprList added in v0.3.1

type ExprList = exprList

type MatchData

type MatchData struct {
	Node    ast.Node
	Capture []CapturedNode
}

MatchData describes a successful pattern match.

func (MatchData) CapturedByName added in v0.3.1

func (data MatchData) CapturedByName(name string) (ast.Node, bool)

type Pattern

type Pattern struct {
	Expr ast.Node
	// contains filtered or unexported fields
}

Pattern is a compiled gogrep pattern.

func Parse

func Parse(fset *token.FileSet, expr string, strict bool) (*Pattern, error)

Parse creates a gogrep pattern out of a given string expression.

func (*Pattern) Clone added in v0.3.0

func (p *Pattern) Clone() *Pattern

Clone creates a pattern copy.

func (*Pattern) MatchExprList added in v0.3.1

func (p *Pattern) MatchExprList(exprs []ast.Expr, cb func(MatchData))

func (*Pattern) MatchNode

func (p *Pattern) MatchNode(n ast.Node, cb func(MatchData))

MatchNode calls cb if n matches a pattern.

func (*Pattern) MatchStmtList added in v0.3.1

func (p *Pattern) MatchStmtList(stmts []ast.Stmt, cb func(MatchData))

type StmtList added in v0.3.1

type StmtList = stmtList

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL