Renamed Result.Succeeded to Status and removed Failed
This commit is contained in:
@ -14,37 +14,33 @@ import (
|
||||
)
|
||||
|
||||
type Result[In, Out any] struct {
|
||||
consumed, succeeded bool
|
||||
value Out
|
||||
next State[In]
|
||||
message Message
|
||||
consumed, success bool
|
||||
value Out
|
||||
next State[In]
|
||||
message Message
|
||||
}
|
||||
|
||||
func Fail[In, Out any](consumed bool, msg Message) Result[In, Out] {
|
||||
return Result[In, Out]{
|
||||
consumed: consumed,
|
||||
succeeded: false,
|
||||
message: msg,
|
||||
consumed: consumed,
|
||||
success: false,
|
||||
message: msg,
|
||||
}
|
||||
}
|
||||
|
||||
func (r Result[In, Out]) Failed() bool {
|
||||
return !r.succeeded
|
||||
}
|
||||
|
||||
func Succeed[In, Out any](consumed bool, value Out, next State[In], msg Message) Result[In, Out] {
|
||||
return Result[In, Out]{
|
||||
succeeded: true,
|
||||
value: value,
|
||||
consumed: consumed,
|
||||
next: next,
|
||||
message: msg,
|
||||
success: true,
|
||||
value: value,
|
||||
consumed: consumed,
|
||||
next: next,
|
||||
message: msg,
|
||||
}
|
||||
}
|
||||
|
||||
func (r Result[In, Out]) Succeeded() (succeeded bool, value Out, next State[In]) {
|
||||
succeeded = r.succeeded
|
||||
if succeeded {
|
||||
func (r Result[In, Out]) Status() (success bool, value Out, next State[In]) {
|
||||
success = r.success
|
||||
if success {
|
||||
value = r.value
|
||||
next = r.next
|
||||
}
|
||||
@ -154,7 +150,7 @@ func (p Parser[In, Out]) Label(label string) Parser[In, Out] {
|
||||
return result, err
|
||||
}
|
||||
msg := result.Message()
|
||||
if succeeded, value, next := result.Succeeded(); succeeded {
|
||||
if success, value, next := result.Status(); success {
|
||||
return Succeed(false, value, next, msg.expect(label)), nil
|
||||
}
|
||||
return Fail[In, Out](false, msg.expect(label)), nil
|
||||
@ -174,11 +170,11 @@ func Run[In, Out any](p Parser[In, Out], c cursor.Cursor[In]) (out Out, err erro
|
||||
err = fmt.Errorf("Run: %w", err)
|
||||
return
|
||||
}
|
||||
if result.Failed() {
|
||||
success, out, _ := result.Status()
|
||||
if !success {
|
||||
err = ParseError(result.Message())
|
||||
return
|
||||
}
|
||||
_, out, _ = result.Succeeded()
|
||||
return
|
||||
}
|
||||
|
||||
@ -269,11 +265,11 @@ func Choose[In, Out any](p Parser[In, Out], ps ...Parser[In, Out]) Parser[In, Ou
|
||||
}
|
||||
var qMsg Message
|
||||
msg := result.Message()
|
||||
if result.Failed() {
|
||||
success, qValue, _ := result.Status()
|
||||
if !success {
|
||||
qMsg = msg
|
||||
failed = true
|
||||
} else {
|
||||
_, qValue, _ := result.Succeeded()
|
||||
if failed {
|
||||
value = qValue
|
||||
failed = false
|
||||
@ -303,7 +299,8 @@ func Try[In, Out any](p Parser[In, Out]) Parser[In, Out] {
|
||||
if err != nil {
|
||||
return result, err
|
||||
}
|
||||
if result.Failed() {
|
||||
success, _, _ := result.Status()
|
||||
if !success {
|
||||
return Fail[In, Out](false, result.Message()), nil
|
||||
}
|
||||
return result, nil
|
||||
@ -350,19 +347,20 @@ func Repeat[In, Out any](minCount int, p Parser[In, Out]) Parser[In, []Out] {
|
||||
var consumed bool
|
||||
next := s
|
||||
for {
|
||||
result, err := p(s)
|
||||
result, err := p(next)
|
||||
if err != nil {
|
||||
return Result[In, []Out]{}, fmt.Errorf("AtLeastN: %w", err)
|
||||
}
|
||||
consumed = consumed || result.Consumed()
|
||||
if result.Failed() {
|
||||
var value Out
|
||||
var success bool
|
||||
success, value, next = result.Status()
|
||||
if !success {
|
||||
if len(values) >= minCount {
|
||||
return Succeed(consumed, values, next, MessageOK(s.Pos())), nil
|
||||
}
|
||||
return Fail[In, []Out](consumed, result.Message()), nil
|
||||
}
|
||||
var value Out
|
||||
_, value, next = result.Succeeded()
|
||||
values = append(values, value)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user