Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 21 additions & 9 deletions spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -309,10 +309,13 @@ has integer, floating-point, string, and bytes literals.
123 # decimal int
0x7f # hexadecimal int
0o755 # octal int
0b1011 # binary int
1_000_000 0xFFFF_FFFF # underscores may separate digits

0.0 0. .0 # float
1e10 1e+10 1e-10
1.1e10 1.1e+10 1.1e-10
1_000.000_1 # underscores may separate digits

"hello" 'hello' # string
'''hello''' """hello""" # triple-quoted string
Expand All @@ -326,23 +329,36 @@ rb'hello' br"hello" # raw bytes literal
Integer and floating-point literal tokens are defined by the following grammar:

```text
int = decimal_lit | octal_lit | hex_lit | 0 .
decimal_lit = ('1' … '9') {decimal_digit} .
octal_lit = '0' ('o' | 'O') octal_digit {octal_digit} .
hex_lit = '0' ('x' | 'X') hex_digit {hex_digit} .
int = decimal_lit | octal_lit | hex_lit | binary_lit | 0 .
decimal_lit = ('1' … '9') {['_'] decimal_digit} .
octal_lit = '0' ('o' | 'O') ['_'] octal_digit {['_'] octal_digit} .
hex_lit = '0' ('x' | 'X') ['_'] hex_digit {['_'] hex_digit} .
binary_lit = '0' ('b' | 'B') ['_'] binary_digit {['_'] binary_digit} .

float = decimals '.' [decimals] [exponent]
| decimals exponent
| '.' decimals [exponent]
.
decimals = decimal_digit {decimal_digit} .
decimals = decimal_digit {['_'] decimal_digit} .
exponent = ('e'|'E') ['+'|'-'] decimals .

decimal_digit = '0' … '9' .
octal_digit = '0' … '7' .
hex_digit = '0' … '9' | 'A' … 'F' | 'a' … 'f' .
binary_digit = '0' | '1' .
```

As in Python ([PEP 515](https://peps.python.org/pep-0515/)), a single
underscore (`_`) may appear between two digits of a number literal, or
between the base prefix and the first digit. Underscores serve only to
group digits for readability and have no effect on the value of the
literal.

It is a static error if the digits of a number literal are immediately

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would delete this second paragraph. I believe in all cases it is already a static error regardless of this text, so all the paragraph is doing is specifying that it's a lexical error rather than a parsing one, which is really an implementation detail.

followed by a decimal digit that is not valid in the literal's base:
for example, `0o12345678` is an error rather than the two tokens
`0o1234567` and `8`.

It is a static error if a floating-point literal denotes a value whose
magnitude is too large to be represented as a finite `float` value.

Expand Down Expand Up @@ -3419,10 +3435,6 @@ specified base, decimal by default.
If `base` is zero, x is interpreted like an integer literal,
the base being inferred from an optional base prefix such as
`0b`, `0o`, or `0x` preceding the first digit.
<!-- Note: the spec does not currently support 0b literals, but
int("0b...") works in both implementation;
see bazelbuild/starlark#117.
-->

When a nonzero `base` is provided explicitly,
its value must be between 2 and 36.
Expand Down