Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Package abnf generates parsers from ABNF grammar (RFC 5234, RFC 7405).

License

Notifications You must be signed in to change notification settings

ghettovoice/abnf

abnf

Go Reference Go Report Card Tests Coverage Status CodeQL

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.

Table of Contents

Features

  • 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.

Installation

Library

go get github.com/ghettovoice/abnf@latest

CLI

go install github.com/ghettovoice/abnf/cmd/abnf@latest

Quick Start

package 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())
}

Packages

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.

CLI Overview

The abnf CLI scaffolds configs and generates Go code from .abnf sources. Typical workflow:

  1. Generate a starter config: abnf config ./grammar.yml
  2. Update the YAML with your grammar files and output options.
  3. Run abnf generate ./grammar.yml to emit ready-to-use Go sources.

Contributing

Issues and pull requests are welcome. To get started:

make test
make lint
make bench

License

Distributed under the MIT License. See LICENSE for details.

Third-party dependencies are listed in THIRD-PARTY-LICENSES.md.

About

Package abnf generates parsers from ABNF grammar (RFC 5234, RFC 7405).

Topics

Resources

License

Code of conduct

Contributing

Security policy

Stars

Watchers

Forks

Packages

No packages published

Contributors 3

  •  
  •  
  •