progs/app8.scala
changeset 93 4794759139ea
parent 92 e85600529ca5
child 170 fa187fa5b642
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/progs/app8.scala	Sat Jun 15 09:23:18 2013 -0400
@@ -0,0 +1,23 @@
+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 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 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)
+}
+
+