-
Notifications
You must be signed in to change notification settings - Fork 0
/
take_until.go
37 lines (29 loc) · 960 Bytes
/
take_until.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package repeat
import (
"github.com/flier/gocombine/pkg/parser"
"github.com/flier/gocombine/pkg/parser/combinator"
"github.com/flier/gocombine/pkg/stream"
)
// TakeUntil takes input until `end` is encountered
// or `end` indicates that it has committed input before failing.
func TakeUntil[T stream.Token, O any](end parser.Func[T, O]) parser.Func[T, []T] {
return parser.Expected(func(input []T) (out []T, remaining []T, err error) {
remaining = input
for {
if _, _, err = end(remaining); err == nil {
break
}
var tok T
if tok, remaining, err = stream.Uncons(remaining); err != nil {
return
}
out = append(out, tok)
}
return
}, "take until")
}
// SkipUntil skips input until `end` is encountered
// or `end` indicates that it has committed input before failing.
func SkipUntil[T stream.Token, O any](end parser.Func[T, O]) parser.Func[T, any] {
return combinator.Ignore(TakeUntil(end)).Expected("skip until")
}