abnf is a toolkit for working with Augmented Backus–Naur Form (ABNF) grammars in Go, implementing the specifications from RFC 5234 and RFC 7405. It delivers reusable parsing operators, ready-made rule sets, and a CLI/code generation pipeline that help you build fast and reliable parsers.
Inspired by declaresub/abnf and elimity-com/abnf.
- Composable ABNF operators mirroring the RFC syntax and semantics.
- High-performance node reuse with pooling and optional caching.
- Generated rule sets for RFC core and definition grammars.
- Detailed error tracing with optional lightweight errors when you need speed.
- CLI tool and code generator for turning ABNF grammar files into Go packages.
go get github.com/ghettovoice/abnf@latestgo install github.com/ghettovoice/abnf/cmd/abnf@latestpackage main
import (
"fmt"
"github.com/ghettovoice/abnf"
)
var op = abnf.Concat(
`"a" "b" *"cd"`,
abnf.Literal(`"a"`, []byte("a")),
abnf.Literal(`"b"`, []byte("b")),
abnf.Repeat0Inf(`*"cd"`, abnf.Literal(`"cd"`, []byte("cd"))),
)
func main() {
nodes := abnf.NewNodes()
defer nodes.Free()
input := []byte("abcdcd")
if err := op(input, 0, nodes); err != nil {
panic(err)
}
best := nodes.Best()
fmt.Printf("matched: %s (len=%d)\n", best.String(), best.Len())
}| Package | Description |
|---|---|
github.com/ghettovoice/abnf |
Core operators, node utilities, and error helpers. |
pkg/abnf_core |
Generated implementation of RFC 5234 Appendix B core rules. |
pkg/abnf_def |
Generated implementation of the main ABNF grammar rules. |
pkg/abnf_gen |
Parser and code generation helpers you can embed in tooling. |
cmd/abnf |
CLI for generating Go code directly from ABNF files. |
The abnf CLI scaffolds configs and generates Go code from .abnf sources. Typical workflow:
- Generate a starter config:
abnf config ./grammar.yml - Update the YAML with your grammar files and output options.
- Run
abnf generate ./grammar.ymlto emit ready-to-use Go sources.
Issues and pull requests are welcome. To get started:
make test
make lint
make benchDistributed under the MIT License. See LICENSE for details.
Third-party dependencies are listed in THIRD-PARTY-LICENSES.md.