abnf

package module
v0.1.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 30, 2024 License: MIT Imports: 5 Imported by: 0

README

abnf

Go Reference Tests Coverage Status CodeQL

Package abnf implements ABNF grammar as described in RFC 5234 and RFC 7405.

Inspired by:

Installation

Add abnf package and all subpackages to your project:

go get github.com/ghettovoice/abnf@latest

Usage

Build a rule from basic operators:

package main

import (
    "fmt"

    "github.com/ghettovoice/abnf"
)

var abc = 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() {
    var ns abnf.Nodes

    fmt.Println(abc([]byte("ab"), ns[:0]))
    fmt.Println(abc([]byte("abcd"), ns[:0]))
    fmt.Println(abc([]byte("abcdcd"), ns[:0]))
}

CLI

Checkout abnf CLI README.

License

MIT License - see LICENSE file for a full text.

Documentation

Overview

Package abnf provides basic ABNF operators (RFC 5234, RFC 7405).

Core ABNF rules implementation can be found in github.com/ghettovoice/abnf/pkg/abnf_core, ABNF definition rules are in github.com/ghettovoice/abnf/pkg/abnf_def, code and parser generators are in github.com/ghettovoice/abnf/pkg/abnf_gen.

Index

Constants

View Source
const (
	VERSION = "0.1.2" // package version
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Node

type Node struct {
	Key      string
	Value    []byte
	Children Nodes
}

Node represents a single node in a tree generated by Operator.

func (Node) Compare

func (n Node) Compare(other Node) int

Compare compares node values via bytes.Compare. The result will be 0 if n.Value == other.Value, -1 if n.Value < other.Value, and +1 if n.Value > other.Value.

func (Node) Contains

func (n Node) Contains(key string) bool

Contains returns whether the subtree contains the given key.

func (Node) GetNode

func (n Node) GetNode(key string) (Node, bool)

GetNode recursively searches a node with the given key starting from itself. Returns found node and true on success, empty node and false on failure.

func (Node) GetNodes

func (n Node) GetNodes(key string) Nodes

GetNodes recursively searches all nodes with the given key starting from itself.

func (Node) IsEmpty

func (n Node) IsEmpty() bool

IsEmpty returns true if the node's value length = 0.

func (Node) Len

func (n Node) Len() int

Len returns length of the node's value.

func (Node) String

func (n Node) String() string

String returns the node's value as string.

type Nodes

type Nodes []Node

Nodes represents a list of nodes.

func (Nodes) Best

func (ns Nodes) Best() Node

Best returns a node with the longest value.

func (Nodes) Compare

func (ns Nodes) Compare(other Nodes) int

Compare compares two best nodes. The result will be 0 if a == b, -1 if a < b, and +1 if a > b where a - self best node, b - other best node.

func (Nodes) Contains

func (ns Nodes) Contains(key string) bool

Contains returns whether the subtree contains the given key.

func (Nodes) Get

func (ns Nodes) Get(key string) (Node, bool)

Get recursively searches a node with the given key.

func (Nodes) GetAll

func (ns Nodes) GetAll(key string) Nodes

GetAll recursively searches all nodes with the given key.

type Operator

type Operator func(s []byte, ns Nodes) Nodes

Operator represents an ABNF operator.

func Alt

func Alt(key string, oprts ...Operator) Operator

Alt defines a sequence of alternative elements that are separated by a forward slash ("/"). Created operator will return all matched alternatives.

func AltFirst

func AltFirst(key string, oprts ...Operator) Operator

AltFirst defines a sequence of alternative elements that are separated by a forward slash ("/"). Created operator will return first matched alternative.

func Concat

func Concat(key string, oprts ...Operator) Operator

Concat defines a simple, ordered string of values. Created operator will return the longest alternative.

func ConcatAll

func ConcatAll(key string, oprts ...Operator) Operator

ConcatAll defines a simple, ordered string of values. Created operator will return all alternatives.

func Literal

func Literal(key string, val []byte) Operator

Literal defines a case-insensitive characters sequence.

func LiteralCS

func LiteralCS(key string, val []byte) Operator

LiteralCS defines a case-sensitive characters sequence.

func Optional

func Optional(key string, op Operator) Operator

Optional defines an optional element sequence. It is equivalent to repeat 0-1.

func Range

func Range(key string, low, high []byte) Operator

Range defines a range of alternative numeric values.

func Repeat

func Repeat(key string, min, max uint, op Operator) Operator

Repeat defines a variable repetition.

func Repeat0Inf

func Repeat0Inf(key string, op Operator) Operator

Repeat0Inf defines a specific repetition from 0 to infinity.

func Repeat1Inf

func Repeat1Inf(key string, op Operator) Operator

Repeat1Inf defines a specific repetition from 1 to infinity.

func RepeatN

func RepeatN(key string, n uint, op Operator) Operator

RepeatN defines a specific repetition.

Directories

Path Synopsis
cmd
pkg
abnf_core
Package abnf_core implements core ABNF rules defined in [RFC 5234 Appendix B].
Package abnf_core implements core ABNF rules defined in [RFC 5234 Appendix B].
abnf_def
Package abnf_def implements ABNF grammar rules defined in [RFC 5234] and [RFC 7405].
Package abnf_def implements ABNF grammar rules defined in [RFC 5234] and [RFC 7405].
abnf_gen
Package abnf_gen implements parser and code generation from ABNF grammar.
Package abnf_gen implements parser and code generation from ABNF grammar.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL