--- a/progs/parser-combinators/comb2.sc Thu Oct 05 14:36:54 2023 +0100
+++ b/progs/parser-combinators/comb2.sc Fri Oct 13 15:07:37 2023 +0100
@@ -18,6 +18,8 @@
case class ~[+A, +B](x: A, y: B)
+type IsSeq[I] = I => Seq[_]
+
abstract class Parser[I, T](using is: I => Seq[_]) {
def parse(in: I): Set[(T, I)]
@@ -29,22 +31,22 @@
// parser combinators
// alternative parser
-class AltParser[I, T](p: => Parser[I, T],
- q: => Parser[I, T])(using I => Seq[_]) extends Parser[I, T] {
+class AltParser[I : IsSeq, T](p: => Parser[I, T],
+ q: => Parser[I, T]) extends Parser[I, T] {
def parse(in: I) = p.parse(in) ++ q.parse(in)
}
// sequence parser
-class SeqParser[I, T, S](p: => Parser[I, T],
- q: => Parser[I, S])(using I => Seq[_]) extends Parser[I, ~[T, S]] {
+class SeqParser[I : IsSeq, T, S](p: => Parser[I, T],
+ q: => Parser[I, S]) extends Parser[I, ~[T, S]] {
def parse(in: I) =
for ((hd1, tl1) <- p.parse(in);
(hd2, tl2) <- q.parse(tl1)) yield (new ~(hd1, hd2), tl2)
}
// map parser
-class maparser[I, T, S](p: => Parser[I, T],
- f: T => S)(using I => Seq[_]) extends Parser[I, S] {
+class maparser[I : IsSeq, T, S](p: => Parser[I, T],
+ f: T => S) extends Parser[I, S] {
def parse(in: I) = for ((hd, tl) <- p.parse(in)) yield (f(hd), tl)
}
@@ -90,7 +92,7 @@
// more convenient syntax for parser combinators
-extension [I, T](p: Parser[I, T])(using I => Seq[_]) {
+extension [I : IsSeq, T](p: Parser[I, T]) {
def ||(q : => Parser[I, T]) = new AltParser[I, T](p, q)
def ~[S] (q : => Parser[I, S]) = new SeqParser[I, T, S](p, q)
def map[S](f: => T => S) = new maparser[I, T, S](p, f)