Improved float parsing

* Fixed #30
 * Accept underscores in float literals
 * Added test
This commit is contained in:
Felix Krause 2016-11-10 10:12:32 +01:00
parent 52298298e5
commit a51befe30d
3 changed files with 16 additions and 3 deletions

View File

@ -5,7 +5,7 @@
# distribution, for details about the copyright.
import "../yaml"
import unittest, strutils, streams, tables, times
import unittest, strutils, streams, tables, times, math
type
MyTuple = tuple
@ -197,6 +197,15 @@ suite "Serialization":
load(input, result)
assert(result == 14)
test "Load floats":
let input = "[6.8523015e+5, 685.230_15e+03, 685_230.15, -.inf, .NaN]"
var result: seq[float]
load(input, result)
for i in 0..2:
assert result[i] == 6.8523015e+5
assert result[3] == NegInf
assert classify(result[4]) == fcNan
test "Load nil string":
let input = newStringStream("!<tag:nimyaml.org,2016:nil:string> \"\"")
var result: string

View File

@ -124,6 +124,9 @@ template advanceTypeHint(ch: char) {.dirty.} =
ythMonth1 => ythMonthMinusNoYmd
ythMonth2 => ythMonthMinus
[ythFraction, ythSecond2] => ythAfterTimePlusMinus
of '_':
[ythInt1, ythInt2, ythInt3, ythInt4] => ythInt
[ythInt, ythDecimal] => nil
of ':':
[ythHour1, ythHour2] => ythHourColon
ythMinute2 => ythMinuteColon

View File

@ -16,7 +16,7 @@
## type. Please consult the serialization guide on the NimYAML website for more
## information.
import tables, typetraits, strutils, macros, streams, times
import tables, typetraits, strutils, macros, streams, times, parseutils
import parser, taglib, presenter, stream, ../private/internal, hints
export stream
# *something* in here needs externally visible `==`(x,y: AnchorId),
@ -138,7 +138,8 @@ template constructScalarItem*(s: var YamlStream, i: untyped,
except YamlConstructionError: raise
except Exception:
var e = constructionError(s,
"Cannot construct to " & name(t) & ": " & item.scalarContent)
"Cannot construct to " & name(t) & ": " & item.scalarContent &
"; error: " & getCurrentExceptionMsg())
e.parent = getCurrentException()
raise e