Documentation ¶
Index ¶
- func LengthPrefix[T stream.Token, N constraints.Integer](length parser.Func[T, N]) parser.Func[T, []T]
- func Recognize[T stream.Token, O any](p parser.Func[T, O]) parser.Func[T, []T]
- func Take[T stream.Token](n int) parser.Func[T, []T]
- func TakeUntil[T stream.Token](f func(T) bool) parser.Func[T, []T]
- func TakeUntil1[T stream.Token](f func(T) bool) parser.Func[T, []T]
- func TakeWhile[T stream.Token](f func(T) bool) parser.Func[T, []T]
- func TakeWhile1[T stream.Token](f func(T) bool) parser.Func[T, []T]
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func LengthPrefix ¶
func LengthPrefix[T stream.Token, N constraints.Integer](length parser.Func[T, N]) parser.Func[T, []T]
LengthPrefix takes a parser which parses a `length` then extracts a range of that length and returns it. Commonly used in binary formats.
Example ¶
package main import ( "fmt" "github.com/flier/gocombine/pkg/parser/bytes/be" "github.com/flier/gocombine/pkg/parser/ranges" ) func main() { p := ranges.LengthPrefix(be.Uint16()) fmt.Println(p([]byte{0x00, 0x03, 0x01, 0x02, 0x03, 0x04})) fmt.Println(p([]byte{0x00, 0x03, 0x01, 0x02})) }
Output: [1 2 3] [4] <nil> [] [1 2] length prefix, unexpected EOF
func Recognize ¶
Recognize returns committed input range.
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/ranges" "github.com/flier/gocombine/pkg/parser/repeat" "github.com/flier/gocombine/pkg/parser/sequence" "github.com/flier/gocombine/pkg/parser/to" ) func main() { p := to.String(ranges.Recognize(combinator.Pair( repeat.SkipMany1(char.Digit()), choice.Optional( sequence.With(char.Char('.'), repeat.SkipMany1(char.Digit()))), ))) fmt.Println(p([]rune("1234!"))) fmt.Println(p([]rune("1234.0001!"))) fmt.Println(p([]rune("!"))) fmt.Println(p([]rune("1234."))) }
Output: 1234 [33] <nil> 1234.0001 [33] <nil> [] map, recognize, pair, skip, ignore, many1, digit, satisfy, actual '!', unexpected 1234 [46] <nil>
func Take ¶
Take reads a range of length `n`.
Example ¶
package main import ( "fmt" "github.com/flier/gocombine/pkg/parser/ranges" ) func main() { p := ranges.Take[byte](1) fmt.Println(p([]byte("!"))) fmt.Println(p(nil)) p = ranges.Take[byte](4) fmt.Println(p([]byte("1234abc"))) fmt.Println(p([]byte("123"))) }
Output: [33] [] <nil> [] [] take, unexpected EOF [49 50 51 52] [97 98 99] <nil> [] [49 50 51] take, unexpected EOF
func TakeUntil ¶
TakeUntil reads a range of 0 or more tokens until token `r` which satisfy `f`.
The range `r` will not be committed. If `r` is not found, the parser will return an error.
Example ¶
package main import ( "fmt" "github.com/flier/gocombine/pkg/parser/ranges" ) func main() { p := ranges.TakeUntil(func(b byte) bool { return b == '\n' }) fmt.Println(p([]byte("123\nabc"))) fmt.Println(p([]byte("abc"))) }
Output: [49 50 51] [10 97 98 99] <nil> [97 98 99] [] take until, unexpected EOF
func TakeUntil1 ¶
TakeUntil reads a range of 1 or more tokens until token `r` which satisfy `f`.
The range `r` will not be committed. If `r` is not found, the parser will return an error.
Example ¶
package main import ( "fmt" "github.com/flier/gocombine/pkg/parser/ranges" ) func main() { p := ranges.TakeUntil1(func(b byte) bool { return b == '\n' }) fmt.Println(p([]byte("123\nabc"))) fmt.Println(p([]byte("\n"))) fmt.Println(p([]byte("abc"))) }
Output: [49 50 51] [10 97 98 99] <nil> [] [10] take until, one or more elements, expected [97 98 99] [] take until, unexpected EOF
func TakeWhile ¶
TakeWhile reads a range of 0 or more tokens which satisfy `f`.
Example ¶
package main import ( "fmt" "github.com/flier/gocombine/pkg/parser/ranges" ) func main() { p := ranges.TakeWhile(func(b byte) bool { return '0' <= b && b <= '9' }) fmt.Println(p([]byte("123abc"))) fmt.Println(p([]byte("abc"))) }
Output: [49 50 51] [97 98 99] <nil> [] [97 98 99] <nil>
func TakeWhile1 ¶
TakeWhile1 reads a range of 1 or more tokens which satisfy `f`.
Example ¶
package main import ( "fmt" "github.com/flier/gocombine/pkg/parser/ranges" ) func main() { p := ranges.TakeWhile1(func(b byte) bool { return '0' <= b && b <= '9' }) fmt.Println(p([]byte("123abc"))) fmt.Println(p([]byte("abc"))) }
Output: [49 50 51] [97 98 99] <nil> [] [97 98 99] take while1, one or more elements, expected
Types ¶
This section is empty.