Skip to content

Commit

Permalink
Rename generic type arguments for clarity
Browse files Browse the repository at this point in the history
  • Loading branch information
oleiade committed Dec 11, 2023
1 parent 729f476 commit 4594a00
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 144 deletions.
6 changes: 3 additions & 3 deletions branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ package gomme
// succeeds.
//
// If none of the parsers succeed, this combinator produces an error Result.
func Alternative[I Bytes, O any](parsers ...Parser[I, O]) Parser[I, O] {
return func(input I) Result[O, I] {
func Alternative[Input Bytes, Output any](parsers ...Parser[Input, Output]) Parser[Input, Output] {
return func(input Input) Result[Output, Input] {
for _, parse := range parsers {
result := parse(input)
if result.Err == nil {
return result
}
}

return Failure[I, O](NewError(input, "Alternative"), input)
return Failure[Input, Output](NewError(input, "Alternative"), input)
}
}
32 changes: 16 additions & 16 deletions bytes.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import (
)

// Take returns a subset of the input of size `count`.
func Take[I Bytes](count uint) Parser[I, I] {
return func(input I) Result[I, I] {
func Take[Input Bytes](count uint) Parser[Input, Input] {
return func(input Input) Result[Input, Input] {
if len(input) == 0 && count > 0 {
return Failure[I, I](NewError(input, "TakeUntil"), input)
return Failure[Input, Input](NewError(input, "TakeUntil"), input)
}

if uint(len(input)) < count {
return Failure[I, I](NewError(input, "Take"), input)
return Failure[Input, Input](NewError(input, "Take"), input)
}

return Success(input[:count], input[count:])
Expand All @@ -23,10 +23,10 @@ func Take[I Bytes](count uint) Parser[I, I] {
// TakeUntil parses any number of characters until the provided parser is successful.
// If the provided parser is not successful, the parser fails, and the entire input is
// returned as the Result's Remaining.
func TakeUntil[I Bytes, O any](parse Parser[I, O]) Parser[I, I] {
return func(input I) Result[I, I] {
func TakeUntil[Input Bytes, Output any](parse Parser[Input, Output]) Parser[Input, Input] {
return func(input Input) Result[Input, Input] {
if len(input) == 0 {
return Failure[I, I](NewError(input, "TakeUntil"), input)
return Failure[Input, Input](NewError(input, "TakeUntil"), input)
}

pos := 0
Expand All @@ -40,7 +40,7 @@ func TakeUntil[I Bytes, O any](parse Parser[I, O]) Parser[I, I] {
continue
}

return Failure[I, I](NewError(input, "TakeUntil"), input)
return Failure[Input, Input](NewError(input, "TakeUntil"), input)
}
}

Expand All @@ -50,17 +50,17 @@ func TakeUntil[I Bytes, O any](parse Parser[I, O]) Parser[I, I] {
// If the provided parser is not successful or the pattern is out of the
// `atLeast` <= len(input) <= `atMost` range, the parser fails, and the entire
// input is returned as the Result's Remaining.
func TakeWhileMN[I Bytes](atLeast, atMost uint, predicate func(rune) bool) Parser[I, I] {
return func(input I) Result[I, I] {
func TakeWhileMN[Input Bytes](atLeast, atMost uint, predicate func(rune) bool) Parser[Input, Input] {
return func(input Input) Result[Input, Input] {
if len(input) == 0 {
return Failure[I, I](NewError(input, "TakeWhileMN"), input)
return Failure[Input, Input](NewError(input, "TakeWhileMN"), input)
}

// Input is shorter than the minimum expected matching length,
// it is thus not possible to match it within the established
// constraints.
if uint(len(input)) < atLeast {
return Failure[I, I](NewError(input, "TakeWhileMN"), input)
return Failure[Input, Input](NewError(input, "TakeWhileMN"), input)
}

lastValidPos := 0
Expand All @@ -72,7 +72,7 @@ func TakeWhileMN[I Bytes](atLeast, atMost uint, predicate func(rune) bool) Parse
matched := predicate(rune(input[idx]))
if !matched {
if uint(idx) < atLeast {
return Failure[I, I](NewError(input, "TakeWhileMN"), input)
return Failure[Input, Input](NewError(input, "TakeWhileMN"), input)
}

return Success(input[:idx], input[idx:])
Expand All @@ -88,10 +88,10 @@ func TakeWhileMN[I Bytes](atLeast, atMost uint, predicate func(rune) bool) Parse
// Token parses a token from the input, and returns the part of the input that
// matched the token.
// If the token could not be found, the parser returns an error result.
func Token[I Bytes](token string) Parser[I, I] {
return func(input I) Result[I, I] {
func Token[Input Bytes](token string) Parser[Input, Input] {
return func(input Input) Result[Input, Input] {
if !strings.HasPrefix(string(input), token) {
return Failure[I, I](NewError(input, fmt.Sprintf("Token(%s)", token)), input)
return Failure[Input, Input](NewError(input, fmt.Sprintf("Token(%s)", token)), input)
}

return Success(input[:len(token)], input[len(token):])
Expand Down
Loading

0 comments on commit 4594a00

Please sign in to comment.