progs/app8.scala
author Christian Urban <christian dot urban at kcl dot ac dot uk>
Tue, 18 Oct 2016 20:39:54 +0100
changeset 456 2fddf8ab744f
parent 170 fa187fa5b642
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
170
fa187fa5b642 added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 93
diff changeset
     1
class AltParser[I, T](p: => Parser[I, T], 
fa187fa5b642 added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 93
diff changeset
     2
                      q: => Parser[I, T]) 
fa187fa5b642 added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 93
diff changeset
     3
                          extends Parser[I, T] {
fa187fa5b642 added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 93
diff changeset
     4
  def parse(sb: I) = p.parse(sb) ++ q.parse(sb)   
fa187fa5b642 added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 93
diff changeset
     5
}
fa187fa5b642 added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 93
diff changeset
     6
66
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     7
class SeqParser[I, T, S](p: => Parser[I, T], 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     8
                         q: => Parser[I, S]) 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     9
                             extends Parser[I, (T, S)] {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    10
  def parse(sb: I) = 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    11
    for ((head1, tail1) <- p.parse(sb); 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    12
         (head2, tail2) <- q.parse(tail1)) 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    13
            yield ((head1, head2), tail2)
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) 
170
fa187fa5b642 added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 93
diff changeset
    17
                                  extends Parser[I, S] {
66
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