progs/comb1.scala
changeset 590 c6a1e19e9801
parent 588 a4646557016d
child 593 bb24d4e207b6
equal deleted inserted replaced
589:0451b8b67f62 590:c6a1e19e9801
    36   def parse(sb: String) = 
    36   def parse(sb: String) = 
    37     if (sb != "" && sb.head == c) Set((c, sb.tail)) else Set()
    37     if (sb != "" && sb.head == c) Set((c, sb.tail)) else Set()
    38 }
    38 }
    39 
    39 
    40 import scala.util.matching.Regex
    40 import scala.util.matching.Regex
       
    41 
    41 case class RegexParser(reg: Regex) extends Parser[String, String] {
    42 case class RegexParser(reg: Regex) extends Parser[String, String] {
    42   def parse(sb: String) = reg.findPrefixMatchOf(sb) match {
    43   def parse(sb: String) = reg.findPrefixMatchOf(sb) match {
    43     case None => Set()
    44     case None => Set()
    44     case Some(m) => Set((m.matched, m.after.toString))  
    45     case Some(m) => Set((m.matched, m.after.toString))  
    45   }
    46   }
    49 def StringParser(s: String) = RegexParser(s.r)
    50 def StringParser(s: String) = RegexParser(s.r)
    50 
    51 
    51 
    52 
    52 // convenience
    53 // convenience
    53 implicit def string2parser(s: String) = StringParser(s)
    54 implicit def string2parser(s: String) = StringParser(s)
    54 implicit def char2parser(c: Char) = CharParser(c)
    55 //implicit def char2parser(c: Char) = CharParser(c)
    55 
    56 
    56 implicit def ParserOps[I<% Seq[_], T](p: Parser[I, T]) = new {
    57 implicit def ParserOps[I<% Seq[_], T](p: Parser[I, T]) = new {
    57   def || (q : => Parser[I, T]) = new AltParser[I, T](p, q)
    58   def | (q : => Parser[I, T]) = new AltParser[I, T](p, q)
    58   def ==>[S] (f: => T => S) = new FunParser[I, T, S](p, f)
    59   def ==>[S] (f: => T => S) = new FunParser[I, T, S](p, f)
    59   def ~[S] (q : => Parser[I, S]) = new SeqParser[I, T, S](p, q)
    60   def ~[S] (q : => Parser[I, S]) = new SeqParser[I, T, S](p, q)
    60 }
    61 }
    61 
    62 
    62 implicit def StringOps(s: String) = new {
    63 implicit def StringOps(s: String) = new {
    63   def || (q : => Parser[String, String]) = new AltParser[String, String](s, q)
    64   def | (q : => Parser[String, String]) = new AltParser[String, String](s, q)
    64   def || (r: String) = new AltParser[String, String](s, r)
    65   def | (r: String) = new AltParser[String, String](s, r)
    65   def ==>[S] (f: => String => S) = new FunParser[String, String, S](s, f)
    66   def ==>[S] (f: => String => S) = new FunParser[String, String, S](s, f)
    66   def ~[S] (q : => Parser[String, S]) = 
    67   def ~[S] (q : => Parser[String, S]) = 
    67     new SeqParser[String, String, S](s, q)
    68     new SeqParser[String, String, S](s, q)
    68   def ~ (r: String) = 
    69   def ~ (r: String) = 
    69     new SeqParser[String, String, String](s, r)
    70     new SeqParser[String, String, String](s, r)