11use rustc_ast:: ast:: { LitFloatType , LitIntType , LitKind } ;
22use std:: iter;
33
4+ /// Represents the base of a numeric literal, used for parsing and formatting.
45#[ derive( Debug , PartialEq , Eq , Copy , Clone ) ]
56pub enum Radix {
7+ /// A binary literal (e.g., `0b1010`)
68 Binary ,
9+ /// An octal literal (e.g., `0o670`)
710 Octal ,
11+ /// A decimal literal (e.g., `123`)
812 Decimal ,
13+ /// A hexadecimal literal (e.g., `0xFF`)
914 Hexadecimal ,
1015}
1116
@@ -46,6 +51,7 @@ pub struct NumericLiteral<'a> {
4651}
4752
4853impl < ' a > NumericLiteral < ' a > {
54+ /// Attempts to parse a `NumericLiteral` from the source string of an `ast::LitKind`.
4955 pub fn from_lit_kind ( src : & ' a str , lit_kind : & LitKind ) -> Option < NumericLiteral < ' a > > {
5056 let unsigned_src = src. strip_prefix ( '-' ) . map_or ( src, |s| s) ;
5157 if lit_kind. is_numeric ( )
@@ -63,6 +69,7 @@ impl<'a> NumericLiteral<'a> {
6369 }
6470 }
6571
72+ /// Parses a raw numeric literal string into its structured `NumericLiteral` parts.
6673 #[ must_use]
6774 pub fn new ( lit : & ' a str , suffix : Option < & ' a str > , float : bool ) -> Self {
6875 let unsigned_lit = lit. trim_start_matches ( '-' ) ;
@@ -102,11 +109,12 @@ impl<'a> NumericLiteral<'a> {
102109 }
103110 }
104111
112+ /// Checks if the literal's radix is `Radix::Decimal`
105113 pub fn is_decimal ( & self ) -> bool {
106114 self . radix == Radix :: Decimal
107115 }
108116
109- pub fn split_digit_parts ( digits : & str , float : bool ) -> ( & str , Option < & str > , Option < ( & str , & str ) > ) {
117+ fn split_digit_parts ( digits : & str , float : bool ) -> ( & str , Option < & str > , Option < ( & str , & str ) > ) {
110118 let mut integer = digits;
111119 let mut fraction = None ;
112120 let mut exponent = None ;
@@ -180,7 +188,7 @@ impl<'a> NumericLiteral<'a> {
180188 output
181189 }
182190
183- pub fn group_digits ( output : & mut String , input : & str , group_size : usize , partial_group_first : bool , pad : bool ) {
191+ fn group_digits ( output : & mut String , input : & str , group_size : usize , partial_group_first : bool , zero_pad : bool ) {
184192 debug_assert ! ( group_size > 0 ) ;
185193
186194 let mut digits = input. chars ( ) . filter ( |& c| c != '_' ) ;
@@ -196,7 +204,7 @@ impl<'a> NumericLiteral<'a> {
196204
197205 if partial_group_first {
198206 first_group_size = ( digits. clone ( ) . count ( ) - 1 ) % group_size + 1 ;
199- if pad {
207+ if zero_pad {
200208 for _ in 0 ..group_size - first_group_size {
201209 output. push ( '0' ) ;
202210 }
0 commit comments