Announcing gg ("gigi") your new friend

197 views
Skip to first unread message

Michael Jones

unread,
Jul 4, 2019, 12:11:47 PM7/4/19
to golang-nuts
Recently I shared my 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:

Man page (all is explained here):

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
michae...@gmail.com

Niko Schwarz

unread,
Jul 4, 2019, 1:22:50 PM7/4/19
to golang-nuts
What's your recommended strategy for installing it on linux? I guess it'll take a few years before distributions have easy-to-install packages just listed in their indices.

Jan Mercl

unread,
Jul 4, 2019, 2:03:14 PM7/4/19
to Niko Schwarz, golang-nuts
$ go get ...

--
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...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/078487a3-cbda-4479-b414-da5aed21f86f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Michael Jones

unread,
Jul 4, 2019, 7:42:38 PM7/4/19
to Niko Schwarz, golang-nuts
Niko, good question. You have your answer already but i’ll add installation advice in the readme or an install guide. Should be trivial anywhere Go is, “go get path && go install gg”

--
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...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/078487a3-cbda-4479-b414-da5aed21f86f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Bakul Shah

unread,
Jul 4, 2019, 8:30:29 PM7/4/19
to Michael Jones, golang-nuts
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 :-)


--
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...@googlegroups.com.

Andy Balholm

unread,
Jul 4, 2019, 9:00:02 PM7/4/19
to Bakul Shah, Michael Jones, golang-nuts
I recently ran across https://github.com/mvdan/gogrep. It does some of that.

Andy

Michael Jones

unread,
Jul 5, 2019, 12:27:50 AM7/5/19
to Andy Balholm, Bakul Shah, golang-nuts
Interesting. I will look. the issue is the level of the smartness. gg is basic (tokens) and below a full parse (syntax). The higher you go, the different the search questions can be.

If you downloaded, please update and rebuild. I had mail about a performance problem and found an error of configuration. It is better now.

Michael

Wojciech S. Czarnecki

unread,
Jul 5, 2019, 2:16:48 AM7/5/19
to golan...@googlegroups.com
On Thu, 4 Jul 2019 09:10:09 -0700
Michael Jones <michae...@gmail.com> wrote:

Great work, thank you :)

> 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


--
Wojciech S. Czarnecki
<< ^oo^ >> OHIR-RIPE
Reply all
Reply to author
Forward
0 new messages