Documentation ¶
Index ¶
- func LineAndColOf(input, clip []rune) (line, col int)
- func ParseDotEnvFile(envFileBytes []byte) (map[string]string, error)
- type Context
- func (pCtx Context) CustomImporter(fn func(name string) ([]byte, error)) Context
- func (pCtx Context) Deactivated() Context
- func (pCtx Context) DisabledImports() Context
- func (pCtx Context) HasNamedContext(name string) bool
- func (pCtx Context) InitFunction(name string, args *query.ParsedParams) (query.Function, error)
- func (pCtx Context) InitMethod(name string, target query.Function, args *query.ParsedParams) (query.Function, error)
- func (pCtx Context) WithImporter(importer Importer) Context
- func (pCtx Context) WithImporterRelativeToFile(pathStr string) Context
- func (pCtx Context) WithNamedContext(name string) Context
- type DelimitedResult
- type Error
- func (e *Error) Add(from *Error)
- func (e *Error) Error() string
- func (e *Error) ErrorAtChar(input []rune) string
- func (e *Error) ErrorAtPosition(input []rune) string
- func (e *Error) ErrorAtPositionStructured(filepath string, input []rune) string
- func (e *Error) IsFatal() bool
- func (e *Error) Unwrap() error
- type Func
- func Array() Func
- func BestMatch(parsers ...Func) Func
- func Boolean() Func
- func Char(c rune) Func
- func Comment() Func
- func Delimited(primary, delimiter Func) Func
- func DelimitedPattern(start, primary, delimiter, stop Func, allowTrailing bool) Func
- func Discard(parser Func) Func
- func DiscardAll(parser Func) Func
- func EmptyLine() Func
- func EndOfInput() Func
- func Expect(parser Func, expected ...string) Func
- func InRange(lower, upper rune) Func
- func InSet(set ...rune) Func
- func JoinStringPayloads(p Func) Func
- func LiteralValue() Func
- func MustBe(parser Func) Func
- func Newline() Func
- func NewlineAllowComment() Func
- func NotChar(c rune) Func
- func NotInSet(set ...rune) Func
- func Nothing() Func
- func Null() Func
- func Number() Func
- func Object() Func
- func OneOf(parsers ...Func) Func
- func Optional(parser Func) Func
- func QuotedString() Func
- func Sequence(parsers ...Func) Func
- func SnakeCase() Func
- func SpacesAndTabs() Func
- func Term(term string) Func
- func TripleQuoteString() Func
- func UntilFail(parser Func) Func
- func UntilTerm(term string) Func
- type ImportError
- type Importer
- type Result
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func LineAndColOf ¶
LineAndColOf returns the line and column position of a tailing clip from an input.
Types ¶
type Context ¶
type Context struct { Functions *query.FunctionSet Methods *query.MethodSet // contains filtered or unexported fields }
Context contains context used throughout a Bloblang parser for accessing function and method constructors.
func EmptyContext ¶
func EmptyContext() Context
EmptyContext returns a parser context with no functions, methods or import capabilities.
func GlobalContext ¶
func GlobalContext() Context
GlobalContext returns a parser context with globally defined functions and methods.
func (Context) CustomImporter ¶
CustomImporter returns a version of the parser context where file imports are done exclusively through a provided closure function, which takes an import path (relative or absolute).
func (Context) Deactivated ¶
Deactivated returns a version of the parser context where all functions and methods exist but can no longer be instantiated. This means it's possible to parse and validate mappings but not execute them. If the context also has an importer then it will also be replaced with an implementation that always returns empty files.
func (Context) DisabledImports ¶
DisabledImports returns a version of the parser context where file imports are entirely disabled. Any import statement within parsed mappings will return parse errors explaining that file imports are disabled.
func (Context) HasNamedContext ¶
HasNamedContext returns true if a given name exists as a named context.
func (Context) InitFunction ¶
InitFunction attempts to initialise a function from the available constructors of the parser context.
func (Context) InitMethod ¶
func (pCtx Context) InitMethod(name string, target query.Function, args *query.ParsedParams) (query.Function, error)
InitMethod attempts to initialise a method from the available constructors of the parser context.
func (Context) WithImporter ¶
WithImporter returns a Context where imports are made from the provided Importer implementation.
func (Context) WithImporterRelativeToFile ¶
WithImporterRelativeToFile returns a Context where any relative imports will be made from the directory of the provided file path. The provided path can itself be relative (to the current importer directory) or absolute.
func (Context) WithNamedContext ¶
WithNamedContext returns a Context with a named execution context.
type DelimitedResult ¶
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 ¶
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 ¶
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 ¶
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 ParseField ¶
func ParseField(pCtx Context, expr string) (*field.Expression, *Error)
ParseField attempts to parse a field expression.
func ParseMapping ¶
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) ErrorAtChar ¶
ErrorAtChar returns a human readable error string including the character position of the error.
func (*Error) ErrorAtPosition ¶
ErrorAtPosition returns a human readable error string including the line and character position of the error.
func (*Error) ErrorAtPositionStructured ¶
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.
type Func ¶
Func is the common signature of a parser function.
func BestMatch ¶
BestMatch accepts one or more parsers and tries them all against an input. If all parsers return either a result or an error 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 Delimited ¶
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 ¶
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 ¶
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 ¶
DiscardAll the results of a child parser, applied until it fails. This has the effect of running the parser and returning only Remaining.
func EmptyLine ¶
func EmptyLine() Func
EmptyLine ensures that a line is empty, but doesn't advance the parser beyond the newline char.
func Expect ¶
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 JoinStringPayloads ¶
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 ¶
MustBe applies a parser and if the result is a non-fatal error then it is upgraded to a fatal one.
func NewlineAllowComment ¶
func NewlineAllowComment() Func
NewlineAllowComment parses an optional comment followed by a mandatory line break.
func NotInSet ¶
NotInSet parses any number of characters until a rune within a given set is encountered.
func Nothing ¶
func Nothing() Func
Nothing is a parser that always returns an error. This can be useful for disabling parsers.
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 OneOf ¶
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 ¶
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 ¶
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 TripleQuoteString ¶
func TripleQuoteString() Func
TripleQuoteString parses a single instance of a triple-quoted multiple line string. The result is the inner contents.
type ImportError ¶
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 ¶
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 ¶
func (i *ImportError) Error() string
Error implements the standard error interface.
type Importer ¶
type Importer interface { // Import a file from a relative or absolute path. Import(pathStr string) ([]byte, error) // Derive a new importer where relative import paths are resolved from the // directory of the provided file path. The provided path could be absolute, // or relative itself in which case it should be resolved from the // pre-existing relative directory. RelativeToFile(filePath string) Importer }
Importer represents a repository of bloblang files that can be imported by mappings. It's possible for mappings to import files using relative paths, if the import is from a mapping which was itself imported then the path should be interpretted as relative to that file.