parser3.scala
changeset 85 1a4065f965fb
parent 76 373cf55a3ca5
equal deleted inserted replaced
84:719fd738d2a0 85:1a4065f965fb
       
     1 package object parser {
     1 
     2 
     2 // parser combinators with input type I and return type T
     3 // parser combinators 
       
     4 // with input type I and return type T
       
     5 //
       
     6 // needs to be compiled with scalac parser3.scala
     3 
     7 
     4 abstract class Parser[I <% Seq[_], T] {
     8 abstract class Parser[I <% Seq[_], T] {
     5   def parse(ts: I): Set[(T, I)]
     9   def parse(ts: I): Set[(T, I)]
     6 
    10 
     7   def parse_all(ts: I) : Set[T] =
    11   def parse_all(ts: I) : Set[T] =
    32 class FunParser[I <% Seq[_], T, S](p: => Parser[I, T], f: T => S) extends Parser[I, S] {
    36 class FunParser[I <% Seq[_], T, S](p: => Parser[I, T], f: T => S) extends Parser[I, S] {
    33   def parse(sb: I) = 
    37   def parse(sb: I) = 
    34     for ((head, tail) <- p.parse(sb)) yield (f(head), tail)
    38     for ((head, tail) <- p.parse(sb)) yield (f(head), tail)
    35 }
    39 }
    36 
    40 
    37 
    41 }