progs/app8.scala
author Christian Urban <christian dot urban at kcl dot ac dot uk>
Wed, 23 Oct 2013 01:05:22 +0100
changeset 149 b728c9dbf120
parent 93 4794759139ea
child 170 fa187fa5b642
permissions -rw-r--r--
add
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
66
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     1
class SeqParser[I, T, S](p: => Parser[I, T], 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     2
                         q: => Parser[I, S]) 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     3
                             extends Parser[I, (T, S)] {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     4
  def parse(sb: I) = 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     5
    for ((head1, tail1) <- p.parse(sb); 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     6
         (head2, tail2) <- q.parse(tail1)) 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     7
            yield ((head1, head2), tail2)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     8
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     9
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    10
class AltParser[I, T](p: => Parser[I, T], 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    11
                      q: => Parser[I, T]) 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    12
                          extends Parser[I, T] {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    13
  def parse(sb: I) = p.parse(sb) ++ q.parse(sb)   
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    14
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    15
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    16
class FunParser[I, T, S](p: => Parser[I, T], f: T => S) 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    17
  extends Parser[I, S] {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    18
  def parse(sb: I) = 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    19
    for ((head, tail) <- p.parse(sb)) 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    20
      yield (f(head), tail)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    21
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    22
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    23