progs/re3.scala
changeset 363 0d6deecdb2eb
parent 341 ac1187b2e5c9
--- a/progs/re3.scala	Fri Oct 23 14:45:57 2015 +0100
+++ b/progs/re3.scala	Sat Oct 31 11:16:52 2015 +0000
@@ -77,3 +77,64 @@
 }
 
 
+// some convenience for typing in regular expressions
+import scala.language.implicitConversions    
+import scala.language.reflectiveCalls 
+def charlist2rexp(s : List[Char]) : Rexp = s match {
+  case Nil => EMPTY
+  case c::Nil => CHAR(c)
+  case c::s => SEQ(CHAR(c), charlist2rexp(s))
+}
+implicit def string2rexp(s : String) : Rexp = charlist2rexp(s.toList)
+
+implicit def RexpOps (r: Rexp) = new {
+  def | (s: Rexp) = ALT(r, s)
+  def % = STAR(r)
+  def ~ (s: Rexp) = SEQ(r, s)
+}
+
+implicit def stringOps (s: String) = new {
+  def | (r: Rexp) = ALT(s, r)
+  def | (r: String) = ALT(s, r)
+  def % = STAR(s)
+  def ~ (r: Rexp) = SEQ(s, r)
+  def ~ (r: String) = SEQ(s, r)
+}
+
+
+
+def PLUS(r: Rexp) = SEQ(r, STAR(r))
+def RANGE(s: List[Char]) : Rexp = s match {
+  case Nil => NULL
+  case c::s => ALT(CHAR(c), RANGE(s))
+} 
+
+println("EVILS:")
+val EVIL1 = PLUS(PLUS("a" ~ "a" ~ "a"))
+val EVIL2 = PLUS(PLUS("aaaaaaaaaaaaaaaaaaa" ~ OPT("a")))  // 19 as ~ a?
+
+
+//40 * aaa matches
+//43 * aaa + aa does not
+//45 * aaa + a
+
+println("EVIL1:")
+println(matches(EVIL1, "aaa" * 40))
+println(matches(EVIL1, "aaa" * 43 + "aa"))
+println(matches(EVIL1, "aaa" * 45 + "a"))
+println("EVIL2:")
+println(matches(EVIL2, "aaa" * 40))
+println(matches(EVIL2, "aaa" * 43 + "aa"))
+println(matches(EVIL2, "aaa" * 45 + "a"))
+
+
+
+
+println("EMAIL:")
+val LOWERCASE = "abcdefghijklmnopqrstuvwxyz"
+val DIGITS = "0123456789"
+val EMAIL = PLUS(RANGE((LOWERCASE + DIGITS + "_" + "." + "-").toList)) ~ "@" ~ 
+            PLUS(RANGE((LOWERCASE + DIGITS + "." + "-").toList)) ~ "." ~
+            NMTIMES(RANGE((LOWERCASE + ".").toList), 2, 6)
+
+val my_email = "christian.urban@kcl.ac.uk"