progs/parser-combinators/comb2.sc
changeset 948 6bb67c2dcfd3
parent 941 66adcae6c762
child 954 eda0ccf56c72
--- a/progs/parser-combinators/comb2.sc	Sun Oct 29 00:06:30 2023 +0100
+++ b/progs/parser-combinators/comb2.sc	Sun Oct 29 13:05:09 2023 +0000
@@ -14,9 +14,9 @@
 // it allows writing nested patterns as
 // case x ~ y ~ z => ...
 
+case class ~[+A, +B](x: A, y: B)
 
 
-case class ~[+A, +B](x: A, y: B)
 
 type IsSeq[I] = I => Seq[_]
 
@@ -45,7 +45,7 @@
 }
 
 // map parser
-class maparser[I : IsSeq, T, S](p: => Parser[I, T], 
+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)
 }
@@ -70,7 +70,7 @@
 }
 
 
-// atomic parser for numbers (transformed into ints)
+// atomic parser for numbers (transformed into Ints)
 case object NumParser extends Parser[String, Int] {
   val reg = "[0-9]+".r
   def parse(sb: String) = reg.findPrefixOf(sb) match {
@@ -95,7 +95,7 @@
 extension [I : IsSeq, T](p: Parser[I, T]) {
   def ||(q : => Parser[I, T]) = new AltParser[I, T](p, q)
   def ~[S] (q : => Parser[I, S]) = new SeqParser[I, T, S](p, q)
-  def map[S](f: => T => S) = new maparser[I, T, S](p, f)
+  def map[S](f: => T => S) = new MapParser[I, T, S](p, f)
 }
 
 
@@ -134,8 +134,8 @@
   (Fa ~ p"/" ~ Te).map[AExp]{ case x ~ _ ~ z => Aop("/", x, z) } || Fa  
 lazy val Fa: Parser[String, AExp] = 
    (p"(" ~ AExp ~ p")").map{ case _ ~ y ~ _ => y } || 
-   IdParser.map(Var) || 
-   NumParser.map(Num)
+   IdParser.map(Var(_)) || 
+   NumParser.map(Num(_))
 
 // boolean expressions with some simple nesting
 lazy val BExp: Parser[String, BExp] =