parser

package
v3.15.0 Latest Latest
Warning

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

Go to latest
Published: May 24, 2020 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ExpectedError

type ExpectedError []string

ExpectedError represents a parser error where one of a list of possible tokens was expected but not found.

func (ExpectedError) Error

func (e ExpectedError) Error() string

Error returns a human readable error string.

type ExpectedFatalError added in v3.13.0

type ExpectedFatalError []string

ExpectedFatalError represents a parser error where one of a list of possible tokens was expected but not found, and this is a fatal error.

func (ExpectedFatalError) Error added in v3.13.0

func (e ExpectedFatalError) Error() string

Error returns a human readable error string.

type PositionalError

type PositionalError struct {
	Position int
	Err      error
}

PositionalError represents an error that has occurred at a particular position in the input.

func ErrAtPosition

func ErrAtPosition(i int, err error) PositionalError

ErrAtPosition takes an error and returns a positional wrapper. If the provided error is itself a positional type then the position is aggregated.

func (PositionalError) Error

func (e PositionalError) Error() string

Error returns a human readable error string.

func (PositionalError) Expand

func (e PositionalError) Expand(fn func(error) error) PositionalError

Expand the underlying error with more context.

func (PositionalError) Unwrap

func (e PositionalError) Unwrap() error

Unwrap returns the underlying error.

type Result

type Result struct {
	Payload   interface{}
	Err       error
	Remaining []rune
}

Result represents the result of a parser given an input.

type Type

type Type func([]rune) Result

Type is a general parser method.

func Array added in v3.13.0

func Array() Type

Array parses an array literal.

func BestMatch added in v3.13.0

func BestMatch(parsers ...Type) Type

BestMatch accepts one or more parsers and tries them all against an input. If any parser returns a non ExpectedError error then it is returned. If all parsers return either a result or an ExpectedError then the parser that got further through the input will have its result returned. This means that an error may be returned even if a parser was successful.

For example, given two parsers, A searching for 'aa', and B searching for 'aaaa', if the input 'aaab' were provided then an error from parser B would be returned, as although the input didn't match, it matched more of parser B than parser A.

func Boolean

func Boolean() Type

Boolean parses either 'true' or 'false' into a boolean value.

func Char

func Char(c rune) Type

Char parses a single character and expects it to match one candidate.

func Comment added in v3.13.0

func Comment() Type

Comment parses a # comment (always followed by a line break).

func Delimited added in v3.13.0

func Delimited(primary, delimiter Type) Type

Delimited attempts to parse one or more primary parsers, where after the first parse a delimiter is expected. Parsing is stopped only once a delimiter parse is not successful.

Two slices are returned, the first element being a slice of primary results and the second element being the delimiter results.

func DelimitedPattern added in v3.13.0

func DelimitedPattern(
	start, primary, delimiter, stop Type,
	allowTrailing, returnDelimiters bool,
) Type

DelimitedPattern attempts to parse zero or more primary parsers in between an start and stop parser, where after the first parse a delimiter is expected. Parsing is stopped only once an explicit stop parser is successful.

If allowTrailing is set to false and a delimiter is parsed but a subsequent primary parse fails then an error is returned.

Only the results of the primary parser are returned, the results of the start, delimiter and stop parsers are discarded. If returnDelimiters is set to true then two slices are returned, the first element being a slice of primary results and the second element being the delimiter results.

func Discard added in v3.13.0

func Discard(parser Type) Type

Discard the result of a child parser, regardless of the result. This has the effect of running the parser and returning only Remaining.

func DiscardAll added in v3.13.0

func DiscardAll(parser Type) Type

DiscardAll the results of a child parser, applied until it fails. This has the effect of running the parser and returning only Remaining.

func Expect added in v3.14.0

func Expect(parser Type, expected ...string) Type

Expect applies a parser and if an ExpectedError (or ExpectedFatalError) is returned its contents are replaced with the provided list. This is useful for providing better context to users.

func InRange

func InRange(lower, upper rune) Type

InRange parses any number of characters between two runes inclusive.

func InSet

func InSet(set ...rune) Type

InSet parses any number of characters within a set of runes.

func JoinStringPayloads added in v3.14.0

func JoinStringPayloads(p Type) Type

JoinStringPayloads wraps a parser that returns a []interface{} of exclusively string values and returns a result of a joined string of all the elements.

Warning! If the result is not a []interface{}, or if an element is not a string, then this parser returns a zero value instead.

func LiteralValue added in v3.13.0

func LiteralValue() Type

LiteralValue parses a literal bool, number, quoted string, null value, array of literal values, or object.

func MustBe added in v3.13.0

func MustBe(parser Type) Type

MustBe applies a parser and if the result is an ExpectedError converts it into a fatal error in order to prevent fallback parsers during AnyOf.

func Newline

func Newline() Type

Newline parses a line break.

func NewlineAllowComment added in v3.13.0

func NewlineAllowComment() Type

NewlineAllowComment parses an optional comment followed by a mandatory line break.

func NotChar

func NotChar(c rune) Type

NotChar parses any number of characters until they match a single candidate.

func NotEnd

func NotEnd(p Type, exp ExpectedError) Type

NotEnd parses zero characters from an input and expects it to not have ended. An ExpectedError must be provided which provides the error returned on empty input.

func Null added in v3.13.0

func Null() Type

Null parses a null literal value.

func Number

func Number() Type

Number parses any number of numerical characters into either an int64 or, if the number contains float characters, a float64.

func Object added in v3.13.0

func Object() Type

Object parses an object literal.

func OneOf added in v3.14.0

func OneOf(Types ...Type) Type

OneOf accepts one or more parsers and tries them in order against an input. If a parser returns an ExpectedError then the next parser is tried and so on. Otherwise, the result is returned.

func Optional added in v3.13.0

func Optional(parser Type) Type

Optional applies a child parser and if it returns an ExpectedError then it is cleared and a nil result is returned instead. Any other form of error will be returned unchanged.

func QuotedString

func QuotedString() Type

QuotedString parses a single instance of a quoted string. The result is the inner contents unescaped.

func Sequence added in v3.13.0

func Sequence(parsers ...Type) Type

Sequence applies a sequence of parsers and returns either a slice of the results or an error if any parser fails.

func SnakeCase added in v3.13.0

func SnakeCase() Type

SnakeCase parses any number of characters of a camel case string. This parser is very strict and does not support double underscores, prefix or suffix underscores.

func SpacesAndTabs

func SpacesAndTabs() Type

SpacesAndTabs parses any number of space or tab characters.

func Term added in v3.14.0

func Term(str string) Type

Term parses a single instance of a string.

func UntilFail added in v3.14.0

func UntilFail(parser Type) Type

UntilFail applies a parser until it fails, and returns a slice containing all results. If the parser does not succeed at least once an error is returned.

Jump to

Keyboard shortcuts

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