parser

package
v0.0.0-...-1dacd80 Latest Latest
Warning

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

Go to latest
Published: Apr 15, 2022 License: Apache-2.0, MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrExpected indicates an expected value.
	ErrExpected = errors.New("expected")

	// ErrUnexpected indicates a unexpected value.
	ErrUnexpected = errors.New("unexpected")
)

Functions

func FormatRange

func FormatRange[T stream.Token](r []T) string

func FormatToken

func FormatToken[T stream.Token](t T) string

Types

type EOI

type EOI struct{}

func (EOI) ErrorInfo

func (i EOI) ErrorInfo() string

type ErrorInfo

type ErrorInfo interface {
	ErrorInfo() string
}

type Func

type Func[T stream.Token, O any] func(input []T) (out O, remaining []T, err error)

Func is a function that it can be used to parse an input stream `S` of token `T` into a `O` value.

func Expected

func Expected[T stream.Token, O any](parser Func[T, O], msg string) Func[T, O]

Expected parses with `parser` and if it fails without consuming any input any expected errors are replaced by `msg`. `msg` is then used in error messages as "Expected `msg`".

func MapErr

func MapErr[T stream.Token, O any](p Func[T, O], f func(error) error) Func[T, O]

MapErr uses `f` to map over the parser `p` error.

func Message

func Message[T stream.Token, O any](parser Func[T, O], msg string) Func[T, O]

Message parses with `parser` and if it fails, adds the message `msg` to the error.

func (Func[T, O]) AndThen

func (f Func[T, O]) AndThen(fn func(O) (O, error)) Func[T, O]

AndThen parses with `f` and applies `fn` on the result if `parser` parses successfully. `fn` may optionally fail with an error.

func (Func[T, O]) Expected

func (f Func[T, O]) Expected(msg string) Func[T, O]

Expected parses with `f` and if it fails without consuming any input any expected errors are replaced by `msg`. `msg` is then used in error messages as "Expected `msg`".

func (Func[T, O]) Map

func (f Func[T, O]) Map(fn func(O) O) Func[T, O]

Map uses `fn` to map over the parsed value.

func (Func[T, O]) MapErr

func (f Func[T, O]) MapErr(fn func(error) error) Func[T, O]

MapErr uses `fn` to map over the error.

func (Func[T, O]) Message

func (f Func[T, O]) Message(msg string) Func[T, O]

Message parses with `f` and if it fails, adds the message `msg` to the error.

func (Func[T, O]) Parse

func (f Func[T, O]) Parse(input []T) (out O, remaining []T, err error)

func (Func[T, O]) Spanned

func (f Func[T, O]) Spanned() Func[T, O]

Spanned marks errors produced inside the `f` parser with the span from the start of the parse to the end of it.

type MessageInfo

type MessageInfo string

func (MessageInfo) ErrorInfo

func (i MessageInfo) ErrorInfo() string

type Parser

type Parser[T stream.Token, O any] interface {
	Parse(input []T) (out O, remaining []T, err error)
}

Parser is a type that it can be used to parse an input stream `S` of token `T` into a `O` value.

type RangeInfo

type RangeInfo[T stream.Token] struct {
	Expected []T
	Actual   []T
}

func (*RangeInfo[T]) ErrorInfo

func (i *RangeInfo[T]) ErrorInfo() string

type Spanned

type Spanned struct {
	Start, End stream.Offset
	Err        error
}

func (*Spanned) Error

func (s *Spanned) Error() string

type TokenInfo

type TokenInfo[T stream.Token] struct {
	Expected *T
	Actual   T
}

func (*TokenInfo[T]) ErrorInfo

func (i *TokenInfo[T]) ErrorInfo() string

type UnexpectedErr

type UnexpectedErr[E ErrorInfo] struct {
	Info E
}

UnexpectedErr is an error that indicates an unexpected value.

func UnexpectedEOI

func UnexpectedEOI() *UnexpectedErr[EOI]

Unexpected returns an error that indicates an unexpected EOI.

func UnexpectedMessage

func UnexpectedMessage(msg string) *UnexpectedErr[MessageInfo]

Unexpected returns an error that indicates an unexpected message.

func UnexpectedRange

func UnexpectedRange[T stream.Token](expected, actual []T) *UnexpectedErr[*RangeInfo[T]]

Unexpected returns an error that indicates an unexpected range.

func UnexpectedToken

func UnexpectedToken[T stream.Token](actual T, x ...T) *UnexpectedErr[*TokenInfo[T]]

Unexpected returns an error that indicates an unexpected value.

func (*UnexpectedErr[E]) Err

func (e *UnexpectedErr[E]) Err() error

func (*UnexpectedErr[E]) Error

func (e *UnexpectedErr[E]) Error() string

Directories

Path Synopsis
be
le
num

Jump to

Keyboard shortcuts

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