progs/parser-combinators/comb2.sc
changeset 977 1e6eca42d90b
parent 973 f25d338d16c9
equal deleted inserted replaced
976:e9eac62928f5 977:1e6eca42d90b
    12 
    12 
    13 // more convenience for the map parsers later on;
    13 // more convenience for the map parsers later on;
    14 // it allows writing nested patterns as
    14 // it allows writing nested patterns as
    15 // case x ~ y ~ z => ...
    15 // case x ~ y ~ z => ...
    16 
    16 
    17 case class ~[+A, +B](x: A, y: B)
    17 case class ~[+A, +B](x: A, y: B) 
    18 
    18 
    19 // to make sure the input has an empty-method
    19 // to make sure the input has an empty-method
    20 trait IsSeq[I] {
    20 trait IsSeq[I] {
    21   extension (i: I) def isEmpty: Boolean
    21   extension (i: I) def isEmpty: Boolean
    22 }
    22 }
    32   def parse_all(in: I) : Set[T] =
    32   def parse_all(in: I) : Set[T] =
    33     for ((hd, tl) <- parse(in); 
    33     for ((hd, tl) <- parse(in); 
    34         if tl.isEmpty) yield hd
    34         if tl.isEmpty) yield hd
    35 
    35 
    36   // alternative parser
    36   // alternative parser
    37   def ||(q: => Parser[I, T]) : Parser[I, T] = Parser[I, T] {
    37   def ||(q: => Parser[I, T]) : Parser[I, T] = new Parser[I, T] {
    38     def parse(in: I) = p.parse(in) ++ q.parse(in)
    38     def parse(in: I) = p.parse(in) ++ q.parse(in)
    39   }
    39   }
    40 
    40 
    41   // sequence parser
    41   // sequence parser
    42   def ~[S](q: => Parser[I, S]) = new Parser[I, T ~ S] {
    42   def ~[S](q: => Parser[I, S]) = new Parser[I, T ~ S] {