@@ -216,9 +216,15 @@ rather than referring to it by name or some other evaluation rule. A literal is
216
216
a form of constant expression, so is evaluated (primarily) at compile time.
217
217
218
218
``` {.ebnf .gram}
219
- literal : string_lit | char_lit | byte_string_lit | byte_lit | num_lit ;
219
+ lit_suffix : ident;
220
+ literal : [ string_lit | char_lit | byte_string_lit | byte_lit | num_lit ] lit_suffix ?;
220
221
```
221
222
223
+ The optional suffix is only used for certain numeric literals, but is
224
+ reserved for future extension, that is, the above gives the lexical
225
+ grammar, but a Rust parser will reject everything but the 12 special
226
+ cases mentioned in [ Number literals] ( #number-literals ) below.
227
+
222
228
#### Character and string literals
223
229
224
230
``` {.ebnf .gram}
@@ -371,27 +377,20 @@ b"\\x52"; br"\x52"; // \x52
371
377
#### Number literals
372
378
373
379
``` {.ebnf .gram}
374
- num_lit : nonzero_dec [ dec_digit | '_' ] * num_suffix ?
375
- | '0' [ [ dec_digit | '_' ] * num_suffix ?
376
- | 'b' [ '1' | '0' | '_' ] + int_suffix ?
377
- | 'o' [ oct_digit | '_' ] + int_suffix ?
378
- | 'x' [ hex_digit | '_' ] + int_suffix ? ] ;
379
-
380
- num_suffix : int_suffix | float_suffix ;
380
+ num_lit : nonzero_dec [ dec_digit | '_' ] * float_suffix ?
381
+ | '0' [ [ dec_digit | '_' ] * float_suffix ?
382
+ | 'b' [ '1' | '0' | '_' ] +
383
+ | 'o' [ oct_digit | '_' ] +
384
+ | 'x' [ hex_digit | '_' ] + ] ;
381
385
382
- int_suffix : 'u' int_suffix_size ?
383
- | 'i' int_suffix_size ? ;
384
- int_suffix_size : [ '8' | "16" | "32" | "64" ] ;
386
+ float_suffix : [ exponent | '.' dec_lit exponent ? ] ? ;
385
387
386
- float_suffix : [ exponent | '.' dec_lit exponent ? ] ? float_suffix_ty ? ;
387
- float_suffix_ty : 'f' [ "32" | "64" ] ;
388
388
exponent : ['E' | 'e'] ['-' | '+' ] ? dec_lit ;
389
389
dec_lit : [ dec_digit | '_' ] + ;
390
390
```
391
391
392
392
A _ number literal_ is either an _ integer literal_ or a _ floating-point
393
- literal_ . The grammar for recognizing the two kinds of literals is mixed, as
394
- they are differentiated by suffixes.
393
+ literal_ . The grammar for recognizing the two kinds of literals is mixed.
395
394
396
395
##### Integer literals
397
396
@@ -406,9 +405,9 @@ An _integer literal_ has one of four forms:
406
405
* A _ binary literal_ starts with the character sequence ` U+0030 ` ` U+0062 `
407
406
(` 0b ` ) and continues as any mixture of binary digits and underscores.
408
407
409
- An integer literal may be followed (immediately, without any spaces) by an
410
- _ integer suffix _ , which changes the type of the literal. There are two kinds of
411
- integer literal suffix:
408
+ Like any literal, an integer literal may be followed (immediately,
409
+ without any spaces) by an _ integer suffix _ , which forcibly sets the
410
+ type of the literal. There are 10 valid values for an integer suffix:
412
411
413
412
* The ` i ` and ` u ` suffixes give the literal type ` int ` or ` uint ` ,
414
413
respectively.
@@ -443,11 +442,9 @@ A _floating-point literal_ has one of two forms:
443
442
* A single _ decimal literal_ followed by an _ exponent_ .
444
443
445
444
By default, a floating-point literal has a generic type, and, like integer
446
- literals, the type must be uniquely determined from the context. A
447
- floating-point literal may be followed (immediately, without any spaces) by a
448
- _ floating-point suffix_ , which changes the type of the literal. There are two
449
- floating-point suffixes: ` f32 ` , and ` f64 ` (the 32-bit and 64-bit floating point
450
- types).
445
+ literals, the type must be uniquely determined from the context. There are two valid
446
+ _ floating-point suffixes_ , ` f32 ` and ` f64 ` (the 32-bit and 64-bit floating point
447
+ types), which explicitly determine the type of the literal.
451
448
452
449
Examples of floating-point literals of various forms:
453
450
0 commit comments