summaryrefslogtreecommitdiff
path: root/src/compiler/scala/tools/nsc/settings/ScalaVersion.scala
diff options
context:
space:
mode:
authorSom Snytt <som.snytt@gmail.com>2015-02-20 16:46:00 -0800
committerSom Snytt <som.snytt@gmail.com>2015-02-20 17:22:33 -0800
commit80362f9fc2dfa1c9d5560ad2aa8b2eef320e5fd6 (patch)
tree97a955f7778dfd4fc01b758d4d62cf35beb692e3 /src/compiler/scala/tools/nsc/settings/ScalaVersion.scala
parent178e8df9b6a91375a6162721a0cbc2d90bcc7451 (diff)
downloadscala-80362f9fc2dfa1c9d5560ad2aa8b2eef320e5fd6.tar.gz
scala-80362f9fc2dfa1c9d5560ad2aa8b2eef320e5fd6.tar.bz2
scala-80362f9fc2dfa1c9d5560ad2aa8b2eef320e5fd6.zip
SI-9167 Clarify ScalaVersion parsing
Probably it was unintended to accept "2.." and "2.-11.7". This commit makes it a bit more regular and also accepts arbitrary text after the dash in the build string, including newlines. Since the number parts must be numbers, accept only digits. That also disallows "2.+11.7", which could be misconstrued as some sort of range. As before, the special build string prefixes "rc" and "m" are case-insensitive, but "FINAL" is not.
Diffstat (limited to 'src/compiler/scala/tools/nsc/settings/ScalaVersion.scala')
-rw-r--r--src/compiler/scala/tools/nsc/settings/ScalaVersion.scala50
1 files changed, 22 insertions, 28 deletions
diff --git a/src/compiler/scala/tools/nsc/settings/ScalaVersion.scala b/src/compiler/scala/tools/nsc/settings/ScalaVersion.scala
index 43bdad5882..7e67b7bec6 100644
--- a/src/compiler/scala/tools/nsc/settings/ScalaVersion.scala
+++ b/src/compiler/scala/tools/nsc/settings/ScalaVersion.scala
@@ -68,45 +68,39 @@ case object AnyScalaVersion extends ScalaVersion {
* Factory methods for producing ScalaVersions
*/
object ScalaVersion {
- private val dot = "\\."
- private val dash = "\\-"
- private def not(s:String) = s"[^${s}]"
- private val R = s"((${not(dot)}*)(${dot}(${not(dot)}*)(${dot}(${not(dash)}*)(${dash}(.*))?)?)?)".r
-
- def apply(versionString : String, errorHandler: String => Unit): ScalaVersion = {
- def errorAndValue() = {
- errorHandler(
- s"There was a problem parsing ${versionString}. " +
- "Versions should be in the form major[.minor[.revision]] " +
- "where each part is a positive number, as in 2.10.1. " +
- "The minor and revision parts are optional."
- )
- AnyScalaVersion
- }
+ private val dot = """\."""
+ private val dash = "-"
+ private val vchar = """\d""" //"[^-+.]"
+ private val vpat = s"(?s)($vchar+)(?:$dot($vchar+)(?:$dot($vchar+)(?:$dash(.*))?)?)?".r
+ private val rcpat = """(?i)rc(\d*)""".r
+ private val mspat = """(?i)m(\d*)""".r
+
+ def apply(versionString: String, errorHandler: String => Unit): ScalaVersion = {
+ def error() = errorHandler(
+ s"There was a problem parsing ${versionString}. " +
+ "Versions should be in the form major[.minor[.revision]] " +
+ "where each part is a positive number, as in 2.10.1. " +
+ "The minor and revision parts are optional."
+ )
def toInt(s: String) = s match {
case null | "" => 0
- case _ => s.toInt
+ case _ => s.toInt
}
- def isInt(s: String) = util.Try(toInt(s)).isSuccess
-
def toBuild(s: String) = s match {
case null | "FINAL" => Final
- case s if (s.toUpperCase.startsWith("RC") && isInt(s.substring(2))) => RC(toInt(s.substring(2)))
- case s if (s.toUpperCase.startsWith("M") && isInt(s.substring(1))) => Milestone(toInt(s.substring(1)))
- case _ => Development(s)
+ case rcpat(i) => RC(toInt(i))
+ case mspat(i) => Milestone(toInt(i))
+ case _ /* | "" */ => Development(s)
}
- try versionString match {
+ versionString match {
case "none" => NoScalaVersion
- case "any" => AnyScalaVersion
- case R(_, majorS, _, minorS, _, revS, _, buildS) =>
+ case "any" => AnyScalaVersion
+ case vpat(majorS, minorS, revS, buildS) =>
SpecificScalaVersion(toInt(majorS), toInt(minorS), toInt(revS), toBuild(buildS))
- case _ =>
- errorAndValue()
- } catch {
- case e: NumberFormatException => errorAndValue()
+ case _ => error() ; AnyScalaVersion
}
}