--- a/progs/fun_parser.scala Sun Oct 13 00:53:20 2019 +0100
+++ b/progs/fun_parser.scala Mon Oct 14 00:02:24 2019 +0100
@@ -34,13 +34,12 @@
abstract class Parser[I, T](implicit ev: I => Seq[_]) {
def parse(ts: I): Set[(T, I)]
- def parse_all(ts: I) : Set[T] =
- for ((head, tail) <- parse(ts); if (tail.isEmpty)) yield head
-
- def parse_single(ts: I) : T = parse_all(ts).toList match {
- case List(t) => t
- case _ => { println ("Parse Error\n") ; sys.exit(-1) }
- }
+ def parse_single(ts: I) : T =
+ parse(ts).partition(_._2.isEmpty) match {
+ case (good, _) if !good.isEmpty => good.head._1
+ case (_, err) => {
+ println (s"Parse Error\n${err.minBy(_._2.length)}") ; sys.exit(-1) }
+ }
}
// convenience for writing grammar rules