parser

package module
v0.0.0-...-f6c63a6 Latest Latest
Warning

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

Go to latest
Published: Mar 11, 2024 License: MIT Imports: 5 Imported by: 0

README

Parser Combinator (Unofficial Golang port of Sprache)

move to https://github.com/omnius-labs/core-go/tree/main/parserc

Respect

https://github.com/sprache/Sprache

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Parser

type Parser[T any] func(ParserInput) ParserResult[T]

func AnyRune

func AnyRune() Parser[rune]

Parse any character.

func AtLeastOnce

func AtLeastOnce[T any](parser Parser[T]) Parser[[]T]

TryParse a stream of elements with at least one item.

func Concat

func Concat[T any](first, second Parser[[]T]) Parser[[]T]

Concatenate two streams of elements.

func Digit

func Digit() Parser[rune]

Parse a digit.

func End

func End[T any](parser Parser[T]) Parser[T]

Parse end-of-input.

func Except

func Except[T, U any](parser Parser[T], except Parser[U]) Parser[T]

Attempt parsing only if the except parser fails.

func IgnoreCase

func IgnoreCase(c rune) Parser[rune]

Parse a single character in a case-insensitive fashion.

func IgnoreCaseString

func IgnoreCaseString(s string) Parser[[]rune]

Parse a string in a case-insensitive fashion.

func Letter

func Letter() Parser[rune]

Parse a letter.

func LetterOrDigit

func LetterOrDigit() Parser[rune]

Parse a letter or digit.

func Lower

func Lower() Parser[rune]

Parse a lowercase letter.

func Many

func Many[T any](parser Parser[T]) Parser[[]T]

Parse a stream of elements.

func Not

func Not[T any](parser Parser[T]) Parser[T]

Constructs a parser that will fail if the given parser succeeds, and will succeed if the given parser fails. In any case, it won't consume any input. It's like a negative look-ahead in regex.

func Number

func Number() Parser[string]

Parse a number.

func Numeric

func Numeric() Parser[rune]

Parse a numeric character.

func Once

func Once[T any](parser Parser[T]) Parser[[]T]

Parse a stream of elements containing only one item.

func Or

func Or[T any](first Parser[T], second Parser[T]) Parser[T]

Parse first, if it succeeds, return first, otherwise try second.

func Return

func Return[T any](value T) Parser[T]

Succeed immediately and return value.

func ReturnValue

func ReturnValue[T, U any](parser Parser[T], value U) Parser[U]

Version of Return with simpler inline syntax.

func Rune

func Rune(c rune) Parser[rune]

Parse a single character c.

func RuneExcept

func RuneExcept(c rune) Parser[rune]

Parse a single character except c.

func RuneExceptFunc

func RuneExceptFunc(predicate func(rune) bool, description string) Parser[rune]

Parse a single character except those matching 'predicate'

func RuneFunc

func RuneFunc(predicate func(rune) bool, description string) Parser[rune]

TryParse a single character matching 'predicate'

func Runes

func Runes(rs ...rune) Parser[rune]

Parse a single character of any in rs

func RunesExcept

func RunesExcept(rs ...rune) Parser[rune]

Parses a single character except for those in rs

func RunesString

func RunesString(s string) Parser[rune]

Parse a single character of any in s

func RunesStringExcept

func RunesStringExcept(s string) Parser[rune]

Parses a single character except for those in s

func Select

func Select[T any, U any](parser Parser[T], convert func(T) U) Parser[U]

Take the result of parsing, and project it onto a different domain.

func SelectMany

func SelectMany[T, U, V any](parser Parser[T], selector func(T) Parser[U], projector func(T, U) V) Parser[V]

Monadic combinator Then, adapted for Linq comprehension syntax.

func SetExpectationIfError

func SetExpectationIfError[T any](parser Parser[T], expectation string) Parser[T]

Names part of the grammar for help with error messages.

func String

func String(s string) Parser[[]rune]

Parse a string of characters.

func Text

func Text(parser Parser[[]rune]) Parser[string]

Convert a stream of characters to a string.

func Then

func Then[T, U any](first Parser[T], second func(T) Parser[U]) Parser[U]

Parse first, and if successful, then parse second.

func Token

func Token[T any](parser Parser[T]) Parser[T]

Parse the token, embedded in any amount of whitespace characters.

func Until

func Until[T, U any](parser Parser[T], until Parser[U]) Parser[[]T]

Parse a sequence of items until a terminator is reached. Returns the sequence, discarding the terminator.

func Upper

func Upper() Parser[rune]

Parse an uppercase letter.

func Where

func Where[T any](parser Parser[T], predicate func(T) bool) Parser[T]

Succeed if the parsed value matches predicate.

func WhiteSpace

func WhiteSpace() Parser[rune]

Parse a whitespace.

func XAtLeastOnce

func XAtLeastOnce[T any](parser Parser[T]) Parser[[]T]

TryParse a stream of elements with at least one item. Except the first

func XMany

func XMany[T any](parser Parser[T]) Parser[[]T]

Parse a stream of elements, failing if any element is only partially parsed.

func XOr

func XOr[T any](first Parser[T], second Parser[T]) Parser[T]

Parse first, if it succeeds, return first, otherwise try second.

type ParserInput

type ParserInput struct {
	// contains filtered or unexported fields
}

func NewParserInput

func NewParserInput(source string) ParserInput

func (ParserInput) Advance

func (p ParserInput) Advance() ParserInput

func (ParserInput) Column

func (p ParserInput) Column() int

func (ParserInput) Current

func (p ParserInput) Current() rune

func (ParserInput) Equal

func (p ParserInput) Equal(other ParserInput) bool

func (ParserInput) IsEnd

func (p ParserInput) IsEnd() bool

func (ParserInput) Line

func (p ParserInput) Line() int

func (ParserInput) Position

func (p ParserInput) Position() int

func (ParserInput) Source

func (p ParserInput) Source() string

func (ParserInput) String

func (p ParserInput) String() string

type ParserResult

type ParserResult[T any] struct {
	Value        T
	Remainder    ParserInput
	Succeeded    bool
	Message      string
	Expectations []string
}

func IfFailure

func IfFailure[T any](result ParserResult[T], next func(ParserResult[T]) ParserResult[T]) ParserResult[T]

func IfSuccess

func IfSuccess[T any, U any](result ParserResult[T], next func(ParserResult[T]) ParserResult[U]) ParserResult[U]

func NewFailureResult

func NewFailureResult[T any](remainder ParserInput, message string, expectations []string) ParserResult[T]

func NewSuccessResult

func NewSuccessResult[T any](value T, remainder ParserInput) ParserResult[T]

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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