Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

token: escape number with exponent without dot #357

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
8 changes: 4 additions & 4 deletions token/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -519,14 +519,14 @@ func getNumberStat(str string) *numStat {
// binary number
continue
}
if (c == 'e' || c == 'E') && dotFound {
if (c == 'e' || c == 'E') && !isExponent && ((isNegative && idx > 2) || (!isNegative && idx > 1)) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

diff --git a/token/token.go b/token/token.go
index 4943023..f7da0b7 100644
--- a/token/token.go
+++ b/token/token.go
@@ -2,6 +2,7 @@ package token

 import (
        "fmt"
+       "strconv"
        "strings"
 )

@@ -525,9 +526,9 @@ func getNumberStat(str string) *numStat {
        if str[0] == '_' {
                return stat
        }
-       dotFound := false
+       dotCount := 0
+       exponentCount := 0
        isNegative := false
-       isExponent := false
        if str[0] == '-' {
                isNegative = true
        }
@@ -553,24 +554,28 @@ func getNumberStat(str string) *numStat {
                                // binary number
                                continue
                        }
-                       if (c == 'e' || c == 'E') && dotFound {
+                       if exponentCount > 0 {
+                               // multiple exponent
+                               return stat
+                       }
+                       if c == 'e' || c == 'E' {
                                // exponent
-                               isExponent = true
+                               exponentCount++
                                continue
                        }
                case '.':
-                       if dotFound {
+                       if dotCount > 0 {
                                // multiple dot
                                return stat
                        }
-                       dotFound = true
+                       dotCount++
                        continue
                case '-':
-                       if idx == 0 || isExponent {
+                       if idx == 0 || exponentCount == 1 {
                                continue
                        }
                case '+':
-                       if idx == 0 || isExponent {
+                       if idx == 0 || exponentCount == 1 {
                                continue
                        }
                case '_':
@@ -578,9 +583,14 @@ func getNumberStat(str string) *numStat {
                }
                return stat
        }
+       if dotCount > 0 || exponentCount > 0 {
+               if _, err := strconv.ParseFloat(str, 64); err != nil {
+                       return stat
+               }
+       }
        stat.isNum = true
        switch {
-       case dotFound:
+       case dotCount > 0:
                stat.typ = numTypeFloat
        case strings.HasPrefix(str, "0b") || strings.HasPrefix(str, "-0b"):
                stat.typ = numTypeBinary

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@martin-sucha Do you think about my changes ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@goccy if it passes the tests, I guess it could work 🤷 Maybe we could replace the parser in getNumberStat with regular expression to make it more clear, but I don't see regular expressions used anywhere else in the codebase.

// exponent
isExponent = true
continue
}
case '.':
if dotFound {
// multiple dot
if dotFound || isExponent {
// multiple dots or dot used in exponent
return stat
}
dotFound = true
Expand All @@ -546,7 +546,7 @@ func getNumberStat(str string) *numStat {
}
stat.isNum = true
switch {
case dotFound:
case dotFound || isExponent:
stat.typ = numTypeFloat
case strings.HasPrefix(str, "0b") || strings.HasPrefix(str, "-0b"):
stat.typ = numTypeBinary
Expand Down
8 changes: 8 additions & 0 deletions token/token_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,10 @@ func TestIsNeedQuoted(t *testing.T) {
needQuotedTests := []string{
"",
"true",
"1",
"1.234",
"44987e08",
"-44987e08",
"1:1",
"hoge # comment",
"\\0",
Expand Down Expand Up @@ -109,6 +112,11 @@ func TestIsNeedQuoted(t *testing.T) {
}
notNeedQuotedTests := []string{
"Hello World",
"e",
"ee",
"eee",
"ae34",
"123e4.5",
}
for i, test := range notNeedQuotedTests {
if token.IsNeedQuoted(test) {
Expand Down