Skip to content

Commit c0c57ac

Browse files
committed
syntax: Use Token in StringReader and TokenTreesReader
1 parent e0127db commit c0c57ac

File tree

3 files changed

+37
-51
lines changed

3 files changed

+37
-51
lines changed

src/librustdoc/html/highlight.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ impl<'a> Classifier<'a> {
234234
// reference or dereference operator or a reference or pointer type, instead of the
235235
// bit-and or multiplication operator.
236236
token::BinOp(token::And) | token::BinOp(token::Star)
237-
if self.lexer.peek() != token::Whitespace => Class::RefKeyWord,
237+
if self.lexer.peek() != &token::Whitespace => Class::RefKeyWord,
238238

239239
// Consider this as part of a macro invocation if there was a
240240
// leading identifier.
@@ -280,9 +280,9 @@ impl<'a> Classifier<'a> {
280280
// as an attribute.
281281

282282
// Case 1: #![inner_attribute]
283-
if self.lexer.peek() == token::Not {
283+
if self.lexer.peek() == &token::Not {
284284
self.try_next_token()?; // NOTE: consumes `!` token!
285-
if self.lexer.peek() == token::OpenDelim(token::Bracket) {
285+
if self.lexer.peek() == &token::OpenDelim(token::Bracket) {
286286
self.in_attribute = true;
287287
out.enter_span(Class::Attribute)?;
288288
}
@@ -292,7 +292,7 @@ impl<'a> Classifier<'a> {
292292
}
293293

294294
// Case 2: #[outer_attribute]
295-
if self.lexer.peek() == token::OpenDelim(token::Bracket) {
295+
if self.lexer.peek() == &token::OpenDelim(token::Bracket) {
296296
self.in_attribute = true;
297297
out.enter_span(Class::Attribute)?;
298298
}
@@ -341,7 +341,7 @@ impl<'a> Classifier<'a> {
341341
if self.in_macro_nonterminal {
342342
self.in_macro_nonterminal = false;
343343
Class::MacroNonTerminal
344-
} else if self.lexer.peek() == token::Not {
344+
} else if self.lexer.peek() == &token::Not {
345345
self.in_macro = true;
346346
Class::Macro
347347
} else {

src/libsyntax/parse/lexer/mod.rs

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ use core::unicode::property::Pattern_White_Space;
1212
use std::borrow::Cow;
1313
use std::char;
1414
use std::iter;
15-
use std::mem::replace;
1615
use rustc_data_structures::sync::Lrc;
1716
use log::debug;
1817

@@ -41,8 +40,7 @@ pub struct StringReader<'a> {
4140
/// Stop reading src at this index.
4241
crate end_src_index: usize,
4342
// cached:
44-
peek_tok: TokenKind,
45-
peek_span: Span,
43+
peek_token: Token,
4644
peek_span_src_raw: Span,
4745
fatal_errs: Vec<DiagnosticBuilder<'a>>,
4846
// cache a direct reference to the source text, so that we don't have to
@@ -90,10 +88,7 @@ impl<'a> StringReader<'a> {
9088
/// Returns the next token. EFFECT: advances the string_reader.
9189
pub fn try_next_token(&mut self) -> Result<Token, ()> {
9290
assert!(self.fatal_errs.is_empty());
93-
let ret_val = Token {
94-
kind: replace(&mut self.peek_tok, token::Whitespace),
95-
span: self.peek_span,
96-
};
91+
let ret_val = self.peek_token.clone();
9792
self.advance_token()?;
9893
Ok(ret_val)
9994
}
@@ -158,7 +153,7 @@ impl<'a> StringReader<'a> {
158153
}
159154

160155
fn fatal(&self, m: &str) -> FatalError {
161-
self.fatal_span(self.peek_span, m)
156+
self.fatal_span(self.peek_token.span, m)
162157
}
163158

164159
crate fn emit_fatal_errors(&mut self) {
@@ -179,12 +174,8 @@ impl<'a> StringReader<'a> {
179174
buffer
180175
}
181176

182-
pub fn peek(&self) -> Token {
183-
// FIXME(pcwalton): Bad copy!
184-
Token {
185-
kind: self.peek_tok.clone(),
186-
span: self.peek_span,
187-
}
177+
pub fn peek(&self) -> &Token {
178+
&self.peek_token
188179
}
189180

190181
/// For comments.rs, which hackily pokes into next_pos and ch
@@ -215,8 +206,7 @@ impl<'a> StringReader<'a> {
215206
source_file,
216207
end_src_index: src.len(),
217208
// dummy values; not read
218-
peek_tok: token::Eof,
219-
peek_span: syntax_pos::DUMMY_SP,
209+
peek_token: Token { kind: token::Eof, span: syntax_pos::DUMMY_SP },
220210
peek_span_src_raw: syntax_pos::DUMMY_SP,
221211
src,
222212
fatal_errs: Vec::new(),
@@ -321,29 +311,28 @@ impl<'a> StringReader<'a> {
321311
self.err_span_(from_pos, to_pos, &m[..]);
322312
}
323313

324-
/// Advance peek_tok and peek_span to refer to the next token, and
314+
/// Advance peek_token to refer to the next token, and
325315
/// possibly update the interner.
326316
fn advance_token(&mut self) -> Result<(), ()> {
327317
match self.scan_whitespace_or_comment() {
328318
Some(comment) => {
329319
self.peek_span_src_raw = comment.span;
330-
self.peek_span = comment.span;
331-
self.peek_tok = comment.kind;
320+
self.peek_token = comment;
332321
}
333322
None => {
334323
if self.is_eof() {
335-
self.peek_tok = token::Eof;
324+
336325
let (real, raw) = self.mk_sp_and_raw(
337326
self.source_file.end_pos,
338327
self.source_file.end_pos,
339328
);
340-
self.peek_span = real;
329+
self.peek_token = Token { kind: token::Eof, span: real };
341330
self.peek_span_src_raw = raw;
342331
} else {
343332
let start_bytepos = self.pos;
344-
self.peek_tok = self.next_token_inner()?;
333+
let kind = self.next_token_inner()?;
345334
let (real, raw) = self.mk_sp_and_raw(start_bytepos, self.pos);
346-
self.peek_span = real;
335+
self.peek_token = Token { kind, span: real };
347336
self.peek_span_src_raw = raw;
348337
};
349338
}

src/libsyntax/parse/lexer/tokentrees.rs

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ use syntax_pos::Span;
22

33
use crate::print::pprust::token_to_string;
44
use crate::parse::lexer::{StringReader, UnmatchedBrace};
5-
use crate::parse::{token, PResult};
5+
use crate::parse::token::{self, Token};
6+
use crate::parse::PResult;
67
use crate::tokenstream::{DelimSpan, IsJoint::*, TokenStream, TokenTree, TreeAndJoint};
78

89
impl<'a> StringReader<'a> {
910
crate fn into_token_trees(self) -> (PResult<'a, TokenStream>, Vec<UnmatchedBrace>) {
1011
let mut tt_reader = TokenTreesReader {
1112
string_reader: self,
12-
token: token::Eof,
13-
span: syntax_pos::DUMMY_SP,
13+
token: token::Token { kind: token::Eof, span: syntax_pos::DUMMY_SP },
1414
open_braces: Vec::new(),
1515
unmatched_braces: Vec::new(),
1616
matching_delim_spans: Vec::new(),
@@ -23,8 +23,7 @@ impl<'a> StringReader<'a> {
2323

2424
struct TokenTreesReader<'a> {
2525
string_reader: StringReader<'a>,
26-
token: token::TokenKind,
27-
span: Span,
26+
token: Token,
2827
/// Stack of open delimiters and their spans. Used for error message.
2928
open_braces: Vec<(token::DelimToken, Span)>,
3029
unmatched_braces: Vec<UnmatchedBrace>,
@@ -52,7 +51,7 @@ impl<'a> TokenTreesReader<'a> {
5251
fn parse_token_trees_until_close_delim(&mut self) -> TokenStream {
5352
let mut tts = vec![];
5453
loop {
55-
if let token::CloseDelim(..) = self.token {
54+
if let token::CloseDelim(..) = self.token.kind {
5655
return TokenStream::new(tts);
5756
}
5857

@@ -68,11 +67,11 @@ impl<'a> TokenTreesReader<'a> {
6867

6968
fn parse_token_tree(&mut self) -> PResult<'a, TreeAndJoint> {
7069
let sm = self.string_reader.sess.source_map();
71-
match self.token {
70+
match self.token.kind {
7271
token::Eof => {
7372
let msg = "this file contains an un-closed delimiter";
7473
let mut err = self.string_reader.sess.span_diagnostic
75-
.struct_span_err(self.span, msg);
74+
.struct_span_err(self.token.span, msg);
7675
for &(_, sp) in &self.open_braces {
7776
err.span_label(sp, "un-closed delimiter");
7877
}
@@ -102,10 +101,10 @@ impl<'a> TokenTreesReader<'a> {
102101
},
103102
token::OpenDelim(delim) => {
104103
// The span for beginning of the delimited section
105-
let pre_span = self.span;
104+
let pre_span = self.token.span;
106105

107106
// Parse the open delimiter.
108-
self.open_braces.push((delim, self.span));
107+
self.open_braces.push((delim, self.token.span));
109108
self.real_token();
110109

111110
// Parse the token trees within the delimiters.
@@ -114,9 +113,9 @@ impl<'a> TokenTreesReader<'a> {
114113
let tts = self.parse_token_trees_until_close_delim();
115114

116115
// Expand to cover the entire delimited token tree
117-
let delim_span = DelimSpan::from_pair(pre_span, self.span);
116+
let delim_span = DelimSpan::from_pair(pre_span, self.token.span);
118117

119-
match self.token {
118+
match self.token.kind {
120119
// Correct delimiter.
121120
token::CloseDelim(d) if d == delim => {
122121
let (open_brace, open_brace_span) = self.open_braces.pop().unwrap();
@@ -126,7 +125,7 @@ impl<'a> TokenTreesReader<'a> {
126125
self.matching_delim_spans.clear();
127126
} else {
128127
self.matching_delim_spans.push(
129-
(open_brace, open_brace_span, self.span),
128+
(open_brace, open_brace_span, self.token.span),
130129
);
131130
}
132131
// Parse the close delimiter.
@@ -136,16 +135,16 @@ impl<'a> TokenTreesReader<'a> {
136135
token::CloseDelim(other) => {
137136
let mut unclosed_delimiter = None;
138137
let mut candidate = None;
139-
if self.last_unclosed_found_span != Some(self.span) {
138+
if self.last_unclosed_found_span != Some(self.token.span) {
140139
// do not complain about the same unclosed delimiter multiple times
141-
self.last_unclosed_found_span = Some(self.span);
140+
self.last_unclosed_found_span = Some(self.token.span);
142141
// This is a conservative error: only report the last unclosed
143142
// delimiter. The previous unclosed delimiters could actually be
144143
// closed! The parser just hasn't gotten to them yet.
145144
if let Some(&(_, sp)) = self.open_braces.last() {
146145
unclosed_delimiter = Some(sp);
147146
};
148-
if let Some(current_padding) = sm.span_to_margin(self.span) {
147+
if let Some(current_padding) = sm.span_to_margin(self.token.span) {
149148
for (brace, brace_span) in &self.open_braces {
150149
if let Some(padding) = sm.span_to_margin(*brace_span) {
151150
// high likelihood of these two corresponding
@@ -159,7 +158,7 @@ impl<'a> TokenTreesReader<'a> {
159158
self.unmatched_braces.push(UnmatchedBrace {
160159
expected_delim: tok,
161160
found_delim: other,
162-
found_span: self.span,
161+
found_span: self.token.span,
163162
unclosed_span: unclosed_delimiter,
164163
candidate_span: candidate,
165164
});
@@ -198,12 +197,12 @@ impl<'a> TokenTreesReader<'a> {
198197
let token_str = token_to_string(&self.token);
199198
let msg = format!("unexpected close delimiter: `{}`", token_str);
200199
let mut err = self.string_reader.sess.span_diagnostic
201-
.struct_span_err(self.span, &msg);
202-
err.span_label(self.span, "unexpected close delimiter");
200+
.struct_span_err(self.token.span, &msg);
201+
err.span_label(self.token.span, "unexpected close delimiter");
203202
Err(err)
204203
},
205204
_ => {
206-
let tt = TokenTree::token(self.span, self.token.clone());
205+
let tt = TokenTree::Token(self.token.clone());
207206
// Note that testing for joint-ness here is done via the raw
208207
// source span as the joint-ness is a property of the raw source
209208
// rather than wanting to take `override_span` into account.
@@ -219,8 +218,6 @@ impl<'a> TokenTreesReader<'a> {
219218
}
220219

221220
fn real_token(&mut self) {
222-
let t = self.string_reader.real_token();
223-
self.token = t.kind;
224-
self.span = t.span;
221+
self.token = self.string_reader.real_token();
225222
}
226223
}

0 commit comments

Comments
 (0)