progs/parser-combinators/comb2.sc
changeset 972 ebb4a40d9bae
parent 971 51e00f223792
child 973 f25d338d16c9
--- a/progs/parser-combinators/comb2.sc	Fri Oct 25 18:54:08 2024 +0100
+++ b/progs/parser-combinators/comb2.sc	Sat Nov 09 06:23:35 2024 +0000
@@ -17,16 +17,24 @@
 case class ~[+A, +B](x: A, y: B)
 
 
+trait IsSeq[I] {
+  extension (i: I) def isEmpty: Boolean
+}
+
+given IsSeq[String] = _.isEmpty
+given [I]: IsSeq[Seq[I]] = _.isEmpty
+
+
 // parser combinators
-abstract class Parser[I, T] { p =>
+abstract class Parser[I : IsSeq, T] { p =>
   def parse(in: I): Set[(T, I)]  
 
-  def parse_all(in: I)(using toSeq: I => Seq[?]) : Set[T] =
+  def parse_all(in: I) : Set[T] =
     for ((hd, tl) <- parse(in); 
-        if toSeq(tl).isEmpty) yield hd
+        if tl.isEmpty) yield hd
 
   // alternative parser
-  def ||(q: => Parser[I, T]) = new Parser[I, T] {
+  def ||(q: => Parser[I, T]) : Parser[I, T] = Parser[I, T] {
     def parse(in: I) = p.parse(in) ++ q.parse(in)
   }