compar

package module
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Aug 25, 2021 License: MIT Imports: 8 Imported by: 0

README

compar

A combinator parsing library for Go, inspired by nom.

Combinator parsers let users describe a set of parsing functions that otherwise define the shape of the things they would like to parse.

Contributing

If you would like to contribute to compar, clone this repository, and install the pre-commit and pre-push hooks:

make -C hooks install
Git hooks

The pre-commit hook will run tests on packages containing any .go files that have been changed. The pre-push hook will run all tests, across all packages.

Pull requests

Pull requests are done using the compar-devel mailing list. If you would like to learn how to submit pull requests over email, see git-send-email.io.

Documentation

Index

Constants

View Source
const Whitespace = "\t\n\v\f\r \u0085\u00a0"

Whitespace is a useful constant containing most common whitespace characters.

Variables

View Source
var (
	// ErrNoMatch is used as a sentinel error to indicate that the given parser
	// did not match what was in the input.
	ErrNoMatch = errors.New("no match")
)
View Source
var ErrSkip = errors.New("skip")

ErrSkip is a sentinel error that can be used by TransformFuncs to indicate that the token being transformed should be dropped, but that transofmration should otherwise be considered successful.

Functions

func Anything added in v0.2.0

func Anything(_ rune) bool

Anything is a predicate function used to indicate all runes should be consumed.

func IsAlphanumeric

func IsAlphanumeric(r rune) bool

IsAlphanumeric is a predicate function which returns true if r is a letter, or number.

func IsNewline added in v0.2.0

func IsNewline(r rune) bool

IsNewline is a predicate function that returns true if r is "\n".

Types

type Parser

type Parser func(Reader) (Token, error)

Parser is a functional type that consumes data from a Reader, and returns a Token along with a parsing error (if any).

func Any

func Any(parsers ...Parser) Parser

Any returns a Parser that will run all parsers on the input. The first parser to return a Token and a non-nil error will consume the input. If none of the parsers match, Any will return ErrNoMatch.

func Assign

func Assign(p Parser, dst interface{}) Parser

Assign runs the parser p, and attempts to assign the value to dst. dst may be a pointer to a string, []byte, or []string or [][]byte.

If the caller wishes to convert the values to any other type, a custom transformation function should be written.

func ConsumeUntil added in v0.2.0

func ConsumeUntil(predicate Predicate) Parser

ConsumeUntil will consume all bytes from a reader until p returns true, or EOF.

func Delimited

func Delimited(start, end string, p Predicate) Parser

Delimited returns a parser that will consume all data matching the given predicate, between the start and end delimiters. The start and end delimiters will not be a part of the resulting data, but will be consumed.

func List added in v0.2.0

func List(start, end, separators string) Parser

List parses a collection of elements from a reader, contained within the start and end delimiters. Elements are separated by any of the code points in separators.

func Many

func Many(n int, p Parser) Parser

Many applies p at least n number of times, where p must apply at least n times.

If p returns io.EOF, Many will return immediately, with a nil error.

When n == 0, p is run continuously until the input source returns io.EOF.

func Space

func Space(n int) Parser

Space returns a parser that will consume at least n space and tab characters. The value of n is used as a required, minimum number of whitespace characters to consume. For example,

token, err := Space(2)(r)

would require there be at least 2 whitespace characters.

The caller should use Multispace if they wish to also capture newline and carriage return characters.

func Tag

func Tag(s string) Parser

Tag returns a parser that will match s exactly.

func Transform added in v0.2.0

func Transform(p Parser, fn TransformFunc) Parser

Transform passes the token returned from p, to the function fn.

For tokens returned from the List parser, fn will be called on each element. If fn returns ErrSkip, the token passed to fn should be dropped from the final list of tokens.

type Predicate

type Predicate func(rune) bool

Predicate is a function that can be used to match a rune.

Instead of providing a bunch of bespoke functions that satisfy this type, the type definition should cover most of the functions within the "unicode" package.

func AnyOf added in v0.2.0

func AnyOf(r ...rune) Predicate

AnyOf returns a predicate function that will return true for any of the provided runes.

func Char added in v0.2.0

func Char(r rune) Predicate

Char returns a predicate function that will return true when called for r.

type Reader

type Reader interface {
	Peek(int) ([]byte, error)
	Read([]byte) (int, error)
	ReadByte() (byte, error)
}

Reader defines the interface of a data source for Parse. This is nothing more than a whittled-down list of methods exposed by types like bufio.Reader, limited to those that we actually use.

type Token

type Token interface {
	fmt.Stringer

	ReadFrom(Reader) (int64, error)
}

Token defines the interface for any type that is parsed from an input. Users are encouraged to implement this interface on the types they wish to be able to populate from an input.

When a user implements the fmt.Stringer interface on their type, it should return the textual representation that was parsed to result in the object. There is no requirement that the textual representation be exactly the same as was used to instantiate the implementer.

func Parse

func Parse(r io.Reader, parsers ...Parser) ([]Token, error)

Parse runs the given parsers in order, consuming data from r.

type TransformFunc added in v0.2.0

type TransformFunc func(Token) (Token, error)

TransformFunc is a function type that transforms one token into another.

Jump to

Keyboard shortcuts

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