progs/c.scala
changeset 722 14914b57e207
parent 717 9a431d1eac85
equal deleted inserted replaced
721:e3c64f22dd31 722:14914b57e207
    15   def parse(ts: I): Set[(T, I)]
    15   def parse(ts: I): Set[(T, I)]
    16 
    16 
    17   def parse_all(ts: I) : Set[T] =
    17   def parse_all(ts: I) : Set[T] =
    18     for ((head, tail) <- parse(ts); if tail.isEmpty) yield head
    18     for ((head, tail) <- parse(ts); if tail.isEmpty) yield head
    19 }
    19 }
       
    20 
       
    21 
       
    22 
    20 
    23 
    21 class SeqParser[I : IsSeq, T, S](p: => Parser[I, T], q: => Parser[I, S]) extends Parser[I, ~[T, S]] {
    24 class SeqParser[I : IsSeq, T, S](p: => Parser[I, T], q: => Parser[I, S]) extends Parser[I, ~[T, S]] {
    22   def parse(sb: I) = 
    25   def parse(sb: I) = 
    23     for ((head1, tail1) <- p.parse(sb); 
    26     for ((head1, tail1) <- p.parse(sb); 
    24          (head2, tail2) <- q.parse(tail1)) yield (new ~(head1, head2), tail2)
    27          (head2, tail2) <- q.parse(tail1)) yield (new ~(head1, head2), tail2)
    65 
    68 
    66 
    69 
    67 implicit def ParserOps[I : IsSeq, T](p: Parser[I, T]) = new {
    70 implicit def ParserOps[I : IsSeq, T](p: Parser[I, T]) = new {
    68   def ||(q : => Parser[I, T]) = new AltParser[I, T](p, q)
    71   def ||(q : => Parser[I, T]) = new AltParser[I, T](p, q)
    69   def ~[S](q : => Parser[I, S]) = new SeqParser[I, T, S](p, q)
    72   def ~[S](q : => Parser[I, S]) = new SeqParser[I, T, S](p, q)
    70   def map[S](f: => T => S) = new MapParser[I, T, S](p, f)
    73   def map[S](f: => T => S) = new MapParser[I, T, S](p, f) 
    71 }
    74 }
    72 
    75 
    73 // these implicits allow us to use infic notation for
    76 // these implicits allow us to use infic notation for
    74 // sequences and alternatives; we also can write map
    77 // sequences and alternatives; we also can write map
    75 // for a parser
    78 // for a parser