progs/app8.scala
author Christian Urban <christian dot urban at kcl dot ac dot uk>
Wed, 30 Oct 2013 03:44:40 +0000
changeset 170 fa187fa5b642
parent 93 4794759139ea
permissions -rw-r--r--
added slides

class AltParser[I, T](p: => Parser[I, T], 
                      q: => Parser[I, T]) 
                          extends Parser[I, T] {
  def parse(sb: I) = p.parse(sb) ++ q.parse(sb)   
}

class SeqParser[I, T, S](p: => Parser[I, T], 
                         q: => Parser[I, S]) 
                             extends Parser[I, (T, S)] {
  def parse(sb: I) = 
    for ((head1, tail1) <- p.parse(sb); 
         (head2, tail2) <- q.parse(tail1)) 
            yield ((head1, head2), tail2)
}

class FunParser[I, T, S](p: => Parser[I, T], f: T => S) 
                                  extends Parser[I, S] {
  def parse(sb: I) = 
    for ((head, tail) <- p.parse(sb)) 
      yield (f(head), tail)
}