Skip to content

Commit 024c25d

Browse files
committed
Auto merge of #60763 - matklad:tt-parser, r=petrochenkov
Move token tree related lexer state to a separate struct Just a types-based refactoring. We only used a bunch of fields when tokenizing into a token tree, so let's move them out of the base lexer
2 parents 49d139c + e249f2e commit 024c25d

File tree

3 files changed

+62
-43
lines changed

3 files changed

+62
-43
lines changed

Diff for: src/libsyntax/parse/lexer/mod.rs

+1-25
Original file line numberDiff line numberDiff line change
@@ -62,19 +62,7 @@ pub struct StringReader<'a> {
6262
// cache a direct reference to the source text, so that we don't have to
6363
// retrieve it via `self.source_file.src.as_ref().unwrap()` all the time.
6464
src: Lrc<String>,
65-
token: token::Token,
66-
span: Span,
67-
/// The raw source span which *does not* take `override_span` into account
68-
span_src_raw: Span,
69-
/// Stack of open delimiters and their spans. Used for error message.
70-
open_braces: Vec<(token::DelimToken, Span)>,
71-
crate unmatched_braces: Vec<UnmatchedBrace>,
72-
/// The type and spans for all braces
73-
///
74-
/// Used only for error recovery when arriving to EOF with mismatched braces.
75-
matching_delim_spans: Vec<(token::DelimToken, Span, Span)>,
76-
crate override_span: Option<Span>,
77-
last_unclosed_found_span: Option<Span>,
65+
override_span: Option<Span>,
7866
}
7967

8068
impl<'a> StringReader<'a> {
@@ -121,8 +109,6 @@ impl<'a> StringReader<'a> {
121109
sp: self.peek_span,
122110
};
123111
self.advance_token()?;
124-
self.span_src_raw = self.peek_span_src_raw;
125-
126112
Ok(ret_val)
127113
}
128114

@@ -159,9 +145,6 @@ impl<'a> StringReader<'a> {
159145
}
160146
}
161147

162-
self.token = t.tok.clone();
163-
self.span = t.sp;
164-
165148
Ok(t)
166149
}
167150

@@ -251,14 +234,7 @@ impl<'a> StringReader<'a> {
251234
peek_span_src_raw: syntax_pos::DUMMY_SP,
252235
src,
253236
fatal_errs: Vec::new(),
254-
token: token::Eof,
255-
span: syntax_pos::DUMMY_SP,
256-
span_src_raw: syntax_pos::DUMMY_SP,
257-
open_braces: Vec::new(),
258-
unmatched_braces: Vec::new(),
259-
matching_delim_spans: Vec::new(),
260237
override_span,
261-
last_unclosed_found_span: None,
262238
}
263239
}
264240

Diff for: src/libsyntax/parse/lexer/tokentrees.rs

+55-12
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,46 @@
1+
use syntax_pos::Span;
2+
13
use crate::print::pprust::token_to_string;
24
use crate::parse::lexer::{StringReader, UnmatchedBrace};
35
use crate::parse::{token, PResult};
46
use crate::tokenstream::{DelimSpan, IsJoint::*, TokenStream, TokenTree, TreeAndJoint};
57

