syntax: replace sess.span_diagnostic.cm with sess.codemap().
This commit is contained in:
parent
f786437bd2
commit
6a59d1824d
8 changed files with 18 additions and 28 deletions
|
@ -287,11 +287,9 @@ fn main() {
|
||||||
let options = config::basic_options();
|
let options = config::basic_options();
|
||||||
let session = session::build_session(options, None,
|
let session = session::build_session(options, None,
|
||||||
syntax::diagnostics::registry::Registry::new(&[]));
|
syntax::diagnostics::registry::Registry::new(&[]));
|
||||||
let filemap = parse::string_to_filemap(&session.parse_sess,
|
let filemap = session.parse_sess.codemap().new_filemap(String::from_str("<n/a>"), code);
|
||||||
code,
|
|
||||||
String::from_str("<n/a>"));
|
|
||||||
let mut lexer = lexer::StringReader::new(session.diagnostic(), filemap);
|
let mut lexer = lexer::StringReader::new(session.diagnostic(), filemap);
|
||||||
let ref cm = lexer.span_diagnostic.cm;
|
let cm = session.codemap();
|
||||||
|
|
||||||
// ANTLR
|
// ANTLR
|
||||||
let mut token_file = File::open(&Path::new(&args.next().unwrap())).unwrap();
|
let mut token_file = File::open(&Path::new(&args.next().unwrap())).unwrap();
|
||||||
|
|
|
@ -231,7 +231,7 @@ impl Session {
|
||||||
&self.parse_sess.span_diagnostic
|
&self.parse_sess.span_diagnostic
|
||||||
}
|
}
|
||||||
pub fn codemap<'a>(&'a self) -> &'a codemap::CodeMap {
|
pub fn codemap<'a>(&'a self) -> &'a codemap::CodeMap {
|
||||||
&self.parse_sess.span_diagnostic.cm
|
self.parse_sess.codemap()
|
||||||
}
|
}
|
||||||
// This exists to help with refactoring to eliminate impossible
|
// This exists to help with refactoring to eliminate impossible
|
||||||
// cases later on
|
// cases later on
|
||||||
|
|
|
@ -25,9 +25,7 @@ use syntax::parse;
|
||||||
pub fn highlight(src: &str, class: Option<&str>, id: Option<&str>) -> String {
|
pub fn highlight(src: &str, class: Option<&str>, id: Option<&str>) -> String {
|
||||||
debug!("highlighting: ================\n{}\n==============", src);
|
debug!("highlighting: ================\n{}\n==============", src);
|
||||||
let sess = parse::ParseSess::new();
|
let sess = parse::ParseSess::new();
|
||||||
let fm = parse::string_to_filemap(&sess,
|
let fm = sess.codemap().new_filemap("<stdin>".to_string(), src.to_string());
|
||||||
src.to_string(),
|
|
||||||
"<stdin>".to_string());
|
|
||||||
|
|
||||||
let mut out = Vec::new();
|
let mut out = Vec::new();
|
||||||
doit(&sess,
|
doit(&sess,
|
||||||
|
@ -62,7 +60,7 @@ fn doit(sess: &parse::ParseSess, mut lexer: lexer::StringReader,
|
||||||
loop {
|
loop {
|
||||||
let next = lexer.next_token();
|
let next = lexer.next_token();
|
||||||
|
|
||||||
let snip = |sp| sess.span_diagnostic.cm.span_to_snippet(sp).unwrap();
|
let snip = |sp| sess.codemap().span_to_snippet(sp).unwrap();
|
||||||
|
|
||||||
if next.tok == token::Eof { break }
|
if next.tok == token::Eof { break }
|
||||||
|
|
||||||
|
@ -178,7 +176,7 @@ fn doit(sess: &parse::ParseSess, mut lexer: lexer::StringReader,
|
||||||
|
|
||||||
// as mentioned above, use the original source code instead of
|
// as mentioned above, use the original source code instead of
|
||||||
// stringifying this token
|
// stringifying this token
|
||||||
let snip = sess.span_diagnostic.cm.span_to_snippet(next.sp).unwrap();
|
let snip = sess.codemap().span_to_snippet(next.sp).unwrap();
|
||||||
if klass == "" {
|
if klass == "" {
|
||||||
try!(write!(out, "{}", Escape(&snip)));
|
try!(write!(out, "{}", Escape(&snip)));
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -648,7 +648,7 @@ impl<'a> ExtCtxt<'a> {
|
||||||
parse::tts_to_parser(self.parse_sess, tts.to_vec(), self.cfg())
|
parse::tts_to_parser(self.parse_sess, tts.to_vec(), self.cfg())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn codemap(&self) -> &'a CodeMap { &self.parse_sess.span_diagnostic.cm }
|
pub fn codemap(&self) -> &'a CodeMap { self.parse_sess.codemap() }
|
||||||
pub fn parse_sess(&self) -> &'a parse::ParseSess { self.parse_sess }
|
pub fn parse_sess(&self) -> &'a parse::ParseSess { self.parse_sess }
|
||||||
pub fn cfg(&self) -> ast::CrateConfig { self.cfg.clone() }
|
pub fn cfg(&self) -> ast::CrateConfig { self.cfg.clone() }
|
||||||
pub fn call_site(&self) -> Span {
|
pub fn call_site(&self) -> Span {
|
||||||
|
|
|
@ -58,6 +58,10 @@ impl ParseSess {
|
||||||
included_mod_stack: RefCell::new(vec![])
|
included_mod_stack: RefCell::new(vec![])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn codemap(&self) -> &CodeMap {
|
||||||
|
&self.span_diagnostic.cm
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// a bunch of utility functions of the form parse_<thing>_from_<source>
|
// a bunch of utility functions of the form parse_<thing>_from_<source>
|
||||||
|
@ -170,7 +174,7 @@ pub fn new_parser_from_source_str<'a>(sess: &'a ParseSess,
|
||||||
name: String,
|
name: String,
|
||||||
source: String)
|
source: String)
|
||||||
-> Parser<'a> {
|
-> Parser<'a> {
|
||||||
filemap_to_parser(sess, string_to_filemap(sess, source, name), cfg)
|
filemap_to_parser(sess, sess.codemap().new_filemap(name, source), cfg)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Create a new parser, handling errors as appropriate
|
/// Create a new parser, handling errors as appropriate
|
||||||
|
@ -234,8 +238,7 @@ pub fn file_to_filemap(sess: &ParseSess, path: &Path, spanopt: Option<Span>)
|
||||||
};
|
};
|
||||||
match str::from_utf8(&bytes[..]).ok() {
|
match str::from_utf8(&bytes[..]).ok() {
|
||||||
Some(s) => {
|
Some(s) => {
|
||||||
string_to_filemap(sess, s.to_string(),
|
sess.codemap().new_filemap(path.to_str().unwrap().to_string(), s.to_string())
|
||||||
path.to_str().unwrap().to_string())
|
|
||||||
}
|
}
|
||||||
None => {
|
None => {
|
||||||
err(&format!("{:?} is not UTF-8 encoded", path.display()));
|
err(&format!("{:?} is not UTF-8 encoded", path.display()));
|
||||||
|
@ -244,13 +247,6 @@ pub fn file_to_filemap(sess: &ParseSess, path: &Path, spanopt: Option<Span>)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Given a session and a string, add the string to
|
|
||||||
/// the session's codemap and return the new filemap
|
|
||||||
pub fn string_to_filemap(sess: &ParseSess, source: String, path: String)
|
|
||||||
-> Rc<FileMap> {
|
|
||||||
sess.span_diagnostic.cm.new_filemap(path, source)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Given a filemap, produce a sequence of token-trees
|
/// Given a filemap, produce a sequence of token-trees
|
||||||
pub fn filemap_to_tts(sess: &ParseSess, filemap: Rc<FileMap>)
|
pub fn filemap_to_tts(sess: &ParseSess, filemap: Rc<FileMap>)
|
||||||
-> Vec<ast::TokenTree> {
|
-> Vec<ast::TokenTree> {
|
||||||
|
@ -1104,7 +1100,7 @@ mod tests {
|
||||||
|
|
||||||
let span = tts.iter().rev().next().unwrap().get_span();
|
let span = tts.iter().rev().next().unwrap().get_span();
|
||||||
|
|
||||||
match sess.span_diagnostic.cm.span_to_snippet(span) {
|
match sess.codemap().span_to_snippet(span) {
|
||||||
Ok(s) => assert_eq!(&s[..], "{ body }"),
|
Ok(s) => assert_eq!(&s[..], "{ body }"),
|
||||||
Err(_) => panic!("could not get snippet"),
|
Err(_) => panic!("could not get snippet"),
|
||||||
}
|
}
|
||||||
|
|
|
@ -4835,8 +4835,7 @@ impl<'a> Parser<'a> {
|
||||||
outer_attrs: &[ast::Attribute],
|
outer_attrs: &[ast::Attribute],
|
||||||
id_sp: Span)
|
id_sp: Span)
|
||||||
-> PResult<(ast::Item_, Vec<ast::Attribute> )> {
|
-> PResult<(ast::Item_, Vec<ast::Attribute> )> {
|
||||||
let mut prefix = PathBuf::from(&self.sess.span_diagnostic.cm
|
let mut prefix = PathBuf::from(&self.sess.codemap().span_to_filename(self.span));
|
||||||
.span_to_filename(self.span));
|
|
||||||
prefix.pop();
|
prefix.pop();
|
||||||
let mut dir_path = prefix;
|
let mut dir_path = prefix;
|
||||||
for part in &self.mod_path_stack {
|
for part in &self.mod_path_stack {
|
||||||
|
|
|
@ -301,7 +301,7 @@ fn ignored_span(cx: &TestCtxt, sp: Span) -> Span {
|
||||||
allow_internal_unstable: true,
|
allow_internal_unstable: true,
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
let expn_id = cx.sess.span_diagnostic.cm.record_expansion(info);
|
let expn_id = cx.sess.codemap().record_expansion(info);
|
||||||
let mut sp = sp;
|
let mut sp = sp;
|
||||||
sp.expn_id = expn_id;
|
sp.expn_id = expn_id;
|
||||||
return sp;
|
return sp;
|
||||||
|
|
|
@ -9,7 +9,7 @@
|
||||||
// except according to those terms.
|
// except according to those terms.
|
||||||
|
|
||||||
use ast;
|
use ast;
|
||||||
use parse::{ParseSess,string_to_filemap,filemap_to_tts};
|
use parse::{ParseSess,filemap_to_tts};
|
||||||
use parse::new_parser_from_source_str;
|
use parse::new_parser_from_source_str;
|
||||||
use parse::parser::Parser;
|
use parse::parser::Parser;
|
||||||
use parse::token;
|
use parse::token;
|
||||||
|
@ -19,8 +19,7 @@ use str::char_at;
|
||||||
/// Map a string to tts, using a made-up filename:
|
/// Map a string to tts, using a made-up filename:
|
||||||
pub fn string_to_tts(source_str: String) -> Vec<ast::TokenTree> {
|
pub fn string_to_tts(source_str: String) -> Vec<ast::TokenTree> {
|
||||||
let ps = ParseSess::new();
|
let ps = ParseSess::new();
|
||||||
filemap_to_tts(&ps,
|
filemap_to_tts(&ps, ps.codemap().new_filemap("bogofile".to_string(), source_str))
|
||||||
string_to_filemap(&ps, source_str, "bogofile".to_string()))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Map string to parser (via tts)
|
/// Map string to parser (via tts)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue