Skip to content

Commit

Permalink
Merge pull request scala#5717 from som-snytt/issue/10148-followup
Browse files Browse the repository at this point in the history
SI-10148 Accept verbose zero
  • Loading branch information
adriaanm committed Feb 19, 2017
2 parents 147e5dd + f3d271b commit 2fec08b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
8 changes: 4 additions & 4 deletions src/compiler/scala/tools/nsc/ast/parser/Scanners.scala
Expand Up @@ -983,15 +983,16 @@ trait Scanners extends ScannersCommon {

def intVal: Long = intVal(negated = false)

private val zeroFloat = raw"[0.]+(?:[eE][+-]?[0-9]+)?[fFdD]?".r

/** Convert current strVal, base to float value.
*/
def floatVal(negated: Boolean): Float = {
try {
val value: Float = java.lang.Float.parseFloat(strVal)
if (value > Float.MaxValue)
syntaxError("floating point number too large")
val zeroly = "0.fF"
if (value == 0.0f && strVal.exists(c => !zeroly.contains(c)))
if (value == 0.0f && !zeroFloat.pattern.matcher(strVal).matches)
syntaxError("floating point number too small")
if (negated) -value else value
} catch {
Expand All @@ -1010,8 +1011,7 @@ trait Scanners extends ScannersCommon {
val value: Double = java.lang.Double.parseDouble(strVal)
if (value > Double.MaxValue)
syntaxError("double precision floating point number too large")
val zeroly = "0.dD"
if (value == 0.0d && strVal.exists(c => !zeroly.contains(c)))
if (value == 0.0d && !zeroFloat.pattern.matcher(strVal).matches)
syntaxError("double precision floating point number too small")
if (negated) -value else value
} catch {
Expand Down
10 changes: 9 additions & 1 deletion test/files/run/literals.scala
Expand Up @@ -6,7 +6,7 @@

object Test {

/* I add a couple of Unicode identifier tests here temporarily */
/* I add a couple of Unicode identifier tests here "temporarily" */

def \u03b1\u03c1\u03b5\u03c4\u03b7 = "alpha rho epsilon tau eta"

Expand Down Expand Up @@ -80,6 +80,9 @@ object Test {
check_success("1e1f == 10.0f", 1e1f, 10.0f)
check_success(".3f == 0.3f", .3f, 0.3f)
check_success("0f == 0.0f", 0f, 0.0f)
check_success("0f == -0.000000000000000000e+00f", 0f, -0.000000000000000000e+00f)
check_success("0f == -0.000000000000000000e+00F", 0f, -0.000000000000000000e+00F)
check_success("0f == -0.0000000000000000e14f", 0f, -0.0000000000000000e14f)
check_success("01.23f == 1.23f", 01.23f, 1.23f)
check_success("3.14f == 3.14f", 3.14f, 3.14f)
check_success("6.022e23f == 6.022e23f", 6.022e23f, 6.022e23f)
Expand All @@ -96,6 +99,11 @@ object Test {
check_success(".3 == 0.3", .3, 0.3)
check_success("0.0 == 0.0", 0.0, 0.0)
check_success("0d == 0.0", 0d, 0.0)
check_success("0d == 0.000000000000000000e+00d", 0d, 0.000000000000000000e+00d)
check_success("0d == -0.000000000000000000e+00d", 0d, -0.000000000000000000e+00d)
check_success("0d == -0.000000000000000000e+00D", 0d, -0.000000000000000000e+00D)
check_success("0.0 == 0.000000000000000000e+00", 0.0, 0.000000000000000000e+00)
check_success("0.0 == -0.000000000000000000e+00", 0.0, -0.000000000000000000e+00)
check_success("01.23 == 1.23", 01.23, 1.23)
check_success("01.23d == 1.23d", 01.23d, 1.23d)
check_success("3.14 == 3.14", 3.14, 3.14)
Expand Down

0 comments on commit 2fec08b

Please sign in to comment.