parser

package
v3.37.0 Latest Latest
Warning

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

Go to latest
Published: Jan 6, 2021 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func LineAndColOf added in v3.25.0

func LineAndColOf(input, clip []rune) (int, int)

LineAndColOf returns the line and column position of a tailing clip from an input.

Types

type DelimitedResult added in v3.30.0

type DelimitedResult struct {
	Primary   []interface{}
	Delimiter []interface{}
}

DelimitedResult is an explicit result struct returned by the Delimited parser, containing a slice of primary parser payloads and a slice of delimited parser payloads.

type Error added in v3.25.0

type Error struct {
	Input    []rune
	Err      error
	Expected []string
}

Error represents an error that has occurred whilst attempting to apply a parser function to a given input. A slice of abstract names should be provided outlining tokens or characters that were expected and not found at the input in order to provide a useful error message.

The input at the point of the error can be used in order to infer where exactly in the input the error occurred with len(input) - len(err.Input).

func NewError added in v3.25.0

func NewError(input []rune, expected ...string) *Error

NewError creates a parser error from the input and a list of expected tokens. This is a passive error indicating that this particular parser did not succeed, but that other parsers should be tried if applicable.

func NewFatalError added in v3.25.0

func NewFatalError(input []rune, err error, expected ...string) *Error

NewFatalError creates a parser error from the input and a wrapped fatal error indicating that this parse succeeded partially, but a requirement was not met that means the parsed input is invalid and that all parsing should stop.

func NewQuery added in v3.25.0

func NewQuery(queryStr string) (query.Function, *Error)

NewQuery parses a new query function from a query string.

func ParseField added in v3.25.0

func ParseField(expr string) (field.Expression, *Error)

ParseField attempts to parse a field expression.

func ParseMapping added in v3.25.0

func ParseMapping(filepath string, expr string) (*mapping.Executor, *Error)

ParseMapping parses a bloblang mapping and returns an executor to run it, or an error if the parsing fails.

The filepath is optional and used for relative file imports and error messages.

func (*Error) Add added in v3.25.0

func (e *Error) Add(from *Error)

Add context from another error into this one.

func (*Error) Error added in v3.25.0

func (e *Error) Error() string

Error returns a human readable error string.

func (*Error) ErrorAtChar added in v3.25.0

func (e *Error) ErrorAtChar(input []rune) string

ErrorAtChar returns a human readable error string including the character position of the error.

func (*Error) ErrorAtPosition added in v3.25.0

func (e *Error) ErrorAtPosition(input []rune) string

ErrorAtPosition returns a human readable error string including the line and character position of the error.

func (*Error) ErrorAtPositionStructured added in v3.25.0

func (e *Error) ErrorAtPositionStructured(filepath string, input []rune) string

ErrorAtPositionStructured returns a human readable error string including the line and character position of the error formatted in a more structured way, this message isn't appropriate to write within structured logs as the formatting will be broken.

func (*Error) IsFatal added in v3.25.0

func (e *Error) IsFatal() bool

IsFatal returns whether this parser error should be considered fatal, and therefore sibling parser candidates should not be tried.

func (*Error) Unwrap added in v3.25.0

func (e *Error) Unwrap() error

Unwrap returns the underlying fatal error (or nil).

type Func added in v3.29.0

type Func func([]rune) Result

Func is the common signature of a parser function.

func Array

func Array() Func

Array parses an array literal.

func BestMatch

func BestMatch(parsers ...Func) Func

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() Func

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

func Char

func Char(c rune) Func

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

func Comment

func Comment() Func

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

func Delimited

func Delimited(primary, delimiter Func) Func

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

func DelimitedPattern(
	start, primary, delimiter, stop Func,
	allowTrailing bool,
) Func

DelimitedPattern attempts to parse zero or more primary parsers in between a 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.

func Discard

func Discard(parser Func) Func

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

func DiscardAll(parser Func) Func

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

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

Expect applies a parser and if an error is returned the list of expected candidates is replaced with the given strings. This is useful for providing better context to users.

func InRange

func InRange(lower, upper rune) Func

InRange parses any number of characters between two runes inclusive.

func InSet

func InSet(set ...rune) Func

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

func JoinStringPayloads

func JoinStringPayloads(p Func) Func

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

func LiteralValue() Func

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

func MustBe

func MustBe(parser Func) Func

MustBe applies a parser and if the result is a non-fatal error then it is upgraded to a fatal one.

func Newline

func Newline() Func

Newline parses a line break.

func NewlineAllowComment

func NewlineAllowComment() Func

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

func NotChar

func NotChar(c rune) Func

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

func Null

func Null() Func

Null parses a null literal value.

func Number

func Number() Func

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

func Object

func Object() Func

Object parses an object literal.

func OneOf

func OneOf(parsers ...Func) Func

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

func Optional(parser Func) Func

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() Func

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

func Sequence

func Sequence(parsers ...Func) Func

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

func SnakeCase

func SnakeCase() Func

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() Func

SpacesAndTabs parses any number of space or tab characters.

func Term

func Term(term string) Func

Term parses a single instance of a string.

func TripleQuoteString

func TripleQuoteString() Func

TripleQuoteString parses a single instance of a triple-quoted multiple line string. The result is the inner contents.

func UntilFail

func UntilFail(parser Func) Func

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.

type ImportError added in v3.25.0

type ImportError struct {
	// contains filtered or unexported fields
}

ImportError wraps a parser error with an import file path. When a fatal error wraps

func NewImportError added in v3.25.0

func NewImportError(path string, input []rune, err *Error) *ImportError

NewImportError wraps a parser error with a filepath for when a parser has attempted to parse content of an imported file.

func (*ImportError) Error added in v3.25.0

func (i *ImportError) Error() string

Error implements the standard error interface.

type Result

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

Result represents the result of a parser given an input.

func Fail added in v3.29.0

func Fail(err *Error, input []rune) Result

Fail creates a result with an error from failed parsing.

func ParseDeprecatedQuery added in v3.25.0

func ParseDeprecatedQuery(input []rune) Result

ParseDeprecatedQuery parses an input into a query.Function, but permits deprecated function interpolations. In order to support old functions this parser does not include field literals.

func ParseQuery added in v3.25.0

func ParseQuery(input []rune) Result

ParseQuery parses an input into a query.Function.

func Success added in v3.29.0

func Success(payload interface{}, remaining []rune) Result

Success creates a result with a payload from successful parsing.

Jump to

Keyboard shortcuts

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