Skip to content

Commit 830ff4a

Browse files
committed
remove StringReader::peek
The reader itself doesn't need ability to peek tokens, so it's better if clients implement this functionality. This hopefully becomes especially easy once we use iterator interface for lexer, but this is not too easy at the moment, because of buffered errors.
1 parent b43eb42 commit 830ff4a

File tree

2 files changed

+20
-13
lines changed

2 files changed

+20
-13
lines changed

src/librustdoc/html/highlight.rs

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ pub fn render_with_highlighting(
7979
/// each span of text in sequence.
8080
struct Classifier<'a> {
8181
lexer: lexer::StringReader<'a>,
82+
peek_token: Option<Token>,
8283
source_map: &'a SourceMap,
8384

8485
// State of the classifier.
@@ -178,6 +179,7 @@ impl<'a> Classifier<'a> {
178179
fn new(lexer: lexer::StringReader<'a>, source_map: &'a SourceMap) -> Classifier<'a> {
179180
Classifier {
180181
lexer,
182+
peek_token: None,
181183
source_map,
182184
in_attribute: false,
183185
in_macro: false,
@@ -187,10 +189,19 @@ impl<'a> Classifier<'a> {
187189

188190
/// Gets the next token out of the lexer.
189191
fn try_next_token(&mut self) -> Result<Token, HighlightError> {
190-
match self.lexer.try_next_token() {
191-
Ok(token) => Ok(token),
192-
Err(_) => Err(HighlightError::LexError),
192+
if let Some(token) = self.peek_token.take() {
193+
return Ok(token);
193194
}
195+
self.lexer.try_next_token().map_err(|()| HighlightError::LexError)
196+
}
197+
198+
fn peek(&mut self) -> Result<&Token, HighlightError> {
199+
if self.peek_token.is_none() {
200+
self.peek_token = Some(
201+
self.lexer.try_next_token().map_err(|()| HighlightError::LexError)?
202+
);
203+
}
204+
Ok(self.peek_token.as_ref().unwrap())
194205
}
195206

196207
/// Exhausts the `lexer` writing the output into `out`.
@@ -234,7 +245,7 @@ impl<'a> Classifier<'a> {
234245
// reference or dereference operator or a reference or pointer type, instead of the
235246
// bit-and or multiplication operator.
236247
token::BinOp(token::And) | token::BinOp(token::Star)
237-
if self.lexer.peek() != &token::Whitespace => Class::RefKeyWord,
248+
if self.peek()? != &token::Whitespace => Class::RefKeyWord,
238249

239250
// Consider this as part of a macro invocation if there was a
240251
// leading identifier.
@@ -257,7 +268,7 @@ impl<'a> Classifier<'a> {
257268
token::Question => Class::QuestionMark,
258269

259270
token::Dollar => {
260-
if self.lexer.peek().is_ident() {
271+
if self.peek()?.is_ident() {
261272
self.in_macro_nonterminal = true;
262273
Class::MacroNonTerminal
263274
} else {
@@ -280,9 +291,9 @@ impl<'a> Classifier<'a> {
280291
// as an attribute.
281292

282293
// Case 1: #![inner_attribute]
283-
if self.lexer.peek() == &token::Not {
294+
if self.peek()? == &token::Not {
284295
self.try_next_token()?; // NOTE: consumes `!` token!
285-
if self.lexer.peek() == &token::OpenDelim(token::Bracket) {
296+
if self.peek()? == &token::OpenDelim(token::Bracket) {
286297
self.in_attribute = true;
287298
out.enter_span(Class::Attribute)?;
288299
}
@@ -292,7 +303,7 @@ impl<'a> Classifier<'a> {
292303
}
293304

294305
// Case 2: #[outer_attribute]
295-
if self.lexer.peek() == &token::OpenDelim(token::Bracket) {
306+
if self.peek()? == &token::OpenDelim(token::Bracket) {
296307
self.in_attribute = true;
297308
out.enter_span(Class::Attribute)?;
298309
}
@@ -341,7 +352,7 @@ impl<'a> Classifier<'a> {
341352
if self.in_macro_nonterminal {
342353
self.in_macro_nonterminal = false;
343354
Class::MacroNonTerminal
344-
} else if self.lexer.peek() == &token::Not {
355+
} else if self.peek()? == &token::Not {
345356
self.in_macro = true;
346357
Class::Macro
347358
} else {

src/libsyntax/parse/lexer/mod.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,10 +142,6 @@ impl<'a> StringReader<'a> {
142142
buffer
143143
}
144144

145-
pub fn peek(&self) -> &Token {
146-
&self.peek_token
147-
}
148-
149145
/// For comments.rs, which hackily pokes into next_pos and ch
150146
fn new_raw(sess: &'a ParseSess,
151147
source_file: Lrc<syntax_pos::SourceFile>,

0 commit comments

Comments
 (0)