Skip to content

Commit

Permalink
Quote the numbers even if they are out of range
Browse files Browse the repository at this point in the history
  • Loading branch information
shuheiktgw committed Dec 15, 2024
1 parent e1d8782 commit b5d5b31
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 16 deletions.
10 changes: 5 additions & 5 deletions ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,12 +301,12 @@ func checkLineBreak(t *token.Token) bool {
}
// Due to the way that comment parsing works its assumed that when a null value does not have new line in origin
// it was squashed therefore difference is ignored.
//foo:
// foo:
// bar:
// # comment
// baz: 1
//becomes
//foo:
// becomes
// foo:
// bar: null # comment
//
// baz: 1
Expand Down Expand Up @@ -342,7 +342,7 @@ func Bool(tk *token.Token) *BoolNode {
// Integer create node for integer value
func Integer(tk *token.Token) *IntegerNode {
var v any
if num := token.ToNumber(tk.Value); num != nil {
if num, _ := token.ToNumber(tk.Value); num != nil {
v = num.Value
}
return &IntegerNode{
Expand All @@ -355,7 +355,7 @@ func Integer(tk *token.Token) *IntegerNode {
// Float create node for float value
func Float(tk *token.Token) *FloatNode {
var v float64
if num := token.ToNumber(tk.Value); num != nil && num.Type == token.NumberTypeFloat {
if num, _ := token.ToNumber(tk.Value); num != nil && num.Type == token.NumberTypeFloat {
value, ok := num.Value.(float64)
if ok {
v = value
Expand Down
31 changes: 20 additions & 11 deletions token/token.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package token

import (
"errors"
"fmt"
"strconv"
"strings"
Expand Down Expand Up @@ -538,16 +539,16 @@ type NumberValue struct {
Text string
}

func ToNumber(value string) *NumberValue {
func ToNumber(value string) (*NumberValue, error) {
if len(value) == 0 {
return nil
return nil, nil
}
if strings.HasPrefix(value, "_") {
return nil
return nil, nil
}
dotCount := strings.Count(value, ".")
if dotCount > 1 {
return nil
return nil, nil
}

isNegative := strings.HasPrefix(value, "-")
Expand Down Expand Up @@ -589,19 +590,19 @@ func ToNumber(value string) *NumberValue {
if typ == NumberTypeFloat {
f, err := strconv.ParseFloat(text, 64)
if err != nil {
return nil
return nil, err
}
v = f
} else if isNegative {
i, err := strconv.ParseInt(text, base, 64)
if err != nil {
return nil
return nil, err
}
v = i
} else {
u, err := strconv.ParseUint(text, base, 64)
if err != nil {
return nil
return nil, err
}
v = u
}
Expand All @@ -610,7 +611,7 @@ func ToNumber(value string) *NumberValue {
Type: typ,
Value: v,
Text: text,
}
}, nil
}

// This is a subset of the formats permitted by the regular expression
Expand All @@ -635,15 +636,23 @@ func isTimestamp(value string) bool {
return false
}

// IsNeedQuoted whether need quote for passed string or not
// IsNeedQuoted checks whether the value needs quote for passed string or not
func IsNeedQuoted(value string) bool {
if value == "" {
return true
}
if _, exists := reservedEncKeywordMap[value]; exists {
return true
}
if num := ToNumber(value); num != nil {
num, err := ToNumber(value)
if err != nil {
// Need quotes even when the value is out of range
var numErr *strconv.NumError
if errors.As(err, &numErr) && errors.Is(numErr.Err, strconv.ErrRange) {
return true
}
}
if num != nil {
return true
}
first := value[0]
Expand Down Expand Up @@ -694,7 +703,7 @@ func New(value string, org string, pos *Position) *Token {
if fn != nil {
return fn(value, org, pos)
}
if num := ToNumber(value); num != nil {
if num, _ := ToNumber(value); num != nil {
tk := &Token{
Type: IntegerType,
CharacterType: CharacterTypeMiscellaneous,
Expand Down
4 changes: 4 additions & 0 deletions token/token_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ func TestIsNeedQuoted(t *testing.T) {
"",
"true",
"1.234",
"0b11111111111111111111111111111111111111111111111111111111111111111",
"0o7777777777777777777777777777777777777777",
"999999999999999999999999999999999999999999",
"0xffffffffffffffffffffffffffffffffffffffff",
"1:1",
"2001-12-15T02:59:43.1Z",
"2001-12-14t21:59:43.10-05:00",
Expand Down

0 comments on commit b5d5b31

Please sign in to comment.