parse

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

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

Go to latest
Published: Jul 11, 2023 License: Apache-2.0 Imports: 2 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNoMatch indicates parsing failed against the token stream.
	ErrNoMatch = errors.New("no match")

	// ErrNonExhaustiveParse indicates parsing completed but there were
	// still tokens remaining in the input stream.
	ErrNonExhaustiveParse = errors.New("did not consume all tokens")
)

Functions

func Parse

func Parse[T any](parser Parser[T], input []token.Token) (T, error)

Parse applies the given parser against the input stream.

Types

type Empty

type Empty struct{}

Empty is an empty type to return when a parser successfully parsed the input stream and nothing should be returned.

type Parser

type Parser[T any] func(state) (T, state, error)

Parser is a Parser function that can read an input token stream and produce the specified type or an error.

func All2

func All2[A any, B any](first Parser[A], second Parser[B]) Parser[Seq2[A, B]]

All2 requires both parsers to succeed and it returns a tuple of the parsed objects.

func All3

func All3[A any, B any, C any](first Parser[A], second Parser[B], third Parser[C]) Parser[Seq3[A, B, C]]

All3 requires all three parsers to succeed and it returns a tuple of the parsed objects.

func ConsumeAtLeastOne

func ConsumeAtLeastOne[T any](parser Parser[T]) Parser[[]T]

ConsumeAtLeastOne attempts to parse at least once with the given parser and then continues until it fails to parse.

func Exactly

func Exactly(tok token.Token) Parser[Empty]

Exactly consumes the exact token from the input stream or fails.

func ManyOf

func ManyOf[T any](parser Parser[T]) Parser[[]T]

ManyOf attempts to successfully parse 0 or more times.

func Map

func Map[T any, U any](parser Parser[T], mapper func(T) U) Parser[U]

Map converts one parser to another parser.

func MatchesToken

func MatchesToken(matcher func(token.Token) bool) Parser[token.Token]

MatchesToken consumes the token from the input stream if matcher returns true.

func Nothing

func Nothing() Parser[Empty]

Nothing is a parser that always succeeds and doesn't consume input.

func OneOf

func OneOf[T any](parsers ...Parser[T]) Parser[T]

OneOf tries a list of parsers and if one of them succeeds, returns that parser.

type Seq2

type Seq2[A any, B any] struct {
	First  A
	Second B
}

Seq2 is a tuple of two parsed objects.

type Seq3

type Seq3[A any, B any, C any] struct {
	First  A
	Second B
	Third  C
}

Seq3 is a tuple of three parsed objects.

Jump to

Keyboard shortcuts

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