--- a/progs/parser-combinators/comb1.sc Thu Oct 05 14:36:54 2023 +0100
+++ b/progs/parser-combinators/comb1.sc Fri Oct 13 15:07:37 2023 +0100
@@ -10,7 +10,9 @@
// using is: I => Seq[_], which means that the input
// type 'I' needs to be a sequence.
-abstract class Parser[I, T](using is: I => Seq[_]) {
+type IsSeq[I] = I => Seq[_]
+
+abstract class Parser[I, T](using is: IsSeq[I]) {
def parse(in: I): Set[(T, I)]
def parse_all(in: I) : Set[T] =
@@ -20,23 +22,25 @@
// 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 ((hd1, hd2), tl2)
}
// map parser
-class MapParser[I, T, S](p: => Parser[I, T],
- f: T => S)(using I => Seq[_]) extends Parser[I, S] {
+class MapParser[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)
}
@@ -86,7 +90,7 @@
(p"else").parse("elsethen")
// 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 MapParser[I, T, S](p, f)