Skip to content
This repository was archived by the owner on Oct 24, 2024. It is now read-only.

Rules: Fix parsing of floating point numbers #12

Merged
merged 1 commit into from
Aug 21, 2019
Merged
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
22 changes: 17 additions & 5 deletions shared/src/main/scala/toml/Rules.scala
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,27 @@ object Rules extends PlatformRules {

def rmUnderscore(s: String) = s.replace("_", "")

val +- = P(CharIn(List('+', '-')))
val sign = P(CharIn("+-"))
val integral = P(digits.rep(min = 1, sep = "_"))
val fractional = P("." ~ integral)
val exponent = P(CharIn("eE") ~ +-.? ~ integral)
val exponent = P(CharIn("eE") ~ sign.? ~ integral)
val integer: Parser[Value.Num] =
P(+-.? ~ integral).!.map(s => Value.Num(rmUnderscore(s).toLong))
P(sign.? ~ integral).!.map(s => Value.Num(rmUnderscore(s).toLong))
val double: Parser[Value.Real] =
P(+-.? ~ integral ~ (fractional | exponent)).!.map(s =>
Value.Real(rmUnderscore(s).toDouble))
P(
sign.?.! ~
(
P("inf").map(_ => Double.PositiveInfinity) |
P("nan").map(_ => Double.NaN) |
P(integral ~ (
(fractional ~ exponent) |
fractional |
exponent
)).!.map(s => rmUnderscore(s).toDouble)
)
).map { case (sign, value) =>
if (sign == "-") Value.Real(-value) else Value.Real(value)
}

val `true` = P("true") .map(_ => Value.Bool(true))
val `false` = P("false").map(_ => Value.Bool(false))
Expand Down
72 changes: 72 additions & 0 deletions shared/src/test/scala/toml/RulesSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,76 @@ class RulesSpec extends FunSuite with Matchers {
val example = "number = 3.14 p"
testFailure(example)
}

test("Parse floats with fractional part") {
val example =
"""flt1 = +1.0
|flt2 = 3.1415
|flt3 = -0.01
""".stripMargin

assert(testSuccess(example) == Root(List(
Node.Pair("flt1", Value.Real(+1.0)),
Node.Pair("flt2", Value.Real(3.1415)),
Node.Pair("flt3", Value.Real(-0.01)))))
}

test("Parse floats with exponent part") {
val example =
"""flt4 = 5e+22
|flt5 = 1e6
|flt6 = -2E-2
""".stripMargin

assert(testSuccess(example) == Root(List(
Node.Pair("flt4", Value.Real(5e+22)),
Node.Pair("flt5", Value.Real(1e6)),
Node.Pair("flt6", Value.Real(-2E-2)))))
}

test("Parse floats with fractional and exponent part") {
val example = "flt7 = 6.626e-34\n" +
"poly = -45.321e12"

assert(testSuccess(example) == Root(List(
Node.Pair("flt7", Value.Real(6.626e-34)),
Node.Pair("poly", Value.Real(-45.321e12)))))
}

test("Parse floats with underscores") {
val example = "flt8 = 224_617.445_991_228"

assert(testSuccess(example) == Root(List(
Node.Pair("flt8", Value.Real(224617.445991228)))))
}

test("Parse infinity constant") {
val example =
"""sf1 = inf
|sf2 = +inf
|sf3 = -inf
|""".stripMargin

assert(testSuccess(example) == Root(List(
Node.Pair("sf1", Value.Real(Double.PositiveInfinity)),
Node.Pair("sf2", Value.Real(Double.PositiveInfinity)),
Node.Pair("sf3", Value.Real(Double.NegativeInfinity)))))
}

test("Parse NaN constant") {
val example =
"""sf4 = nan
|sf5 = +nan
|sf6 = -nan
|""".stripMargin

val result = testSuccess(example).nodes
assert(result.length == 3)

// Cannot use `==` here since Double.NaN != Double.NaN
assert(result.forall {
case Node.Pair(_, Value.Real(v)) => v.equals(Double.NaN)
case _ => false
})
}
}