Auto merge of #50924 - petrochenkov:spanover, r=alexcrichton
lexer: Fix span override for the first token in a string Previously due to peculiarities of `StringReader` construction something like `"a b c d".parse::<TokenStream>()` gave you one non-overridden span for `a` and then three correctly overridden spans for `b`, `c` and `d`. Now all the spans are overridden.
This commit is contained in:
commit
ba1363ffe1
@ -48,7 +48,7 @@ pub fn render_with_highlighting(src: &str, class: Option<&str>, id: Option<&str>
|
||||
}
|
||||
write_header(class, id, &mut out).unwrap();
|
||||
|
||||
let mut classifier = Classifier::new(lexer::StringReader::new(&sess, fm), sess.codemap());
|
||||
let mut classifier = Classifier::new(lexer::StringReader::new(&sess, fm, None), sess.codemap());
|
||||
if let Err(_) = classifier.write_source(&mut out) {
|
||||
return format!("<pre>{}</pre>", src);
|
||||
}
|
||||
@ -68,7 +68,7 @@ pub fn render_inner_with_highlighting(src: &str) -> io::Result<String> {
|
||||
let fm = sess.codemap().new_filemap(FileName::Custom("stdin".to_string()), src.to_string());
|
||||
|
||||
let mut out = Vec::new();
|
||||
let mut classifier = Classifier::new(lexer::StringReader::new(&sess, fm), sess.codemap());
|
||||
let mut classifier = Classifier::new(lexer::StringReader::new(&sess, fm, None), sess.codemap());
|
||||
classifier.write_source(&mut out)?;
|
||||
|
||||
Ok(String::from_utf8_lossy(&out).into_owned())
|
||||
|
@ -362,7 +362,7 @@ pub fn gather_comments_and_literals(sess: &ParseSess, path: FileName, srdr: &mut
|
||||
let src = String::from_utf8(src).unwrap();
|
||||
let cm = CodeMap::new(sess.codemap().path_mapping().clone());
|
||||
let filemap = cm.new_filemap(path, src);
|
||||
let mut rdr = lexer::StringReader::new_raw(sess, filemap);
|
||||
let mut rdr = lexer::StringReader::new_raw(sess, filemap, None);
|
||||
|
||||
let mut comments: Vec<Comment> = Vec::new();
|
||||
let mut literals: Vec<Literal> = Vec::new();
|
||||
|
@ -155,13 +155,15 @@ impl<'a> StringReader<'a> {
|
||||
|
||||
impl<'a> StringReader<'a> {
|
||||
/// For comments.rs, which hackily pokes into next_pos and ch
|
||||
pub fn new_raw(sess: &'a ParseSess, filemap: Lrc<syntax_pos::FileMap>) -> Self {
|
||||
let mut sr = StringReader::new_raw_internal(sess, filemap);
|
||||
pub fn new_raw(sess: &'a ParseSess, filemap: Lrc<syntax_pos::FileMap>,
|
||||
override_span: Option<Span>) -> Self {
|
||||
let mut sr = StringReader::new_raw_internal(sess, filemap, override_span);
|
||||
sr.bump();
|
||||
sr
|
||||
}
|
||||
|
||||
fn new_raw_internal(sess: &'a ParseSess, filemap: Lrc<syntax_pos::FileMap>) -> Self {
|
||||
fn new_raw_internal(sess: &'a ParseSess, filemap: Lrc<syntax_pos::FileMap>,
|
||||
override_span: Option<Span>) -> Self {
|
||||
if filemap.src.is_none() {
|
||||
sess.span_diagnostic.bug(&format!("Cannot lex filemap without source: {}",
|
||||
filemap.name));
|
||||
@ -185,12 +187,13 @@ impl<'a> StringReader<'a> {
|
||||
token: token::Eof,
|
||||
span: syntax_pos::DUMMY_SP,
|
||||
open_braces: Vec::new(),
|
||||
override_span: None,
|
||||
override_span,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn new(sess: &'a ParseSess, filemap: Lrc<syntax_pos::FileMap>) -> Self {
|
||||
let mut sr = StringReader::new_raw(sess, filemap);
|
||||
pub fn new(sess: &'a ParseSess, filemap: Lrc<syntax_pos::FileMap>, override_span: Option<Span>)
|
||||
-> Self {
|
||||
let mut sr = StringReader::new_raw(sess, filemap, override_span);
|
||||
if sr.advance_token().is_err() {
|
||||
sr.emit_fatal_errors();
|
||||
FatalError.raise();
|
||||
@ -207,7 +210,7 @@ impl<'a> StringReader<'a> {
|
||||
span = span.shrink_to_lo();
|
||||
}
|
||||
|
||||
let mut sr = StringReader::new_raw_internal(sess, begin.fm);
|
||||
let mut sr = StringReader::new_raw_internal(sess, begin.fm, None);
|
||||
|
||||
// Seek the lexer to the right byte range.
|
||||
sr.save_new_lines_and_multibyte = false;
|
||||
@ -1795,7 +1798,7 @@ mod tests {
|
||||
teststr: String)
|
||||
-> StringReader<'a> {
|
||||
let fm = cm.new_filemap(PathBuf::from("zebra.rs").into(), teststr);
|
||||
StringReader::new(sess, fm)
|
||||
StringReader::new(sess, fm, None)
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -229,8 +229,7 @@ fn file_to_filemap(sess: &ParseSess, path: &Path, spanopt: Option<Span>)
|
||||
/// Given a filemap, produce a sequence of token-trees
|
||||
pub fn filemap_to_stream(sess: &ParseSess, filemap: Lrc<FileMap>, override_span: Option<Span>)
|
||||
-> TokenStream {
|
||||
let mut srdr = lexer::StringReader::new(sess, filemap);
|
||||
srdr.override_span = override_span;
|
||||
let mut srdr = lexer::StringReader::new(sess, filemap, override_span);
|
||||
srdr.real_token();
|
||||
panictry!(srdr.parse_all_token_trees())
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user