Nope, NOW I'm done with the number types. On reading the docs, I noticed

that I missed an implementation for floats 'nan' and 'inf'. All taken care
of now!
This commit is contained in:
Maurice Makaay 2019-06-18 23:13:59 +00:00
parent 278efd7dbe
commit c29afaeacc
2 changed files with 17 additions and 10 deletions

View File

@ -1,6 +1,7 @@
package parser
import (
"math"
"strconv"
"git.makaay.nl/mauricem/go-parsekit/parse"
@ -61,22 +62,22 @@ var (
zeroFloat = a.Signed(a.Str("0.0"))
// Special float values can also be expressed. They are always lowercase.
infinity = a.Signed(a.Str("inf"))
inf = a.Signed(a.Str("inf"))
nan = a.Signed(a.Str("nan"))
)
func (t *parser) startNumber(p *parse.API) {
switch {
case p.Accept(float):
f, err := strconv.ParseFloat(p.Result().String(), 64)
if err != nil {
p.Error("Cannot parse value 0%s: %s", p.Result().String(), err)
case p.Accept(tok.Float64(nil, float)):
t.emitCommand(csetFloatVal, p.Result().Value(0).(float64))
case p.Accept(nan):
t.emitCommand(csetFloatVal, math.NaN())
case p.Accept(inf):
if p.Result().Rune(0) == '-' {
t.emitCommand(csetFloatVal, math.Inf(-1))
} else {
t.emitCommand(csetFloatVal, f)
t.emitCommand(csetFloatVal, math.Inf(+1))
}
case p.Accept(zeroFloat):
f, _ := strconv.ParseFloat(p.Result().String(), 64)
t.emitCommand(csetFloatVal, f)
case p.Accept(zero):
p.Handle(t.startIntegerStartingWithZero)
case p.Accept(tok.Int64(nil, integer)):

View File

@ -94,6 +94,12 @@ func TestFloat(t *testing.T) {
{`6.626e-34`, []string{`float(6.626e-34)`}},
{`224_617.445_991_228`, []string{`float(224617.445991228)`}},
{`12_345.111_222e+1_2_3`, []string{`float(1.2345111222e+127)`}},
{`+nan`, []string{`float(NaN)`}},
{`-nan`, []string{`float(NaN)`}},
{`nan`, []string{`float(NaN)`}},
{`inf`, []string{`float(+Inf)`}},
{`+inf`, []string{`float(+Inf)`}},
{`-inf`, []string{`float(-Inf)`}},
} {
p := &parser{}
testParseHandler(t, p, p.startNumber, test)