68
impl<'a> StringReader<'a> {
9+
crate fn into_token_trees(self) -> (PResult<'a, TokenStream>, Vec<UnmatchedBrace>) {
10+
let mut tt_reader = TokenTreesReader {
11+
string_reader: self,
12+
token: token::Eof,
13+
span: syntax_pos::DUMMY_SP,
14+
open_braces: Vec::new(),
15+
unmatched_braces: Vec::new(),
16+
matching_delim_spans: Vec::new(),
17+
last_unclosed_found_span: None,
18+
};
19+
let res = tt_reader.parse_all_token_trees();
20+
(res, tt_reader.unmatched_braces)
21+
}
22+
}
23+
24+
struct TokenTreesReader<'a> {
25+
string_reader: StringReader<'a>,
26+
token: token::Token,
27+
span: Span,
28+
/// Stack of open delimiters and their spans. Used for error message.
29+
open_braces: Vec<(token::DelimToken, Span)>,
30+
unmatched_braces: Vec<UnmatchedBrace>,
31+
/// The type and spans for all braces
32+
///
33+
/// Used only for error recovery when arriving to EOF with mismatched braces.
34+
matching_delim_spans: Vec<(token::DelimToken, Span, Span)>,
35+
last_unclosed_found_span: Option<Span>,
36+
}
37+
38+
impl<'a> TokenTreesReader<'a> {
739
// Parse a stream of tokens into a list of `TokenTree`s, up to an `Eof`.
8-
crate fn parse_all_token_trees(&mut self) -> PResult<'a, TokenStream> {
40+
fn parse_all_token_trees(&mut self) -> PResult<'a, TokenStream> {
941
let mut tts = Vec::new();
1042

43+
self.real_token();
1144
while self.token != token::Eof {
1245
tts.push(self.parse_token_tree()?);
1346
}
@@ -34,25 +67,25 @@ impl<'a> StringReader<'a> {
3467
}
3568

3669
fn parse_token_tree(&mut self) -> PResult<'a, TreeAndJoint> {
37-
let sm = self.sess.source_map();
70+
let sm = self.string_reader.sess.source_map();
3871
match self.token {
3972
token::Eof => {
4073
let msg = "this file contains an un-closed delimiter";
41-
let mut err = self.sess.span_diagnostic.struct_span_err(self.span, msg);
74+
let mut err = self.string_reader.sess.span_diagnostic
75+
.struct_span_err(self.span, msg);
4276
for &(_, sp) in &self.open_braces {
4377
err.span_label(sp, "un-closed delimiter");
4478
}
4579

4680
if let Some((delim, _)) = self.open_braces.last() {
4781
if let Some((_, open_sp, close_sp)) = self.matching_delim_spans.iter()
4882
.filter(|(d, open_sp, close_sp)| {
49-
50-
if let Some(close_padding) = sm.span_to_margin(*close_sp) {
51-
if let Some(open_padding) = sm.span_to_margin(*open_sp) {
52-
return delim == d && close_padding != open_padding;
83+
if let Some(close_padding) = sm.span_to_margin(*close_sp) {
84+
if let Some(open_padding) = sm.span_to_margin(*open_sp) {
85+
return delim == d && close_padding != open_padding;
86+
}
5387
}
54-
}
55-
false
88+
false
5689
}).next() // these are in reverse order as they get inserted on close, but
5790
{ // we want the last open/first close
5891
err.span_label(
@@ -164,7 +197,8 @@ impl<'a> StringReader<'a> {
164197
// matching opening delimiter).
165198
let token_str = token_to_string(&self.token);
166199
let msg = format!("unexpected close delimiter: `{}`", token_str);
167-
let mut err = self.sess.span_diagnostic.struct_span_err(self.span, &msg);
200+
let mut err = self.string_reader.sess.span_diagnostic
201+
.struct_span_err(self.span, &msg);
168202
err.span_label(self.span, "unexpected close delimiter");
169203
Err(err)
170204
},
@@ -173,11 +207,20 @@ impl<'a> StringReader<'a> {
173207
// Note that testing for joint-ness here is done via the raw
174208
// source span as the joint-ness is a property of the raw source
175209
// rather than wanting to take `override_span` into account.
176-
let raw = self.span_src_raw;
210+
// Additionally, we actually check if the *next* pair of tokens
211+
// is joint, but this is equivalent to checking the current pair.
212+
let raw = self.string_reader.peek_span_src_raw;
177213
self.real_token();
178-
let is_joint = raw.hi() == self.span_src_raw.lo() && token::is_op(&self.token);
214+
let is_joint = raw.hi() == self.string_reader.peek_span_src_raw.lo()
215+
&& token::is_op(&self.token);
179216
Ok((tt, if is_joint { Joint } else { NonJoint }))
180217
}
181218
}
182219
}
220+
221+
fn real_token(&mut self) {
222+
let t = self.string_reader.real_token();
223+
self.token = t.tok;
224+
self.span = t.sp;
225+
}
183226
}

Diff for: src/libsyntax/parse/mod.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -290,22 +290,22 @@ pub fn source_file_to_stream(
290290
}
291291

292292
/// Given a source file, produces a sequence of token trees. Returns any buffered errors from
293-
/// parsing the token tream.
293+
/// parsing the token stream.
294294
pub fn maybe_file_to_stream(
295295
sess: &ParseSess,
296296
source_file: Lrc<SourceFile>,
297297
override_span: Option<Span>,
298298
) -> Result<(TokenStream, Vec<lexer::UnmatchedBrace>), Vec<Diagnostic>> {
299-
let mut srdr = lexer::StringReader::new_or_buffered_errs(sess, source_file, override_span)?;
300-
srdr.real_token();
299+
let srdr = lexer::StringReader::new_or_buffered_errs(sess, source_file, override_span)?;
300+
let (token_trees, unmatched_braces) = srdr.into_token_trees();
301301

302-
match srdr.parse_all_token_trees() {
303-
Ok(stream) => Ok((stream, srdr.unmatched_braces)),
302+
match token_trees {
303+
Ok(stream) => Ok((stream, unmatched_braces)),
304304
Err(err) => {
305305
let mut buffer = Vec::with_capacity(1);
306306
err.buffer(&mut buffer);
307307
// Not using `emit_unclosed_delims` to use `db.buffer`
308-
for unmatched in srdr.unmatched_braces {
308+
for unmatched in unmatched_braces {
309309
let mut db = sess.span_diagnostic.struct_span_err(unmatched.found_span, &format!(
310310
"incorrect close delimiter: `{}`",
311311
token_to_string(&token::Token::CloseDelim(unmatched.found_delim)),

0 commit comments

Comments
 (0)