@@ -59,13 +59,17 @@ pub enum Delimiter {
59
59
Invisible ,
60
60
}
61
61
62
+ // Note that the suffix is *not* considered when deciding the `LitKind` in this
63
+ // type. This means that float literals like `1f32` are classified by this type
64
+ // as `Int`. Only upon conversion to `ast::LitKind` will such a literal be
65
+ // given the `Float` kind.
62
66
#[ derive( Clone , Copy , PartialEq , Encodable , Decodable , Debug , HashStable_Generic ) ]
63
67
pub enum LitKind {
64
68
Bool , // AST only, must never appear in a `Token`
65
69
Byte ,
66
70
Char ,
67
- Integer ,
68
- Float ,
71
+ Integer , // e.g. `1`, `1u8`, `1f32`
72
+ Float , // e.g. `1.`, `1.0`, `1e3f32`
69
73
Str ,
70
74
StrRaw ( u8 ) , // raw string delimited by `n` hash symbols
71
75
ByteStr ,
@@ -81,6 +85,42 @@ pub struct Lit {
81
85
pub suffix : Option < Symbol > ,
82
86
}
83
87
88
+ impl Lit {
89
+ pub fn new ( kind : LitKind , symbol : Symbol , suffix : Option < Symbol > ) -> Lit {
90
+ Lit { kind, symbol, suffix }
91
+ }
92
+
93
+ /// Returns `true` if this is semantically a float literal. This includes
94
+ /// ones like `1f32` that have an `Integer` kind but a float suffix.
95
+ pub fn is_semantic_float ( & self ) -> bool {
96
+ match self . kind {
97
+ LitKind :: Float => true ,
98
+ LitKind :: Integer => match self . suffix {
99
+ Some ( sym) => sym == sym:: f32 || sym == sym:: f64,
100
+ None => false ,
101
+ } ,
102
+ _ => false ,
103
+ }
104
+ }
105
+
106
+ /// Keep this in sync with `Token::can_begin_literal_or_bool` excluding unary negation.
107
+ pub fn from_token ( token : & Token ) -> Option < Lit > {
108
+ match token. uninterpolate ( ) . kind {
109
+ Ident ( name, false ) if name. is_bool_lit ( ) => {
110
+ Some ( Lit :: new ( Bool , name, None ) )
111
+ }
112
+ Literal ( token_lit) => Some ( token_lit) ,
113
+ Interpolated ( ref nt)
114
+ if let NtExpr ( expr) | NtLiteral ( expr) = & * * nt
115
+ && let ast:: ExprKind :: Lit ( token_lit) = expr. kind =>
116
+ {
117
+ Some ( token_lit. clone ( ) )
118
+ }
119
+ _ => None ,
120
+ }
121
+ }
122
+ }
123
+
84
124
impl fmt:: Display for Lit {
85
125
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
86
126
let Lit { kind, symbol, suffix } = * self ;
@@ -139,12 +179,6 @@ impl LitKind {
139
179
}
140
180
}
141
181
142
- impl Lit {
143
- pub fn new ( kind : LitKind , symbol : Symbol , suffix : Option < Symbol > ) -> Lit {
144
- Lit { kind, symbol, suffix }
145
- }
146
- }
147
-
148
182
pub fn ident_can_begin_expr ( name : Symbol , span : Span , is_raw : bool ) -> bool {
149
183
let ident_token = Token :: new ( Ident ( name, is_raw) , span) ;
150
184
0 commit comments