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:
parent
278efd7dbe
commit
c29afaeacc
|
@ -1,6 +1,7 @@
|
||||||
package parser
|
package parser
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"math"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
"git.makaay.nl/mauricem/go-parsekit/parse"
|
"git.makaay.nl/mauricem/go-parsekit/parse"
|
||||||
|
@ -61,22 +62,22 @@ var (
|
||||||
zeroFloat = a.Signed(a.Str("0.0"))
|
zeroFloat = a.Signed(a.Str("0.0"))
|
||||||
|
|
||||||
// Special float values can also be expressed. They are always lowercase.
|
// 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"))
|
nan = a.Signed(a.Str("nan"))
|
||||||
)
|
)
|
||||||
|
|
||||||
func (t *parser) startNumber(p *parse.API) {
|
func (t *parser) startNumber(p *parse.API) {
|
||||||
switch {
|
switch {
|
||||||
case p.Accept(float):
|
case p.Accept(tok.Float64(nil, float)):
|
||||||
f, err := strconv.ParseFloat(p.Result().String(), 64)
|
t.emitCommand(csetFloatVal, p.Result().Value(0).(float64))
|
||||||
if err != nil {
|
case p.Accept(nan):
|
||||||
p.Error("Cannot parse value 0%s: %s", p.Result().String(), err)
|
t.emitCommand(csetFloatVal, math.NaN())
|
||||||
|
case p.Accept(inf):
|
||||||
|
if p.Result().Rune(0) == '-' {
|
||||||
|
t.emitCommand(csetFloatVal, math.Inf(-1))
|
||||||
} else {
|
} 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):
|
case p.Accept(zero):
|
||||||
p.Handle(t.startIntegerStartingWithZero)
|
p.Handle(t.startIntegerStartingWithZero)
|
||||||
case p.Accept(tok.Int64(nil, integer)):
|
case p.Accept(tok.Int64(nil, integer)):
|
||||||
|
|
|
@ -94,6 +94,12 @@ func TestFloat(t *testing.T) {
|
||||||
{`6.626e-34`, []string{`float(6.626e-34)`}},
|
{`6.626e-34`, []string{`float(6.626e-34)`}},
|
||||||
{`224_617.445_991_228`, []string{`float(224617.445991228)`}},
|
{`224_617.445_991_228`, []string{`float(224617.445991228)`}},
|
||||||
{`12_345.111_222e+1_2_3`, []string{`float(1.2345111222e+127)`}},
|
{`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{}
|
p := &parser{}
|
||||||
testParseHandler(t, p, p.startNumber, test)
|
testParseHandler(t, p, p.startNumber, test)
|
||||||
|
|
Loading…
Reference in New Issue