@@ -754,22 +754,6 @@ impl<'a> Iterator for TokenIter<'a> {
754754 }
755755}
756756
757- /// Classifies into identifier class; returns `None` if this is a non-keyword identifier.
758- fn get_real_ident_class ( text : & str , allow_path_keywords : bool ) -> Option < Class > {
759- let ignore: & [ & str ] =
760- if allow_path_keywords { & [ "self" , "Self" , "super" , "crate" ] } else { & [ "self" , "Self" ] } ;
761- if ignore. contains ( & text) {
762- return None ;
763- }
764- Some ( match text {
765- "ref" | "mut" => Class :: RefKeyWord ,
766- "false" | "true" => Class :: Bool ,
767- // FIXME(#148221): Don't hard-code the edition. The classifier should take it as an argument.
768- _ if Symbol :: intern ( text) . is_reserved ( || Edition :: Edition2024 ) => Class :: KeyWord ,
769- _ => return None ,
770- } )
771- }
772-
773757/// This iterator comes from the same idea than "Peekable" except that it allows to "peek" more than
774758/// just the next item by using `peek_next`. The `peek` method always returns the next item after
775759/// the current one whereas `peek_next` will return the next item after the last one peeked.
@@ -787,16 +771,16 @@ impl<'a> PeekIter<'a> {
787771 Self { stored : VecDeque :: new ( ) , peek_pos : 0 , iter }
788772 }
789773 /// Returns the next item after the current one. It doesn't interfere with `peek_next` output.
790- fn peek ( & mut self ) -> Option < & ( TokenKind , & ' a str ) > {
774+ fn peek ( & mut self ) -> Option < ( TokenKind , & ' a str ) > {
791775 if self . stored . is_empty ( )
792776 && let Some ( next) = self . iter . next ( )
793777 {
794778 self . stored . push_back ( next) ;
795779 }
796- self . stored . front ( )
780+ self . stored . front ( ) . copied ( )
797781 }
798782 /// Returns the next item after the last one peeked. It doesn't interfere with `peek` output.
799- fn peek_next ( & mut self ) -> Option < & ( TokenKind , & ' a str ) > {
783+ fn peek_next ( & mut self ) -> Option < ( TokenKind , & ' a str ) > {
800784 self . peek_pos += 1 ;
801785 if self . peek_pos - 1 < self . stored . len ( ) {
802786 self . stored . get ( self . peek_pos - 1 )
@@ -806,6 +790,7 @@ impl<'a> PeekIter<'a> {
806790 } else {
807791 None
808792 }
793+ . copied ( )
809794 }
810795
811796 fn stop_peeking ( & mut self ) {
@@ -956,15 +941,9 @@ impl<'src> Classifier<'src> {
956941 }
957942 }
958943
959- if let Some ( ( None , text) ) = self . tokens . peek ( ) . map ( |( token, text) | {
960- if * token == TokenKind :: Ident {
961- let class = get_real_ident_class ( text, true ) ;
962- ( class, text)
963- } else {
964- // Doesn't matter which Class we put in here...
965- ( Some ( Class :: Comment ) , text)
966- }
967- } ) {
944+ if let Some ( ( TokenKind :: Ident , text) ) = self . tokens . peek ( )
945+ && !ident_is_keyword ( text)
946+ {
968947 // We only "add" the colon if there is an ident behind.
969948 pos += text. len ( ) + nb;
970949 has_ident = true ;
@@ -1210,22 +1189,7 @@ impl<'src> Classifier<'src> {
12101189 sink ( span, Highlight :: Token { text, class : None } ) ;
12111190 return ;
12121191 }
1213- TokenKind :: Ident => match get_real_ident_class ( text, false ) {
1214- None => match text {
1215- "Option" | "Result" => Class :: PreludeTy ( new_span ( before, text, file_span) ) ,
1216- "Some" | "None" | "Ok" | "Err" => {
1217- Class :: PreludeVal ( new_span ( before, text, file_span) )
1218- }
1219- _ if self . ident_is_weak_keyword ( text) => Class :: KeyWord ,
1220- _ if self . in_macro_nonterminal => {
1221- self . in_macro_nonterminal = false ;
1222- Class :: MacroNonTerminal
1223- }
1224- "self" | "Self" => Class :: Self_ ( new_span ( before, text, file_span) ) ,
1225- _ => Class :: Ident ( new_span ( before, text, file_span) ) ,
1226- } ,
1227- Some ( c) => c,
1228- } ,
1192+ TokenKind :: Ident => self . classify_ident ( before, text) ,
12291193 TokenKind :: RawIdent | TokenKind :: UnknownPrefix | TokenKind :: InvalidIdent => {
12301194 Class :: Ident ( new_span ( before, text, file_span) )
12311195 }
@@ -1246,6 +1210,27 @@ impl<'src> Classifier<'src> {
12461210 }
12471211 }
12481212
1213+ fn classify_ident ( & mut self , before : u32 , text : & ' src str ) -> Class {
1214+ // Macro non-terminals (meta vars) take precedence.
1215+ if self . in_macro_nonterminal {
1216+ self . in_macro_nonterminal = false ;
1217+ return Class :: MacroNonTerminal ;
1218+ }
1219+
1220+ let file_span = self . file_span ;
1221+ let span = || new_span ( before, text, file_span) ;
1222+
1223+ match text {
1224+ "ref" | "mut" => Class :: RefKeyWord ,
1225+ "false" | "true" => Class :: Bool ,
1226+ "self" | "Self" => Class :: Self_ ( span ( ) ) ,
1227+ "Option" | "Result" => Class :: PreludeTy ( span ( ) ) ,
1228+ "Some" | "None" | "Ok" | "Err" => Class :: PreludeVal ( span ( ) ) ,
1229+ _ if ident_is_keyword ( text) || self . ident_is_weak_keyword ( text) => Class :: KeyWord ,
1230+ _ => Class :: Ident ( span ( ) ) ,
1231+ }
1232+ }
1233+
12491234 fn ident_is_weak_keyword ( & mut self , text : & str ) -> bool {
12501235 let matches = match text {
12511236 "auto" => |text| text == "trait" , // `auto trait Trait {}` (`auto_traits`)
@@ -1259,11 +1244,11 @@ impl<'src> Classifier<'src> {
12591244 }
12601245
12611246 fn peek ( & mut self ) -> Option < TokenKind > {
1262- self . tokens . peek ( ) . map ( |& ( kind, _) | kind)
1247+ self . tokens . peek ( ) . map ( |( kind, _) | kind)
12631248 }
12641249
12651250 fn peek_non_trivia ( & mut self ) -> Option < ( TokenKind , & str ) > {
1266- while let Some ( & token @ ( kind, _) ) = self . tokens . peek_next ( ) {
1251+ while let Some ( token @ ( kind, _) ) = self . tokens . peek_next ( ) {
12671252 if let TokenKind :: Whitespace
12681253 | TokenKind :: LineComment { doc_style : None }
12691254 | TokenKind :: BlockComment { doc_style : None , .. } = kind
@@ -1278,6 +1263,11 @@ impl<'src> Classifier<'src> {
12781263 }
12791264}
12801265
1266+ fn ident_is_keyword ( text : & str ) -> bool {
1267+ // FIXME(#148221): Don't hard-code the edition. The classifier should take it as an argument.
1268+ Symbol :: intern ( text) . is_reserved ( || Edition :: Edition2024 )
1269+ }
1270+
12811271fn generate_link_to_def (
12821272 out : & mut impl Write ,
12831273 text_s : & str ,
0 commit comments