--- a/main_templates3/re.scala Tue Dec 07 01:35:00 2021 +0000
+++ b/main_templates3/re.scala Tue Dec 07 23:17:51 2021 +0000
@@ -12,6 +12,21 @@
case class SEQ(r1: Rexp, r2: Rexp) extends Rexp // sequence
case class STAR(r: Rexp) extends Rexp // star
+import scala.language.implicitConversions
+import scala.language.reflectiveCalls
+
+def charlist2rexp(s: List[Char]): Rexp = s match {
+ case Nil => ONE
+ case c::Nil => CHAR(c)
+ case c::s => SEQ(CHAR(c), charlist2rexp(s))
+}
+implicit def string2rexp(s: String): Rexp = charlist2rexp(s.toList)
+
+
+// "a|b"
+ALT(CHAR('a'), CHAR('b'))
+
+val reg : Rexp = "a" | "b" // CHAR('a')
// some convenience for typing regular expressions
@@ -29,6 +44,9 @@
}
implicit def string2rexp(s: String): Rexp = charlist2rexp(s.toList)
+
+
+
implicit def RexpOps (r: Rexp) = new {
def | (s: Rexp) = ALT(r, s)
def % = STAR(r)