I recently ran across https://github.com/mvdan/gogrep 
<https://github.com/mvdan/gogrep>. It does some of that.

Andy

> On Jul 4, 2019, at 5:30 PM, Bakul Shah <ba...@bitblocks.com> wrote:
> 
> Very nice!
> 
> A natural great extension[1] would be language aware grep/sed/awk:
> - return an enclosing parse construct after matching something
> - match on a construct with some pieces wildcarded
> - replace one structure with another (but based on the match)
> 
> Possible uses:
> - when you change an API of a package, fix up its users
> - rename variables
> - replace Go1 constructs with simpler/more efficient Go2 ones.
> - write generic code and replace with type specific code
> - program construction (ala Beta language's fragment system)
> - literate programming (tangle/weave)
> - optimize code
> - program analysis
> - structured diff/merge
> 
> The tough part would be coming up with a simple but flexible
> structured regular expression language. [Initially I had thought
> this is what Rob was writing about in his "Structured Regular
> Expressions" paper! So the idea is very old.] 
> 
> [1] No good deed goes unpunished :-)
> 
> 
>> On Jul 4, 2019, at 9:10 AM, Michael Jones <michael.jo...@gmail.com 
>> <mailto:michael.jo...@gmail.com>> wrote:
>> 
>> Recently I shared my Survey <https://github.com/MichaelTJones/survey> 
>> program, saying that it was a sidestep from what I was working on but 
>> interesting and hopefully useful to others. Here is the real thing, named 
>> gg, that Survey was a test for. GG combines lexical analysis and Go-native 
>> pattern matching to extend grep(1) for Go developers
>> 
>> GG is smart; the search is restricted, seeking matches only in chosen token 
>> classes.  A search in number literals can match values in addition to 
>> patterns: "v 255" matches the numeric value 255 in source code as 
>> 0b1111_1111, 0377, 0o377, 255, 0xff, etc.  Go's linear-time regular 
>> expression engine is Unicode-aware and supports many extensions: numbers in 
>> identifiers are found with "gg i [0-9]" or "gg i [\d]", find comments with 
>> math symbols using "gg c \p{Sm}", and Greek in strings via "gg s \p{Greek}".
>> 
>> GG is fast, uses all cores, understands filesystem hierarchies, archives, 
>> and compression schemes, and is general like grep while focused in a new 
>> way: find within package names, identifiers, types, strings, comments, and 
>> more.
>> 
>> Source code:
>> https://github.com/MichaelTJones/gg <https://github.com/MichaelTJones/gg>
>> 
>> Man page (all is explained here):
>> https://github.com/MichaelTJones/gg/blob/master/gg.pdf 
>> <https://github.com/MichaelTJones/gg/blob/master/gg.pdf>
>> 
>> Examples: 
>> 
>> Search the Go 1.13 source code for strings containing Megalosaurus, but not 
>> comments:
>> 
>> $ gg -r s Megalosaurus ~/go
>> /Users/mtj/go/src/cmd/link/link_test.go:`text:"London. Michaelmas term 
>> lately over, and the Lord Chancellor sitting in Lincoln’s Inn Hall. 
>> Implacable November weather. As much mud in the streets as if the waters had 
>> but newly retired from the face of the earth, and it would not be wonderful 
>> to meet a Megalosaurus, forty feet long or so, waddling like an elephantine 
>> lizard up Holborn Hill. ...
>> 
>> Search the Go 1.13 source code for identifiers with greek letters (but not 
>> strings or comments):
>> 
>> $ gg -r i '\p{Greek}' ~/go
>> /Users/mtj/go/src/encoding/json/encode_test.go:      A0, À, Aβ int
>> /Users/mtj/go/src/math/cmplx/polar.go:func Polar(x complex128) (r, θ 
>> float64) {
>> /Users/mtj/go/src/math/cmplx/rect.go:func Rect(r, θ float64) complex128 {
>> /Users/mtj/go/src/math/cmplx/rect.go:        s, c := math.Sincos(θ)
>> /Users/mtj/go/src/math/rand/rand_test.go:    var χ2 float64
>> :
>> 
>> Best to all,
>> Michael
>> 
>> -- 
>> Michael T. Jones
>> michael.jo...@gmail.com <mailto:michael.jo...@gmail.com>
>> 
>> -- 
>> You received this message because you are subscribed to the Google Groups 
>> "golang-nuts" group.
>> To unsubscribe from this group and stop receiving emails from it, send an 
>> email to golang-nuts+unsubscr...@googlegroups.com 
>> <mailto:golang-nuts+unsubscr...@googlegroups.com>.
>> To view this discussion on the web visit 
>> https://groups.google.com/d/msgid/golang-nuts/CALoEmQz-KdFaWjS%2Bfyd-QQS4LEgYmnofiZNmugnoQ7sNaG0HEA%40mail.gmail.com
>>  
>> <https://groups.google.com/d/msgid/golang-nuts/CALoEmQz-KdFaWjS%2Bfyd-QQS4LEgYmnofiZNmugnoQ7sNaG0HEA%40mail.gmail.com?utm_medium=email&utm_source=footer>.
>> For more options, visit https://groups.google.com/d/optout 
>> <https://groups.google.com/d/optout>.
> 
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to golang-nuts+unsubscr...@googlegroups.com 
> <mailto:golang-nuts+unsubscr...@googlegroups.com>.
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/golang-nuts/18AEB87E-E8A1-40BB-A63C-1DA84A607AF7%40bitblocks.com
>  
> <https://groups.google.com/d/msgid/golang-nuts/18AEB87E-E8A1-40BB-A63C-1DA84A607AF7%40bitblocks.com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout 
> <https://groups.google.com/d/optout>.

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/93934543-259E-4F3C-8CA1-EACD24F53630%40gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to