Compare commits
14 Commits
bc2f7aa911
...
master
Author | SHA1 | Date | |
---|---|---|---|
e60f6ae015 | |||
035fa7da14 | |||
82ed6b5546 | |||
4e157f7a0e | |||
1892a97070 | |||
981edc92f7 | |||
74237e2593 | |||
17c468a3f7 | |||
ac739c0f3b | |||
b3a6bfc02e | |||
c85d0d280e | |||
fa6c15566d | |||
13d83e70ad | |||
56536b04f6 |
@ -1,8 +1,8 @@
|
|||||||
# Gigaparsec
|
# Gigaparsec
|
||||||
|
|
||||||
[](https://pkg.go.dev/git.codemonkeysoftware.net/b/gigaparsec)
|
[](https://pkg.go.dev/git.codemonkeysoftware.net/b/gigaparsec)
|
||||||

|
|
||||||
|
by Brandon Dyck <[brandon@dyck.us](mailto:brandon@dyck.us)>
|
||||||
|
|
||||||
Monadic parser combinators in Go
|
Monadic parser combinators in Go
|
||||||
|
|
||||||
**I don't recommend using this yet. It is very unfinished and it will break.**
|
|
||||||
|
3
TODO.txt
3
TODO.txt
@ -4,3 +4,6 @@ Rename Seq2 to Seq
|
|||||||
Document Seq
|
Document Seq
|
||||||
Should MakeState be private now that there's Run?
|
Should MakeState be private now that there's Run?
|
||||||
What's Megaparsec got that we ain't got?
|
What's Megaparsec got that we ain't got?
|
||||||
|
Add and benchmark naïve Seq
|
||||||
|
chainl
|
||||||
|
whitespace handling
|
||||||
|
418
bind.go
418
bind.go
@ -278,3 +278,421 @@ func Bind5[In, Out, T, T2, T3, T4, T5 any](
|
|||||||
return Succeed(anyConsumed, val6, next, MessageOK(s.Pos())), nil
|
return Succeed(anyConsumed, val6, next, MessageOK(s.Pos())), nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Bind6 is equivalent to 6 nested calls to Bind.
|
||||||
|
func Bind6[In, Out, T, T2, T3, T4, T5, T6 any](
|
||||||
|
p Parser[In, T],
|
||||||
|
f func(T) Parser[In, T2],
|
||||||
|
f2 func(T2) Parser[In, T3],
|
||||||
|
f3 func(T3) Parser[In, T4],
|
||||||
|
f4 func(T4) Parser[In, T5],
|
||||||
|
f5 func(T5) Parser[In, T6],
|
||||||
|
f6 func(T6) Parser[In, Out],
|
||||||
|
) Parser[In, Out] {
|
||||||
|
return func(s State[In]) (Result[In, Out], error) {
|
||||||
|
var anyConsumed bool
|
||||||
|
var next = s
|
||||||
|
|
||||||
|
r, err := p(next)
|
||||||
|
if err != nil {
|
||||||
|
return Result[In, Out]{}, err
|
||||||
|
}
|
||||||
|
anyConsumed = anyConsumed || r.Consumed()
|
||||||
|
success, val, next := r.Status()
|
||||||
|
if !success {
|
||||||
|
return Fail[In, Out](anyConsumed, r.Message()), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
r2, err := f(val)(next)
|
||||||
|
if err != nil {
|
||||||
|
return Result[In, Out]{}, err
|
||||||
|
}
|
||||||
|
anyConsumed = anyConsumed || r2.Consumed()
|
||||||
|
success, val2, next := r2.Status()
|
||||||
|
if !success {
|
||||||
|
return Fail[In, Out](anyConsumed, r2.Message()), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
r3, err := f2(val2)(next)
|
||||||
|
if err != nil {
|
||||||
|
return Result[In, Out]{}, err
|
||||||
|
}
|
||||||
|
anyConsumed = anyConsumed || r3.Consumed()
|
||||||
|
success, val3, next := r3.Status()
|
||||||
|
if !success {
|
||||||
|
return Fail[In, Out](anyConsumed, r3.Message()), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
r4, err := f3(val3)(next)
|
||||||
|
if err != nil {
|
||||||
|
return Result[In, Out]{}, err
|
||||||
|
}
|
||||||
|
anyConsumed = anyConsumed || r4.Consumed()
|
||||||
|
success, val4, next := r4.Status()
|
||||||
|
if !success {
|
||||||
|
return Fail[In, Out](anyConsumed, r4.Message()), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
r5, err := f4(val4)(next)
|
||||||
|
if err != nil {
|
||||||
|
return Result[In, Out]{}, err
|
||||||
|
}
|
||||||
|
anyConsumed = anyConsumed || r5.Consumed()
|
||||||
|
success, val5, next := r5.Status()
|
||||||
|
if !success {
|
||||||
|
return Fail[In, Out](anyConsumed, r5.Message()), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
r6, err := f5(val5)(next)
|
||||||
|
if err != nil {
|
||||||
|
return Result[In, Out]{}, err
|
||||||
|
}
|
||||||
|
anyConsumed = anyConsumed || r6.Consumed()
|
||||||
|
success, val6, next := r6.Status()
|
||||||
|
if !success {
|
||||||
|
return Fail[In, Out](anyConsumed, r6.Message()), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
r7, err := f6(val6)(next)
|
||||||
|
if err != nil {
|
||||||
|
return Result[In, Out]{}, err
|
||||||
|
}
|
||||||
|
anyConsumed = anyConsumed || r7.Consumed()
|
||||||
|
success, val7, next := r7.Status()
|
||||||
|
if !success {
|
||||||
|
return Fail[In, Out](anyConsumed, r7.Message()), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return Succeed(anyConsumed, val7, next, MessageOK(s.Pos())), nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Bind7 is equivalent to 7 nested calls to Bind.
|
||||||
|
func Bind7[In, Out, T, T2, T3, T4, T5, T6, T7 any](
|
||||||
|
p Parser[In, T],
|
||||||
|
f func(T) Parser[In, T2],
|
||||||
|
f2 func(T2) Parser[In, T3],
|
||||||
|
f3 func(T3) Parser[In, T4],
|
||||||
|
f4 func(T4) Parser[In, T5],
|
||||||
|
f5 func(T5) Parser[In, T6],
|
||||||
|
f6 func(T6) Parser[In, T7],
|
||||||
|
f7 func(T7) Parser[In, Out],
|
||||||
|
) Parser[In, Out] {
|
||||||
|
return func(s State[In]) (Result[In, Out], error) {
|
||||||
|
var anyConsumed bool
|
||||||
|
var next = s
|
||||||
|
|
||||||
|
r, err := p(next)
|
||||||
|
if err != nil {
|
||||||
|
return Result[In, Out]{}, err
|
||||||
|
}
|
||||||
|
anyConsumed = anyConsumed || r.Consumed()
|
||||||
|
success, val, next := r.Status()
|
||||||
|
if !success {
|
||||||
|
return Fail[In, Out](anyConsumed, r.Message()), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
r2, err := f(val)(next)
|
||||||
|
if err != nil {
|
||||||
|
return Result[In, Out]{}, err
|
||||||
|
}
|
||||||
|
anyConsumed = anyConsumed || r2.Consumed()
|
||||||
|
success, val2, next := r2.Status()
|
||||||
|
if !success {
|
||||||
|
return Fail[In, Out](anyConsumed, r2.Message()), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
r3, err := f2(val2)(next)
|
||||||
|
if err != nil {
|
||||||
|
return Result[In, Out]{}, err
|
||||||
|
}
|
||||||
|
anyConsumed = anyConsumed || r3.Consumed()
|
||||||
|
success, val3, next := r3.Status()
|
||||||
|
if !success {
|
||||||
|
return Fail[In, Out](anyConsumed, r3.Message()), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
r4, err := f3(val3)(next)
|
||||||
|
if err != nil {
|
||||||
|
return Result[In, Out]{}, err
|
||||||
|
}
|
||||||
|
anyConsumed = anyConsumed || r4.Consumed()
|
||||||
|
success, val4, next := r4.Status()
|
||||||
|
if !success {
|
||||||
|
return Fail[In, Out](anyConsumed, r4.Message()), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
r5, err := f4(val4)(next)
|
||||||
|
if err != nil {
|
||||||
|
return Result[In, Out]{}, err
|
||||||
|
}
|
||||||
|
anyConsumed = anyConsumed || r5.Consumed()
|
||||||
|
success, val5, next := r5.Status()
|
||||||
|
if !success {
|
||||||
|
return Fail[In, Out](anyConsumed, r5.Message()), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
r6, err := f5(val5)(next)
|
||||||
|
if err != nil {
|
||||||
|
return Result[In, Out]{}, err
|
||||||
|
}
|
||||||
|
anyConsumed = anyConsumed || r6.Consumed()
|
||||||
|
success, val6, next := r6.Status()
|
||||||
|
if !success {
|
||||||
|
return Fail[In, Out](anyConsumed, r6.Message()), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
r7, err := f6(val6)(next)
|
||||||
|
if err != nil {
|
||||||
|
return Result[In, Out]{}, err
|
||||||
|
}
|
||||||
|
anyConsumed = anyConsumed || r7.Consumed()
|
||||||
|
success, val7, next := r7.Status()
|
||||||
|
if !success {
|
||||||
|
return Fail[In, Out](anyConsumed, r7.Message()), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
r8, err := f7(val7)(next)
|
||||||
|
if err != nil {
|
||||||
|
return Result[In, Out]{}, err
|
||||||
|
}
|
||||||
|
anyConsumed = anyConsumed || r8.Consumed()
|
||||||
|
success, val8, next := r8.Status()
|
||||||
|
if !success {
|
||||||
|
return Fail[In, Out](anyConsumed, r8.Message()), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return Succeed(anyConsumed, val8, next, MessageOK(s.Pos())), nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Bind8 is equivalent to 8 nested calls to Bind.
|
||||||
|
func Bind8[In, Out, T, T2, T3, T4, T5, T6, T7, T8 any](
|
||||||
|
p Parser[In, T],
|
||||||
|
f func(T) Parser[In, T2],
|
||||||
|
f2 func(T2) Parser[In, T3],
|
||||||
|
f3 func(T3) Parser[In, T4],
|
||||||
|
f4 func(T4) Parser[In, T5],
|
||||||
|
f5 func(T5) Parser[In, T6],
|
||||||
|
f6 func(T6) Parser[In, T7],
|
||||||
|
f7 func(T7) Parser[In, T8],
|
||||||
|
f8 func(T8) Parser[In, Out],
|
||||||
|
) Parser[In, Out] {
|
||||||
|
return func(s State[In]) (Result[In, Out], error) {
|
||||||
|
var anyConsumed bool
|
||||||
|
var next = s
|
||||||
|
|
||||||
|
r, err := p(next)
|
||||||
|
if err != nil {
|
||||||
|
return Result[In, Out]{}, err
|
||||||
|
}
|
||||||
|
anyConsumed = anyConsumed || r.Consumed()
|
||||||
|
success, val, next := r.Status()
|
||||||
|
if !success {
|
||||||
|
return Fail[In, Out](anyConsumed, r.Message()), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
r2, err := f(val)(next)
|
||||||
|
if err != nil {
|
||||||
|
return Result[In, Out]{}, err
|
||||||
|
}
|
||||||
|
anyConsumed = anyConsumed || r2.Consumed()
|
||||||
|
success, val2, next := r2.Status()
|
||||||
|
if !success {
|
||||||
|
return Fail[In, Out](anyConsumed, r2.Message()), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
r3, err := f2(val2)(next)
|
||||||
|
if err != nil {
|
||||||
|
return Result[In, Out]{}, err
|
||||||
|
}
|
||||||
|
anyConsumed = anyConsumed || r3.Consumed()
|
||||||
|
success, val3, next := r3.Status()
|
||||||
|
if !success {
|
||||||
|
return Fail[In, Out](anyConsumed, r3.Message()), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
r4, err := f3(val3)(next)
|
||||||
|
if err != nil {
|
||||||
|
return Result[In, Out]{}, err
|
||||||
|
}
|
||||||
|
anyConsumed = anyConsumed || r4.Consumed()
|
||||||
|
success, val4, next := r4.Status()
|
||||||
|
if !success {
|
||||||
|
return Fail[In, Out](anyConsumed, r4.Message()), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
r5, err := f4(val4)(next)
|
||||||
|
if err != nil {
|
||||||
|
return Result[In, Out]{}, err
|
||||||
|
}
|
||||||
|
anyConsumed = anyConsumed || r5.Consumed()
|
||||||
|
success, val5, next := r5.Status()
|
||||||
|
if !success {
|
||||||
|
return Fail[In, Out](anyConsumed, r5.Message()), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
r6, err := f5(val5)(next)
|
||||||
|
if err != nil {
|
||||||
|
return Result[In, Out]{}, err
|
||||||
|
}
|
||||||
|
anyConsumed = anyConsumed || r6.Consumed()
|
||||||
|
success, val6, next := r6.Status()
|
||||||
|
if !success {
|
||||||
|
return Fail[In, Out](anyConsumed, r6.Message()), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
r7, err := f6(val6)(next)
|
||||||
|
if err != nil {
|
||||||
|
return Result[In, Out]{}, err
|
||||||
|
}
|
||||||
|
anyConsumed = anyConsumed || r7.Consumed()
|
||||||
|
success, val7, next := r7.Status()
|
||||||
|
if !success {
|
||||||
|
return Fail[In, Out](anyConsumed, r7.Message()), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
r8, err := f7(val7)(next)
|
||||||
|
if err != nil {
|
||||||
|
return Result[In, Out]{}, err
|
||||||
|
}
|
||||||
|
anyConsumed = anyConsumed || r8.Consumed()
|
||||||
|
success, val8, next := r8.Status()
|
||||||
|
if !success {
|
||||||
|
return Fail[In, Out](anyConsumed, r8.Message()), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
r9, err := f8(val8)(next)
|
||||||
|
if err != nil {
|
||||||
|
return Result[In, Out]{}, err
|
||||||
|
}
|
||||||
|
anyConsumed = anyConsumed || r9.Consumed()
|
||||||
|
success, val9, next := r9.Status()
|
||||||
|
if !success {
|
||||||
|
return Fail[In, Out](anyConsumed, r9.Message()), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return Succeed(anyConsumed, val9, next, MessageOK(s.Pos())), nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Bind9 is equivalent to 9 nested calls to Bind.
|
||||||
|
func Bind9[In, Out, T, T2, T3, T4, T5, T6, T7, T8, T9 any](
|
||||||
|
p Parser[In, T],
|
||||||
|
f func(T) Parser[In, T2],
|
||||||
|
f2 func(T2) Parser[In, T3],
|
||||||
|
f3 func(T3) Parser[In, T4],
|
||||||
|
f4 func(T4) Parser[In, T5],
|
||||||
|
f5 func(T5) Parser[In, T6],
|
||||||
|
f6 func(T6) Parser[In, T7],
|
||||||
|
f7 func(T7) Parser[In, T8],
|
||||||
|
f8 func(T8) Parser[In, T9],
|
||||||
|
f9 func(T9) Parser[In, Out],
|
||||||
|
) Parser[In, Out] {
|
||||||
|
return func(s State[In]) (Result[In, Out], error) {
|
||||||
|
var anyConsumed bool
|
||||||
|
var next = s
|
||||||
|
|
||||||
|
r, err := p(next)
|
||||||
|
if err != nil {
|
||||||
|
return Result[In, Out]{}, err
|
||||||
|
}
|
||||||
|
anyConsumed = anyConsumed || r.Consumed()
|
||||||
|
success, val, next := r.Status()
|
||||||
|
if !success {
|
||||||
|
return Fail[In, Out](anyConsumed, r.Message()), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
r2, err := f(val)(next)
|
||||||
|
if err != nil {
|
||||||
|
return Result[In, Out]{}, err
|
||||||
|
}
|
||||||
|
anyConsumed = anyConsumed || r2.Consumed()
|
||||||
|
success, val2, next := r2.Status()
|
||||||
|
if !success {
|
||||||
|
return Fail[In, Out](anyConsumed, r2.Message()), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
r3, err := f2(val2)(next)
|
||||||
|
if err != nil {
|
||||||
|
return Result[In, Out]{}, err
|
||||||
|
}
|
||||||
|
anyConsumed = anyConsumed || r3.Consumed()
|
||||||
|
success, val3, next := r3.Status()
|
||||||
|
if !success {
|
||||||
|
return Fail[In, Out](anyConsumed, r3.Message()), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
r4, err := f3(val3)(next)
|
||||||
|
if err != nil {
|
||||||
|
return Result[In, Out]{}, err
|
||||||
|
}
|
||||||
|
anyConsumed = anyConsumed || r4.Consumed()
|
||||||
|
success, val4, next := r4.Status()
|
||||||
|
if !success {
|
||||||
|
return Fail[In, Out](anyConsumed, r4.Message()), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
r5, err := f4(val4)(next)
|
||||||
|
if err != nil {
|
||||||
|
return Result[In, Out]{}, err
|
||||||
|
}
|
||||||
|
anyConsumed = anyConsumed || r5.Consumed()
|
||||||
|
success, val5, next := r5.Status()
|
||||||
|
if !success {
|
||||||
|
return Fail[In, Out](anyConsumed, r5.Message()), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
r6, err := f5(val5)(next)
|
||||||
|
if err != nil {
|
||||||
|
return Result[In, Out]{}, err
|
||||||
|
}
|
||||||
|
anyConsumed = anyConsumed || r6.Consumed()
|
||||||
|
success, val6, next := r6.Status()
|
||||||
|
if !success {
|
||||||
|
return Fail[In, Out](anyConsumed, r6.Message()), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
r7, err := f6(val6)(next)
|
||||||
|
if err != nil {
|
||||||
|
return Result[In, Out]{}, err
|
||||||
|
}
|
||||||
|
anyConsumed = anyConsumed || r7.Consumed()
|
||||||
|
success, val7, next := r7.Status()
|
||||||
|
if !success {
|
||||||
|
return Fail[In, Out](anyConsumed, r7.Message()), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
r8, err := f7(val7)(next)
|
||||||
|
if err != nil {
|
||||||
|
return Result[In, Out]{}, err
|
||||||
|
}
|
||||||
|
anyConsumed = anyConsumed || r8.Consumed()
|
||||||
|
success, val8, next := r8.Status()
|
||||||
|
if !success {
|
||||||
|
return Fail[In, Out](anyConsumed, r8.Message()), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
r9, err := f8(val8)(next)
|
||||||
|
if err != nil {
|
||||||
|
return Result[In, Out]{}, err
|
||||||
|
}
|
||||||
|
anyConsumed = anyConsumed || r9.Consumed()
|
||||||
|
success, val9, next := r9.Status()
|
||||||
|
if !success {
|
||||||
|
return Fail[In, Out](anyConsumed, r9.Message()), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
r10, err := f9(val9)(next)
|
||||||
|
if err != nil {
|
||||||
|
return Result[In, Out]{}, err
|
||||||
|
}
|
||||||
|
anyConsumed = anyConsumed || r10.Consumed()
|
||||||
|
success, val10, next := r10.Status()
|
||||||
|
if !success {
|
||||||
|
return Fail[In, Out](anyConsumed, r10.Message()), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return Succeed(anyConsumed, val10, next, MessageOK(s.Pos())), nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
16
bytes/bytes.go
Normal file
16
bytes/bytes.go
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
package bytes
|
||||||
|
|
||||||
|
import (
|
||||||
|
"git.codemonkeysoftware.net/b/gigaparsec"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Token[Out, WSOut any](whitespace gigaparsec.Parser[byte, WSOut]) func(p gigaparsec.Parser[byte, Out]) gigaparsec.Parser[byte, Out] {
|
||||||
|
mappedWS := gigaparsec.Map(whitespace, func(WSOut) struct{} { return struct{}{} })
|
||||||
|
var ignoreWS gigaparsec.Parser[byte, struct{}] = func(s gigaparsec.State[byte]) (gigaparsec.Result[byte, struct{}], error) {
|
||||||
|
result, err := mappedWS(s)
|
||||||
|
return result.Consume(false), err
|
||||||
|
}
|
||||||
|
return func(p gigaparsec.Parser[byte, Out]) gigaparsec.Parser[byte, Out] {
|
||||||
|
return gigaparsec.Seq2(p, gigaparsec.Repeat(0, ignoreWS), func(val Out, _ []struct{}) Out { return val })
|
||||||
|
}
|
||||||
|
}
|
@ -69,6 +69,9 @@ func Regexp(pattern string) gigaparsec.Parser[byte, string] {
|
|||||||
return gigaparsec.Result[byte, string]{}, fmt.Errorf("Regexp: reader error: %w", err)
|
return gigaparsec.Result[byte, string]{}, fmt.Errorf("Regexp: reader error: %w", err)
|
||||||
}
|
}
|
||||||
if idx == nil {
|
if idx == nil {
|
||||||
|
if err == io.EOF {
|
||||||
|
return gigaparsec.Fail[byte, string](false, gigaparsec.MessageEnd(input.Pos())), nil
|
||||||
|
}
|
||||||
got := make([]byte, r.Count())
|
got := make([]byte, r.Count())
|
||||||
_, _, err = input.Read(got)
|
_, _, err = input.Read(got)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -80,7 +83,7 @@ func Regexp(pattern string) gigaparsec.Parser[byte, string] {
|
|||||||
// when searching a RuneReader.
|
// when searching a RuneReader.
|
||||||
dst := make([]byte, idx[1]-idx[0])
|
dst := make([]byte, idx[1]-idx[0])
|
||||||
n, _, err := input.Read(dst)
|
n, _, err := input.Read(dst)
|
||||||
if err != nil {
|
if err != nil && (!errors.Is(err, io.EOF) || n < uint64(len(dst))) {
|
||||||
return gigaparsec.Result[byte, string]{}, fmt.Errorf("Regexp: unexpected error: %w", err)
|
return gigaparsec.Result[byte, string]{}, fmt.Errorf("Regexp: unexpected error: %w", err)
|
||||||
}
|
}
|
||||||
next := input.At(input.Pos() + n)
|
next := input.At(input.Pos() + n)
|
||||||
|
@ -61,6 +61,21 @@ func TestRegexp(t *testing.T) {
|
|||||||
must.NoError(t, err)
|
must.NoError(t, err)
|
||||||
test.StrContains(t, result.Message().Got(), "hella")
|
test.StrContains(t, result.Message().Got(), "hella")
|
||||||
})
|
})
|
||||||
|
t.Run("succeeds on empty matches", func(t *testing.T) {
|
||||||
|
p := pbytes.Regexp(".*")
|
||||||
|
result, err := p(gigaparsec.MakeState(strings.NewReader("")))
|
||||||
|
succeeded, value, _ := result.Status()
|
||||||
|
must.NoError(t, err)
|
||||||
|
must.True(t, succeeded)
|
||||||
|
must.EqOp(t, "", value)
|
||||||
|
})
|
||||||
|
t.Run("fails without an error at EOF", func(t *testing.T) {
|
||||||
|
p := pbytes.Regexp("a")
|
||||||
|
result, err := p(gigaparsec.MakeState(strings.NewReader("")))
|
||||||
|
succeeded, _, _ := result.Status()
|
||||||
|
must.NoError(t, err)
|
||||||
|
must.False(t, succeeded)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRuneReader(t *testing.T) {
|
func TestRuneReader(t *testing.T) {
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
package gigaparsec
|
package gigaparsec
|
||||||
|
|
||||||
//go:generate go run ./internal/bindgen -bindpath bind.go -seqpath seq.go -max 5 -pkg gigaparsec
|
//go:generate go run ./internal/bindgen -bindpath bind.go -seqpath seq.go -max 9 -pkg gigaparsec
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
@ -194,6 +194,17 @@ func (p Parser[In, Out]) Label(label string) Parser[In, Out] {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p Parser[In, Out]) Where(pred func(Out) bool) Parser[In, Out] {
|
||||||
|
return func(s State[In]) (Result[In, Out], error) {
|
||||||
|
result, err := p(s)
|
||||||
|
if result.success && !pred(result.value) {
|
||||||
|
result.success = false
|
||||||
|
result.message.got = "failed Where predicate"
|
||||||
|
}
|
||||||
|
return result, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
type ParseError Message
|
type ParseError Message
|
||||||
|
|
||||||
func (pe ParseError) Error() string {
|
func (pe ParseError) Error() string {
|
||||||
@ -403,3 +414,16 @@ func Repeat[In, Out any](minCount int, p Parser[In, Out]) Parser[In, []Out] {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Lazy delays creating a parser from p until the parser is called.
|
||||||
|
// This is useful for preventing recursive function calls in the
|
||||||
|
// definition of a recursive parser.
|
||||||
|
func Lazy[In, Out any](p func() Parser[In, Out]) Parser[In, Out] {
|
||||||
|
return func(s State[In]) (Result[In, Out], error) {
|
||||||
|
return p()(s)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Bracket[In, Out, LOut, ROut any](left Parser[In, LOut], p Parser[In, Out], right Parser[In, ROut]) Parser[In, Out] {
|
||||||
|
return Seq3(left, p, right, func(_ LOut, val Out, _ ROut) Out { return val })
|
||||||
|
}
|
||||||
|
@ -1,19 +1,19 @@
|
|||||||
- Hutton & Meijer, _[Monadic Parser Combinators](http://www.cs.nott.ac.uk/~pszgmh/monparsing.pdf)_
|
- Hutton & Meijer, _[Monadic Parser Combinators](http://www.cs.nott.ac.uk/~pszgmh/monparsing.pdf)_
|
||||||
|
|
||||||
The original paper on monadic parser combinators.
|
The original paper on monadic parser combinators.
|
||||||
|
|
||||||
- Leijen & Meijer, _[Parsec: Direct Style Monadic Parser Combinators For The Real World](https://www.microsoft.com/en-us/research/wp-content/uploads/2016/02/parsec-paper-letter.pdf)_
|
- Leijen & Meijer, _[Parsec: Direct Style Monadic Parser Combinators For The Real World](https://www.microsoft.com/en-us/research/wp-content/uploads/2016/02/parsec-paper-letter.pdf)_
|
||||||
|
|
||||||
Describes Parsec, a monadic parser combinator library that limits backtracking by default and adds better error messages.
|
Describes Parsec, a monadic parser combinator library that limits backtracking by default and adds better error messages.
|
||||||
|
|
||||||
- [Megaparsec](https://hackage.haskell.org/package/megaparsec)
|
- [Megaparsec](https://hackage.haskell.org/package/megaparsec)
|
||||||
|
|
||||||
Improved fork of Megaparsec.
|
Improved fork of Megaparsec.
|
||||||
|
|
||||||
- [FParsec](https://github.com/stephan-tolksdorf/fparsec)
|
- [FParsec](https://github.com/stephan-tolksdorf/fparsec)
|
||||||
|
|
||||||
Parsec implementation in F#.
|
Parsec implementation in F#.
|
||||||
|
|
||||||
- [Sprache](https://github.com/sprache/Sprache)
|
- [Sprache](https://github.com/sprache/Sprache)
|
||||||
|
|
||||||
Parsec implementation in C#.
|
Parsec implementation in C#.
|
||||||
|
265
naive/naive.go
Normal file
265
naive/naive.go
Normal file
@ -0,0 +1,265 @@
|
|||||||
|
// SPDX-License-Identifier: Unlicense
|
||||||
|
|
||||||
|
// Package naive contains naïve implementations of the Bind and Seq combinators.
|
||||||
|
// The accompanying tests include simple benchmarks comparing their performance.
|
||||||
|
package naive
|
||||||
|
|
||||||
|
import gp "git.codemonkeysoftware.net/b/gigaparsec"
|
||||||
|
|
||||||
|
func Bind2[In, Out, T, T2 any](
|
||||||
|
p gp.Parser[In, T],
|
||||||
|
f func(T) gp.Parser[In, T2],
|
||||||
|
f2 func(T2) gp.Parser[In, Out],
|
||||||
|
) gp.Parser[In, Out] {
|
||||||
|
return gp.Bind(gp.Bind(p, f), f2)
|
||||||
|
}
|
||||||
|
func Bind3[In, Out, T, T2, T3, T4, T5, T6, T7, T8, T9 any](
|
||||||
|
p gp.Parser[In, T],
|
||||||
|
f func(T) gp.Parser[In, T2],
|
||||||
|
f2 func(T2) gp.Parser[In, T3],
|
||||||
|
f3 func(T3) gp.Parser[In, Out],
|
||||||
|
) gp.Parser[In, Out] {
|
||||||
|
return gp.Bind(gp.Bind(gp.Bind(p, f), f2), f3)
|
||||||
|
}
|
||||||
|
func Bind4[In, Out, T, T2, T3, T4 any](
|
||||||
|
p gp.Parser[In, T],
|
||||||
|
f func(T) gp.Parser[In, T2],
|
||||||
|
f2 func(T2) gp.Parser[In, T3],
|
||||||
|
f3 func(T3) gp.Parser[In, T4],
|
||||||
|
f4 func(T4) gp.Parser[In, Out],
|
||||||
|
) gp.Parser[In, Out] {
|
||||||
|
return gp.Bind(gp.Bind(gp.Bind(gp.Bind(p, f), f2), f3), f4)
|
||||||
|
}
|
||||||
|
func Bind5[In, Out, T, T2, T3, T4, T5 any](
|
||||||
|
p gp.Parser[In, T],
|
||||||
|
f func(T) gp.Parser[In, T2],
|
||||||
|
f2 func(T2) gp.Parser[In, T3],
|
||||||
|
f3 func(T3) gp.Parser[In, T4],
|
||||||
|
f4 func(T4) gp.Parser[In, T5],
|
||||||
|
f5 func(T5) gp.Parser[In, Out],
|
||||||
|
) gp.Parser[In, Out] {
|
||||||
|
return gp.Bind(gp.Bind(gp.Bind(gp.Bind(gp.Bind(p, f), f2), f3), f4), f5)
|
||||||
|
}
|
||||||
|
func Bind6[In, Out, T, T2, T3, T4, T5, T6 any](
|
||||||
|
p gp.Parser[In, T],
|
||||||
|
f func(T) gp.Parser[In, T2],
|
||||||
|
f2 func(T2) gp.Parser[In, T3],
|
||||||
|
f3 func(T3) gp.Parser[In, T4],
|
||||||
|
f4 func(T4) gp.Parser[In, T5],
|
||||||
|
f5 func(T5) gp.Parser[In, T6],
|
||||||
|
f6 func(T6) gp.Parser[In, Out],
|
||||||
|
) gp.Parser[In, Out] {
|
||||||
|
return gp.Bind(gp.Bind(gp.Bind(gp.Bind(gp.Bind(gp.Bind(p, f), f2), f3), f4), f5), f6)
|
||||||
|
}
|
||||||
|
func Bind7[In, Out, T, T2, T3, T4, T5, T6, T7 any](
|
||||||
|
p gp.Parser[In, T],
|
||||||
|
f func(T) gp.Parser[In, T2],
|
||||||
|
f2 func(T2) gp.Parser[In, T3],
|
||||||
|
f3 func(T3) gp.Parser[In, T4],
|
||||||
|
f4 func(T4) gp.Parser[In, T5],
|
||||||
|
f5 func(T5) gp.Parser[In, T6],
|
||||||
|
f6 func(T6) gp.Parser[In, T7],
|
||||||
|
f7 func(T7) gp.Parser[In, Out],
|
||||||
|
) gp.Parser[In, Out] {
|
||||||
|
return gp.Bind(gp.Bind(gp.Bind(gp.Bind(gp.Bind(gp.Bind(gp.Bind(p, f), f2), f3), f4), f5), f6), f7)
|
||||||
|
}
|
||||||
|
func Bind8[In, Out, T, T2, T3, T4, T5, T6, T7, T8 any](
|
||||||
|
p gp.Parser[In, T],
|
||||||
|
f func(T) gp.Parser[In, T2],
|
||||||
|
f2 func(T2) gp.Parser[In, T3],
|
||||||
|
f3 func(T3) gp.Parser[In, T4],
|
||||||
|
f4 func(T4) gp.Parser[In, T5],
|
||||||
|
f5 func(T5) gp.Parser[In, T6],
|
||||||
|
f6 func(T6) gp.Parser[In, T7],
|
||||||
|
f7 func(T7) gp.Parser[In, T8],
|
||||||
|
f8 func(T8) gp.Parser[In, Out],
|
||||||
|
) gp.Parser[In, Out] {
|
||||||
|
return gp.Bind(gp.Bind(gp.Bind(gp.Bind(gp.Bind(gp.Bind(gp.Bind(gp.Bind(p, f), f2), f3), f4), f5), f6), f7), f8)
|
||||||
|
}
|
||||||
|
func Bind9[In, Out, T, T2, T3, T4, T5, T6, T7, T8, T9 any](
|
||||||
|
p gp.Parser[In, T],
|
||||||
|
f func(T) gp.Parser[In, T2],
|
||||||
|
f2 func(T2) gp.Parser[In, T3],
|
||||||
|
f3 func(T3) gp.Parser[In, T4],
|
||||||
|
f4 func(T4) gp.Parser[In, T5],
|
||||||
|
f5 func(T5) gp.Parser[In, T6],
|
||||||
|
f6 func(T6) gp.Parser[In, T7],
|
||||||
|
f7 func(T7) gp.Parser[In, T8],
|
||||||
|
f8 func(T8) gp.Parser[In, T9],
|
||||||
|
f9 func(T9) gp.Parser[In, Out],
|
||||||
|
) gp.Parser[In, Out] {
|
||||||
|
return gp.Bind(gp.Bind(gp.Bind(gp.Bind(gp.Bind(gp.Bind(gp.Bind(gp.Bind(gp.Bind(p, f), f2), f3), f4), f5), f6), f7), f8), f9)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Seq2[In, Out, T, T2 any](
|
||||||
|
p gp.Parser[In, T],
|
||||||
|
p2 gp.Parser[In, T2],
|
||||||
|
f func(T, T2) Out,
|
||||||
|
) gp.Parser[In, Out] {
|
||||||
|
return gp.Bind(p, func(x T) gp.Parser[In, Out] {
|
||||||
|
return gp.Bind(p2, func(x2 T2) gp.Parser[In, Out] {
|
||||||
|
return gp.Return[In](f(x, x2))
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
func Seq3[In, Out, T, T2, T3 any](
|
||||||
|
p gp.Parser[In, T],
|
||||||
|
p2 gp.Parser[In, T2],
|
||||||
|
p3 gp.Parser[In, T3],
|
||||||
|
f func(T, T2, T3) Out,
|
||||||
|
) gp.Parser[In, Out] {
|
||||||
|
return gp.Bind(p, func(x T) gp.Parser[In, Out] {
|
||||||
|
return gp.Bind(p2, func(x2 T2) gp.Parser[In, Out] {
|
||||||
|
return gp.Bind(p3, func(x3 T3) gp.Parser[In, Out] {
|
||||||
|
return gp.Return[In](f(x, x2, x3))
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
func Seq4[In, Out, T, T2, T3, T4 any](
|
||||||
|
p gp.Parser[In, T],
|
||||||
|
p2 gp.Parser[In, T2],
|
||||||
|
p3 gp.Parser[In, T3],
|
||||||
|
p4 gp.Parser[In, T4],
|
||||||
|
f func(T, T2, T3, T4) Out,
|
||||||
|
) gp.Parser[In, Out] {
|
||||||
|
return gp.Bind(p, func(x T) gp.Parser[In, Out] {
|
||||||
|
return gp.Bind(p2, func(x2 T2) gp.Parser[In, Out] {
|
||||||
|
return gp.Bind(p3, func(x3 T3) gp.Parser[In, Out] {
|
||||||
|
return gp.Bind(p4, func(x4 T4) gp.Parser[In, Out] {
|
||||||
|
return gp.Return[In](f(x, x2, x3, x4))
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
func Seq5[In, Out, T, T2, T3, T4, T5 any](
|
||||||
|
p gp.Parser[In, T],
|
||||||
|
p2 gp.Parser[In, T2],
|
||||||
|
p3 gp.Parser[In, T3],
|
||||||
|
p4 gp.Parser[In, T4],
|
||||||
|
p5 gp.Parser[In, T5],
|
||||||
|
f func(T, T2, T3, T4, T5) Out,
|
||||||
|
) gp.Parser[In, Out] {
|
||||||
|
return gp.Bind(p, func(x T) gp.Parser[In, Out] {
|
||||||
|
return gp.Bind(p2, func(x2 T2) gp.Parser[In, Out] {
|
||||||
|
return gp.Bind(p3, func(x3 T3) gp.Parser[In, Out] {
|
||||||
|
return gp.Bind(p4, func(x4 T4) gp.Parser[In, Out] {
|
||||||
|
return gp.Bind(p5, func(x5 T5) gp.Parser[In, Out] {
|
||||||
|
return gp.Return[In](f(x, x2, x3, x4, x5))
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
func Seq6[In, Out, T, T2, T3, T4, T5, T6 any](
|
||||||
|
p gp.Parser[In, T],
|
||||||
|
p2 gp.Parser[In, T2],
|
||||||
|
p3 gp.Parser[In, T3],
|
||||||
|
p4 gp.Parser[In, T4],
|
||||||
|
p5 gp.Parser[In, T5],
|
||||||
|
p6 gp.Parser[In, T6],
|
||||||
|
f func(T, T2, T3, T4, T5, T6) Out,
|
||||||
|
) gp.Parser[In, Out] {
|
||||||
|
return gp.Bind(p, func(x T) gp.Parser[In, Out] {
|
||||||
|
return gp.Bind(p2, func(x2 T2) gp.Parser[In, Out] {
|
||||||
|
return gp.Bind(p3, func(x3 T3) gp.Parser[In, Out] {
|
||||||
|
return gp.Bind(p4, func(x4 T4) gp.Parser[In, Out] {
|
||||||
|
return gp.Bind(p5, func(x5 T5) gp.Parser[In, Out] {
|
||||||
|
return gp.Bind(p6, func(x6 T6) gp.Parser[In, Out] {
|
||||||
|
return gp.Return[In](f(x, x2, x3, x4, x5, x6))
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
func Seq7[In, Out, T, T2, T3, T4, T5, T6, T7 any](
|
||||||
|
p gp.Parser[In, T],
|
||||||
|
p2 gp.Parser[In, T2],
|
||||||
|
p3 gp.Parser[In, T3],
|
||||||
|
p4 gp.Parser[In, T4],
|
||||||
|
p5 gp.Parser[In, T5],
|
||||||
|
p6 gp.Parser[In, T6],
|
||||||
|
p7 gp.Parser[In, T7],
|
||||||
|
f func(T, T2, T3, T4, T5, T6, T7) Out,
|
||||||
|
) gp.Parser[In, Out] {
|
||||||
|
return gp.Bind(p, func(x T) gp.Parser[In, Out] {
|
||||||
|
return gp.Bind(p2, func(x2 T2) gp.Parser[In, Out] {
|
||||||
|
return gp.Bind(p3, func(x3 T3) gp.Parser[In, Out] {
|
||||||
|
return gp.Bind(p4, func(x4 T4) gp.Parser[In, Out] {
|
||||||
|
return gp.Bind(p5, func(x5 T5) gp.Parser[In, Out] {
|
||||||
|
return gp.Bind(p6, func(x6 T6) gp.Parser[In, Out] {
|
||||||
|
return gp.Bind(p7, func(x7 T7) gp.Parser[In, Out] {
|
||||||
|
return gp.Return[In](f(x, x2, x3, x4, x5, x6, x7))
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
func Seq8[In, Out, T, T2, T3, T4, T5, T6, T7, T8 any](
|
||||||
|
p gp.Parser[In, T],
|
||||||
|
p2 gp.Parser[In, T2],
|
||||||
|
p3 gp.Parser[In, T3],
|
||||||
|
p4 gp.Parser[In, T4],
|
||||||
|
p5 gp.Parser[In, T5],
|
||||||
|
p6 gp.Parser[In, T6],
|
||||||
|
p7 gp.Parser[In, T7],
|
||||||
|
p8 gp.Parser[In, T8],
|
||||||
|
f func(T, T2, T3, T4, T5, T6, T7, T8) Out,
|
||||||
|
) gp.Parser[In, Out] {
|
||||||
|
return gp.Bind(p, func(x T) gp.Parser[In, Out] {
|
||||||
|
return gp.Bind(p2, func(x2 T2) gp.Parser[In, Out] {
|
||||||
|
return gp.Bind(p3, func(x3 T3) gp.Parser[In, Out] {
|
||||||
|
return gp.Bind(p4, func(x4 T4) gp.Parser[In, Out] {
|
||||||
|
return gp.Bind(p5, func(x5 T5) gp.Parser[In, Out] {
|
||||||
|
return gp.Bind(p6, func(x6 T6) gp.Parser[In, Out] {
|
||||||
|
return gp.Bind(p7, func(x7 T7) gp.Parser[In, Out] {
|
||||||
|
return gp.Bind(p8, func(x8 T8) gp.Parser[In, Out] {
|
||||||
|
return gp.Return[In](f(x, x2, x3, x4, x5, x6, x7, x8))
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
func Seq9[In, Out, T, T2, T3, T4, T5, T6, T7, T8, T9 any](
|
||||||
|
p gp.Parser[In, T],
|
||||||
|
p2 gp.Parser[In, T2],
|
||||||
|
p3 gp.Parser[In, T3],
|
||||||
|
p4 gp.Parser[In, T4],
|
||||||
|
p5 gp.Parser[In, T5],
|
||||||
|
p6 gp.Parser[In, T6],
|
||||||
|
p7 gp.Parser[In, T7],
|
||||||
|
p8 gp.Parser[In, T8],
|
||||||
|
p9 gp.Parser[In, T9],
|
||||||
|
f func(T, T2, T3, T4, T5, T6, T7, T8, T9) Out,
|
||||||
|
) gp.Parser[In, Out] {
|
||||||
|
return gp.Bind(p, func(x T) gp.Parser[In, Out] {
|
||||||
|
return gp.Bind(p2, func(x2 T2) gp.Parser[In, Out] {
|
||||||
|
return gp.Bind(p3, func(x3 T3) gp.Parser[In, Out] {
|
||||||
|
return gp.Bind(p4, func(x4 T4) gp.Parser[In, Out] {
|
||||||
|
return gp.Bind(p5, func(x5 T5) gp.Parser[In, Out] {
|
||||||
|
return gp.Bind(p6, func(x6 T6) gp.Parser[In, Out] {
|
||||||
|
return gp.Bind(p7, func(x7 T7) gp.Parser[In, Out] {
|
||||||
|
return gp.Bind(p8, func(x8 T8) gp.Parser[In, Out] {
|
||||||
|
return gp.Bind(p9, func(x9 T9) gp.Parser[In, Out] {
|
||||||
|
return gp.Return[In](f(x, x2, x3, x4, x5, x6, x7, x8, x9))
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
108
naive/naive_test.go
Normal file
108
naive/naive_test.go
Normal file
@ -0,0 +1,108 @@
|
|||||||
|
// SPDX-License-Identifier: Unlicense
|
||||||
|
|
||||||
|
package naive_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"git.codemonkeysoftware.net/b/gigaparsec"
|
||||||
|
"git.codemonkeysoftware.net/b/gigaparsec/naive"
|
||||||
|
)
|
||||||
|
|
||||||
|
func BenchmarkBind5(b *testing.B) {
|
||||||
|
type Bind5T = func(p gigaparsec.Parser[byte, byte], f func(byte) gigaparsec.Parser[byte, byte], f2 func(byte) gigaparsec.Parser[byte, byte], f3 func(byte) gigaparsec.Parser[byte, byte], f4 func(byte) gigaparsec.Parser[byte, byte], f5 func(byte) gigaparsec.Parser[byte, byte]) gigaparsec.Parser[byte, byte]
|
||||||
|
|
||||||
|
f := func(b byte) gigaparsec.Parser[byte, byte] {
|
||||||
|
return gigaparsec.Return[byte](b + 1)
|
||||||
|
}
|
||||||
|
p := func(bind5 Bind5T) gigaparsec.Parser[byte, byte] {
|
||||||
|
// gigaparsec.Bind5()
|
||||||
|
return bind5(gigaparsec.Match(byte(0)), f, f, f, f, f)
|
||||||
|
}
|
||||||
|
input := gigaparsec.SliceReaderAt[byte]{0}
|
||||||
|
b.Run("gigaparsec.Bind5", func(b *testing.B) {
|
||||||
|
for range b.N {
|
||||||
|
gigaparsec.Run(p(gigaparsec.Bind5), input)
|
||||||
|
}
|
||||||
|
|
||||||
|
})
|
||||||
|
b.Run("naïve.Bind5", func(b *testing.B) {
|
||||||
|
for range b.N {
|
||||||
|
gigaparsec.Run(p(naive.Bind5), input)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkBind9(b *testing.B) {
|
||||||
|
type Bind9T = func(p gigaparsec.Parser[byte, byte], f func(byte) gigaparsec.Parser[byte, byte], f2 func(byte) gigaparsec.Parser[byte, byte], f3 func(byte) gigaparsec.Parser[byte, byte], f4 func(byte) gigaparsec.Parser[byte, byte], f5 func(byte) gigaparsec.Parser[byte, byte], f6 func(byte) gigaparsec.Parser[byte, byte], f7 func(byte) gigaparsec.Parser[byte, byte], f8 func(byte) gigaparsec.Parser[byte, byte], f9 func(byte) gigaparsec.Parser[byte, byte]) gigaparsec.Parser[byte, byte]
|
||||||
|
|
||||||
|
f := func(b byte) gigaparsec.Parser[byte, byte] {
|
||||||
|
return gigaparsec.Return[byte](b + 1)
|
||||||
|
}
|
||||||
|
p := func(bind5 Bind9T) gigaparsec.Parser[byte, byte] {
|
||||||
|
// gigaparsec.Bind5()
|
||||||
|
return bind5(gigaparsec.Match(byte(0)), f, f, f, f, f, f, f, f, f)
|
||||||
|
}
|
||||||
|
input := gigaparsec.SliceReaderAt[byte]{0}
|
||||||
|
b.Run("gigaparsec.Bind9", func(b *testing.B) {
|
||||||
|
for range b.N {
|
||||||
|
gigaparsec.Run(p(gigaparsec.Bind9), input)
|
||||||
|
}
|
||||||
|
|
||||||
|
})
|
||||||
|
b.Run("naive.Bind9", func(b *testing.B) {
|
||||||
|
for range b.N {
|
||||||
|
gigaparsec.Run(p(naive.Bind9), input)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkSeq5(b *testing.B) {
|
||||||
|
type P = gigaparsec.Parser[byte, byte]
|
||||||
|
type Seq5T = func(P, P, P, P, P, func(byte, byte, byte, byte, byte) byte) P
|
||||||
|
|
||||||
|
zero := gigaparsec.Return[byte, byte](0)
|
||||||
|
|
||||||
|
f := func(a, b, c, d, e byte) byte {
|
||||||
|
return a + b + c + d + e
|
||||||
|
}
|
||||||
|
p := func(seq5 Seq5T) P {
|
||||||
|
return seq5(zero, zero, zero, zero, zero, f)
|
||||||
|
}
|
||||||
|
input := gigaparsec.SliceReaderAt[byte]{0}
|
||||||
|
b.Run("gigaparsec.Seq5", func(b *testing.B) {
|
||||||
|
for range b.N {
|
||||||
|
gigaparsec.Run(p(gigaparsec.Seq5), input)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
b.Run("naive.Seq5", func(b *testing.B) {
|
||||||
|
for range b.N {
|
||||||
|
gigaparsec.Run(p(naive.Seq5), input)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkSeq9(b *testing.B) {
|
||||||
|
type P = gigaparsec.Parser[byte, byte]
|
||||||
|
type Seq9T = func(P, P, P, P, P, P, P, P, P, func(byte, byte, byte, byte, byte, byte, byte, byte, byte) byte) P
|
||||||
|
|
||||||
|
zero := gigaparsec.Return[byte, byte](0)
|
||||||
|
|
||||||
|
f := func(a, b, c, d, e, f, g, h, i byte) byte {
|
||||||
|
return a + b + c + d + e + f + g + h + i
|
||||||
|
}
|
||||||
|
p := func(seq9 Seq9T) P {
|
||||||
|
return seq9(zero, zero, zero, zero, zero, zero, zero, zero, zero, f)
|
||||||
|
}
|
||||||
|
input := gigaparsec.SliceReaderAt[byte]{0}
|
||||||
|
b.Run("gigaparsec.Seq9", func(b *testing.B) {
|
||||||
|
for range b.N {
|
||||||
|
gigaparsec.Run(p(gigaparsec.Seq9), input)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
b.Run("naive.Seq9", func(b *testing.B) {
|
||||||
|
for range b.N {
|
||||||
|
gigaparsec.Run(p(naive.Seq9), input)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
@ -210,3 +210,11 @@ func TestRepeat(t *testing.T) {
|
|||||||
test.False(t, succeeded)
|
test.False(t, succeeded)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestBracket(t *testing.T) {
|
||||||
|
Todo(t)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestWhere(t *testing.T) {
|
||||||
|
Todo(t)
|
||||||
|
}
|
||||||
|
378
seq.go
378
seq.go
@ -205,3 +205,381 @@ func Seq5[In, Out, T, T2, T3, T4, T5 any](
|
|||||||
return Succeed(anyConsumed, final, next, MessageOK(s.Pos())), nil
|
return Succeed(anyConsumed, final, next, MessageOK(s.Pos())), nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Seq6[In, Out, T, T2, T3, T4, T5, T6 any](
|
||||||
|
p Parser[In, T],
|
||||||
|
p2 Parser[In, T2],
|
||||||
|
p3 Parser[In, T3],
|
||||||
|
p4 Parser[In, T4],
|
||||||
|
p5 Parser[In, T5],
|
||||||
|
p6 Parser[In, T6],
|
||||||
|
f func(T, T2, T3, T4, T5, T6) Out,
|
||||||
|
) Parser[In, Out] {
|
||||||
|
return func(s State[In]) (Result[In, Out], error) {
|
||||||
|
var anyConsumed bool
|
||||||
|
var next = s
|
||||||
|
|
||||||
|
r, err := p(next)
|
||||||
|
if err != nil {
|
||||||
|
return Result[In, Out]{}, err
|
||||||
|
}
|
||||||
|
anyConsumed = anyConsumed || r.Consumed()
|
||||||
|
success, val, next := r.Status()
|
||||||
|
if !success {
|
||||||
|
return Fail[In, Out](anyConsumed, r.Message()), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
r2, err := p2(next)
|
||||||
|
if err != nil {
|
||||||
|
return Result[In, Out]{}, err
|
||||||
|
}
|
||||||
|
anyConsumed = anyConsumed || r2.Consumed()
|
||||||
|
success, val2, next := r2.Status()
|
||||||
|
if !success {
|
||||||
|
return Fail[In, Out](anyConsumed, r2.Message()), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
r3, err := p3(next)
|
||||||
|
if err != nil {
|
||||||
|
return Result[In, Out]{}, err
|
||||||
|
}
|
||||||
|
anyConsumed = anyConsumed || r3.Consumed()
|
||||||
|
success, val3, next := r3.Status()
|
||||||
|
if !success {
|
||||||
|
return Fail[In, Out](anyConsumed, r3.Message()), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
r4, err := p4(next)
|
||||||
|
if err != nil {
|
||||||
|
return Result[In, Out]{}, err
|
||||||
|
}
|
||||||
|
anyConsumed = anyConsumed || r4.Consumed()
|
||||||
|
success, val4, next := r4.Status()
|
||||||
|
if !success {
|
||||||
|
return Fail[In, Out](anyConsumed, r4.Message()), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
r5, err := p5(next)
|
||||||
|
if err != nil {
|
||||||
|
return Result[In, Out]{}, err
|
||||||
|
}
|
||||||
|
anyConsumed = anyConsumed || r5.Consumed()
|
||||||
|
success, val5, next := r5.Status()
|
||||||
|
if !success {
|
||||||
|
return Fail[In, Out](anyConsumed, r5.Message()), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
r6, err := p6(next)
|
||||||
|
if err != nil {
|
||||||
|
return Result[In, Out]{}, err
|
||||||
|
}
|
||||||
|
anyConsumed = anyConsumed || r6.Consumed()
|
||||||
|
success, val6, next := r6.Status()
|
||||||
|
if !success {
|
||||||
|
return Fail[In, Out](anyConsumed, r6.Message()), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
final := f(val, val2, val3, val4, val5, val6)
|
||||||
|
return Succeed(anyConsumed, final, next, MessageOK(s.Pos())), nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Seq7[In, Out, T, T2, T3, T4, T5, T6, T7 any](
|
||||||
|
p Parser[In, T],
|
||||||
|
p2 Parser[In, T2],
|
||||||
|
p3 Parser[In, T3],
|
||||||
|
p4 Parser[In, T4],
|
||||||
|
p5 Parser[In, T5],
|
||||||
|
p6 Parser[In, T6],
|
||||||
|
p7 Parser[In, T7],
|
||||||
|
f func(T, T2, T3, T4, T5, T6, T7) Out,
|
||||||
|
) Parser[In, Out] {
|
||||||
|
return func(s State[In]) (Result[In, Out], error) {
|
||||||
|
var anyConsumed bool
|
||||||
|
var next = s
|
||||||
|
|
||||||
|
r, err := p(next)
|
||||||
|
if err != nil {
|
||||||
|
return Result[In, Out]{}, err
|
||||||
|
}
|
||||||
|
anyConsumed = anyConsumed || r.Consumed()
|
||||||
|
success, val, next := r.Status()
|
||||||
|
if !success {
|
||||||
|
return Fail[In, Out](anyConsumed, r.Message()), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
r2, err := p2(next)
|
||||||
|
if err != nil {
|
||||||
|
return Result[In, Out]{}, err
|
||||||
|
}
|
||||||
|
anyConsumed = anyConsumed || r2.Consumed()
|
||||||
|
success, val2, next := r2.Status()
|
||||||
|
if !success {
|
||||||
|
return Fail[In, Out](anyConsumed, r2.Message()), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
r3, err := p3(next)
|
||||||
|
if err != nil {
|
||||||
|
return Result[In, Out]{}, err
|
||||||
|
}
|
||||||
|
anyConsumed = anyConsumed || r3.Consumed()
|
||||||
|
success, val3, next := r3.Status()
|
||||||
|
if !success {
|
||||||
|
return Fail[In, Out](anyConsumed, r3.Message()), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
r4, err := p4(next)
|
||||||
|
if err != nil {
|
||||||
|
return Result[In, Out]{}, err
|
||||||
|
}
|
||||||
|
anyConsumed = anyConsumed || r4.Consumed()
|
||||||
|
success, val4, next := r4.Status()
|
||||||
|
if !success {
|
||||||
|
return Fail[In, Out](anyConsumed, r4.Message()), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
r5, err := p5(next)
|
||||||
|
if err != nil {
|
||||||
|
return Result[In, Out]{}, err
|
||||||
|
}
|
||||||
|
anyConsumed = anyConsumed || r5.Consumed()
|
||||||
|
success, val5, next := r5.Status()
|
||||||
|
if !success {
|
||||||
|
return Fail[In, Out](anyConsumed, r5.Message()), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
r6, err := p6(next)
|
||||||
|
if err != nil {
|
||||||
|
return Result[In, Out]{}, err
|
||||||
|
}
|
||||||
|
anyConsumed = anyConsumed || r6.Consumed()
|
||||||
|
success, val6, next := r6.Status()
|
||||||
|
if !success {
|
||||||
|
return Fail[In, Out](anyConsumed, r6.Message()), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
r7, err := p7(next)
|
||||||
|
if err != nil {
|
||||||
|
return Result[In, Out]{}, err
|
||||||
|
}
|
||||||
|
anyConsumed = anyConsumed || r7.Consumed()
|
||||||
|
success, val7, next := r7.Status()
|
||||||
|
if !success {
|
||||||
|
return Fail[In, Out](anyConsumed, r7.Message()), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
final := f(val, val2, val3, val4, val5, val6, val7)
|
||||||
|
return Succeed(anyConsumed, final, next, MessageOK(s.Pos())), nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Seq8[In, Out, T, T2, T3, T4, T5, T6, T7, T8 any](
|
||||||
|
p Parser[In, T],
|
||||||
|
p2 Parser[In, T2],
|
||||||
|
p3 Parser[In, T3],
|
||||||
|
p4 Parser[In, T4],
|
||||||
|
p5 Parser[In, T5],
|
||||||
|
p6 Parser[In, T6],
|
||||||
|
p7 Parser[In, T7],
|
||||||
|
p8 Parser[In, T8],
|
||||||
|
f func(T, T2, T3, T4, T5, T6, T7, T8) Out,
|
||||||
|
) Parser[In, Out] {
|
||||||
|
return func(s State[In]) (Result[In, Out], error) {
|
||||||
|
var anyConsumed bool
|
||||||
|
var next = s
|
||||||
|
|
||||||
|
r, err := p(next)
|
||||||
|
if err != nil {
|
||||||
|
return Result[In, Out]{}, err
|
||||||
|
}
|
||||||
|
anyConsumed = anyConsumed || r.Consumed()
|
||||||
|
success, val, next := r.Status()
|
||||||
|
if !success {
|
||||||
|
return Fail[In, Out](anyConsumed, r.Message()), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
r2, err := p2(next)
|
||||||
|
if err != nil {
|
||||||
|
return Result[In, Out]{}, err
|
||||||
|
}
|
||||||
|
anyConsumed = anyConsumed || r2.Consumed()
|
||||||
|
success, val2, next := r2.Status()
|
||||||
|
if !success {
|
||||||
|
return Fail[In, Out](anyConsumed, r2.Message()), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
r3, err := p3(next)
|
||||||
|
if err != nil {
|
||||||
|
return Result[In, Out]{}, err
|
||||||
|
}
|
||||||
|
anyConsumed = anyConsumed || r3.Consumed()
|
||||||
|
success, val3, next := r3.Status()
|
||||||
|
if !success {
|
||||||
|
return Fail[In, Out](anyConsumed, r3.Message()), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
r4, err := p4(next)
|
||||||
|
if err != nil {
|
||||||
|
return Result[In, Out]{}, err
|
||||||
|
}
|
||||||
|
anyConsumed = anyConsumed || r4.Consumed()
|
||||||
|
success, val4, next := r4.Status()
|
||||||
|
if !success {
|
||||||
|
return Fail[In, Out](anyConsumed, r4.Message()), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
r5, err := p5(next)
|
||||||
|
if err != nil {
|
||||||
|
return Result[In, Out]{}, err
|
||||||
|
}
|
||||||
|
anyConsumed = anyConsumed || r5.Consumed()
|
||||||
|
success, val5, next := r5.Status()
|
||||||
|
if !success {
|
||||||
|
return Fail[In, Out](anyConsumed, r5.Message()), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
r6, err := p6(next)
|
||||||
|
if err != nil {
|
||||||
|
return Result[In, Out]{}, err
|
||||||
|
}
|
||||||
|
anyConsumed = anyConsumed || r6.Consumed()
|
||||||
|
success, val6, next := r6.Status()
|
||||||
|
if !success {
|
||||||
|
return Fail[In, Out](anyConsumed, r6.Message()), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
r7, err := p7(next)
|
||||||
|
if err != nil {
|
||||||
|
return Result[In, Out]{}, err
|
||||||
|
}
|
||||||
|
anyConsumed = anyConsumed || r7.Consumed()
|
||||||
|
success, val7, next := r7.Status()
|
||||||
|
if !success {
|
||||||
|
return Fail[In, Out](anyConsumed, r7.Message()), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
r8, err := p8(next)
|
||||||
|
if err != nil {
|
||||||
|
return Result[In, Out]{}, err
|
||||||
|
}
|
||||||
|
anyConsumed = anyConsumed || r8.Consumed()
|
||||||
|
success, val8, next := r8.Status()
|
||||||
|
if !success {
|
||||||
|
return Fail[In, Out](anyConsumed, r8.Message()), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
final := f(val, val2, val3, val4, val5, val6, val7, val8)
|
||||||
|
return Succeed(anyConsumed, final, next, MessageOK(s.Pos())), nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Seq9[In, Out, T, T2, T3, T4, T5, T6, T7, T8, T9 any](
|
||||||
|
p Parser[In, T],
|
||||||
|
p2 Parser[In, T2],
|
||||||
|
p3 Parser[In, T3],
|
||||||
|
p4 Parser[In, T4],
|
||||||
|
p5 Parser[In, T5],
|
||||||
|
p6 Parser[In, T6],
|
||||||
|
p7 Parser[In, T7],
|
||||||
|
p8 Parser[In, T8],
|
||||||
|
p9 Parser[In, T9],
|
||||||
|
f func(T, T2, T3, T4, T5, T6, T7, T8, T9) Out,
|
||||||
|
) Parser[In, Out] {
|
||||||
|
return func(s State[In]) (Result[In, Out], error) {
|
||||||
|
var anyConsumed bool
|
||||||
|
var next = s
|
||||||
|
|
||||||
|
r, err := p(next)
|
||||||
|
if err != nil {
|
||||||
|
return Result[In, Out]{}, err
|
||||||
|
}
|
||||||
|
anyConsumed = anyConsumed || r.Consumed()
|
||||||
|
success, val, next := r.Status()
|
||||||
|
if !success {
|
||||||
|
return Fail[In, Out](anyConsumed, r.Message()), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
r2, err := p2(next)
|
||||||
|
if err != nil {
|
||||||
|
return Result[In, Out]{}, err
|
||||||
|
}
|
||||||
|
anyConsumed = anyConsumed || r2.Consumed()
|
||||||
|
success, val2, next := r2.Status()
|
||||||
|
if !success {
|
||||||
|
return Fail[In, Out](anyConsumed, r2.Message()), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
r3, err := p3(next)
|
||||||
|
if err != nil {
|
||||||
|
return Result[In, Out]{}, err
|
||||||
|
}
|
||||||
|
anyConsumed = anyConsumed || r3.Consumed()
|
||||||
|
success, val3, next := r3.Status()
|
||||||
|
if !success {
|
||||||
|
return Fail[In, Out](anyConsumed, r3.Message()), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
r4, err := p4(next)
|
||||||
|
if err != nil {
|
||||||
|
return Result[In, Out]{}, err
|
||||||
|
}
|
||||||
|
anyConsumed = anyConsumed || r4.Consumed()
|
||||||
|
success, val4, next := r4.Status()
|
||||||
|
if !success {
|
||||||
|
return Fail[In, Out](anyConsumed, r4.Message()), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
r5, err := p5(next)
|
||||||
|
if err != nil {
|
||||||
|
return Result[In, Out]{}, err
|
||||||
|
}
|
||||||
|
anyConsumed = anyConsumed || r5.Consumed()
|
||||||
|
success, val5, next := r5.Status()
|
||||||
|
if !success {
|
||||||
|
return Fail[In, Out](anyConsumed, r5.Message()), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
r6, err := p6(next)
|
||||||
|
if err != nil {
|
||||||
|
return Result[In, Out]{}, err
|
||||||
|
}
|
||||||
|
anyConsumed = anyConsumed || r6.Consumed()
|
||||||
|
success, val6, next := r6.Status()
|
||||||
|
if !success {
|
||||||
|
return Fail[In, Out](anyConsumed, r6.Message()), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
r7, err := p7(next)
|
||||||
|
if err != nil {
|
||||||
|
return Result[In, Out]{}, err
|
||||||
|
}
|
||||||
|
anyConsumed = anyConsumed || r7.Consumed()
|
||||||
|
success, val7, next := r7.Status()
|
||||||
|
if !success {
|
||||||
|
return Fail[In, Out](anyConsumed, r7.Message()), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
r8, err := p8(next)
|
||||||
|
if err != nil {
|
||||||
|
return Result[In, Out]{}, err
|
||||||
|
}
|
||||||
|
anyConsumed = anyConsumed || r8.Consumed()
|
||||||
|
success, val8, next := r8.Status()
|
||||||
|
if !success {
|
||||||
|
return Fail[In, Out](anyConsumed, r8.Message()), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
r9, err := p9(next)
|
||||||
|
if err != nil {
|
||||||
|
return Result[In, Out]{}, err
|
||||||
|
}
|
||||||
|
anyConsumed = anyConsumed || r9.Consumed()
|
||||||
|
success, val9, next := r9.Status()
|
||||||
|
if !success {
|
||||||
|
return Fail[In, Out](anyConsumed, r9.Message()), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
final := f(val, val2, val3, val4, val5, val6, val7, val8, val9)
|
||||||
|
return Succeed(anyConsumed, final, next, MessageOK(s.Pos())), nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user