diff -r 2d579af2bb98 -r 4d787a8b79a6 progs/parser-combinators/comb1.sc --- a/progs/parser-combinators/comb1.sc Sun Oct 29 00:06:30 2023 +0100 +++ b/progs/parser-combinators/comb1.sc Sun Oct 29 13:05:09 2023 +0000 @@ -23,7 +23,6 @@ // parser combinators - // alternative parser class AltParser[I : IsSeq, T](p: => Parser[I, T], q: => Parser[I, T]) extends Parser[I, T] { @@ -96,9 +95,11 @@ def map[S](f: => T => S) = new MapParser[I, T, S](p, f) } +// simple example of transforming the +// result into capital letters def toU(s: String) = s.map(_.toUpper) -(p"ELSE").map(toU(_)).parse("ELSEifthen") +(p"else").map(toU(_)).parse("elseifthen") // these implicits allow us to use an infix notation for // sequences and alternatives; we also can write the usual @@ -112,6 +113,7 @@ // A parser for palindromes (just returns them as string) +// since the parser is recursive it needs to be lazy lazy val Pal : Parser[String, String] = { (p"a" ~ Pal ~ p"a").map{ case ((x, y), z) => s"$x$y$z" } || (p"b" ~ Pal ~ p"b").map{ case ((x, y), z) => s"$x$y$z" } ||