progs/comb1.scala
changeset 177 53def1fbf472
parent 172 47b5c91eff47
child 178 d36363d648e3
equal deleted inserted replaced
176:3c2653fc8b5a 177:53def1fbf472
    22   def parse(sb: I) = 
    22   def parse(sb: I) = 
    23     for ((head, tail) <- p.parse(sb)) yield (f(head), tail)
    23     for ((head, tail) <- p.parse(sb)) yield (f(head), tail)
    24 }
    24 }
    25 
    25 
    26 // atomic parsers
    26 // atomic parsers
       
    27 case class CharParser(c: Char) extends Parser[String, Char] {
       
    28   def parse(sb: String) = 
       
    29     if (sb.head == c) Set((c, sb.tail)) else Set()
       
    30 }
       
    31 
    27 case class StringParser(s: String) extends Parser[String, String] {
    32 case class StringParser(s: String) extends Parser[String, String] {
    28   def parse(sb: String) = {
    33   def parse(sb: String) = {
    29     val (prefix, suffix) = sb.splitAt(s.length)
    34     val (prefix, suffix) = sb.splitAt(s.length)
    30     if (prefix == s) Set((prefix, suffix)) else Set()
    35     if (prefix == s) Set((prefix, suffix)) else Set()
    31   }
    36   }