combinator

package
v0.0.0-...-1dacd80 Latest Latest
Warning

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

Go to latest
Published: Apr 15, 2022 License: Apache-2.0, MIT Imports: 5 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func AndThen

func AndThen[T stream.Token, I, O any](p parser.Func[T, I], f func(I) (O, error)) parser.Func[T, O]

AndThen parses with `p` and applies `f` on the result if `p` parses successfully. `f` may optionally fail with an error.

Example
package main

import (
	"fmt"
	"strconv"

	"github.com/flier/gocombine/pkg/parser/char"
	"github.com/flier/gocombine/pkg/parser/combinator"
	"github.com/flier/gocombine/pkg/parser/repeat"
)

func main() {
	p := combinator.AndThen(repeat.Many1(char.Digit()), func(s []rune) (int, error) {
		return strconv.Atoi(string(s))
	})

	fmt.Println(p([]rune("123")))
	fmt.Println(p([]rune("9999999999999999")))
	fmt.Println(p([]rune("foobar")))

}
Output:

123 [] <nil>
9999999999999999 [] <nil>
0 [111 111 98 97 114] and then, many1, digit, satisfy, actual 'f', unexpected

func Attempt

func Attempt[T stream.Token, O any](parser parser.Func[T, O]) parser.Func[T, O]

Attempt behaves as `parser` except it always acts as `parser` peeked instead of committed on its parse.

func Factory

func Factory[T stream.Token, O any](f func([]T) parser.Func[T, O]) parser.Func[T, O]

Factory constructs the `parser` lazily.

This is similar to `Lazy` but it takes `input` as an argument and allows different parsers to be returned on each call to `p` while still reporting the correct errors.

Example
package main

import (
	"fmt"

	"github.com/flier/gocombine/pkg/parser"
	"github.com/flier/gocombine/pkg/parser/char"
	"github.com/flier/gocombine/pkg/parser/combinator"
)

func main() {
	p := combinator.Factory(func(s []rune) parser.Func[rune, []rune] {
		if s[0] == 'a' {
			return char.String("apple")
		}

		return char.String("banana")
	})

	fmt.Println(p([]rune("apple")))
	fmt.Println(p([]rune("banana")))

}
Output:

[97 112 112 108 101] [] <nil>
[98] [97 110 97 110 97] string, cmp, expected "apple", actual "b", unexpected

func FlatMap

func FlatMap[T stream.Token, O any](p parser.Func[T, []T], f func([]T) (O, error)) parser.Func[T, O]

FlatMap uses `f` to map over the output of `p`. If `f` returns an error the parser fails.

Example
package main

import (
	"fmt"

	"github.com/flier/gocombine/pkg/parser/char"
	"github.com/flier/gocombine/pkg/parser/combinator"
	"github.com/flier/gocombine/pkg/parser/repeat"
)

func main() {
	p := combinator.FlatMap(
		char.Take(4),
		func(input []rune) (out []rune, err error) {
			out, _, err = repeat.Many1(char.Digit())(input)

			return
		},
	)

	fmt.Println(p([]rune("12abcd")))
	fmt.Println(p([]rune("123")))
	fmt.Println(p([]rune("foobar")))

}
Output:

[49 50] [99 100] <nil>
[] [49 50 51] flat map, take, unexpected EOF
[] [97 114] flat map, many1, digit, satisfy, actual 'f', unexpected

func Fold

func Fold[T stream.Token, I, B any](p parser.Func[T, []I], init func() B, f func(B, I) B) parser.Func[T, B]

Fold every element into an accumulator by applying an operation, returning the final result.

Example
package main

import (
	"fmt"

	"github.com/flier/gocombine/pkg/parser/char"
	"github.com/flier/gocombine/pkg/parser/combinator"
	"github.com/flier/gocombine/pkg/parser/repeat"
)

func main() {
	p := combinator.Fold(repeat.Many1(char.Digit()),
		func() int { return 0 },
		func(acc int, c rune) int { return acc*10 + int(c-'0') })

	fmt.Println(p([]rune("123")))
	fmt.Println(p([]rune("9999999999999999")))
	fmt.Println(p([]rune("foobar")))

}
Output:

123 [] <nil>
9999999999999999 [] <nil>
0 [111 111 98 97 114] fold, many1, digit, satisfy, actual 'f', unexpected

func Ignore

func Ignore[T stream.Token, I any](p parser.Func[T, I]) parser.Func[T, any]

Ignore discards the value of the `p`.

func Lazy

func Lazy[T stream.Token, O any](f func() parser.Func[T, O]) parser.Func[T, O]

Lazy constructs the `parser` lazily.

Can be used to effectively reduce the size of deeply nested parsers as only the function producing the parser is stored.

func Map

func Map[T stream.Token, I, O any](p parser.Func[T, I], f func(I) O) parser.Func[T, O]

Map uses `f` to map over the `p` parsed value.

Example
package main

import (
	"fmt"

	"github.com/flier/gocombine/pkg/parser/char"
	"github.com/flier/gocombine/pkg/parser/combinator"
)

func main() {
	p := combinator.Map(char.Digit(), func(r rune) bool { return r == '9' })

	fmt.Println(p([]rune("9i")))

}
Output:

true [105] <nil>

func Pair

func Pair[T stream.Token, O1, O2 any](p1 parser.Func[T, O1], P2 parser.Func[T, O2]) parser.Func[T, pair.Pair[O1, O2]]

Pair parses two heterogeneous value.

Example
package main

import (
	"fmt"

	"github.com/flier/gocombine/pkg/parser/char"
	"github.com/flier/gocombine/pkg/parser/choice"
	"github.com/flier/gocombine/pkg/parser/combinator"
	"github.com/flier/gocombine/pkg/parser/repeat"
)

func main() {
	p := combinator.Pair(choice.Optional(char.OneOf("+-")), repeat.Many1(char.Digit()))

	fmt.Println(p([]rune("123")))
	fmt.Println(p([]rune("+123")))
	fmt.Println(p([]rune("foobar")))

}
Output:

{<none> [49 50 51]} [] <nil>
{43 [49 50 51]} [] <nil>
{<none> []} [111 111 98 97 114] pair, many1, digit, satisfy, actual 'f', unexpected

func Tuple3

func Tuple3[
	T stream.Token,
	O1, O2, O3 any,
](
	p1 parser.Func[T, O1],
	p2 parser.Func[T, O2],
	p3 parser.Func[T, O3],
) parser.Func[T, tuple.Tuple3[O1, O2, O3]]

Tuple3 parses a tuple of heterogeneous values.

func Tuple4

func Tuple4[
	T stream.Token,
	O1, O2, O3, O4 any,
](
	p1 parser.Func[T, O1],
	p2 parser.Func[T, O2],
	p3 parser.Func[T, O3],
	p4 parser.Func[T, O4],
) parser.Func[T, tuple.Tuple4[O1, O2, O3, O4]]

Tuple4 parses a tuple of heterogeneous values.

func Tuple5

func Tuple5[
	T stream.Token,
	O1, O2, O3, O4, O5 any,
](
	p1 parser.Func[T, O1],
	p2 parser.Func[T, O2],
	p3 parser.Func[T, O3],
	p4 parser.Func[T, O4],
	p5 parser.Func[T, O5],
) parser.Func[T, tuple.Tuple5[O1, O2, O3, O4, O5]]

Tuple5 parses a tuple of heterogeneous values.

Types

This section is empty.

Jump to

Keyboard shortcuts

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