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

Clarify use of _ in numbers #364

Open
tabatkins opened this issue Dec 26, 2023 · 3 comments
Open

Clarify use of _ in numbers #364

tabatkins opened this issue Dec 26, 2023 · 3 comments

Comments

@tabatkins
Copy link
Contributor

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

@IceDragon200
Copy link
Contributor

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

@tabatkins
Copy link
Contributor Author

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.

@zkat
Copy link
Member

zkat commented Feb 8, 2024

Is this already taken care of?

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

No branches or pull requests

3 participants