summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Phillips <paulp@improving.org>2009-05-06 15:38:18 +0000
committerPaul Phillips <paulp@improving.org>2009-05-06 15:38:18 +0000
commit98be3213152ccec134da3e7856b8096beafcc6d8 (patch)
tree8e6cb835f6ff31843ef9787cc1b551b099eb806b
parent5b1da4217f7f36eab1ba14b5b95667de5bda09ed (diff)
downloadscala-98be3213152ccec134da3e7856b8096beafcc6d8.tar.gz
scala-98be3213152ccec134da3e7856b8096beafcc6d8.tar.bz2
scala-98be3213152ccec134da3e7856b8096beafcc6d8.zip
Added NoSuccess extractor to combinator lib.
-rw-r--r--src/library/scala/util/parsing/combinator/Parsers.scala19
-rw-r--r--test/files/run/packrat1.scala5
-rw-r--r--test/files/run/packrat2.scala3
-rw-r--r--test/files/run/packrat3.scala3
4 files changed, 18 insertions, 12 deletions
diff --git a/src/library/scala/util/parsing/combinator/Parsers.scala b/src/library/scala/util/parsing/combinator/Parsers.scala
index 29fbbebc28..b2c72153fe 100644
--- a/src/library/scala/util/parsing/combinator/Parsers.scala
+++ b/src/library/scala/util/parsing/combinator/Parsers.scala
@@ -153,6 +153,17 @@ trait Parsers {
def get: Nothing = error("No result when parsing failed")
}
+ /** An extractor so NoSuccess(msg, next) can be used in matches
+ * Note: case class inheritance is currently sketchy and may be
+ * deprecated, so an explicit extractor is better.
+ */
+ object NoSuccess {
+ def unapply[T](x: ParseResult[T]) = x match {
+ case Failure(msg, next) => Some(msg, next)
+ case Error(msg, next) => Some(msg, next)
+ case _ => None
+ }
+ }
/** The failure case of ParseResult: contains an error-message and the remaining input.
* Parsing will back-track when a failure occurs.
@@ -304,8 +315,7 @@ trait Parsers {
case (s1 @ Success(_, _), _) => s1
case (_, s2 @ Success(_, _)) => s2
case (e1 @ Error(_, _), _) => e1
- case (f1 @ Failure(_, next1), f2 @ Failure(_, next2)) => if (next2.pos < next1.pos) f1 else f2
- case (f1 @ Failure(_, next1), e2 @ Error(_, next2)) => if (next2.pos < next1.pos) f1 else e2
+ case (f1 @ Failure(_, next1), ns2 @ NoSuccess(_, next2)) => if (next2.pos < next1.pos) f1 else ns2
}
}
override def toString = "|||"
@@ -699,9 +709,8 @@ trait Parsers {
*/
def not[T](p: => Parser[T]): Parser[Unit] = Parser { in =>
p(in) match {
- case s @ Success(_, _) => Failure("Expected failure", in)
- case e @ Error(_, _) => Success((), in)
- case f @ Failure(msg, next) => Success((), in)
+ case Success(_, _) => Failure("Expected failure", in)
+ case _ => Success((), in)
}
}
diff --git a/test/files/run/packrat1.scala b/test/files/run/packrat1.scala
index 69eb8c5fc9..26fa8e76f2 100644
--- a/test/files/run/packrat1.scala
+++ b/test/files/run/packrat1.scala
@@ -22,10 +22,9 @@ object Test extends Application{
object grammars extends StandardTokenParsers with PackratParsers{
- def extractResult(r : ParseResult[_]) = r match{
+ def extractResult(r : ParseResult[_]) = r match {
case Success(a,_) => a
- case Failure(a,_) => a
- case Error(a,_) => a
+ case NoSuccess(a,_) => a
}
lexical.delimiters ++= List("+","-","*","/","(",")")
diff --git a/test/files/run/packrat2.scala b/test/files/run/packrat2.scala
index 3361552561..dda9a32906 100644
--- a/test/files/run/packrat2.scala
+++ b/test/files/run/packrat2.scala
@@ -25,8 +25,7 @@ object grammars2 extends StandardTokenParsers with PackratParsers{
def extractResult(r : ParseResult[_]) = r match{
case Success(a,_) => a
- case Failure(a,_) => a
- case Error(a,_) => a
+ case NoSuccess(a,_) => a
}
lexical.delimiters ++= List("+","-","*","/","(",")")
diff --git a/test/files/run/packrat3.scala b/test/files/run/packrat3.scala
index 34695ef2ed..09acb6e7ff 100644
--- a/test/files/run/packrat3.scala
+++ b/test/files/run/packrat3.scala
@@ -27,8 +27,7 @@ object grammars3 extends StandardTokenParsers with PackratParsers {
def extractResult(r: ParseResult[_]) = r match {
case Success(a,_) => a
- case Failure(a,_) => a
- case Error(a,_) => a
+ case NoSuccess(a,_) => a
}