summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorStephen Judkins <stephen.judkins@gmail.com>2012-01-21 17:08:52 -0800
committerStephen Judkins <stephen.judkins@gmail.com>2012-01-21 17:08:52 -0800
commitdce6b34c38a6d774961ca6f9fd50b11300ecddd6 (patch)
tree449361e5c2936c67370e85ebc97844371201187d /src
parent8051740e1be3b33081b2179d1d1fd35a4c8b5c84 (diff)
downloadscala-dce6b34c38a6d774961ca6f9fd50b11300ecddd6.tar.gz
scala-dce6b34c38a6d774961ca6f9fd50b11300ecddd6.tar.bz2
scala-dce6b34c38a6d774961ca6f9fd50b11300ecddd6.zip
Fixes SI-4929, with a test to verify.
Also fixes potential issue with Parsers.phrase not being reentrant; however, I was unable to actually reproduce this issue in practice. (The order in which lastNoSuccess was being set and compared seemed to guarantee that it would never actually be a problem).
Diffstat (limited to 'src')
-rw-r--r--src/library/scala/util/parsing/combinator/Parsers.scala29
1 files changed, 15 insertions, 14 deletions
diff --git a/src/library/scala/util/parsing/combinator/Parsers.scala b/src/library/scala/util/parsing/combinator/Parsers.scala
index 4004a01ad9..70c92342b4 100644
--- a/src/library/scala/util/parsing/combinator/Parsers.scala
+++ b/src/library/scala/util/parsing/combinator/Parsers.scala
@@ -12,6 +12,7 @@ import scala.util.parsing.input._
import scala.collection.mutable.ListBuffer
import scala.annotation.tailrec
import annotation.migration
+import scala.util.DynamicVariable
// TODO: better error handling (labelling like parsec's <?>)
@@ -153,13 +154,14 @@ trait Parsers {
val successful = true
}
- var lastNoSuccess: NoSuccess = null
+ private lazy val lastNoSuccess = new DynamicVariable[Option[NoSuccess]](None)
/** A common super-class for unsuccessful parse results. */
sealed abstract class NoSuccess(val msg: String, override val next: Input) extends ParseResult[Nothing] { // when we don't care about the difference between Failure and Error
val successful = false
- if (!(lastNoSuccess != null && next.pos < lastNoSuccess.next.pos))
- lastNoSuccess = this
+
+ if (lastNoSuccess.value map { v => !(next.pos < v.next.pos) } getOrElse true)
+ lastNoSuccess.value = Some(this)
def map[U](f: Nothing => U) = this
def mapPartial[U](f: PartialFunction[Nothing, U], error: Nothing => String): ParseResult[U] = this
@@ -487,7 +489,7 @@ trait Parsers {
}
/** Changes the error message produced by a parser.
- *
+ *
* This doesn't change the behavior of a parser on neither
* success nor failure, just on error. The semantics are
* slightly different than those obtained by doing `| error(msg)`,
@@ -877,16 +879,15 @@ trait Parsers {
* if `p` consumed all the input.
*/
def phrase[T](p: Parser[T]) = new Parser[T] {
- lastNoSuccess = null
- def apply(in: Input) = p(in) match {
- case s @ Success(out, in1) =>
- if (in1.atEnd)
- s
- else if (lastNoSuccess == null || lastNoSuccess.next.pos < in1.pos)
- Failure("end of input expected", in1)
- else
- lastNoSuccess
- case _ => lastNoSuccess
+ def apply(in: Input) = lastNoSuccess.withValue(None) {
+ p(in) match {
+ case s @ Success(out, in1) =>
+ if (in1.atEnd)
+ s
+ else
+ lastNoSuccess.value filterNot { _.next.pos < in1.pos } getOrElse Failure("end of input expected", in1)
+ case ns => lastNoSuccess.value.getOrElse(ns)
+ }
}
}