GitHub - mvdan/gogrep: Search for Go code using syntax trees
source link: https://github.com/mvdan/gogrep
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
gogrep
GO111MODULE=on go get mvdan.cc/gogrep
Search for Go code using syntax trees.
gogrep -x 'if $x != nil { return $x, $*_ }'
Note that this project is no longer being developed. See #64 for more details.
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 semicolons)
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
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
Recommend
-
44
v2.2.0 Compare
-
5
Pretty-print syntax trees with this one simple trick prettyprintI want to share a simple trick for pretty-printing syntax trees with the correct precedence that I’ve been using in my own interpreter projects. I believe this...
-
4
Binary search trees explained yourbasic.org Binary tree definitions A binary tree is a data structure most easily desc...
-
7
Treaps: Randomized search trees yourbasic.org A treap stores items in sorted order and offers efficient...
-
3
117346370 Trees planted by Ecosia users Let's plant some trees! Make Ecosia your new search engine and plant trees with your searches - for free!...
-
8
Programmer and Software Interview Questions and Answers What are the differences between a hash table and a binary search tree? Suppose that you are trying to figure out which of those...
-
5
-
5
Problem statement Given an integer n, return the number of structurally unique **BST's* (binary search trees) which has exactly n nodes of unique values from 1 to n*. Problem statement taken from:
-
5
Where’s the trees? Gathering and using timber in Neolithic Orkney Friday, November 5, 2021 “To everyone’s surprise, the pri...
-
3
原文:Order-Preserving Key Compression for In-Memory Search Trees TL;DR 本文提出了一种针对字符串的分段编码框...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK