diff options
| author | bors <bors@rust-lang.org> | 2022-09-28 08:14:04 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2022-09-28 08:14:04 +0000 |
| commit | 6201eabde85db854c1ebb57624be5ec699246b50 (patch) | |
| tree | 41fd8394b4f257b7829380704e9496f3f1ba3077 /src | |
| parent | 837bf370de144a682041e68bb67469b9f68a55ce (diff) | |
| parent | d0a26acb2ae2d000e516eca92ae8feb08d1f6ea0 (diff) | |
| download | rust-6201eabde85db854c1ebb57624be5ec699246b50.tar.gz rust-6201eabde85db854c1ebb57624be5ec699246b50.zip | |
Auto merge of #102302 - nnethercote:more-lexer-improvements, r=matklad
More lexer improvements A follow-up to #99884. r? `@matklad`
Diffstat (limited to 'src')
| -rw-r--r-- | src/librustdoc/html/highlight.rs | 9 |
1 files changed, 6 insertions, 3 deletions
diff --git a/src/librustdoc/html/highlight.rs b/src/librustdoc/html/highlight.rs index 8922bf37785..78b98431b19 100644 --- a/src/librustdoc/html/highlight.rs +++ b/src/librustdoc/html/highlight.rs @@ -13,6 +13,7 @@ use std::collections::VecDeque; use std::fmt::{Display, Write}; use rustc_data_structures::fx::FxHashMap; +use rustc_lexer::Cursor; use rustc_lexer::{LiteralKind, TokenKind}; use rustc_span::edition::Edition; use rustc_span::symbol::Symbol; @@ -408,15 +409,16 @@ enum Highlight<'a> { struct TokenIter<'a> { src: &'a str, + cursor: Cursor<'a>, } impl<'a> Iterator for TokenIter<'a> { type Item = (TokenKind, &'a str); fn next(&mut self) -> Option<(TokenKind, &'a str)> { - if self.src.is_empty() { + let token = self.cursor.advance_token(); + if token.kind == TokenKind::Eof { return None; } - let token = rustc_lexer::first_token(self.src); let (text, rest) = self.src.split_at(token.len as usize); self.src = rest; Some((token.kind, text)) @@ -525,7 +527,7 @@ impl<'a> Classifier<'a> { /// Takes as argument the source code to HTML-ify, the rust edition to use and the source code /// file span which will be used later on by the `span_correspondance_map`. fn new(src: &str, file_span: Span, decoration_info: Option<DecorationInfo>) -> Classifier<'_> { - let tokens = PeekIter::new(TokenIter { src }); + let tokens = PeekIter::new(TokenIter { src, cursor: Cursor::new(src) }); let decorations = decoration_info.map(Decorations::new); Classifier { tokens, @@ -850,6 +852,7 @@ impl<'a> Classifier<'a> { Class::Ident(self.new_span(before, text)) } TokenKind::Lifetime { .. } => Class::Lifetime, + TokenKind::Eof => panic!("Eof in advance"), }; // Anything that didn't return above is the simple case where we the // class just spans a single token, so we can use the `string` method. |
