2024-11-29 07:53:11 +00:00
|
|
|
// 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)
|
|
|
|
}
|
2024-11-29 18:00:34 +00:00
|
|
|
|
|
|
|
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))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|