--- a/progs/parser-combinators/comb1.sc Mon Jul 27 11:02:48 2020 +0100
+++ b/progs/parser-combinators/comb1.sc Thu Jul 30 13:50:54 2020 +0100
@@ -1,11 +1,14 @@
// Parser Combinators: Simple Version
//====================================
+//
+// Call with
+//
+// amm comb1.sc
-/*
- Note, in the lectures I did not show the implicit type constraint
- I : IsSeq, which means that the input type 'I' needs to be
- a sequence.
-*/
+
+// Note, in the lectures I did not show the implicit type constraint
+// I : IsSeq, which means that the input type 'I' needs to be
+// a sequence.
type IsSeq[A] = A => Seq[_]
@@ -33,12 +36,14 @@
def parse(in: I) = p.parse(in) ++ q.parse(in)
}
-// parser map
+// map parser
class MapParser[I : IsSeq, T, S](p: => Parser[I, T],
f: T => S) extends Parser[I, S] {
def parse(in: I) = for ((hd, tl) <- p.parse(in)) yield (f(hd), tl)
}
+
+
// an example of an atomic parser for characters
case class CharParser(c: Char) extends Parser[String, Char] {
def parse(in: String) =
@@ -59,6 +64,7 @@
val NumParser = RegexParser("[0-9]+".r)
def StrParser(s: String) = RegexParser(Regex.quote(s).r)
+
// NumParserInt transforms a "string integer" into a propper Int
// (needs "new" because MapParser is not a case class)
@@ -210,7 +216,7 @@
// a problem with the arithmetic expression parser: it
-// gets vert slow with deeply nested parentheses
+// gets very slow with deeply nested parentheses
println("Runtime problem")
println(E.parse("1"))