repeat

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: 8 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func ChainL1

func ChainL1[T stream.Token, O any](parser parser.Func[T, O], op parser.Func[T, func(l, h O) O]) parser.Func[T, O]

ChainL1 parses `parser` one or more times separated by `op`. The value returned is the one produced by the left associative application of the function returned by the parser `op`.

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"
	"github.com/flier/gocombine/pkg/parser/to"
)

func main() {
	number := combinator.AndThen(to.String(char.Digit()), strconv.Atoi)
	sub := combinator.Map(char.Char('-'), func(rune) func(l, r int) int {
		return func(l, r int) int { return l - r }
	})
	p := repeat.ChainL1(number, sub)

	fmt.Println(p([]rune("9-3-5")))

}
Output:

1 [] <nil>

func ChainR1

func ChainR1[T stream.Token, O any](parser parser.Func[T, O], op parser.Func[T, func(l, h O) O]) parser.Func[T, O]

ChainR1 parses `p` one or more times separated by `op`. The value returned is the one produced by the right associative application of the function returned by `op`.

Example
package main

import (
	"fmt"
	"math"
	"strconv"

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

func main() {
	number := combinator.AndThen(to.String(char.Digit()), strconv.Atoi)
	sub := combinator.Map(char.Char('^'), func(rune) func(l, r int) int {
		return func(l, r int) int { return int(math.Pow(float64(l), float64(r))) }
	})
	p := repeat.ChainR1(number, sub)

	fmt.Println(p([]rune("2^3^2")))

}
Output:

512 [] <nil>

func Count

func Count[T stream.Token, O any](count int, parser parser.Func[T, O]) parser.Func[T, []O]

Count parses `parser` from zero up to `count` times.

Example
package main

import (
	"fmt"

	"github.com/flier/gocombine/pkg/parser/repeat"
	"github.com/flier/gocombine/pkg/parser/token"
)

func main() {
	p := repeat.Count(2, token.Token('a'))

	fmt.Println(p([]rune("aaab")))
	fmt.Println(p([]rune("foobar")))

}
Output:

[97 97] [97 98] <nil>
[] [102 111 111 98 97 114] <nil>

func CountMinMax

func CountMinMax[T stream.Token, O any](min, max int, p parser.Func[T, O]) parser.Func[T, []O]

CountMinMax parses `p` from `min` to `max` times (including `min` and `max`).

Example
package main

import (
	"fmt"

	"github.com/flier/gocombine/pkg/parser/repeat"
	"github.com/flier/gocombine/pkg/parser/token"
)

func main() {
	p := repeat.CountMinMax(1, 2, token.Token('a'))

	fmt.Println(p([]rune("apple")))
	fmt.Println(p([]rune("aaab")))
	fmt.Println(p([]rune("foobar")))

}
Output:

[97] [112 112 108 101] <nil>
[97 97] [97 98] <nil>
[] [102 111 111 98 97 114] count min max, 1 more elements, expected

func Escaped

func Escaped[T stream.Token](p parser.Func[T, []T], escape T, escapeParser parser.Func[T, T]) parser.Func[T, []T]

Escaped parses an escaped string by first applying `p` which accept the normal characters which do not need escaping. Once `p` can not consume any more input it checks if the next token is `escape`. If it is then `escapeParser` is used to parse the escaped character and then resumes parsing using `p`. If `escape` was not found then the parser finishes successfully.

Example
package main

import (
	"fmt"

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

func main() {
	p := to.String(repeat.Escaped(
		ranges.TakeWhile1(func(c rune) bool { return c != '"' && c != '\\' }),
		'\\',
		char.OneOf(`nrt\"`),
	))

	fmt.Println(p([]rune(`ab\"12\n\rc"`)))
	fmt.Println(p([]rune(`\a`)))

}
Output:

ab\"12\n\rc [34] <nil>
 [92 97] map, escaped, one of, one of "nrt\\\"", satisfy, actual 'a', unexpected

func Many

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

Many parses `p` zero or more times returning a collection with the values from `parser`.

Example
package main

import (
	"fmt"

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

func main() {
	p := repeat.Many(char.Digit())

	fmt.Println(p([]rune("123A")))

}
Output:

[49 50 51] [65] <nil>

func Many1

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

Many1 parses `p` one or more times returning a collection with the values from `parser`.

Example
package main

import (
	"fmt"

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

func main() {
	p := repeat.Many1(char.Digit())

	fmt.Println(p([]rune("1")))
	fmt.Println(p([]rune("123A")))
	fmt.Println(p([]rune("A")))

}
Output:

[49] [] <nil>
[49 50 51] [65] <nil>
[] [] many1, digit, satisfy, actual 'A', unexpected

func SepBy

func SepBy[T stream.Token, O, P any](parser parser.Func[T, O], separator parser.Func[T, P]) parser.Func[T, []O]

SepBy parses `parser` zero or more time separated by `separator`, returning a collection with the values from `p`.

Example
package main

import (
	"fmt"

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

func main() {
	p := repeat.SepBy(char.Digit(), char.Char(','))

	fmt.Println(p([]rune("1,2,3")))
	fmt.Println(p([]rune("")))

}
Output:

[49 50 51] [] <nil>
[] [] <nil>

func SepBy1

func SepBy1[T stream.Token, O, P any](parser parser.Func[T, O], separator parser.Func[T, P]) parser.Func[T, []O]

SepBy1 parses `parser` one or more time separated by `separator`, returning a collection with the values from `parser`.

Example
package main

import (
	"fmt"

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

func main() {
	p := repeat.SepBy1(char.Digit(), char.Char(','))

	fmt.Println(p([]rune("1,2,3")))
	fmt.Println(p([]rune("")))

}
Output:

[49 50 51] [] <nil>
[] [] sep by1, map, pair, digit, satisfy, unexpected EOF

func SepEndBy

func SepEndBy[T stream.Token, O, P any](parser parser.Func[T, O], separator parser.Func[T, P]) parser.Func[T, []O]

SepEndBy parses `parser` zero or more times separated and ended by `separator`, returning a collection with the values from `parser`.

Example
package main

import (
	"fmt"

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

func main() {
	p := repeat.SepEndBy(char.Digit(), char.Char(';'))

	fmt.Println(p([]rune("1;2;3;")))
	fmt.Println(p([]rune("")))

}
Output:

[49 50 51] [] <nil>
[] [] <nil>

func SepEndBy1

func SepEndBy1[T stream.Token, O, P any](parser parser.Func[T, O], separator parser.Func[T, P]) parser.Func[T, []O]

SepEndBy1 parses `parser` one or more times separated and ended by `separator`, returning a collection with the values from `p`.

Example
package main

import (
	"fmt"

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

func main() {
	p := repeat.SepEndBy1(char.Digit(), char.Char(';'))

	fmt.Println(p([]rune("1;2;3;")))
	fmt.Println(p([]rune("")))

}
Output:

[49 50 51] [] <nil>
[] [] sep end by1, many1, skip, digit, satisfy, unexpected EOF

func SkipCount

func SkipCount[T stream.Token, O any](count int, parser parser.Func[T, O]) parser.Func[T, any]

SkipCount parses `parser` from zero up to `count` times skipping the output of `parser`.

Example
package main

import (
	"fmt"

	"github.com/flier/gocombine/pkg/parser/repeat"
	"github.com/flier/gocombine/pkg/parser/token"
)

func main() {
	p := repeat.SkipCount(2, token.Token('a'))

	fmt.Println(p([]rune("aaab")))
	fmt.Println(p([]rune("foobar")))

}
Output:

<nil> [97 98] <nil>
<nil> [102 111 111 98 97 114] <nil>

func SkipCountMinMax

func SkipCountMinMax[T stream.Token, O any](min, max int, parser parser.Func[T, O]) parser.Func[T, any]

SkipCountMinMax parses `parser` from `min` to `max` times (including `min` and `max`) skipping the output of `parser`.

Example
package main

import (
	"fmt"

	"github.com/flier/gocombine/pkg/parser/repeat"
	"github.com/flier/gocombine/pkg/parser/token"
)

func main() {
	p := repeat.SkipCountMinMax(1, 2, token.Token('a'))

	fmt.Println(p([]rune("aaab")))
	fmt.Println(p([]rune("foobar")))

}
Output:

<nil> [97 98] <nil>
<nil> [102 111 111 98 97 114] skip count min max, ignore, count min max, 1 more elements, expected

func SkipMany

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

SkipMany parses `p` zero or more times ignoring the result.

Example
package main

import (
	"fmt"

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

func main() {
	p := repeat.SkipMany(char.Digit())

	fmt.Println(p([]rune("123A")))

}
Output:

<nil> [65] <nil>

func SkipMany1

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

SkipMany1 parses `p` one or more times ignoring the result.

Example
package main

import (
	"fmt"

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

func main() {
	p := repeat.SkipMany1(char.Digit())

	fmt.Println(p([]rune("1")))
	fmt.Println(p([]rune("123A")))
	fmt.Println(p([]rune("A")))

}
Output:

<nil> [] <nil>
<nil> [65] <nil>
<nil> [] skip, ignore, many1, digit, satisfy, actual 'A', unexpected

func SkipUntil

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

SkipUntil skips input until `end` is encountered or `end` indicates that it has committed input before failing.

Example
package main

import (
	"fmt"

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

func main() {
	char_parser := repeat.SkipUntil(char.Digit())

	fmt.Println(char_parser([]rune("abc123")))

	byte_parser := repeat.SkipUntil(bytes.Bytes([]byte("TAG")))

	fmt.Println(byte_parser([]byte("123TAG")))

}
Output:

<nil> [49 50 51] <nil>
<nil> [84 65 71] <nil>

func TakeUntil

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

TakeUntil takes input until `end` is encountered or `end` indicates that it has committed input before failing.

Example
package main

import (
	"fmt"

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

func main() {
	char_parser := repeat.TakeUntil(char.Digit())

	fmt.Println(char_parser([]rune("abc123")))

	byte_parser := repeat.TakeUntil(bytes.Bytes([]byte("TAG")))

	fmt.Println(byte_parser([]byte("123TAG")))

}
Output:

[97 98 99] [49 50 51] <nil>
[49 50 51] [84 65 71] <nil>

func Until

func Until[T stream.Token, O, E any](p parser.Func[T, O], end parser.Func[T, E]) parser.Func[T, []O]

Until parses `parser` zero or more times until `end` is encountered or `end` indicates that it has committed input before failing.

Example
package main

import (
	"fmt"

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

func main() {
	p := repeat.Until(
		sequence.Skip(to.String(repeat.Many1(char.Letter())), char.Spaces()),
		char.Char('!'))

	fmt.Println(p([]rune("Hello World!")))

}
Output:

[Hello World] [33] <nil>

Types

This section is empty.

Jump to

Keyboard shortcuts

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