parser

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Mar 22, 2025 License: MIT Imports: 2 Imported by: 0

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

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

func Just[T any](value T) Maybe[T]

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

func Nothing[T any]() Maybe[T]

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.

func (Maybe[T]) IsJust

func (o Maybe[T]) IsJust() bool

IsJust checks if the Maybe instance contains a value. It returns true if the Maybe is Just, false otherwise.

func (Maybe[T]) IsNothing

func (o Maybe[T]) IsNothing() bool

IsNothing checks if the Maybe instance is empty. It returns true if the Maybe is Nothing, false otherwise.

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

func Alpha() Parser[rune]

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

func Alphas() Parser[string]

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

func Between[T, U, V any](p Parser[T], q Parser[U], r Parser[V]) Parser[U]

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

func Bind[T, U any](p Parser[T], f func(T) Parser[U]) Parser[U]

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

func Char(c rune) Parser[rune]

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

func Digit() Parser[rune]

Digit creates a parser that matches a single digit character.

Returns: - A parser that matches a single digit character.

func Digits

func Digits() Parser[string]

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

func Fail[T any]() Parser[T]

Fail creates a parser that always fails without consuming any input.

Returns: - A parser that always fails.

func Float

func Float() Parser[float64]

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

func FloatWithoutSign() Parser[float64]

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

func Fmap[T, U any](p Parser[T], f func(T) U) Parser[U]

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

func Integer() Parser[int64]

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

func IntegerWithoutSign() Parser[int64]

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

func Lazy[T any](f func() Parser[T]) Parser[T]

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

func OmitLeft[T, U any](p Parser[T], q Parser[U]) Parser[U]

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

func OmitRight[T, U any](p Parser[T], q Parser[U]) Parser[T]

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

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

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

func OrElse[T any](ps ...Parser[T]) Parser[T]

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

func Pure[T any](val T) Parser[T]

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

func Satisfy(f func(rune) bool) Parser[rune]

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

func SatisfyWith[T any](p Parser[T], f func(T) bool) Parser[T]

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

func SepBy[T, U any](p Parser[T], sep Parser[U]) Parser[[]T]

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

func Seq[T any](ps ...Parser[T]) Parser[[]T]

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

func Sign() Parser[rune]

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

func Space() Parser[rune]

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

func Spaces() Parser[string]

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

func Str(str string) Parser[string]

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

func String() Parser[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

func Symbol(str string) Parser[string]

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

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

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

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

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

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

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

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

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.

func ZeroOrOne

func ZeroOrOne[T any](p Parser[T]) Parser[Maybe[T]]

ZeroOrOne matches zero or one occurrence of a parser. It takes a parser p of type T and returns a new parser that produces a Maybe type of 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

type ParserFuncRet[T any] = Maybe[Tuple[T, string]]

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.

type Tuple

type Tuple[U, V any] struct {
	// First is the first value in the tuple.
	First U
	// Second is the second value in the tuple.
	Second V
}

Tuple is a generic type that represents a pair of values. It holds two values of types U and V, accessible via the First and Second fields.

func NewTuple

func NewTuple[U, V any](first U, second V) Tuple[U, V]

NewTuple creates a new Tuple instance with the given values. It takes two values of types U and V and returns a Tuple[U, V] containing those values.

Jump to

Keyboard shortcuts

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