Documentation ¶
Index ¶
- Constants
- Variables
- func Anything(_ rune) bool
- func IsAlphanumeric(r rune) bool
- func IsNewline(r rune) bool
- type Parser
- func Any(parsers ...Parser) Parser
- func Assign(p Parser, dst interface{}) Parser
- func ConsumeUntil(predicate Predicate) Parser
- func Delimited(start, end string, p Predicate) Parser
- func List(start, end, separators string) Parser
- func Many(n int, p Parser) Parser
- func Space(n int) Parser
- func Tag(s string) Parser
- func Transform(p Parser, fn TransformFunc) Parser
- type Predicate
- type Reader
- type Token
- type TransformFunc
Constants ¶
const Whitespace = "\t\n\v\f\r \u0085\u00a0"
Whitespace is a useful constant containing most common whitespace characters.
Variables ¶
var ( // ErrNoMatch is used as a sentinel error to indicate that the given parser // did not match what was in the input. ErrNoMatch = errors.New("no match") )
var ErrSkip = errors.New("skip")
ErrSkip is a sentinel error that can be used by TransformFuncs to indicate that the token being transformed should be dropped, but that transofmration should otherwise be considered successful.
Functions ¶
func Anything ¶ added in v0.2.0
Anything is a predicate function used to indicate all runes should be consumed.
func IsAlphanumeric ¶
IsAlphanumeric is a predicate function which returns true if r is a letter, or number.
Types ¶
type Parser ¶
Parser is a functional type that consumes data from a Reader, and returns a Token along with a parsing error (if any).
func Any ¶
Any returns a Parser that will run all parsers on the input. The first parser to return a Token and a non-nil error will consume the input. If none of the parsers match, Any will return ErrNoMatch.
func Assign ¶
Assign runs the parser p, and attempts to assign the value to dst. dst may be a pointer to a string, []byte, or []string or [][]byte.
If the caller wishes to convert the values to any other type, a custom transformation function should be written.
func ConsumeUntil ¶ added in v0.2.0
ConsumeUntil will consume all bytes from a reader until p returns true, or EOF.
func Delimited ¶
Delimited returns a parser that will consume all data matching the given predicate, between the start and end delimiters. The start and end delimiters will not be a part of the resulting data, but will be consumed.
func List ¶ added in v0.2.0
List parses a collection of elements from a reader, contained within the start and end delimiters. Elements are separated by any of the code points in separators.
func Many ¶
Many applies p at least n number of times, where p must apply at least n times.
If p returns io.EOF, Many will return immediately, with a nil error.
When n == 0, p is run continuously until the input source returns io.EOF.
func Space ¶
Space returns a parser that will consume at least n space and tab characters. The value of n is used as a required, minimum number of whitespace characters to consume. For example,
token, err := Space(2)(r)
would require there be at least 2 whitespace characters.
The caller should use Multispace if they wish to also capture newline and carriage return characters.
func Transform ¶ added in v0.2.0
func Transform(p Parser, fn TransformFunc) Parser
Transform passes the token returned from p, to the function fn.
For tokens returned from the List parser, fn will be called on each element. If fn returns ErrSkip, the token passed to fn should be dropped from the final list of tokens.
type Predicate ¶
Predicate is a function that can be used to match a rune.
Instead of providing a bunch of bespoke functions that satisfy this type, the type definition should cover most of the functions within the "unicode" package.
type Reader ¶
type Reader interface { Peek(int) ([]byte, error) Read([]byte) (int, error) ReadByte() (byte, error) }
Reader defines the interface of a data source for Parse. This is nothing more than a whittled-down list of methods exposed by types like bufio.Reader, limited to those that we actually use.
type Token ¶
Token defines the interface for any type that is parsed from an input. Users are encouraged to implement this interface on the types they wish to be able to populate from an input.
When a user implements the fmt.Stringer interface on their type, it should return the textual representation that was parsed to result in the object. There is no requirement that the textual representation be exactly the same as was used to instantiate the implementer.
type TransformFunc ¶ added in v0.2.0
TransformFunc is a function type that transforms one token into another.