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

Conversation

martin-sucha
Copy link
Contributor

A string like "44987e08" needs to be escaped, otherwise it would be parsed as float with exponent.

A string like "44987e08" needs to be escaped, otherwise it would be
parsed as float with exponent.
@goccy
Copy link
Owner

goccy commented Mar 19, 2023

Thank you for your contribution. I've also tried to think of a way to handle a wider variety of cases. Please refer the implementation.

@goccy goccy added the reviewed label Mar 19, 2023
@@ -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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants