Skip to content

Commit 4b358ac

Browse files
authored
Merge pull request rust-lang#108 from brauliobz/grammar_integer_literals
Add integer literal's grammar
2 parents d7253ab + 4de83de commit 4b358ac

File tree

1 file changed

+73
-4
lines changed

1 file changed

+73
-4
lines changed

src/tokens.md

+73-4
Original file line numberDiff line numberDiff line change
@@ -216,16 +216,51 @@ literal_. The grammar for recognizing the two kinds of literals is mixed.
216216

217217
#### Integer literals
218218

219+
> **<sup>Lexer</sup>**
220+
> INTEGER_LITERAL :
221+
> &nbsp;&nbsp; ( DEC_LITERAL | BIN_LITERAL | OCT_LITERAL | HEX_LITERAL )
222+
> INTEGER_SUFFIX<sup>?</sup>
223+
>
224+
> DEC_LITERAL :
225+
> &nbsp;&nbsp; DEC_DIGIT (DEC_DIGIT|`_`)<sup>\*</sup>
226+
>
227+
> BIN_LITERAL :
228+
> &nbsp;&nbsp; `0b` (BIN_DIGIT|`_`)<sup>\*</sup> BIN_DIGIT (BIN_DIGIT|`_`)<sup>\*</sup>
229+
>
230+
> OCT_LITERAL :
231+
> &nbsp;&nbsp; `0o` (OCT_DIGIT|`_`)<sup>\*</sup> OCT_DIGIT (OCT_DIGIT|`_`)<sup>\*</sup>
232+
>
233+
> HEX_LITERAL :
234+
> &nbsp;&nbsp; `0x` (HEX_DIGIT|`_`)<sup>\*</sup> HEX_DIGIT (HEX_DIGIT|`_`)<sup>\*</sup>
235+
>
236+
> BIN_DIGIT : [`0`-`1`]
237+
>
238+
> OCT_DIGIT : [`0`-`7`]
239+
>
240+
> DEC_DIGIT : [`0`-`9`]
241+
>
242+
> HEX_DIGIT : [`0`-`9` `a`-`f` `A`-`F`]
243+
>
244+
> INTEGER_SUFFIX :
245+
> &nbsp;&nbsp; &nbsp;&nbsp; `u8` | `u16` | `u32` | `u64` | `usize`
246+
> &nbsp;&nbsp; | `i8` | `u16` | `i32` | `i64` | `usize`
247+
248+
<!-- FIXME: separate the DECIMAL_LITERAL with no prefix or suffix (used on tuple indexing and float_literal -->
249+
<!-- FIXME: u128 and i128 -->
250+
219251
An _integer literal_ has one of four forms:
220252

221253
* A _decimal literal_ starts with a *decimal digit* and continues with any
222254
mixture of *decimal digits* and _underscores_.
223255
* A _hex literal_ starts with the character sequence `U+0030` `U+0078`
224-
(`0x`) and continues as any mixture of hex digits and underscores.
256+
(`0x`) and continues as any mixture (with at least one digit) of hex digits
257+
and underscores.
225258
* An _octal literal_ starts with the character sequence `U+0030` `U+006F`
226-
(`0o`) and continues as any mixture of octal digits and underscores.
259+
(`0o`) and continues as any mixture (with at least one digit) of octal digits
260+
and underscores.
227261
* A _binary literal_ starts with the character sequence `U+0030` `U+0062`
228-
(`0b`) and continues as any mixture of binary digits and underscores.
262+
(`0b`) and continues as any mixture (with at least one digit) of binary digits
263+
and underscores.
229264

230265
Like any literal, an integer literal may be followed (immediately,
231266
without any spaces) by an _integer suffix_, which forcibly sets the
@@ -247,15 +282,49 @@ The type of an _unsuffixed_ integer literal is determined by type inference:
247282
Examples of integer literals of various forms:
248283

249284
```rust
285+
123; // type i32
250286
123i32; // type i32
251287
123u32; // type u32
252288
123_u32; // type u32
289+
let a: u64 = 123; // type u64
290+
291+
0xff; // type i32
253292
0xff_u8; // type u8
293+
294+
0o70; // type i32
254295
0o70_i16; // type i16
255-
0b1111_1111_1001_0000_i32; // type i32
296+
297+
0b1111_1111_1001_0000; // type i32
298+
0b1111_1111_1001_0000i32; // type i64
299+
0b________1; // type i32
300+
256301
0usize; // type usize
257302
```
258303

304+
Examples of invalid integer literals:
305+
306+
```rust,ignore
307+
// invalid suffixes
308+
309+
0invalidSuffix;
310+
311+
// uses numbers of the wrong base
312+
313+
123AFB43;
314+
0b0102;
315+
0o0581;
316+
317+
// integers too big for their type (they overflow)
318+
319+
128_i8;
320+
256_u8;
321+
322+
// bin, hex and octal literals must have at least one digit
323+
324+
0b_;
325+
0b____;
326+
```
327+
259328
Note that the Rust syntax considers `-1i8` as an application of the [unary minus
260329
operator] to an integer literal `1i8`, rather than
261330
a single integer literal.

0 commit comments

Comments
 (0)