rd

package
v0.0.0-...-2b86a76 Latest Latest
Warning

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

Go to latest
Published: Jan 5, 2022 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package rd provides recursive descent parser helpers. This is not really intended of use outside of this library, but some objects here are exposed and could be useful in some applications. This should probably be a separate package library.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ATag

type ATag int

ATag is the type used to tag matches by type.

const (
	TNone ATag = iota
	TLiteral
	TLast
)

A few standard tags for matches.

type Match

type Match struct {
	Tag      ATag              // an identifier describing what the match represents
	Content  []byte            // the full content of the match
	Group    map[string]*Match // identifies named submatches
	Submatch []*Match          // identifies a list of submatches
	Made     interface{}       // a place to put high-level objects generated from this match
}

Match is the object used to represent some segment of a parsed string.

func BuildMatch

func BuildMatch(t ATag, ms ...interface{}) (m *Match)

BuildMatch is a short hand for building a match with named submatches.

func MatchLongest

func MatchLongest(cs []byte, ms ...Matcher) (*Match, []byte)

MatchLongest tries all the given matchers against the current input. It then returns whichever of these matches works to match the most input.

func MatchMany

func MatchMany(t ATag, cs []byte, min int, mtch Matcher) (*Match, []byte)

MatchMany matches the given matcher as many times as possible one after another on the input. If the number of matches is fewer than min, it returns a failure.

func MatchManyWithSep

func MatchManyWithSep(t ATag, cs []byte, min int, mtch Matcher, sep Matcher) (*Match, []byte)

MatchManyWithSep matches the given matcher against the input provided that the separator matcher matches in between. It returns a match containing those matches. If fewer than min matches are present, the match returns no match.

func MatchOne

func MatchOne(t ATag, cs []byte, pred func(c byte) bool) (*Match, []byte)

MatchOne matches exactly one byte if the next byte in the input matches the given predicate.

func MatchOneRune

func MatchOneRune(t ATag, cs []byte, c rune) (*Match, []byte)

MatchOneRune matches the next byte if it exactly matches the given rune.

func (*Match) Length

func (m *Match) Length() int

Length returns the number of bytes matched for this match.

type Matcher

type Matcher func(cs []byte) (*Match, []byte)

Matcher is the type for matching functions. These accept a list of bytes to start matching from and return a pointer to a Match and a list of remaining unmatched bytes.

If the match is successful, then a pointer to a Match is returned and the remaining input is also returned. It is possible for a match to match zero bytes.

If the match fails, then the Match should be returned as nil. Usually the remaining input is also returned as nil in that case.

Jump to

Keyboard shortcuts

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