aboutsummaryrefslogtreecommitdiffstats
path: root/asspb.go
diff options
context:
space:
mode:
authorRose Hogenson <rosehogenson@posteo.net>2025-10-26 08:31:12 -0700
committerRose Hogenson <rosehogenson@posteo.net>2025-10-26 08:31:12 -0700
commit121b6d8e6f7a07cd119def27aaddc9e1e83dae1d (patch)
treeca0e2261532e3c6985724ae8bc9164615f4b8a3c /asspb.go
parent2089fa738b2299d864b614567e97d2bc57556fb4 (diff)
downloadccl-121b6d8e6f7a07cd119def27aaddc9e1e83dae1d.tar.zst
Refine numeric literal syntax
Leading zeros are no longer permitted in decimal literals, which is technically a backwards-incompatible change, but I consider it to be something like a bug fix. Anyway, the project is unlikely to have any users that would be impacted.
Diffstat (limited to 'asspb.go')
-rw-r--r--asspb.go15
1 files changed, 9 insertions, 6 deletions
diff --git a/asspb.go b/asspb.go
index f8036f8..624f3ca 100644
--- a/asspb.go
+++ b/asspb.go
@@ -18,8 +18,8 @@
// # Numbers
//
// Numbers are written in base 10 and can optionally have a fractional part or
-// an exponent written with "e". As a special case, a number prefixed with "0x"
-// can be written in base 16.
+// an exponent written with "e" or "E". As a special case, a number prefixed
+// with "0x" or "0X" can be written in base 16.
//
// 100
// -30
@@ -28,6 +28,9 @@
// 13.5
// 1e100
//
+// Leading zeros are not permitted in decimal numbers, due to potential
+// confusion with octal (which is not supported).
+//
// # Strings
//
// Strings are written with " or ' and any sequence of intermediate bytes (with
@@ -189,7 +192,7 @@ func (p *parser) parseLit(s string) bool {
return false
}
-var numRE = regexp.MustCompile(`^-?(0x[0-9a-fA-F]+|([0-9]+(\.[0-9]*)?|\.[0-9]+)(e-?[0-9]+)?)`)
+var numRE = regexp.MustCompile(`^[-+]?(0[xX][0-9a-fA-F]+|((0|[1-9][0-9]*)(\.[0-9]*)?|\.[0-9]+)([eE][-+]?[0-9]+)?)`)
func (p *parser) parseNum() (any, bool) {
p.skipSpace()
@@ -197,7 +200,7 @@ func (p *parser) parseNum() (any, bool) {
if numBytes == nil {
return nil, false
}
- if bytes.ContainsAny(numBytes, ".e") {
+ if bytes.ContainsAny(numBytes, ".eE") {
n, err := strconv.ParseFloat(string(numBytes), 64)
if err != nil {
return nil, false
@@ -205,8 +208,8 @@ func (p *parser) parseNum() (any, bool) {
p.i += len(numBytes)
return n, true
}
- if b, ok := bytes.CutPrefix(numBytes, []byte("0x")); ok {
- n, err := strconv.ParseInt(string(b), 16, 64)
+ if bytes.HasPrefix(numBytes, []byte("0x")) || bytes.HasPrefix(numBytes, []byte("0X")) {
+ n, err := strconv.ParseInt(string(numBytes[2:]), 16, 64)
if err != nil {
return nil, false
}