parse

package
v0.0.0-...-c3901be Latest Latest
Warning

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

Go to latest
Published: Dec 17, 2024 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Byte

func Byte[T ~byte](in string) (T, error)

Byte parses a single byte.

func Rune

func Rune(in string) (rune, error)

Rune parses a single UTF-8 codepoint.

func Signed

func Signed[T constraints.Signed](in string) (T, error)

Signed parses a signed number using strconv.ParseInt.

func String

func String[T ~string](in string) (T, error)

String parses any string as itself.

func Unsigned

func Unsigned[T constraints.Unsigned](in string) (T, error)

Unsigned parses an unsigned number using strconv.ParseUint.

Types

type Parser

type Parser[T any] func(string) (T, error)

A Parser is a generic, composable parser.

func Any

func Any[T any](p ...any) Parser[T]

Any runs a set of parser, one after the other, returning the first succesful result. The parsers don't have to return the same type, but they must return something assignable to T.

func Array

func Array[A, T any](s split.Func, p Parser[T]) Parser[A]

Array splits a string and calls p for each piece. A must be an array type with element type T.

func Blocks

func Blocks[T any](p Parser[T]) Parser[[]T]

Blocks is a shorthand for Slice(split.Blocks, …).

func Enum

func Enum[T ~byte | ~rune | ~string](opts ...T) Parser[T]

Enum parses as any of opts.

func Fields

func Fields[T any](p Parser[T]) Parser[[]T]

Fields is a shorthand for Slice(split.Fields, …).

func Lines

func Lines[T any](p Parser[T]) Parser[[]T]

Lines is a shorthand for Slice(split.Lines, …).

func Map

func Map[K comparable, V any](split, cut split.Func, k Parser[K], v Parser[V]) Parser[map[K]V]

Map parses a map. It uses split to separate the input into key/value pairs and then uses cut to cut them up into a key and a value, which are parsed using k and v respectively.

It errors if a key is duplicated.

func MapParser

func MapParser[A, B any](p Parser[A], f func(A) (B, error)) Parser[B]

MapParser converts a Parser[A] into a Parser[B] using f.

func Prefix

func Prefix[T any](prefix string, p Parser[T]) Parser[T]

Prefix expects a prefix and parses the rest of the string using p.

func Slice

func Slice[T any](s split.Func, p Parser[T]) Parser[[]T]

Slice splits a string and calls p on each piece.

func Struct

func Struct[S any](s split.Func, fields ...any) Parser[S]

Struct splits a string and parses the result into the fields of a struct using the given parsers.

S must be a struct type with the same number of exported fields as split returns. The variadic fields argument must all be of type Parser[T] and T must match the exported field of S in the same sequence.

func TrimSpace

func TrimSpace[T any](p Parser[T]) Parser[T]

TrimSpace removes all leading and trailing white space from the input and passes the result to p.

func (Parser[T]) Parse

func (p Parser[T]) Parse(r io.Reader) (T, error)

Parse the reader, returning the result.

Jump to

Keyboard shortcuts

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