diff -r da1f8c033b8e -r 94f5cce73a4f progs/parser-combinators/comb2.sc --- a/progs/parser-combinators/comb2.sc Thu Sep 19 19:25:13 2024 +0100 +++ b/progs/parser-combinators/comb2.sc Sun Sep 29 18:46:02 2024 +0100 @@ -14,12 +14,12 @@ // case x ~ y ~ z => ... -case class ~[+A, +B](x: A, y: B) +case class o[+A, +B](x: A, y: B) -type IsSeq[I] = I => Seq[_] +type IsSeq[I] = I => Seq[?] -abstract class Parser[I, T](using is: I => Seq[_]) { +abstract class Parser[I, T](using is: I => Seq[?]) { def parse(in: I): Set[(T, I)] def parse_all(in: I) : Set[T] = @@ -37,10 +37,10 @@ // sequence parser class SeqParser[I : IsSeq, T, S](p: => Parser[I, T], - q: => Parser[I, S]) extends Parser[I, ~[T, S]] { + q: => Parser[I, S]) extends Parser[I, o[T, S]] { def parse(in: I) = for ((hd1, tl1) <- p.parse(in); - (hd2, tl2) <- q.parse(tl1)) yield (new ~(hd1, hd2), tl2) + (hd2, tl2) <- q.parse(tl1)) yield (new o(hd1, hd2), tl2) } // map parser @@ -87,13 +87,13 @@ // p"<_some_string_>" extension (sc: StringContext) - def p(args: Any*) = StrParser(sc.s(args:_*)) + def p(args: Any*) = StrParser(sc.s(args*)) // more convenient syntax for parser combinators 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 o[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) } @@ -124,13 +124,13 @@ // arithmetic expressions lazy val AExp: Parser[String, AExp] = - (Te ~ p"+" ~ AExp).map[AExp]{ case x ~ _ ~ z => Aop("+", x, z) } || - (Te ~ p"-" ~ AExp).map[AExp]{ case x ~ _ ~ z => Aop("-", x, z) } || Te + (Te o p"+" o AExp).map[AExp]{ case x o _ o z => Aop("+", x, z): AExp } || + (Te o p"-" o AExp).map[AExp]{ case x o _ o z => Aop("-", x, z) } || Te lazy val Te: Parser[String, AExp] = - (Fa ~ p"*" ~ Te).map[AExp]{ case x ~ _ ~ z => Aop("*", x, z) } || - (Fa ~ p"/" ~ Te).map[AExp]{ case x ~ _ ~ z => Aop("/", x, z) } || Fa + (Fa o p"*" o Te).map[AExp]{ case x o _ o z => Aop("*", x, z) } || + (Fa o p"/" o Te).map[AExp]{ case x o _ o z => Aop("/", x, z) } || Fa lazy val Fa: Parser[String, AExp] = - (p"(" ~ AExp ~ p")").map{ case _ ~ y ~ _ => y } || + (p"(" o AExp o p")").map{ case _ o y o _ => y } || IdParser.map(Var(_)) || NumParser.map(Num(_))