Documentation
¶
Overview ¶
Package parser provides a set of combinators for building parsers in Go.
Package parser provides the basic types and functions for parsing operations.
Package parser provides a set of combinators for building parsers.
Package parser provides utility types for working with optional values and tuples.
Index ¶
- type Maybe
- type Parser
- func Alpha() Parser[rune]
- func Alphas() Parser[string]
- func Between[T, U, V any](p Parser[T], q Parser[U], r Parser[V]) Parser[U]
- func Bind[T, U any](p Parser[T], f func(T) Parser[U]) Parser[U]
- func Char(c rune) Parser[rune]
- func Digit() Parser[rune]
- func Digits() Parser[string]
- func Fail[T any]() Parser[T]
- func Float() Parser[float64]
- func FloatWithoutSign() Parser[float64]
- func Fmap[T, U any](p Parser[T], f func(T) U) Parser[U]
- func Integer() Parser[int64]
- func IntegerWithoutSign() Parser[int64]
- func Lazy[T any](f func() Parser[T]) Parser[T]
- func NewParser[T any](parse ParserFunc[T]) Parser[T]
- func OmitLeft[T, U any](p Parser[T], q Parser[U]) Parser[U]
- func OmitRight[T, U any](p Parser[T], q Parser[U]) Parser[T]
- func OneOrMore[T any](p Parser[T]) Parser[[]T]
- func OrElse[T any](ps ...Parser[T]) Parser[T]
- func Pure[T any](val T) Parser[T]
- func Satisfy(f func(rune) bool) Parser[rune]
- func SatisfyWith[T any](p Parser[T], f func(T) bool) Parser[T]
- func SepBy[T, U any](p Parser[T], sep Parser[U]) Parser[[]T]
- func Seq[T any](ps ...Parser[T]) Parser[[]T]
- func Sign() Parser[rune]
- func Space() Parser[rune]
- func Spaces() Parser[string]
- func Str(str string) Parser[string]
- func String() Parser[string]
- func Symbol(str string) Parser[string]
- func Trim[T any](p Parser[T]) Parser[T]
- func TrimLeft[T any](p Parser[T]) Parser[T]
- func TrimRight[T any](p Parser[T]) Parser[T]
- func ZeroOrMore[T any](p Parser[T]) Parser[[]T]
- func ZeroOrOne[T any](p Parser[T]) Parser[Maybe[T]]
- type ParserFunc
- type ParserFuncRet
- type Tuple
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Maybe ¶
type Maybe[T any] struct { // contains filtered or unexported fields }
Maybe is a generic type that represents an optional value. It can either hold a value (Just) or be empty (Nothing).
func Just ¶
Just creates a new Maybe instance that holds a value. It takes a value of type T and returns a Maybe[T] containing that value.
func Nothing ¶
Nothing creates a new Maybe instance that represents the absence of a value. It returns a Maybe[T] with a nil value pointer.
func (Maybe[T]) Get ¶
func (o Maybe[T]) Get() T
Get retrieves the value from the Maybe instance. If the Maybe is Nothing, it returns the zero value of type T.
type Parser ¶
type Parser[T any] struct { // Parse is the parsing function that attempts to parse a string and returns a ParserFuncRet[T]. Parse ParserFunc[T] }
Parser is a generic struct that encapsulates a parsing function. It provides a unified interface for different parsing operations.
func Alpha ¶
Alpha creates a parser that matches a single alphabetic character (either uppercase or lowercase).
Returns: - A parser that matches a single alphabetic character.
func Alphas ¶
Alphas creates a parser that matches one or more alphabetic characters and returns them as a string.
Returns: - A parser that matches one or more alphabetic characters.
func Between ¶
Between parses a value between two other values and returns the middle value. It takes a parser p of type T, a parser q of type U, and a parser r of type V, and returns a new parser that produces a result of type U.
func Bind ¶
Bind sequences two parsers where the second parser depends on the result of the first. It takes a parser p of type T and a function f that maps T to a parser of type U, and returns a new parser that produces a result of type U.
func Char ¶
Char creates a parser that matches a single character if it is equal to the given character.
Parameters: - c: The character to match.
Returns: - A parser that matches the given character.
func Digit ¶
Digit creates a parser that matches a single digit character.
Returns: - A parser that matches a single digit character.
func Digits ¶
Digits creates a parser that matches one or more digit characters and returns them as a string.
Returns: - A parser that matches one or more digit characters.
func Fail ¶
Fail creates a parser that always fails without consuming any input.
Returns: - A parser that always fails.
func Float ¶
Float creates a parser that matches an optional sign followed by a floating-point number and returns the resulting float.
Returns: - A parser that matches an optional sign followed by a floating-point number.
func FloatWithoutSign ¶
FloatWithoutSign creates a parser that matches a floating-point number without a sign.
Returns: - A parser that matches a floating-point number without a sign.
func Fmap ¶
Fmap applies a function to the result of a parser. It takes a parser p of type T and a function f that maps T to U, and returns a new parser that produces a result of type U.
func Integer ¶
Integer creates a parser that matches an optional sign followed by one or more digits and returns the resulting integer.
Returns: - A parser that matches an optional sign followed by one or more digits.
func IntegerWithoutSign ¶
IntegerWithoutSign creates a parser that matches one or more digits and returns them as an integer.
Returns: - A parser that matches one or more digits and returns them as an integer.
func Lazy ¶
Lazy defers the creation of a parser until it is needed. It takes a function f that returns a parser of type T and returns a new parser of type T.
func NewParser ¶
func NewParser[T any](parse ParserFunc[T]) Parser[T]
NewParser creates a new Parser instance with the given parsing function. It takes a ParserFunc[T] as input and returns a Parser[T] instance.
func OmitLeft ¶
OmitLeft runs two parsers in sequence and discards the result of the first. It takes a parser p of type T and a parser q of type U, and returns a new parser of type U.
func OmitRight ¶
OmitRight runs two parsers in sequence and discards the result of the second. It takes a parser p of type T and a parser q of type U, and returns a new parser of type T.
func OneOrMore ¶
OneOrMore matches one or more occurrences of a parser. It takes a parser p of type T and returns a new parser that produces a slice of type T.
func OrElse ¶
OrElse tries a sequence of parsers in order and returns the result of the first successful one. It takes a variable number of parsers of type T and returns a new parser of type T.
func Pure ¶
Pure creates a parser that always succeeds without consuming any input and returns the given value.
Parameters: - val: The value to be returned by the parser.
Returns: - A parser that always succeeds and returns the given value.
func Satisfy ¶
Satisfy parses a single rune that satisfies a given predicate. It takes a function f that tests a rune and returns a new parser that produces a rune.
func SatisfyWith ¶
SatisfyWith applies a predicate to the result of a parser and succeeds only if the predicate is true. It takes a parser p of type T and a function f that tests T, and returns a new parser of type T.
func SepBy ¶
SepBy parses a sequence of elements separated by a separator. It takes a parser p of type T and a parser sep of type U, and returns a new parser that produces a slice of type T.
func Seq ¶
Seq parses a sequence of parsers in order and returns a slice of their results. It takes a variable number of parsers of type T and returns a new parser that produces a slice of type T.
func Sign ¶
Sign creates a parser that matches an optional sign character ('+' or '-') and returns it. If no sign is present, it returns '+'.
Returns: - A parser that matches an optional sign character.
func Space ¶
Space creates a parser that matches a single whitespace character (space, tab, or newline).
Returns: - A parser that matches a single whitespace character.
func Spaces ¶
Spaces creates a parser that matches zero or more whitespace characters and returns them as a string.
Returns: - A parser that matches zero or more whitespace characters.
func Str ¶
Str creates a parser that matches a given string at the beginning of the input.
Parameters: - str: The string to match.
Returns: - A parser that matches the given string.
func String ¶
String creates a parser that matches a double-quoted string, handling escape sequences.
Returns: - A parser that matches a double-quoted string.
func Symbol ¶
Symbol creates a parser that matches a given string surrounded by optional whitespace.
Parameters: - str: The string to match.
Returns: - A parser that matches the given string surrounded by optional whitespace.
func Trim ¶
Trim removes leading and trailing whitespace from the result of a parser. It takes a parser p of type T and returns a new parser of type T.
func TrimLeft ¶
TrimLeft removes leading whitespace from the result of a parser. It takes a parser p of type T and returns a new parser of type T.
func TrimRight ¶
TrimRight removes trailing whitespace from the result of a parser. It takes a parser p of type T and returns a new parser of type T.
func ZeroOrMore ¶
ZeroOrMore matches zero or more occurrences of a parser. It takes a parser p of type T and returns a new parser that produces a slice of type T.
type ParserFunc ¶
type ParserFunc[T any] func(string) ParserFuncRet[T]
ParserFunc is a function type that takes a string as input and returns a ParserFuncRet[T]. It represents a parsing function that attempts to parse the input string and returns the result.
type ParserFuncRet ¶
ParserFuncRet is an alias for Maybe[Tuple[T, string]], representing the return type of a parser function. It encapsulates the result of a parsing operation, which may contain a value of type T and the remaining unparsed string.