Giter VIP home page Giter VIP logo

abnf-parser's Introduction

abnf-parser

Go library for Parsing the syntax defined in ABNF.

1. Concept

1.1. Finder

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
}

1.1.1. Finder.Find

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.

Example

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
}

1.1.2. Finder.Copy

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.

Example
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
}

1.2. Parse

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.

Example

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
}

abnf-parser's People

Contributors

um7a avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.