Skip to main content

Module numeric

Module numeric 

Source
Expand description

The one digit-separator rule for numeric literals (§4.3).

1_000_000 is one literal, and the _s in it are punctuation the value does not contain. Two places have to agree about that: the lexer, which decides how far a numeric literal runs, and lowering, which turns its text into an i64 or an f64. If the lexer refuses a separator the decoder strips, the literal is broken up before the decoder ever sees it and the strip is unreachable.

Both halves live here so neither can drift: separator_run_len is what the lexer consumes, strip_digit_separators is what the decoder removes, and the rule is stated once.

The rule: a separator is one or more _ with a digit on each side. So 1_0, 1_000_000 and 1__0 are literals; _1 is an identifier (the lexer never reaches here — dispatch routes on the first byte) and 1_ is the literal 1 followed by the _ token. Nothing here decides where digits may appear; the caller owns that, which is why a separator is legal in a fraction and an exponent too (1_0.5_5e1_0).

Functions§

parse_int_literal
The i64 an IntLit token’s text names, or None when it names a value outside Int (§4.3).
separator_run_len
The length in bytes of the digit-separator run at at, or 0 if there is none.
strip_digit_separators
s with every digit separator removed, ready for str::parse.