A Parsing Expression Grammar ( hence peg) is a way to create grammars similar in principle to regular expressions but which allow better code integration. Specifically, peg is an implementation of the Packrat parser generator originally implemented as peg/leg by Ian Piumarta in C. A Packrat parser is a "descent recursive parser" capable of backtracking and negative look-ahead assertions which are problematic for regular expression engines.
See peg-file-syntax.md for creating your own *.peg file.
Add peg as a tool dependency
go get -tool github.com/pointlander/peg@main
Create your mygrammar.peg
Add a //go:generate line in a Go source of your package
//go:generate go tool peg mygrammar.peg
Widespread community design pattern: Add lines like this in doc.go.
Generate mygrammar.peg.go from mygrammar.peg
go generate
This creates the file peg.peg.go
go tool peg -inline -switch peg.peg
go tool peg -h
Bootstrap and generate grammar *.peg.go. This commands should initially be executed once before other commands.
go generate
go build
(go generate required once beforehand)
Use the version from the tag if the current commit has a tag. If not use the current commit hash.
go build -ldflags "-X main.Version=$(git describe --tags --exact-match 2>/dev/null || git rev-parse --short HEAD)"
Additionally, since Go 1.18 the go command embeds version control information. Read the information:
go version -m peg
go test -short ./...
(go generate required once beforehand)
golangci-lint run
(go generate required once beforehand)
golangci-lint fmt
go test -benchmem -bench .
(go generate required once beforehand)
Andrew Snodgrass
(top 30 of 40)
Go
86.2%
Go Template
9.9%
Java
2.7%
Shell
1.2%
A Parsing Expression Grammar ( hence peg) is a way to create grammars similar in principle to regular expressions but which allow better code integration. Specifically, peg is an implementation of the Packrat parser generator originally implemented as peg/leg by Ian Piumarta in C. A Packrat parser is a "descent recursive parser" capable of backtracking and negative look-ahead assertions which are problematic for regular expression engines.
See peg-file-syntax.md for creating your own *.peg file.
Add peg as a tool dependency
go get -tool github.com/pointlander/peg@main
Create your mygrammar.peg
Add a //go:generate line in a Go source of your package
//go:generate go tool peg mygrammar.peg
Widespread community design pattern: Add lines like this in doc.go.
Generate mygrammar.peg.go from mygrammar.peg
go generate
This creates the file peg.peg.go
go tool peg -inline -switch peg.peg
go tool peg -h
Bootstrap and generate grammar *.peg.go. This commands should initially be executed once before other commands.
go generate
go build
(go generate required once beforehand)
Use the version from the tag if the current commit has a tag. If not use the current commit hash.
go build -ldflags "-X main.Version=$(git describe --tags --exact-match 2>/dev/null || git rev-parse --short HEAD)"
Additionally, since Go 1.18 the go command embeds version control information. Read the information:
go version -m peg
go test -short ./...
(go generate required once beforehand)
golangci-lint run
(go generate required once beforehand)
golangci-lint fmt
go test -benchmem -bench .
(go generate required once beforehand)
Andrew Snodgrass
(top 30 of 40)
Go
86.2%
Go Template
9.9%
Java
2.7%
Shell
1.2%