@@ -59,13 +59,17 @@ pub enum Delimiter {
5959 Invisible ,
6060}
6161
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.
6266#[ derive( Clone , Copy , PartialEq , Encodable , Decodable , Debug , HashStable_Generic ) ]
6367pub enum LitKind {
6468 Bool , // AST only, must never appear in a `Token`
6569 Byte ,
6670 Char ,
67- Integer ,
68- Float ,
71+ Integer , // e.g. `1`, `1u8`, `1f32`
72+ Float , // e.g. `1.`, `1.0`, `1e3f32`
6973 Str ,
7074 StrRaw ( u8 ) , // raw string delimited by `n` hash symbols
7175 ByteStr ,
@@ -81,6 +85,42 @@ pub struct Lit {
8185 pub suffix : Option < Symbol > ,
8286}
8387
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+
84124impl fmt:: Display for Lit {
85125 fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
86126 let Lit { kind, symbol, suffix } = * self ;
@@ -139,12 +179,6 @@ impl LitKind {
139179 }
140180}
141181
142- impl Lit {
143- pub fn new ( kind : LitKind , symbol : Symbol , suffix : Option < Symbol > ) -> Lit {
144- Lit { kind, symbol, suffix }
145- }
146- }
147-
148182pub fn ident_can_begin_expr ( name : Symbol , span : Span , is_raw : bool ) -> bool {
149183 let ident_token = Token :: new ( Ident ( name, is_raw) , span) ;
150184
0 commit comments