Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

upgrade to scalaz 7.1 #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ parallelExecution := true

javacOptions += "-Xlint"

val scalazVersion = "7.0.6"
val scalazVersion = "7.1.0"

libraryDependencies ++= Seq(
"org.scalaz" %% "scalaz-core" % scalazVersion,
Expand Down
40 changes: 20 additions & 20 deletions src/main/scala/scalaparsers/Parser.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import scalaparsers.Document.{ text, fillSep }
import scala.collection.immutable.List
import scalaz.{ Monad }
import scalaz.Scalaz._
import scalaz.Free.{ suspend, Return, Trampoline }
import scalaz.Free.{ suspend, return_, Trampoline }
import scalaz.Ordering._

import Supply._
Expand All @@ -17,7 +17,7 @@ import Supply._
*/
abstract class Parser[S, +A] extends MonadicPlus[Parser[S,+?],A] { that =>
def self = that
def apply(s: ParseState[S], vs: Supply): Trampoline[ParseResult[S,A]]
def apply[B >: A](s: ParseState[S], vs: Supply): Trampoline[ParseResult[S,B]]
def run(s: ParseState[S], vs: Supply): Either[Err, (ParseState[S], A)] = apply(s,vs).run match {
case Pure(a,_) => Right((s,a))
case Commit(t,a,_) => Right((t,a))
Expand All @@ -27,20 +27,20 @@ abstract class Parser[S, +A] extends MonadicPlus[Parser[S,+?],A] { that =>

// functorial
def map[B](f: A => B) = new Parser[S,B] {
def apply(s: ParseState[S], vs: Supply) = that(s, vs).map(_ map f)
def apply[C >: B](s: ParseState[S], vs: Supply) = that(s, vs).map(_ map f)
}

// filtered
def lift[B](p: Parser[S,B]) = p
def withFilter(p : A => Boolean): Parser[S,A] = new Parser[S,A] {
def apply(s: ParseState[S], vs: Supply) = that(s, vs).map {
def apply[B >: A](s: ParseState[S], vs: Supply) = that(s, vs).map {
case Pure(a,e) if !p(a) => e
case Commit(t,a,xs) if !p(a) => Err.report(t.loc,None,List(),xs)
case r => r
}
}
override def filterMap[B](f: A => Option[B]) = new Parser[S,B] {
def apply(s: ParseState[S], vs: Supply) = that(s, vs).map {
def apply[C >: B](s: ParseState[S], vs: Supply) = that(s, vs).map {
case Pure(a,e) => f(a) match {
case Some(b) => Pure(b, e)
case None => e
Expand All @@ -55,7 +55,7 @@ abstract class Parser[S, +A] extends MonadicPlus[Parser[S,+?],A] { that =>

// monadic
def flatMap[B](f: A => Parser[S,B]) = new Parser[S,B] {
def apply(s: ParseState[S], vs: Supply) = that(s, vs).flatMap {
def apply[C >: B](s: ParseState[S], vs: Supply) = that(s, vs).flatMap {
case r@Pure(a, e) => f(a)(s, vs).map {
case Pure(b, ep) => Pure(b, e ++ ep)
case r : Fail => e ++ r
Expand All @@ -66,19 +66,19 @@ abstract class Parser[S, +A] extends MonadicPlus[Parser[S,+?],A] { that =>
case Fail(e, aux, ys) => Err.report(t.loc, e, aux, xs ++ ys)
case r => r
}
case r : ParseFailure => suspend(Return(r))
case r : ParseFailure => return_(r)
}
}

def wouldSucceed: Parser[S, Boolean] = new Parser[S,Boolean] {
def apply(s: ParseState[S], vs: Supply) = that(s, vs).map {
def apply[B >: Boolean](s: ParseState[S], vs: Supply) = that(s, vs).map {
case e : ParseFailure => Pure(false)
case _ => Pure(true)
}
}

def race[B >: A](p: Parser[S, B]) = new Parser[S,B] {
def apply(s: ParseState[S], vs: Supply) = that(s, vs).flatMap {
def apply[C >: B](s: ParseState[S], vs: Supply) = that(s, vs).flatMap {
case e : Fail => p(s, vs) map {
case ep : Fail => e ++ ep
case Pure(b, ep) => Pure[B](b, e ++ ep)
Expand All @@ -93,32 +93,32 @@ abstract class Parser[S, +A] extends MonadicPlus[Parser[S,+?],A] { that =>
}
case r => r
}
case r => suspend(Return(r))
case r => return_(r)
}
}

// monadicplus
def |[B >: A](other: => Parser[S,B]) = new Parser[S,B] {

def apply(s: ParseState[S], vs: Supply) = that(s, vs).flatMap {
def apply[C >: B](s: ParseState[S], vs: Supply) = that(s, vs).flatMap {
case e : Fail => other(s, vs).map {
case ep : Fail => e ++ ep
case Pure(a, ep) => Pure(a, e ++ ep)
case r => r
}
case r => suspend(Return(r))
case r => return_(r)
}
}
def orElse[B >: A](b: => B) = new Parser[S,B] {
def apply(s: ParseState[S], vs: Supply) = that(s, vs).map {
def apply[C >: B](s: ParseState[S], vs: Supply) = that(s, vs).map {
case e : Fail => Pure(b, e)
case r => r
}
}

// context
def scope(desc: String) = new Parser[S,A] {
def apply(s: ParseState[S], vs: Supply) = that(s, vs).map {
def apply[B >: A](s: ParseState[S], vs: Supply) = that(s, vs).map {
case Fail(m, aux, _) => Fail(m, aux, Set(desc))
case Err(p,d,aux,stk) if s.tracing => Err(p,d,aux,(s.loc,desc)::stk)
case Pure(a, Fail(m : Some[Document], aux, _)) => Pure(a, Fail(m, aux, Set(desc))) // TODO: can we drop the Some?
Expand All @@ -128,32 +128,32 @@ abstract class Parser[S, +A] extends MonadicPlus[Parser[S,+?],A] { that =>

// allow backtracking to retry after a parser state change
def attempt = new Parser[S,A] {
def apply(s: ParseState[S], vs: Supply) = that(s, vs).map {
def apply[B >: A](s: ParseState[S], vs: Supply) = that(s, vs).map {
case e@Err(p,d,aux,stk) => Fail(None, List(e.pretty), Set()) // we can attach the current message, now!
case r => r
}
}
def attempt(s: String): Parser[S,A] = attempt scope s

def not = new Parser[S,Unit] {
def apply(s: ParseState[S], vs: Supply) = that(s, vs).map {
def apply[A >: Unit](s: ParseState[S], vs: Supply) = that(s, vs).map {
case Pure(a, _) => Fail(Some("unexpected" :+: text(a.toString)))
case Commit(t, a, _) => Err.report(s.loc, Some("unexpected" :+: text(a.toString)), List(), Set())
case _ => Pure[Unit](())
}
}
def handle[B >: A](f: ParseFailure => Parser[S,B]) = new Parser[S,B] {
def apply(s: ParseState[S], vs: Supply) = that(s, vs).flatMap {
def apply[C >: B](s: ParseState[S], vs: Supply) = that(s, vs).flatMap {
case r : Err => f(r)(s, vs)
case r@Fail(e, aux, xs) => f(r)(s, vs).map {
case Fail(ep, auxp, ys) => Fail(ep orElse e, if (ep.isDefined) auxp else aux, xs ++ ys)
case r => r
}
case r => suspend(Return(r))
case r => return_(r)
}
}
def slice = new Parser[S,String] {
def apply(s: ParseState[S], vs: Supply) = that(s, vs).map {
def apply[A >: String](s: ParseState[S], vs: Supply) = that(s, vs).map {
case Pure(_, e) => Pure("", e)
case Commit(t, _, xs) => Commit(t, s.input.substring(s.offset, t.offset), xs)
// s.rest.take(s.rest.length - t.rest.length), xs)
Expand All @@ -165,6 +165,6 @@ abstract class Parser[S, +A] extends MonadicPlus[Parser[S,+?],A] { that =>

object Parser {
def apply[A,S](f: (ParseState[S], Supply) => ParseResult[S,A]) = new Parser[S,A] {
def apply(s: ParseState[S], vs: Supply) = suspend(Return(f(s, vs)))
def apply[B >: A](s: ParseState[S], vs: Supply) = return_(f(s, vs))
}
}
5 changes: 2 additions & 3 deletions src/main/scala/scalaparsers/ParsingUtil.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package scalaparsers

import Document.{ text }
import scalaz._
import scalaz.Free.{ Return, suspend }
import scalaz.Scalaz._
import scala.collection.immutable.List
import Diagnostic._
Expand All @@ -18,7 +17,7 @@ trait Parsing[S] {
type ParseState = scalaparsers.ParseState[S]

def unit[A](a: A): Parser[A] = new Parser[A] {
def apply(s: ParseState, vs: Supply) = suspend(Return(Pure(a)))
def apply[B >: A](s: ParseState, vs: Supply) = Free.point(Pure(a))
override def map[B](f: A => B) = unit(f(a))
override def flatMap[B](f: A => Parser[B]) = f(a)
}
Expand All @@ -35,7 +34,7 @@ trait Parsing[S] {

implicit def parserMonad: Monad[Parser] = new Monad[Parser] {
def point[A](a: => A) = new Parser[A] {
def apply(s: ParseState, vs: Supply) = suspend(Return(Pure(a)))
def apply[B >: A](s: ParseState, vs: Supply) = Free.point(Pure(a))
override def map[B](f : A => B) = pure(f(a))
}
override def map[A,B](m: Parser[A])(f: A => B) = m map f
Expand Down