Skip to content

Commit f6493dd

Browse files
authored
Rollup merge of #53289 - ljedrz:improve_lexer, r=michaelwoerister
A few cleanups and minor improvements for the lexer - improve readability by adjusting the formatting of some function signatures and adding some newlines - reorder some functions for easier reading - remove redundant `'static` in `const`s - remove some explicit `return`s - read directly to a `String` in `gather_comments_and_literals` - change `unwrap_or!` (macro) to `unwrap_or` (function) - move an `assert!`ion from `try_next_token` (called in a loop) to `try_real_token` after all calls to `try_next_token` - `#[inline]` some one-liner functions - assign directly from an `if-else` expression - refactor a `match` to `map_or` - add a `token::is_irrelevant` function to detect tokens that are not "`real`"
2 parents de7b89d + 40d9bd0 commit f6493dd

File tree

4 files changed

+134
-74
lines changed

4 files changed

+134
-74
lines changed

src/libsyntax/parse/lexer/comments.rs

+16-6
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ pub fn strip_doc_comment_decoration(comment: &str) -> String {
6363
if !lines.is_empty() && lines[0].chars().all(|c| c == '*') {
6464
i += 1;
6565
}
66+
6667
while i < j && lines[i].trim().is_empty() {
6768
i += 1;
6869
}
@@ -74,9 +75,11 @@ pub fn strip_doc_comment_decoration(comment: &str) -> String {
7475
.all(|c| c == '*') {
7576
j -= 1;
7677
}
78+
7779
while j > i && lines[j - 1].trim().is_empty() {
7880
j -= 1;
7981
}
82+
8083
lines[i..j].to_vec()
8184
}
8285

@@ -85,6 +88,7 @@ pub fn strip_doc_comment_decoration(comment: &str) -> String {
8588
let mut i = usize::MAX;
8689
let mut can_trim = true;
8790
let mut first = true;
91+
8892
for line in &lines {
8993
for (j, c) in line.chars().enumerate() {
9094
if j > i || !"* \t".contains(c) {
@@ -119,7 +123,8 @@ pub fn strip_doc_comment_decoration(comment: &str) -> String {
119123
}
120124

121125
// one-line comments lose their prefix
122-
const ONELINERS: &'static [&'static str] = &["///!", "///", "//!", "//"];
126+
const ONELINERS: &[&str] = &["///!", "///", "//!", "//"];
127+
123128
for prefix in ONELINERS {
124129
if comment.starts_with(*prefix) {
125130
return (&comment[prefix.len()..]).to_string();
@@ -205,6 +210,7 @@ fn all_whitespace(s: &str, col: CharPos) -> Option<usize> {
205210
let len = s.len();
206211
let mut col = col.to_usize();
207212
let mut cursor: usize = 0;
213+
208214
while col > 0 && cursor < len {
209215
let ch = char_at(s, cursor);
210216
if !ch.is_whitespace() {
@@ -213,7 +219,8 @@ fn all_whitespace(s: &str, col: CharPos) -> Option<usize> {
213219
cursor += ch.len_utf8();
214220
col -= 1;
215221
}
216-
return Some(cursor);
222+
223+
Some(cursor)
217224
}
218225

219226
fn trim_whitespace_prefix_and_push_line(lines: &mut Vec<String>, s: String, col: CharPos) {
@@ -246,11 +253,13 @@ fn read_block_comment(rdr: &mut StringReader,
246253
"src_index={}, end_src_index={}, line_begin_pos={}",
247254
src_index, end_src_index, rdr.filemap.line_begin_pos(rdr.pos).to_u32());
248255
let mut n = 0;
256+
249257
while src_index < end_src_index {
250258
let c = char_at(&rdr.src, src_index);
251259
src_index += c.len_utf8();
252260
n += 1;
253261
}
262+
254263
let col = CharPos(n);
255264

256265
rdr.bump();
@@ -358,10 +367,10 @@ pub struct Literal {
358367
// it appears this function is called only from pprust... that's
359368
// probably not a good thing.
360369
pub fn gather_comments_and_literals(sess: &ParseSess, path: FileName, srdr: &mut dyn Read)
361-
-> (Vec<Comment>, Vec<Literal>) {
362-
let mut src = Vec::new();
363-
srdr.read_to_end(&mut src).unwrap();
364-
let src = String::from_utf8(src).unwrap();
370+
-> (Vec<Comment>, Vec<Literal>)
371+
{
372+
let mut src = String::new();
373+
srdr.read_to_string(&mut src).unwrap();
365374
let cm = CodeMap::new(sess.codemap().path_mapping().clone());
366375
let filemap = cm.new_filemap(path, src);
367376
let mut rdr = lexer::StringReader::new_raw(sess, filemap, None);
@@ -370,6 +379,7 @@ pub fn gather_comments_and_literals(sess: &ParseSess, path: FileName, srdr: &mut
370379
let mut literals: Vec<Literal> = Vec::new();
371380
let mut code_to_the_left = false; // Only code
372381
let mut anything_to_the_left = false; // Code or comments
382+
373383
while !rdr.is_eof() {
374384
loop {
375385
// Eat all the whitespace and count blank lines.

0 commit comments

Comments
 (0)