Hi all,
I like to understand the tilde-class that is used in the parser combinator library. For example the Programming in Scala book gives
case class ~[+A, +B](x: A, y: B) {
override def toString = “(”+ x +"~"+ y +")"
}
to be able to use pattern matching like case x ~ y ~ z, instead of ((x, y), z).
Unfortunately, I am unable to replicate this in my own code for my own parser combinators. I used so far for the following abstract parser class and the sequence parser code:
abstract class Parser[I, T](implicit ev: I => Seq[_]) {
def parse(ts: I): Set[(T, I)]
def parse_all(ts: I) : Set[T] =
for ((head, tail) <- parse(ts); if (tail.isEmpty)) yield head
}
class SeqParser[I, T, S](p: => Parser[I, T],
q: => Parser[I, S])(implicit ev: I => Seq[_]) extends Parser[I, (T, S)] {
def parse(sb: I) =
for ((head1, tail1) <- p.parse(sb);
(head2, tail2) <- q.parse(tail1)) yield ((head1, head2), tail2)
}
I added the ~-class above, and then modified the return type of the Parser to be of the form
Parser[I, ~[T, S]]. But then how do I have to change the code for what to return in the yield
expression? Both, ~(head1, head2) and head1 ~ head2, give errors.
My question is how do I have to do this properly such that I can later use x ~ y ~ z for pattern-matching parser results.
Thanks a lot!
Christian