Skip to content

Commit

Permalink
refactor: change to use peekable
Browse files Browse the repository at this point in the history
  • Loading branch information
yue4u committed Apr 14, 2022
1 parent 4a0f8d5 commit 1b7008d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 21 deletions.
37 changes: 17 additions & 20 deletions compiler/rustc_parse/src/lexer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -557,39 +557,36 @@ impl<'a> StringReader<'a> {
);
let mut nested_block_comment_open_idxs = vec![];
let mut last_nested_block_comment_idxs = None;
let mut content_chars = self.str_from(start).char_indices();
let mut content_chars = self.str_from(start).char_indices().peekable();

if let Some((_, mut last_char)) = content_chars.next() {
while let Some((idx, c)) = content_chars.next() {
match c {
'*' if last_char == '/' => {
nested_block_comment_open_idxs.push(idx);
}
'/' if last_char == '*' => {
last_nested_block_comment_idxs =
nested_block_comment_open_idxs.pop().map(|open_idx| (open_idx, idx));
}
_ => {}
};
last_char = c;
}
while let Some((idx, current_char)) = content_chars.next() {
match content_chars.peek() {
Some((_, '*')) if current_char == '/' => {
nested_block_comment_open_idxs.push(idx);
}
Some((_, '/')) if current_char == '*' => {
last_nested_block_comment_idxs =
nested_block_comment_open_idxs.pop().map(|open_idx| (open_idx, idx));
}
_ => {}
};
}

if let Some((nested_open_idx, nested_close_idx)) = last_nested_block_comment_idxs {
err.span_label(self.mk_sp(start, start + BytePos(2)), msg)
.span_label(
self.mk_sp(
start + BytePos(nested_open_idx as u32 - 1),
start + BytePos(nested_open_idx as u32 + 1),
start + BytePos(nested_open_idx as u32),
start + BytePos(nested_open_idx as u32 + 2),
),
"...as last nested comment starts here, maybe you want to close this instead?",
)
.span_label(
self.mk_sp(
start + BytePos(nested_close_idx as u32 - 1),
start + BytePos(nested_close_idx as u32 + 1),
start + BytePos(nested_close_idx as u32),
start + BytePos(nested_close_idx as u32 + 2),
),
"...and last nested comment terminates here",
"...and last nested comment terminates here.",
);
}

Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/unterminated-nested-comment.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ LL | | /*
LL | | */
| |_--^
| |
| ...and last nested comment terminates here
| ...and last nested comment terminates here.

error: aborting due to previous error

Expand Down

0 comments on commit 1b7008d

Please sign in to comment.