kdl icon indicating copy to clipboard operation
kdl copied to clipboard

Clarify use of _ in numbers

Open tabatkins opened this issue 1 year ago • 3 comments

The description of using _ in numbers is a little loose. Experience with the feature in JS shows you really want to be very precise about what's allowed, and I suggest just following JS's lead here. Namely: _ can only be placed between digits: not at the start or end of the number, or next to the decimal point or the radix indicator; or next to another _.

Several tests showing these off:

/* invalid tests */
node _1
node 1_
node _0b10
node 0_b10
node 0b_10
node 0x_a
node 1_.0
node 1._0
node 1__2

/* valid tests */
node 1_2
node 0x1_2
node 0xa_b
node 1_2_3
node 1_2.3_4

This would change binary_trailing_underscore.kdl, trailing_underscore_hex.kdl, trailing_underscore_octal.kdl, underscore_before_number.kdl

tabatkins avatar Dec 26 '23 22:12 tabatkins

Leading underscore is an identifier:

_1
_0b10
_0o17
_0xDE

However, leading underscore in a fractional component should be invalid:

1._1

Trailing underscore should be invalid at the end of any numeric sequence:

1_
1_.0
1.0_
1_.0_

Unless it had a leading underscore to begin with (in which case it's an identifier):

_1_
_1_._0_

Repeated underscores within a numeric sequence should be invalid:

1__0

Unless it's an identifier (man this keeps coming up):

a1__0

Elixir, JS and ruby uses the same rules for their numbers (from a quick test)

Therefore the tests should be:

/* invalid tests */
node 1_
node _0b10
node 0_b10
node 0b_10
node 0x_a
node 1_.0
node 1._0
node 1.0_
node 1__2

/* valid tests - id */
node _1
node _1.223
node _1._2_2_3
node _0b10
node _0o17
node _0xDE
node _1__2
// And the real problem child
node .0

/* valid tests - numeric */
node 1_2
node 0b1111_1111
node 0o17_16
node 0x1_2
node 0xa_b
node 1_2_3
node 1_2.3_4

IceDragon200 avatar Dec 27 '23 02:12 IceDragon200

Ah yup, you're right, I wasn't actually reading the JS console error for _1 to realize it was perfectly valid as an ident, it was just complaining that it wasn't defined. ^_^

So yeah, those tests all look reasonable. But .0 being an ident, phew, I'll open a separate issue.

tabatkins avatar Dec 28 '23 01:12 tabatkins

Is this already taken care of?

zkat avatar Feb 08 '24 19:02 zkat