Skip to content

Fix CRLF line-ending parsing for comments. #25219

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from May 15, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 39 additions & 24 deletions src/libsyntax/parse/lexer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -403,45 +403,51 @@ impl<'a> StringReader<'a> {
Some('/') => {
self.bump();
self.bump();

// line comments starting with "///" or "//!" are doc-comments
if self.curr_is('/') || self.curr_is('!') {
let start_bpos = self.pos - BytePos(3);
while !self.is_eof() {
match self.curr.unwrap() {
'\n' => break,
'\r' => {
if self.nextch_is('\n') {
// CRLF
break
} else {
self.err_span_(self.last_pos, self.pos,
"bare CR not allowed in doc-comment");
}
let doc_comment = self.curr_is('/') || self.curr_is('!');
let start_bpos = if doc_comment {
self.pos - BytePos(3)
} else {
self.last_pos - BytePos(2)
};

while !self.is_eof() {
match self.curr.unwrap() {
'\n' => break,
'\r' => {
if self.nextch_is('\n') {
// CRLF
break
} else if doc_comment {
self.err_span_(self.last_pos, self.pos,
"bare CR not allowed in doc-comment");
}
_ => ()
}
self.bump();
_ => ()
}
return self.with_str_from(start_bpos, |string| {
// but comments with only more "/"s are not
self.bump();
}

return if doc_comment {
self.with_str_from(start_bpos, |string| {
// comments with only more "/"s are not doc comments
let tok = if is_doc_comment(string) {
token::DocComment(token::intern(string))
} else {
token::Comment
};

return Some(TokenAndSpan{
Some(TokenAndSpan {
tok: tok,
sp: codemap::mk_sp(start_bpos, self.last_pos)
});
});
})
})
} else {
let start_bpos = self.last_pos - BytePos(2);
while !self.curr_is('\n') && !self.is_eof() { self.bump(); }
return Some(TokenAndSpan {
Some(TokenAndSpan {
tok: token::Comment,
sp: codemap::mk_sp(start_bpos, self.last_pos)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this start_bpos is off by one perhaps? It was originally last-BytesPos(2) but now I think it's last-BytePos(3)

});
})
}
}
Some('*') => {
Expand Down Expand Up @@ -1563,4 +1569,13 @@ mod tests {
assert_eq!(lexer.next_token().tok, token::Literal(token::Char(token::intern("a")), None));
}

#[test] fn crlf_comments() {
let sh = mk_sh();
let mut lexer = setup(&sh, "// test\r\n/// test\r\n".to_string());
let comment = lexer.next_token();
assert_eq!(comment.tok, token::Comment);
assert_eq!(comment.sp, ::codemap::mk_sp(BytePos(0), BytePos(7)));
assert_eq!(lexer.next_token().tok, token::Whitespace);
assert_eq!(lexer.next_token().tok, token::DocComment(token::intern("/// test")));
}
}