Skip to content

Commit

Permalink
Simplify TokenTreesReader
Browse files Browse the repository at this point in the history
This `joint_to_prev` bit of state is no longer needed.
  • Loading branch information
matklad committed Sep 1, 2020
1 parent 5326361 commit fabd8a6
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions compiler/rustc_parse/src/lexer/tokentrees.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ impl<'a> StringReader<'a> {
let mut tt_reader = TokenTreesReader {
string_reader: self,
token: Token::dummy(),
joint_to_prev: Joint,
open_braces: Vec::new(),
unmatched_braces: Vec::new(),
matching_delim_spans: Vec::new(),
Expand All @@ -32,7 +31,6 @@ impl<'a> StringReader<'a> {
struct TokenTreesReader<'a> {
string_reader: StringReader<'a>,
token: Token,
joint_to_prev: IsJoint,
/// Stack of open delimiters and their spans. Used for error message.
open_braces: Vec<(token::DelimToken, Span)>,
unmatched_braces: Vec<UnmatchedBrace>,
Expand All @@ -53,7 +51,7 @@ impl<'a> TokenTreesReader<'a> {
fn parse_all_token_trees(&mut self) -> PResult<'a, TokenStream> {
let mut buf = TokenStreamBuilder::default();

self.real_token();
self.bump();
while self.token != token::Eof {
buf.push(self.parse_token_tree()?);
}
Expand Down Expand Up @@ -126,7 +124,7 @@ impl<'a> TokenTreesReader<'a> {

// Parse the open delimiter.
self.open_braces.push((delim, self.token.span));
self.real_token();
self.bump();

// Parse the token trees within the delimiters.
// We stop at any delimiter so we can try to recover if the user
Expand Down Expand Up @@ -171,7 +169,7 @@ impl<'a> TokenTreesReader<'a> {
));
}
// Parse the closing delimiter.
self.real_token();
self.bump();
}
// Incorrect delimiter.
token::CloseDelim(other) => {
Expand Down Expand Up @@ -217,7 +215,7 @@ impl<'a> TokenTreesReader<'a> {
// bar(baz(
// } // Incorrect delimiter but matches the earlier `{`
if !self.open_braces.iter().any(|&(b, _)| b == other) {
self.real_token();
self.bump();
}
}
token::Eof => {
Expand Down Expand Up @@ -264,17 +262,19 @@ impl<'a> TokenTreesReader<'a> {
}
_ => {
let tt = TokenTree::Token(self.token.take());
self.real_token();
let is_joint = self.joint_to_prev == Joint && self.token.is_op();
Ok((tt, if is_joint { Joint } else { NonJoint }))
let mut is_joint = self.bump();
if !self.token.is_op() {
is_joint = NonJoint;
}
Ok((tt, is_joint))
}
}
}

fn real_token(&mut self) {
fn bump(&mut self) -> IsJoint {
let (joint_to_prev, token) = self.string_reader.next_token();
self.joint_to_prev = joint_to_prev;
self.token = token;
joint_to_prev
}
}

Expand Down

0 comments on commit fabd8a6

Please sign in to comment.