abnf

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Oct 28, 2021 License: MIT Imports: 4 Imported by: 3

README

abnf

ABNF LL-parser by golang

How to use

Make Parser function

Make parser function that implement Rule interface. Functions that returns Basic Operator Rule are provided by this package.

For example,

  • abnf.C(Rule...) function returns Rule that concatenate all argument Rules.
  • abnf.V(rune) function returns Rule that match argument rune.
  • abnf.ALPHA() function returns Rule that match any alphanumeric text.
  • abnf.ETX() function returns Rule that match no more text is left.

Combile multiple Basic Operator by set each function to arguement. abnf.C(abnf.V('-'), abnf.ALPHANUM()) will make Rule that match aplhanumeric text taht start with -.

Parse Text by created Parser

Call abnf.ParseString(string, Rule) function with created Rule. The string will parsed and parsed item Tree is returned.

If text is not match with Rule, nil is returned.

Get parsed value from Tree

Tree contains parsed value with tag. tree.Child(tag).V returns value that correlated with specified tag.

Tag is defined while making parser function. There is special function abnf.K(Rule, tag) that make Special Rule that define tag.

Example

This sample parse FQDN.

Identity ::= label *("." label)
label    ::= ALPHANUM *ldhstr
ldhstr   ::= ALPHANUM / ("-" ALPHANUM)
const (
	idFQDN int = iota
)

type Identity string

func ParseIdentity(str string) (id Identity, e error) {
	t := abnf.ParseString(str, _identity())
	if t == nil {
		e = fmt.Errorf("Invalid id text")
	} else {
		id = Identity(t.Child(idFQDN).V)
	}
	return
}

func _identity() abnf.Rule {
	return abnf.C(_fqdn(), abnf.ETX())
}

func _fqdn() abnf.Rule {
	return abnf.K(abnf.C(_label(), abnf.R0(abnf.C(abnf.V('.'), _label()))), idFQDN)
}

func _label() abnf.Rule {
	return abnf.C(abnf.ALPHANUM(), abnf.R0(_ldhstr()))
}

func _ldhstr() abnf.Rule {
	return abnf.A(abnf.ALPHANUM(), abnf.C(abnf.V('-'), abnf.ALPHANUM()))
}

Notice

This parser is LL parser. You should modify somae of ABNF definition to apply them this package.

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Rule

type Rule func(s *scanner) []rune

Rule is rule function of ABNF

func A

func A(r ...Rule) Rule

A is Alternatives(Rule1 / Rule2) Incremental Alternatives: Rule1 =/ Rule2 different or ETX -> nil

func ALPHA

func ALPHA() Rule

ALPHA = %x41-5A / %x61-7A

func ALPHANUM

func ALPHANUM() Rule

ALPHANUM = ALPHA / DIGIT

func BIT

func BIT() Rule

BIT = "0" / "1"

func C

func C(r ...Rule) Rule

C is Concatenation(Rule1 Rule2) match -> []rune of match value different or ETX -> nil

func CHAR

func CHAR() Rule

CHAR = %x01-7F

func CR

func CR() Rule

CR = %x0D

func CRLF

func CRLF() Rule

CRLF = CR LF

func CTL

func CTL() Rule

CTL = %x00-1F / %x7F

func DIGIT

func DIGIT() Rule

DIGIT = %x30-39

func DQUOTE

func DQUOTE() Rule

DQUOTE = %x22

func ETX

func ETX() Rule

ETX = [End of Text]

func HEXDIG

func HEXDIG() Rule

HEXDIG = DIGIT / %x41-46

func HTAB

func HTAB() Rule

HTAB = %x09

func K

func K(r Rule, k int) Rule

K add new tree node with key k

func LF

func LF() Rule

LF = %x0A

func LHEX

func LHEX() Rule

LHEX = DIGIT / %x61-66

func LWSP

func LWSP() Rule

LWSP = *(WSP / CRLF WSP)

func N

func N(r Rule) Rule

N is Not match match -> nil different or ETX -> empty []rune

func O

func O(r Rule) Rule

O is Optional Sequence([RULE]) match -> []rune of match value different or ETX -> empty []rune

func OCTET

func OCTET() Rule

OCTET = %x00-FF

func R0

func R0(r Rule) Rule

R0 is default repeat count of Variable Repetition From zero to infinity

func R1

func R1(r Rule) Rule

R1 is more than one repeat count of Variable Repetition From one to infinity

func RN

func RN(n int, r Rule) Rule

RN is Specific Repetition(nRule) match -> []rune of match value different or ETX -> nil

func RV

func RV(min int, max int, r Rule) Rule

RV is Variable Repetition(*Rule) match -> []rune of match value different or ETX -> nil If max=-1 then max=infinity.

func SP

func SP() Rule

SP = %x20

func V

func V(c rune) Rule

V match single character

func VCHAR

func VCHAR() Rule

VCHAR = %x21-7E

func VI

func VI(s string) Rule

VI match single string (case insensitive)

func VIL

func VIL(sl ...string) Rule

VIL match multiple strings (case insensitive)

func VL

func VL(cl ...rune) Rule

VL match multiple characters

func VR

func VR(h rune, t rune) Rule

VR is Value Range Alternatives(%c##-##) match -> []rune of match value different or ETX -> nil

func VS

func VS(s string) Rule

VS match single string (case sensitive)

func VSL

func VSL(sl ...string) Rule

VSL match multiple strings (case sensitive)

func WSP

func WSP() Rule

WSP = SP / HTAB

type Tree

type Tree struct {
	K int
	V []rune
	// contains filtered or unexported fields
}

Tree is tree structure that contains pair of key and value

func ParseReader

func ParseReader(r io.RuneReader, blen, slen int, f Rule) *Tree

ParseReader parse reader value by Rule

func ParseString

func ParseString(b string, f Rule) *Tree

ParseString parse string value by Rule

func (*Tree) AllChildren

func (t *Tree) AllChildren() (ret []*Tree)

AllChildren return all children

func (*Tree) Child

func (t *Tree) Child(k int) *Tree

Child return first child that Key = k

func (*Tree) Children

func (t *Tree) Children(k int) (ret []*Tree)

Children return children that Key = k

func (Tree) GetStack

func (t Tree) GetStack() string

GetStack print stack trace

func (*Tree) HasChild

func (t *Tree) HasChild() bool

HasChild return the tree have child or not

func (Tree) String

func (t Tree) String() string

Jump to

Keyboard shortcuts

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