Skip to content

Allow underscores _ as digit separates in float and integer literals. #3983

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

Closed
momumi opened this issue Dec 26, 2019 · 2 comments
Closed

Allow underscores _ as digit separates in float and integer literals. #3983

momumi opened this issue Dec 26, 2019 · 2 comments
Labels
proposal This issue suggests modifications. If it also has the "accepted" label then it is planned.

Comments

@momumi
Copy link
Contributor

momumi commented Dec 26, 2019

Overview

Allow the use of underscore _ in float and integer literals as a visual separator to enhance readability of long number literals. For example:

const n = 1_000_000;
const c = 299_792_458;
const pi = 3.1415_9265_3589_7932;

const x = 0xDEAD_BEEF;
const b = 0b1110_0000;

This is a common feature of several other languages and can make code much easier to read.

Modifications to grammar.

Underscores could be freely used inside number literals except:

  • _ can't be the first or last character of a number literal
  • _ can't appear next to .-+ep in float literals.

So the grammar for integer and float literals would become:

hex <- [0-9a-fA-F]
hex_ <- (_ / hex)
dec <- [0-9]
dec_ <- (_ / dec)

dec_int <- dec (dec_* dec)?
hex_int <- hex (hex_* hex)?

INTEGER
    <- "0b" [_01]* [01]  skip
     / "0o" [_0-7]* [0-7] skip
     / "0x" hex_* hex  skip
     / dec_int skip

FLOAT
    <- "0x" hex_* hex "." hex_int ([pP] [-+]? hex_it )? skip
     /      dec_int "." dec_int ([eE] [-+]? dec_int)? skip
     / "0x" hex_* hex "."? [pP] [-+]? hex_int skip
     /      dec_int "."? [eE] [-+]? dec_int skip

Examples of valid literals

1_000
1_0_0_0
1__0____0

0b___1
0b__10
0b_100

1_000.0
1e1_000
1_000e1_000

0x_abc
0xabc_def.123

Examples of invalid literals

_100
100_

0x_
0x0_
_0x0
0_x0

_._e_
1._e10
_.1e10
1.1e_

1_.0e10
1._0e10
1.0_e10
1.1e_1
1.1e-_1

1_e6
1e6_
_1e6
@emekoi
Copy link
Contributor

emekoi commented Dec 26, 2019

see #504.

@daurnimator daurnimator added proposal This issue suggests modifications. If it also has the "accepted" label then it is planned. duplicate labels Dec 26, 2019
@momumi
Copy link
Contributor Author

momumi commented Dec 26, 2019

Ah, sorry for the duplicate I'm sure I searched for that

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
proposal This issue suggests modifications. If it also has the "accepted" label then it is planned.
Projects
None yet
Development

No branches or pull requests

3 participants