2024-09-02 17:04:00 +00:00
|
|
|
package gigaparsec
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2024-09-02 17:34:26 +00:00
|
|
|
"io"
|
2024-09-02 17:04:00 +00:00
|
|
|
|
2024-09-02 17:34:26 +00:00
|
|
|
"git.codemonkeysoftware.net/b/gigaparsec/cursor"
|
|
|
|
)
|
2024-09-02 17:04:00 +00:00
|
|
|
|
|
|
|
var ErrNoParse = errors.New("no parse")
|
|
|
|
|
2024-09-02 17:34:26 +00:00
|
|
|
type Result[In, Out any] struct {
|
|
|
|
Value Out
|
|
|
|
State[In]
|
2024-09-02 17:04:00 +00:00
|
|
|
Message
|
|
|
|
}
|
|
|
|
|
|
|
|
type Message struct {
|
2024-09-02 17:34:26 +00:00
|
|
|
Pos uint64
|
2024-09-02 21:31:08 +00:00
|
|
|
Got string
|
2024-09-02 17:04:00 +00:00
|
|
|
Expected []string
|
|
|
|
}
|
|
|
|
|
2024-09-02 21:31:08 +00:00
|
|
|
func MessageOK(pos uint64) Message { return Message{Pos: pos} }
|
|
|
|
|
|
|
|
func MessageEnd(pos uint64) Message { return Message{Pos: pos, Got: "end of input"} }
|
|
|
|
|
|
|
|
type ParseError Message
|
2024-09-02 17:04:00 +00:00
|
|
|
|
|
|
|
func (pe ParseError) Error() string {
|
2024-09-02 21:31:08 +00:00
|
|
|
return fmt.Sprintf("parse error: %d: %s", pe.Pos, pe.Got)
|
2024-09-02 17:04:00 +00:00
|
|
|
}
|
|
|
|
|
2024-09-02 17:34:26 +00:00
|
|
|
type State[T any] cursor.Cursor[T]
|
2024-09-02 17:04:00 +00:00
|
|
|
|
2024-09-02 17:34:26 +00:00
|
|
|
type Parser[In, Out any] func(State[In]) (consumed bool, reply Result[In, Out], err error)
|
2024-09-02 17:04:00 +00:00
|
|
|
|
2024-09-02 17:34:26 +00:00
|
|
|
func Return[In, Out any](value Out) Parser[In, Out] {
|
|
|
|
return func(state State[In]) (bool, Result[In, Out], error) {
|
2024-09-02 21:31:08 +00:00
|
|
|
return false, Result[In, Out]{
|
|
|
|
Value: value,
|
|
|
|
State: state,
|
|
|
|
Message: MessageOK(state.Pos()),
|
|
|
|
}, nil
|
2024-09-02 17:04:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-02 17:34:26 +00:00
|
|
|
func Satisfy[T any](pred func(T) bool) Parser[T, T] {
|
|
|
|
return func(state State[T]) (bool, Result[T, T], error) {
|
2024-09-02 21:31:08 +00:00
|
|
|
token := make([]T, 1)
|
|
|
|
n, next, err := state.Read(token)
|
2024-09-02 17:34:26 +00:00
|
|
|
if errors.Is(err, io.EOF) {
|
2024-09-02 21:31:08 +00:00
|
|
|
return false, Result[T, T]{}, ParseError(MessageEnd(state.Pos()))
|
2024-09-02 17:34:26 +00:00
|
|
|
}
|
|
|
|
if n != 1 {
|
|
|
|
panic(fmt.Sprintf("expected 1 element from Read, but got %d", n))
|
2024-09-02 17:04:00 +00:00
|
|
|
}
|
2024-09-02 21:31:08 +00:00
|
|
|
if pred(token[0]) {
|
|
|
|
return true, Result[T, T]{
|
|
|
|
Value: token[0],
|
|
|
|
State: next,
|
|
|
|
Message: MessageOK(state.Pos()),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
return false, Result[T, T]{}, ParseError{
|
|
|
|
Pos: state.Pos(),
|
|
|
|
Got: fmt.Sprint(token),
|
2024-09-02 17:04:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-02 17:34:26 +00:00
|
|
|
func Bind[In, A, B any](p Parser[In, A], f func(A) Parser[In, B]) Parser[In, B] {
|
|
|
|
return func(input State[In]) (bool, Result[In, B], error) {
|
|
|
|
consumed, resultA, err := p(input)
|
2024-09-02 17:04:00 +00:00
|
|
|
if err != nil {
|
2024-09-02 17:34:26 +00:00
|
|
|
return false, Result[In, B]{}, err
|
2024-09-02 17:04:00 +00:00
|
|
|
}
|
2024-09-02 17:34:26 +00:00
|
|
|
consumed2, replyB, err := f(resultA.Value)(resultA.State)
|
2024-09-02 17:04:00 +00:00
|
|
|
return consumed || consumed2, replyB, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-02 21:31:08 +00:00
|
|
|
// TODO Add error propagation
|
2024-09-02 17:34:26 +00:00
|
|
|
func Choose[In, Out any](p, q Parser[In, Out]) Parser[In, Out] {
|
|
|
|
return func(input State[In]) (bool, Result[In, Out], error) {
|
2024-09-02 17:04:00 +00:00
|
|
|
consumedP, replyP, errP := p(input)
|
|
|
|
if consumedP {
|
|
|
|
return consumedP, replyP, errP
|
|
|
|
}
|
|
|
|
if errP != nil {
|
|
|
|
return q(input)
|
|
|
|
}
|
|
|
|
consumedQ, replyQ, errQ := q(input)
|
|
|
|
if consumedQ {
|
|
|
|
return consumedQ, replyQ, errQ
|
|
|
|
}
|
|
|
|
return consumedP, replyP, errP
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-02 18:48:48 +00:00
|
|
|
// 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 did partially succeeded.
|
2024-09-02 17:34:26 +00:00
|
|
|
func Try[In, Out any](p Parser[In, Out]) Parser[In, Out] {
|
|
|
|
return func(input State[In]) (bool, Result[In, Out], error) {
|
2024-09-02 17:04:00 +00:00
|
|
|
consumed, reply, err := p(input)
|
|
|
|
if err != nil {
|
2024-09-02 17:34:26 +00:00
|
|
|
return false, Result[In, Out]{}, err
|
2024-09-02 17:04:00 +00:00
|
|
|
}
|
|
|
|
return consumed, reply, err
|
|
|
|
}
|
|
|
|
}
|