Documentation ¶
Index ¶
- Variables
- func AnyRune(s *Scanner) (rune, error)
- func Error[A, B any](f func(A) B) func(A) (B, error)
- func Error2[A, B, C any](f func(A, B) C) func(A, B) (C, error)
- func Error3[A, B, C, D any](f func(A, B, C) D) func(A, B, C) (D, error)
- func Error4[A, B, C, D, E any](f func(A, B, C, D) E) func(A, B, C, D) (E, error)
- func Input(s *Scanner) (string, error)
- func ParseString[A any](input string, p Parser[A]) (A, error)
- func Position(s *Scanner) (int, error)
- func Remaining(s *Scanner) (string, error)
- func Runes(rs ...rune) func(rune) bool
- type Pair
- type Parser
- func Assert[A any](p Parser[A], pred func(A) bool, fail func(A) error) Parser[A]
- func Bind[A, B any](p Parser[A], f func(A) Parser[B]) Parser[B]
- func Both[A, B any](p Parser[A], q Parser[B]) Parser[Pair[A, B]]
- func ChainL1[A any](p Parser[A], op Parser[func(A, A) A]) Parser[A]
- func ChainR1[A any](p Parser[A], op Parser[func(A, A) A]) Parser[A]
- func Choice[A any](msg string, ps ...Parser[A]) Parser[A]
- func Consumed[A any](p Parser[A]) Parser[string]
- func Count[A any](n int, p Parser[A]) Parser[[]A]
- func DiscardLeft[A, B any](p Parser[A], q Parser[B]) Parser[B]
- func DiscardRight[A, B any](p Parser[A], q Parser[B]) Parser[A]
- func Fail[A any](err error) Parser[A]
- func Finish[A any](p Parser[A]) Parser[A]
- func Fix[A any](f func(Parser[A]) Parser[A]) Parser[A]
- func Lift[A, B any](f func(A) (B, error), p Parser[A]) Parser[B]
- func Lift2[A, B, C any](f func(A, B) (C, error), p1 Parser[A], p2 Parser[B]) Parser[C]
- func Lift3[A, B, C, D any](f func(A, B, C) (D, error), p1 Parser[A], p2 Parser[B], p3 Parser[C]) Parser[D]
- func Lift4[A, B, C, D, E any](f func(A, B, C, D) (E, error), p1 Parser[A], p2 Parser[B], p3 Parser[C], ...) Parser[E]
- func List[A any](ps []Parser[A]) Parser[[]A]
- func Location[A, B any](p Parser[A], f func(start int, end int, parsed A) B) Parser[B]
- func LookAhead[A any](p Parser[A]) Parser[A]
- func Many[A any](p Parser[A]) Parser[[]A]
- func Many1[A any](p Parser[A]) Parser[[]A]
- func ManyTill[A, B any](p Parser[A], e Parser[B]) Parser[[]A]
- func MatchRegexp(re *regexp.Regexp) Parser[string]
- func MatchString(target string) Parser[string]
- func Maybe[A any](p Parser[A]) Parser[*A]
- func Name[A any](name string, p Parser[A]) Parser[A]
- func NotRune(r rune) Parser[rune]
- func Option[A any](fallback A, p Parser[A]) Parser[A]
- func Or[A any](p Parser[A], q Parser[A]) Parser[A]
- func PrecedingWS[A any](p Parser[A]) Parser[A]
- func Range(lo, hi rune) Parser[rune]
- func Return[A any](v A) Parser[A]
- func Rune(r rune) Parser[rune]
- func Satisfy(f func(rune) bool) Parser[rune]
- func SepBy[A, B any](s Parser[A], p Parser[B]) Parser[[]B]
- func SepBy1[A, B any](s Parser[A], p Parser[B]) Parser[[]B]
- func Skip(f func(rune) bool) Parser[Unit]
- func SkipMany[A any](p Parser[A]) Parser[Unit]
- func SkipMany1[A any](p Parser[A]) Parser[Unit]
- func SkipWS[A any](p Parser[A]) Parser[A]
- func SkipWhile(f func(rune) bool) Parser[Unit]
- func Take(n int) Parser[string]
- func TakeTill(f func(rune) bool) Parser[string]
- func TakeTill1(f func(rune) bool) Parser[string]
- func TakeWhile(f func(rune) bool) Parser[string]
- func TakeWhile1(f func(rune) bool) Parser[string]
- func TrailingWS[A any](p Parser[A]) Parser[A]
- func Try[A any](p Parser[A]) Parser[A]
- func TryChoice[A any](msg string, ps ...Parser[A]) Parser[A]
- func Wrap[A, B, C any](left Parser[A], p Parser[B], right Parser[C]) Parser[B]
- type Scanner
- func (s *Scanner) MatchRegexp(re *regexp.Regexp) (string, error)
- func (s *Scanner) MatchRune(match func(rune) error) (r rune, err error)
- func (s *Scanner) MatchString(target string) (string, error)
- func (s *Scanner) ReadRune() (rune, int, error)
- func (s *Scanner) Remaining() string
- func (s *Scanner) UnreadRune() error
- type Unit
Constants ¶
This section is empty.
Variables ¶
var Space = Satisfy(unicode.IsSpace)
Space parses a single valid unicode whitespace
Functions ¶
func Error ¶ added in v0.3.0
Error wraps a non-error returning function to match the expected Lift function signature.
func Error2 ¶ added in v0.3.0
Error2 wraps a non-error returning function to match the expected Lift function signature.
func Error3 ¶ added in v0.3.0
Error3 wraps a non-error returning function to match the expected Lift function signature.
func Error4 ¶ added in v0.3.0
Error4 wraps a non-error returning function to match the expected Lift function signature.
func ParseString ¶ added in v0.2.1
ParseString parses the input string with the parser `p` constructing a new scanner as necessary.
Types ¶
type Pair ¶
type Pair[A, B any] struct { Left A Right B }
Pair is a simple A * B product type holding two different subtypes in its Left and Right branches.
type Parser ¶
Parser parses input text contained in the Scanner and produces type T. Higher order parsers are constructed through application of combinators on Parsers of different types.
func Assert ¶ added in v0.2.1
Assert runs the provided parser `p` and verifies its output against the predicate `pred`. If the predicate returns false, the `fail` function is called to return an error. Otherwise, the output of the parser `p` is returned.
func Bind ¶
Bind creates a parser that will run `p`, pass its result to `f` run the parser that `f` produces and return its result.
func ChainL1 ¶
ChainL1 parses one or more occurrences of `p`, separated by `op` and returns a value obtained by left associative application of all functions returned by `op` to the values returned by `p`.
This parser can be used to eliminate left recursion which typically occurs in expression grammars.
See ChainR1 for example.
func ChainR1 ¶ added in v0.2.1
ChainR1 parses one or more occurrences of `p`, separated by `op` and returns a value obtained by right associative application of all functions returned by `op` to the values returned by `p`.
This parser can be used to eliminate left recursion which typically occurs in expression grammars.
Example:
ParseExpression := Fix(func(expr Parser[int]) Parser[int] { ParseAdd := DiscardLeft(SkipWS(Rune('+')), Return(func(a, b int) int { return a + b })) ParseSub := DiscardLeft(SkipWS(Rune('-')), Return(func(a, b int) int { return a - b })) ParseMul := DiscardLeft(SkipWS(Rune('*')), Return(func(a, b int) int { return a * b })) ParseDiv := DiscardLeft(SkipWS(Rune('/')), Return(func(a, b int) int { return a / b })) ParseInteger := result.Unwrap(Lift( result.Lift(strconv.Atoi), TakeWhile1(Runes('0', '1', '2', '3', '4', '5', '6', '7', '8', '9')), )) ParseFactor := Or(Wrap(Rune('('), expr, Rune(')')), ParseInteger) ParseTerm := ChainR1(ParseFactor, Or(ParseMul, ParseDiv)) return ChainR1(ParseTerm, Or(ParseAdd, ParseSub)) })
func Choice ¶
Choice runs each parser in `ps` in order until one succeeds and returns the result. If any of the failing parsers in `ps` consumes input, the accumulated parse errors will be returned and the parse chain will abort. In the case that none of the parsers succeeds, then the parser will fail with the message "expected {msg}".
NOTE: Like with the Or combinator, this functionality maps the original haskell combinator and if you wish to allow any of the parsers within `ps` to consume input without stopping the parse chain execution, then you should wrap the provided parser with a Try meta-parser.
func Consumed ¶
Consumed runs p and returns the contents that were consumed during the parsing as a string.
func DiscardLeft ¶
DiscardLeft runs `p`, discards its results and then runs `q` and returns its results.
func DiscardRight ¶
DiscardRight runs `p`, then runs `q`, discards its results and returns the initial result of `p`.
func Finish ¶
Finish meta-parser ensures that the completed parser has successfully parsed the entirety of the input string contained in the scanner.
func Fix ¶
Fix computes the fix-point of `f` and runs the resultant parser. The argument that `f` receives is the result of `Fix(f)`, which `f` must use to define `Fix(f)`.
func Lift ¶
Lift promotes functions into a parser. The returned parser first executes the provided parser `p` before transforming the returned value of `p` using `f` and returning it.
func Lift3 ¶
func Lift3[A, B, C, D any]( f func(A, B, C) (D, error), p1 Parser[A], p2 Parser[B], p3 Parser[C], ) Parser[D]
Lift3 promotes 3-ary functions into a parser.
func Lift4 ¶
func Lift4[A, B, C, D, E any]( f func(A, B, C, D) (E, error), p1 Parser[A], p2 Parser[B], p3 Parser[C], p4 Parser[D], ) Parser[E]
Lift4 promotes 4-ary functions into a parser.
func Location ¶ added in v0.2.2
Location meta-parser tracks the start and end location of successfully parsed inputs, allowing the start and end location to be combined into the parsed value through the provided function.
func LookAhead ¶
LookAhead constructs a new parser that will apply the provided parser `p` without consuming any input regardless of whether `p` succeeds or fails.
func ManyTill ¶
ManyTill runs parser `p` zero ore more times until action `e` succeeds and returns the slice of results from the runs of `p`.
func MatchRegexp ¶ added in v0.2.1
MatchRegexp accepts the target regex and returns it.
func MatchString ¶
MatchString accepts the target string and returns it.
func Maybe ¶
Maybe constructs a new parser that will attempt to parse the input using the provided parser `p`. If the parser is successful, it will return a pointer to the parsed value. If the parse is unsuccessful it will return a nil pointer in a poor imitation of an Optional type.
Maybe parsers can never fail.
func Option ¶
Option runs `p`, returning the result of `p` if it succeeds and `fallback` if it fails.
func Or ¶
Or runs `p` and returns the result if it succeeds. If `p` fails and no input has been consumed then `q` will run instead.
NOTE: This functionality maps the implementation of Or from the original haskell parsec combinator. If you wish for the parser `p` to potentially consume input and for that consumed input to be discarded when the parser `q` is run, wrap `q` in the Try meta-parser.
func PrecedingWS ¶
PrecedingWS ignores all whitespace before the data parsed by the parser p.
There must be at least one instance of valid whitespace following the parser p.
func Satisfy ¶
Satisfy accepts any character for which f returns true and returns the accepted character. In the case that none of the parser succeeds, then the parser will fail indicating the offending character.
func Skip ¶
Skip accepts any character for which f returns true and discards the accepted character. Skip(f) is equivalent to Satisfy(f) but discards the accepted character.
func SkipWhile ¶
SkipWhile accepts input as long as f returns true and discards the accepted characters.
func TakeTill ¶
TakeTill accepts input as long as f returns false and returns the accepted characters as a string.
func TakeTill1 ¶ added in v0.2.1
TakeTill1 accepts input as long as returns false and returns the accepted characters as a string so long as at least one character was matched
func TakeWhile ¶
TakeWhile accepts input as long as f returns true and returns the accepted characters as a string.
This parser does not fail, if the first call to f returns false on the first character, it will return an empty string.
func TakeWhile1 ¶
TakeWhile1 accepts input as long as f returns true and returns the accepted characters as a string.
This parser requires that f return true for at least one character of input and will fail if it does not.
func TrailingWS ¶
TrailingWS ignores all whitespace following the data parsed by the parser p.
There must be at least one instance of valid whitespace following the parser p.
func Try ¶
Try constructs a new parser that will attempt to parse the input using the provided parser `p`. If the parser is successful, it will return the parsed value, if the parse is unsuccessful it will rewind the scanner input so that no input appears to have been consumed.
type Scanner ¶
type Scanner struct {
// contains filtered or unexported fields
}
Scanner is responsible for maintaining the iterative state through which the constructed parser moves.
Scanner exposes three matching primitives for the parsers to use:
1. MatchRegexp - Matching on compiled regular expressions 2. MatchString - Matching on concrete strings 3. MatchRune - Matching on an individual rune
Each of these matching methods can potentially advance the state of the the scanner along the input if they successfully find a match given the provided matching criteria.
If no match is found in any of these matching primitives, the state of the scanner is not advanced.
func NewScanner ¶
NewScanner constructs a new avram Scanner from the provided input string.
func (*Scanner) MatchRegexp ¶ added in v0.2.1
MatchRegexp attempts to match the provided regex from the current location of the scanner, returning the first matched instance of the regex as a string if a match is found and an error otherwise.
NOTE: MatchRegexp only advances the scanner position if a valid match is successfully found.
func (*Scanner) MatchRune ¶ added in v0.2.1
MatchRune attempts to match the provided predicate function with the next rune in the scanner's input stream.
NOTE: MatchRune only advances the scanner position if a valid match is successfully found.
func (*Scanner) MatchString ¶
MatchString attempts to match the provided target string rune-by-rune exactly as specified, returning the target string if matched or an error if it was unable to match the string.
NOTE: MatchString only advances the scanner position if a valid match is successfully found.
func (*Scanner) ReadRune ¶
ReadRune reads a single rune from the input text.
This method implements the io.RuneReader interface.
func (*Scanner) UnreadRune ¶
UnreadRune unreads the last read rune, the next call to ReadRune will return the just unread rune.
This method implements the io.RuneScanner interface along with ReadRune.