274 lines
6.5 KiB
Go
274 lines
6.5 KiB
Go
package gigaparsec
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"slices"
|
|
|
|
"git.codemonkeysoftware.net/b/gigaparsec/cursor"
|
|
)
|
|
|
|
type Result[In, Out any] struct {
|
|
consumed, failed 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,
|
|
failed: true,
|
|
message: msg,
|
|
}
|
|
}
|
|
|
|
func (r Result[In, Out]) Failed() (ok, consumed bool, msg Message) {
|
|
ok = r.failed
|
|
if ok {
|
|
consumed = r.consumed
|
|
msg = r.message
|
|
}
|
|
return
|
|
}
|
|
|
|
func Succeed[In, Out any](consumed bool, value Out, next State[In], msg Message) Result[In, Out] {
|
|
return Result[In, Out]{
|
|
failed: false,
|
|
value: value,
|
|
consumed: consumed,
|
|
next: next,
|
|
message: msg,
|
|
}
|
|
}
|
|
|
|
func (r Result[In, Out]) Succeeded() (ok, consumed bool, value Out, next State[In], msg Message) {
|
|
ok = !r.failed
|
|
if ok {
|
|
consumed = r.consumed
|
|
value = r.value
|
|
next = r.next
|
|
msg = r.message
|
|
}
|
|
return
|
|
}
|
|
|
|
func (r Result[In, Out]) Consumed() bool {
|
|
return r.consumed
|
|
}
|
|
|
|
func (r Result[In, Out]) Consume(consumed bool) Result[In, Out] {
|
|
r.consumed = consumed
|
|
return r
|
|
}
|
|
|
|
type Message struct {
|
|
Pos uint64
|
|
Got string
|
|
Expected []string
|
|
}
|
|
|
|
func (m Message) expect(s string) Message {
|
|
m.Expected = []string{s}
|
|
return m
|
|
}
|
|
|
|
func MessageOK(pos uint64) Message { return Message{Pos: pos} }
|
|
|
|
func MessageEnd(pos uint64) Message { return Message{Pos: pos, Got: "end of input"} }
|
|
|
|
func MakeState[In any](c cursor.Cursor[In]) State[In] {
|
|
return State[In]{cursor: c}
|
|
}
|
|
|
|
type State[In any] struct {
|
|
cursor cursor.Cursor[In]
|
|
}
|
|
|
|
func (s State[In]) Cursor() cursor.Cursor[In] {
|
|
return s.cursor
|
|
}
|
|
|
|
func (s State[In]) Read(dst []In) (n uint64, next State[In], err error) {
|
|
n, c, err := s.cursor.Read(dst)
|
|
return n, State[In]{cursor: c}, err
|
|
}
|
|
|
|
func (s State[In]) Pos() uint64 {
|
|
return s.cursor.Pos()
|
|
}
|
|
|
|
func (s State[In]) At(pos uint64) State[In] {
|
|
return State[In]{cursor: s.cursor.At(pos)}
|
|
}
|
|
|
|
type Parser[In, Out any] func(State[In]) (Result[In, Out], error)
|
|
|
|
func Return[In, Out any](value Out) Parser[In, Out] {
|
|
return func(state State[In]) (Result[In, Out], error) {
|
|
return Succeed(false, value, state, MessageOK(state.Pos())), nil
|
|
}
|
|
}
|
|
|
|
func Satisfy[T any](pred func(T) bool) Parser[T, T] {
|
|
return func(state State[T]) (Result[T, T], error) {
|
|
token := make([]T, 1)
|
|
n, next, err := state.Read(token)
|
|
if errors.Is(err, io.EOF) {
|
|
return Fail[T, T](false, MessageEnd(state.Pos())), nil
|
|
}
|
|
if err != nil {
|
|
return Result[T, T]{}, err
|
|
}
|
|
if n != 1 {
|
|
panic(fmt.Sprintf("expected 1 element from Read, but got %d", n))
|
|
}
|
|
if pred(token[0]) {
|
|
return Succeed(true, token[0], next, MessageOK(state.Pos())), nil
|
|
}
|
|
return Fail[T, T](false, Message{
|
|
Pos: state.Pos(),
|
|
Got: fmt.Sprint(token),
|
|
}), nil
|
|
}
|
|
}
|
|
|
|
func Slice[T comparable](s []T) Parser[T, []T] {
|
|
expected := fmt.Sprint(s)
|
|
return func(state State[T]) (Result[T, []T], error) {
|
|
token := make([]T, len(s))
|
|
_, next, err := state.Read(token)
|
|
if errors.Is(err, io.EOF) {
|
|
return Fail[T, []T](false, MessageEnd(state.Pos())), nil
|
|
}
|
|
if err != nil {
|
|
return Result[T, []T]{}, err
|
|
}
|
|
if !slices.Equal(s, token) {
|
|
return Fail[T, []T](false, Message{
|
|
Pos: state.Pos(),
|
|
Got: fmt.Sprint(token),
|
|
Expected: []string{expected},
|
|
}), nil
|
|
}
|
|
return Succeed(true, token, next, MessageOK(state.Pos())), nil
|
|
}
|
|
}
|
|
|
|
func Bind[In, A, B any](p Parser[In, A], f func(A) Parser[In, B]) Parser[In, B] {
|
|
return func(input State[In]) (Result[In, B], error) {
|
|
resultA, err := p(input)
|
|
if err != nil {
|
|
return Result[In, B]{}, err
|
|
}
|
|
if ok, consumed, msg := resultA.Failed(); ok {
|
|
return Fail[In, B](consumed, msg), nil
|
|
}
|
|
|
|
_, consumedA, valueA, next, _ := resultA.Succeeded()
|
|
resultB, err := f(valueA)(next)
|
|
if err != nil {
|
|
return Result[In, B]{}, err
|
|
}
|
|
return resultB.Consume(consumedA || resultB.Consumed()), nil
|
|
}
|
|
}
|
|
|
|
func Choose[In, Out any](p Parser[In, Out], ps ...Parser[In, Out]) Parser[In, Out] {
|
|
// TODO Check this against the Parsec paper again, and simplify it.
|
|
all := append([]Parser[In, Out]{p}, ps...)
|
|
return func(input State[In]) (Result[In, Out], error) {
|
|
expecteds := make([][]string, 0, len(all))
|
|
var value Out
|
|
var got string
|
|
var failed bool
|
|
for _, q := range all {
|
|
result, err := q(input)
|
|
if err != nil {
|
|
return Result[In, Out]{}, err
|
|
}
|
|
if result.Consumed() {
|
|
return result, nil
|
|
}
|
|
var qMsg Message
|
|
if isFailure, _, msg := result.Failed(); isFailure {
|
|
qMsg = msg
|
|
failed = true
|
|
} else {
|
|
_, _, qValue, _, msg := result.Succeeded()
|
|
if failed {
|
|
value = qValue
|
|
failed = false
|
|
}
|
|
qMsg = msg
|
|
}
|
|
if got == "" {
|
|
got = qMsg.Got
|
|
}
|
|
}
|
|
msg := Message{Pos: input.Pos(), Got: got, Expected: slices.Concat(expecteds...)}
|
|
if failed {
|
|
return Fail[In, Out](false, msg), nil
|
|
}
|
|
return Succeed(false, value, input, msg), nil
|
|
}
|
|
}
|
|
|
|
// Try behaves identically to p, except that if p returns an error,
|
|
// Try will pretend that no input was consumed. This allows infinite
|
|
// lookahead: Since Choose only calls another parser when the previous
|
|
// parser consumed nothing, Try will allow backing out of a complex
|
|
// parser that partially succeeded.
|
|
func Try[In, Out any](p Parser[In, Out]) Parser[In, Out] {
|
|
return func(input State[In]) (Result[In, Out], error) {
|
|
result, err := p(input)
|
|
if err != nil {
|
|
return result, err
|
|
}
|
|
if failed, _, msg := result.Failed(); failed {
|
|
return Fail[In, Out](false, msg), nil
|
|
}
|
|
return result, nil
|
|
}
|
|
}
|
|
|
|
func Label[In, Out any](p Parser[In, Out], l string) Parser[In, Out] {
|
|
return func(input State[In]) (Result[In, Out], error) {
|
|
result, err := p(input)
|
|
if err != nil || result.Consumed() {
|
|
return result, err
|
|
}
|
|
if succeeded, _, value, next, msg := result.Succeeded(); succeeded {
|
|
return Succeed(false, value, next, msg.expect(l)), nil
|
|
}
|
|
_, _, msg := result.Failed()
|
|
return Fail[In, Out](false, msg.expect(l)), nil
|
|
}
|
|
}
|
|
|
|
func Map[In, Out1, Out2 any](p Parser[In, Out1], f func(Out1) Out2) Parser[In, Out2] {
|
|
return Bind(p, func(out Out1) Parser[In, Out2] {
|
|
return Return[In](f(out))
|
|
})
|
|
}
|
|
|
|
func end[In any](s State[In]) (Result[In, struct{}], error) {
|
|
_, _, err := s.cursor.Read([]In{})
|
|
if errors.Is(err, io.EOF) {
|
|
return Succeed(true, struct{}{}, s, MessageOK(s.Pos())), nil
|
|
}
|
|
if err != nil {
|
|
return Result[In, struct{}]{}, fmt.Errorf("End: unexpected error: %w", err)
|
|
}
|
|
return Fail[In, struct{}](false, Message{
|
|
Pos: s.Pos(),
|
|
Got: "",
|
|
Expected: []string{"end of input"},
|
|
}), nil
|
|
}
|
|
|
|
func End[In any]() Parser[In, struct{}] {
|
|
return end
|
|
}
|