app7.scala
changeset 66 9215b9fb8852
parent 7 73cf4406b773
child 70 e6868bd2942b
--- a/app7.scala	Tue Nov 20 22:06:05 2012 +0000
+++ b/app7.scala	Wed Nov 21 02:20:16 2012 +0000
@@ -1,17 +1,14 @@
-def matches(r: Rexp, s: String) : Boolean = 
-  nullable(derivs(r, s.toList))
+abstract class Parser[I, T] {
+  def parse(ts: I): Set[(T, I)]
+
+  def parse_all(ts: I) : Set[T] =
+    for ((head, tail) <- parse(ts); if (tail.isEmpty)) 
+      yield head
+
+  def || (right : => Parser[I, T]) : Parser[I, T] = 
+      new AltParser(this, right)
+  def ==>[S] (f: => T => S) : Parser [I, S] = 
+      new FunParser(this, f)
+}
 
 
-/* Examples */
-
-println(matches(SEQ(SEQ(CHAR('c'), CHAR('a')), CHAR('b')),"cab"))
-println(matches(STAR(CHAR('a')),"aaa"))
-
-/* Convenience using implicits */
-implicit def string2rexp(s : String) : Rexp = {
-  s.foldRight (EMPTY: Rexp) ( (c, r) => SEQ(CHAR(c), r) )
-}
-
-println(matches("cab" ,"cab"))
-println(matches(STAR("a"),"aaa"))
-println(matches(STAR("a"),"aaab"))