Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix predicate error cases #15

Merged
merged 1 commit into from
Jan 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
# Test binary, built with `go test -c`
*.test

# Comparative benchmarks
before.txt
after.txt

# Output of the go coverage tool, specifically when used with LiteIDE
*.out
coverage.html
Expand Down
21 changes: 15 additions & 6 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,7 @@ func Char(char rune) Parser[string] {
//
// If the predicate doesn't return false before the entire input is consumed, an error will be returned.
//
// A predicate that never returns true will leave the entire input unparsed and return a
// value that is an empty string, and a remainder that is the entire input.
// A predicate that never returns true will also return an error.
func TakeWhile(predicate func(r rune) bool) Parser[string] {
return func(input string) (string, string, error) {
if input == "" {
Expand All @@ -174,6 +173,10 @@ func TakeWhile(predicate func(r rune) bool) Parser[string] {
return "", "", errors.New("TakeWhile: predicate must be a non-nil function")
}

if strings.IndexFunc(input, predicate) == -1 {
return "", "", errors.New("TakeWhile: predicate never returned true")
}

end := 0 // Byte position of last rune that the predicate returns true for
broken := false // Whether the predicate ever returned false so we broke the loop
for pos, char := range input {
Expand All @@ -200,11 +203,9 @@ func TakeWhile(predicate func(r rune) bool) Parser[string] {
//
// If the input is empty or predicate == nil, an error will be returned.
//
// If the predicate doesn't return true before the entire input is consumed, an error will be returned
// .
// If the predicate doesn't return true before the entire input is consumed, an error will be returned.
//
// A predicate that never returns false will leave the entire input unparsed and return a
// value that is an empty string, and a remainder that is the entire input.
// A predicate that never returns false will also return an error.
func TakeUntil(predicate func(r rune) bool) Parser[string] {
return func(input string) (string, string, error) {
if input == "" {
Expand All @@ -219,6 +220,14 @@ func TakeUntil(predicate func(r rune) bool) Parser[string] {
return "", "", errors.New("TakeUntil: predicate must be a non-nil function")
}

// strings.IndexFunc returns the index of the first char for which the predicate
// returns true, we want the opposite to test if it ever returns false
notPred := func(r rune) bool { return !predicate(r) }

if strings.IndexFunc(input, notPred) == -1 {
return "", "", errors.New("TakeUntil: predicate never returned false")
}

end := 0 // Byte position of last rune that the predicate returns false for
broken := false // Whether the predicate ever returned true so we broke the loop
for pos, char := range input {
Expand Down
16 changes: 8 additions & 8 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 +584,7 @@ func TestTakeWhile(t *testing.T) {
err: "TakeWhile: predicate must be a non-nil function",
},
{
name: "predicate never returns false", // Good libraries don't allow infinite loops
name: "predicate never returns false",
input: "fixed length input",
value: "",
remainder: "",
Expand All @@ -596,10 +596,10 @@ func TestTakeWhile(t *testing.T) {
name: "predicate never returns true",
input: "fixed length input",
value: "",
remainder: "fixed length input",
remainder: "",
predicate: func(r rune) bool { return false },
wantErr: false,
err: "",
wantErr: true,
err: "TakeWhile: predicate never returned true",
},
{
name: "consume whitespace",
Expand Down Expand Up @@ -839,7 +839,7 @@ func TestTakeUntil(t *testing.T) {
err: "TakeUntil: predicate must be a non-nil function",
},
{
name: "predicate never returns true", // Good libraries don't allow infinite loops
name: "predicate never returns true",
input: "fixed length input",
value: "",
remainder: "",
Expand All @@ -851,10 +851,10 @@ func TestTakeUntil(t *testing.T) {
name: "predicate never returns false",
input: "fixed length input",
value: "",
remainder: "fixed length input",
remainder: "",
predicate: func(r rune) bool { return true },
wantErr: false,
err: "",
wantErr: true,
err: "TakeUntil: predicate never returned false",
},
{
name: "consume until whitespace",
Expand Down
Loading