Commits

pchiusano committed 4cf6446

fixed modifyUser, got rid of casts

  • Participants
  • Parent commits 194c960
  • Branches error-reporting

Comments (0)

Files changed (2)

src/main/scala/nomo/Accumulator.scala

   case class Position(charactersRead: Int, line: Int, sinceTab: Int, column: Int) {
     def >=(pos: Position) = 
       charactersRead >= pos.charactersRead 
-    override def toString = "%d.%d".format(line, column)
+    override def toString = "%d.%d".format(line+1, column+1)
   }
 
   def pos[U](tabstop: Int, start: Position, user: U): Accumulator[Char,Position,U] = new Accumulator[Char,Position,U] {

src/main/scala/nomo/Parser.scala

       case Right(a) => 
         val Status(c2, s2) = f(a)
         Status(committed || c2, s2)
-      case _ => this.asInstanceOf[Status[E2,B]]
+      case Left(e) => Status(committed, Left(e))
     }
   def flatMapL[E2,A2>:A](f: E => Status[E2,A2]): Status[E2,A2] =
     status match {
       case Left(e) => 
         val Status(c2, s2) = f(e)
         Status(committed || c2, s2)
-      case _ => this.asInstanceOf[Status[E2,A2]]
+      case Right(a) => Status(committed, Right(a))
     }
 }
 object Failure { def apply[E](e: E) = Status(false, Left(e)) }
               case Status(c,Right(pb)) => 
                 // (if (false) (pb.commitOnEnter) else pb).feedAll(rem,ann)
                 (if (c) (pb.commitOnEnter) else pb).feedAll(rem,ann)
-              case s => suspendS { 
-                (Done(s.asInstanceOf[Status[E,B]],rem,ann), a2) } 
+              case s@Status(c,Left(e)) => suspendS { 
+                (Done(Status(c,Left(e)),rem,ann), a2) } 
             })
           case _ => suspendS { (go(p2), a2) }
         }})
   def attempt[A](p: Parser[A]): Parser[A] = p.mapStatus(_.uncommit) 
 
   /** Parses the single token given. */
-  def single(c: I): Parser[I] = any mapResult (s =>
-    s.status.flatMap(i => if (i == c) Success(i) else Failure(err.single(c, s.position))))
+  def single(c: I): Parser[I] = 
+    any.filter(_ == c, pos => err.single(c, pos))
 
   /** Parses any single token. */
   def any: Parser[I] =
 
   /** Parser which consumes no input and modifies the user state using the given function. */
   def modifyUser(f: U => U): Parser[Unit] = 
-    getUser flatMap (setUser(_))
+    getUser flatMap (u => setUser(f(u)))
 
   /** Parser which consumes no input and always returns the given A. */
   def unit[A](a: A): Parser[A] = {
   
   /** Converts failures of this parser to errors. Use `commit` to do this only 
     * after a successful parse of `p`. */
-  def commitOnEnter[A](p: Parser[A]): Parser[A] = p mapStatus (_.commit)
+  def commitOnEnter[A](p: Parser[A]): Parser[A] = 
+    p.mapStatus(_.commit)
+    //position.flatMap(pos => p mapResult (
+    //  r => if (r.position == pos) r.status else r.status.commit))
 
   /** Converts any failing parses sequenced after `p` to errors. 
     * Note that if `p` fails, this is considered before the commit point. */