Go library for Parsing the syntax defined in ABNF.
This library provides Finders.
Finder is the struct that has the methods named Find
and Copy
.
type Finder interface {
Find(data []byte) (found bool, end int)
Copy() Finder
}
Finder.Find(data []byte) (found bool, end int)
method finds the specific ABNF syntax such as ALPHA, DIGIT, Concatenation, Alternatives, etc. from the beginning of the data
.
If it finds the syntax, it returns true
as found
and the end of the syntax as end
.
For example, this library provides AlphaFinder
.
This Finder finds ALPHA from data
.
When you call its Find
method with data []byte{ 'a', 'b', 'c', }
, it search ALPHA from the beginning of data
. Then it returns true, 1
because data
has 'a'
at the beginning.
package main
import (
"fmt"
abnfp "github.com/um7a/abnf-parser"
)
func main() {
var data []byte = []byte{'a', 'b', 'c'}
alpha := abnfp.NewAlphaFinder()
found, end := alpha.Find(data)
fmt.Printf("%v, %v\n", found, end) // -> true, 1
}
Finder.Copy() Finder
method returns the copy of the Finder.
Some Finders have other finders as its child. This method also copies them. Note that this is the deep copy.
package main
import (
"fmt"
abnfp "github.com/um7a/abnf-parser"
)
func main() {
alpha1 := abnfp.NewAlphaFinder()
alpha2 := alpha1.Copy()
var data []byte = []byte{'a', 'b', 'c'}
found, end := alpha2.Find(data)
fmt.Printf("%v, %v\n", found, end) // -> true, 1
}
The only utility provided by this library other than Finder
is Parse
function.
func Parse(data []byte, finder Finder) (parsed []byte, remaining []byte)
Parse
function returns the parsed data as parsed
and the remaining data as remaining
.
For example, when data
is []byte{'a', 'b', 'c'}
and finder
is AlphaFinder
,
Parse
function parses ALPHA from data
and returns []byte{'a'}, []byte{'b', 'c'}
package main
import (
"fmt"
abnfp "github.com/um7a/abnf-parser"
)
func main() {
var data []byte = []byte{'a', 'b', 'c'}
parsed, remaining := abnfp.Parse(data, abnfp.NewAlphaFinder())
fmt.Printf("parsed: %s, remaining: %s\n", parsed, remaining)
// -> parsed: a, remaining: bc
}