Skip to content

Commit a76244f

Browse files
committed
Fix CRLF line-ending parsing for comments.
1 parent b402c43 commit a76244f

File tree

1 file changed

+26
-24
lines changed
  • src/libsyntax/parse/lexer

1 file changed

+26
-24
lines changed

src/libsyntax/parse/lexer/mod.rs

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -403,45 +403,47 @@ impl<'a> StringReader<'a> {
403403
Some('/') => {
404404
self.bump();
405405
self.bump();
406+
406407
// line comments starting with "///" or "//!" are doc-comments
407-
if self.curr_is('/') || self.curr_is('!') {
408-
let start_bpos = self.pos - BytePos(3);
409-
while !self.is_eof() {
410-
match self.curr.unwrap() {
411-
'\n' => break,
412-
'\r' => {
413-
if self.nextch_is('\n') {
414-
// CRLF
415-
break
416-
} else {
417-
self.err_span_(self.last_pos, self.pos,
418-
"bare CR not allowed in doc-comment");
419-
}
408+
let doc_comment = self.curr_is('/') || self.curr_is('!');
409+
let start_bpos = self.pos - BytePos(3);
410+
411+
while !self.is_eof() {
412+
match self.curr.unwrap() {
413+
'\n' => break,
414+
'\r' => {
415+
if self.nextch_is('\n') {
416+
// CRLF
417+
break
418+
} else {
419+
self.err_span_(self.last_pos, self.pos,
420+
"bare CR not allowed in comment");
420421
}
421-
_ => ()
422422
}
423-
self.bump();
423+
_ => ()
424424
}
425-
return self.with_str_from(start_bpos, |string| {
426-
// but comments with only more "/"s are not
425+
self.bump();
426+
}
427+
428+
return if doc_comment {
429+
self.with_str_from(start_bpos, |string| {
430+
// comments with only more "/"s are not doc comments
427431
let tok = if is_doc_comment(string) {
428432
token::DocComment(token::intern(string))
429433
} else {
430434
token::Comment
431435
};
432436

433-
return Some(TokenAndSpan{
437+
Some(TokenAndSpan {
434438
tok: tok,
435439
sp: codemap::mk_sp(start_bpos, self.last_pos)
436-
});
437-
});
440+
})
441+
})
438442
} else {
439-
let start_bpos = self.last_pos - BytePos(2);
440-
while !self.curr_is('\n') && !self.is_eof() { self.bump(); }
441-
return Some(TokenAndSpan {
443+
Some(TokenAndSpan {
442444
tok: token::Comment,
443445
sp: codemap::mk_sp(start_bpos, self.last_pos)
444-
});
446+
})
445447
}
446448
}
447449
Some('*') => {

0 commit comments

Comments
 (0)