Stop tracking CodeMap offsets in the parse session. Big simplification

This commit is contained in:
Brian Anderson 2012-11-16 14:22:09 -08:00
parent 2374154ded
commit 4a5b28ff0c
5 changed files with 48 additions and 96 deletions

View File

@ -201,25 +201,6 @@ pub struct FileMap {
}
pub impl FileMap {
static fn new_w_substr(+filename: FileName, +substr: FileSubstr,
src: @~str,
+start_pos: BytePos)
-> FileMap {
return FileMap {
name: filename, substr: substr, src: src,
start_pos: start_pos,
mut lines: ~[],
multibyte_chars: DVec()
};
}
static fn new(+filename: FileName, src: @~str,
+start_pos: BytePos)
-> FileMap {
return FileMap::new_w_substr(filename, FssNone, src,
start_pos);
}
fn next_line(&self, +pos: BytePos) {
self.lines.push(pos);
}
@ -262,20 +243,30 @@ pub impl CodeMap {
}
}
pub fn add_filemap(&self, filemap: @FileMap) {
let expected_byte_pos = if self.files.len() == 0 {
fn new_filemap_w_substr(+filename: FileName, +substr: FileSubstr,
src: @~str) -> @FileMap {
let start_pos = if self.files.len() == 0 {
0
} else {
let last_start = self.files.last().start_pos.to_uint();
let last_len = self.files.last().src.len();
last_start + last_len
};
let actual_byte_pos = filemap.start_pos.to_uint();
debug!("codemap: adding filemap: %s", filemap.name);
debug!("codemap: expected offset: %u", expected_byte_pos);
debug!("codemap: actual offset: %u", actual_byte_pos);
assert expected_byte_pos == actual_byte_pos;
let filemap = @FileMap {
name: filename, substr: substr, src: src,
start_pos: BytePos(start_pos),
mut lines: ~[],
multibyte_chars: DVec()
};
self.files.push(filemap);
return filemap;
}
fn new_filemap(+filename: FileName, src: @~str) -> @FileMap {
return self.new_filemap_w_substr(filename, FssNone, src);
}
pub fn mk_substr_filename(&self, sp: span) -> ~str {

View File

@ -58,12 +58,11 @@ fn expand_include(cx: ext_ctxt, sp: span, arg: ast::mac_arg,
_body: ast::mac_body) -> @ast::expr {
let args = get_mac_args(cx, sp, arg, 1u, option::Some(1u), ~"include");
let file = expr_to_str(cx, args[0], ~"#include_str requires a string");
let (p, rdr) = parse::new_parser_etc_from_file(
let p = parse::new_parser_from_file(
cx.parse_sess(), cx.cfg(),
&res_rel_file(cx, sp, &Path(file)),
parse::parser::SOURCE_FILE);
let e = p.parse_expr();
parse::update_parse_sess_position(&cx.parse_sess(), &rdr);
return e;
}

View File

@ -11,7 +11,6 @@ export parse_crate_from_source_str;
export parse_expr_from_source_str, parse_item_from_source_str;
export parse_stmt_from_source_str;
export parse_from_source_str;
export update_parse_sess_position;
use parser::Parser;
use attr::parser_attr;
@ -28,8 +27,6 @@ type parse_sess = @{
mut next_id: node_id,
span_diagnostic: span_handler,
interner: @ident_interner,
// must be kept up to date
mut pos: BytePos
};
fn new_parse_sess(demitter: Option<emitter>) -> parse_sess {
@ -38,7 +35,6 @@ fn new_parse_sess(demitter: Option<emitter>) -> parse_sess {
mut next_id: 1,
span_diagnostic: mk_span_handler(mk_handler(demitter), cm),
interner: mk_ident_interner(),
mut pos: BytePos(0)
};
}
@ -48,7 +44,6 @@ fn new_parse_sess_special_handler(sh: span_handler, cm: @codemap::CodeMap)
mut next_id: 1,
span_diagnostic: sh,
interner: mk_ident_interner(),
mut pos: BytePos(0)
};
}
@ -66,14 +61,13 @@ fn parse_crate_from_file(input: &Path, cfg: ast::crate_cfg,
fn parse_crate_from_crate_file(input: &Path, cfg: ast::crate_cfg,
sess: parse_sess) -> @ast::crate {
let (p, rdr) = new_parser_etc_from_file(sess, cfg, input,
parser::CRATE_FILE);
let p = new_parser_from_file(sess, cfg, input,
parser::CRATE_FILE);
let lo = p.span.lo;
let prefix = input.dir_path();
let leading_attrs = p.parse_inner_attrs_and_next();
let { inner: crate_attrs, next: first_cdir_attr } = leading_attrs;
let cdirs = p.parse_crate_directives(token::EOF, first_cdir_attr);
update_parse_sess_position(&sess, &rdr);
let cx = @{sess: sess, cfg: /* FIXME (#2543) */ copy p.cfg};
let companionmod = input.filestem().map(|s| Path(*s));
let (m, attrs) = eval::eval_crate_directives_to_mod(
@ -90,52 +84,47 @@ fn parse_crate_from_crate_file(input: &Path, cfg: ast::crate_cfg,
fn parse_crate_from_source_file(input: &Path, cfg: ast::crate_cfg,
sess: parse_sess) -> @ast::crate {
let (p, rdr) = new_parser_etc_from_file(sess, cfg, input,
parser::SOURCE_FILE);
let p = new_parser_from_file(sess, cfg, input,
parser::SOURCE_FILE);
let r = p.parse_crate_mod(cfg);
update_parse_sess_position(&sess, &rdr);
return r;
}
fn parse_crate_from_source_str(name: ~str, source: @~str, cfg: ast::crate_cfg,
sess: parse_sess) -> @ast::crate {
let (p, rdr) = new_parser_etc_from_source_str(sess, cfg, name,
codemap::FssNone, source);
let p = new_parser_from_source_str(sess, cfg, name,
codemap::FssNone, source);
let r = p.parse_crate_mod(cfg);
p.abort_if_errors();
update_parse_sess_position(&sess, &rdr);
return r;
}
fn parse_expr_from_source_str(name: ~str, source: @~str, cfg: ast::crate_cfg,
sess: parse_sess) -> @ast::expr {
let (p, rdr) = new_parser_etc_from_source_str(sess, cfg, name,
codemap::FssNone, source);
let p = new_parser_from_source_str(sess, cfg, name,
codemap::FssNone, source);
let r = p.parse_expr();
p.abort_if_errors();
update_parse_sess_position(&sess, &rdr);
return r;
}
fn parse_item_from_source_str(name: ~str, source: @~str, cfg: ast::crate_cfg,
+attrs: ~[ast::attribute],
sess: parse_sess) -> Option<@ast::item> {
let (p, rdr) = new_parser_etc_from_source_str(sess, cfg, name,
codemap::FssNone, source);
let p = new_parser_from_source_str(sess, cfg, name,
codemap::FssNone, source);
let r = p.parse_item(attrs);
p.abort_if_errors();
update_parse_sess_position(&sess, &rdr);
return r;
}
fn parse_stmt_from_source_str(name: ~str, source: @~str, cfg: ast::crate_cfg,
+attrs: ~[ast::attribute],
sess: parse_sess) -> @ast::stmt {
let (p, rdr) = new_parser_etc_from_source_str(sess, cfg, name,
codemap::FssNone, source);
let p = new_parser_from_source_str(sess, cfg, name,
codemap::FssNone, source);
let r = p.parse_stmt(attrs);
p.abort_if_errors();
update_parse_sess_position(&sess, &rdr);
return r;
}
@ -145,14 +134,13 @@ fn parse_from_source_str<T>(f: fn (p: Parser) -> T,
sess: parse_sess)
-> T
{
let (p, rdr) = new_parser_etc_from_source_str(sess, cfg, name, ss,
source);
let p = new_parser_from_source_str(sess, cfg, name, ss,
source);
let r = f(p);
if !p.reader.is_eof() {
p.reader.fatal(~"expected end-of-string");
}
p.abort_if_errors();
update_parse_sess_position(&sess, &rdr);
move r
}
@ -164,47 +152,28 @@ fn next_node_id(sess: parse_sess) -> node_id {
return rv;
}
fn new_parser_etc_from_source_str(sess: parse_sess, cfg: ast::crate_cfg,
+name: ~str, +ss: codemap::FileSubstr,
source: @~str) -> (Parser, string_reader) {
let ftype = parser::SOURCE_FILE;
let filemap = @FileMap::new_w_substr
(name, ss, source, sess.pos);
sess.cm.add_filemap(filemap);
let srdr = lexer::new_string_reader(sess.span_diagnostic, filemap,
sess.interner);
return (Parser(sess, cfg, srdr as reader, ftype), srdr);
}
fn new_parser_from_source_str(sess: parse_sess, cfg: ast::crate_cfg,
+name: ~str, +ss: codemap::FileSubstr,
source: @~str) -> Parser {
let (p, _) = new_parser_etc_from_source_str(sess, cfg, name, ss, source);
move p
let ftype = parser::SOURCE_FILE;
let filemap = sess.cm.new_filemap_w_substr(name, ss, source);
let srdr = lexer::new_string_reader(sess.span_diagnostic, filemap,
sess.interner);
return Parser(sess, cfg, srdr as reader, ftype);
}
fn new_parser_etc_from_file(sess: parse_sess, cfg: ast::crate_cfg,
path: &Path, ftype: parser::file_type) ->
(Parser, string_reader) {
fn new_parser_from_file(sess: parse_sess, cfg: ast::crate_cfg,
path: &Path, ftype: parser::file_type) -> Parser {
let res = io::read_whole_file_str(path);
match res {
result::Ok(_) => { /* Continue. */ }
result::Err(e) => sess.span_diagnostic.handler().fatal(e)
}
let src = @result::unwrap(res);
let filemap = @FileMap::new(path.to_str(), src,
sess.pos);
sess.cm.add_filemap(filemap);
let filemap = sess.cm.new_filemap(path.to_str(), src);
let srdr = lexer::new_string_reader(sess.span_diagnostic, filemap,
sess.interner);
return (Parser(sess, cfg, srdr as reader, ftype), srdr);
}
fn new_parser_from_file(sess: parse_sess, cfg: ast::crate_cfg, path: &Path,
ftype: parser::file_type) -> Parser {
let (p, _) = new_parser_etc_from_file(sess, cfg, path, ftype);
move p
return Parser(sess, cfg, srdr as reader, ftype);
}
fn new_parser_from_tt(sess: parse_sess, cfg: ast::crate_cfg,
@ -213,7 +182,3 @@ fn new_parser_from_tt(sess: parse_sess, cfg: ast::crate_cfg,
None, tt);
return Parser(sess, cfg, trdr as reader, parser::SOURCE_FILE)
}
fn update_parse_sess_position(sess: &parse_sess, r: &lexer::string_reader) {
sess.pos = r.last_pos
}

View File

@ -292,7 +292,8 @@ fn gather_comments_and_literals(span_diagnostic: diagnostic::span_handler,
{cmnts: ~[cmnt], lits: ~[lit]} {
let src = @str::from_bytes(srdr.read_whole_stream());
let itr = parse::token::mk_fake_ident_interner();
let filemap = @FileMap::new(path, src, BytePos(0));
let cm = CodeMap::new();
let filemap = cm.new_filemap(path, src);
let rdr = lexer::new_low_level_string_reader(
span_diagnostic, filemap, itr);

View File

@ -1,4 +1,3 @@
use parse::update_parse_sess_position;
use parser::{Parser, SOURCE_FILE};
use attr::parser_attr;
@ -63,11 +62,10 @@ fn parse_companion_mod(cx: ctx, prefix: &Path, suffix: &Option<Path>)
let modpath = &companion_file(prefix, suffix);
if file_exists(modpath) {
debug!("found companion mod");
let (p0, r0) = new_parser_etc_from_file(cx.sess, cx.cfg,
modpath, SOURCE_FILE);
let p0 = new_parser_from_file(cx.sess, cx.cfg,
modpath, SOURCE_FILE);
let inner_attrs = p0.parse_inner_attrs_and_next();
let m0 = p0.parse_mod_items(token::EOF, inner_attrs.next);
update_parse_sess_position(&cx.sess, &r0);
return (m0.view_items, m0.items, inner_attrs.inner);
} else {
return (~[], ~[], ~[]);
@ -93,9 +91,9 @@ fn eval_crate_directive(cx: ctx, cdir: @ast::crate_directive, prefix: &Path,
} else {
prefix.push_many(file_path.components)
};
let (p0, r0) =
new_parser_etc_from_file(cx.sess, cx.cfg,
&full_path, SOURCE_FILE);
let p0 =
new_parser_from_file(cx.sess, cx.cfg,
&full_path, SOURCE_FILE);
let inner_attrs = p0.parse_inner_attrs_and_next();
let mod_attrs = vec::append(attrs, inner_attrs.inner);
let first_item_outer_attrs = inner_attrs.next;
@ -104,8 +102,6 @@ fn eval_crate_directive(cx: ctx, cdir: @ast::crate_directive, prefix: &Path,
let i = p0.mk_item(cdir.span.lo, cdir.span.hi,
/* FIXME (#2543) */ copy id,
ast::item_mod(m0), vis, mod_attrs);
// Thread defids, chpos and byte_pos through the parsers
update_parse_sess_position(&cx.sess, &r0);
items.push(i);
}
ast::cdir_dir_mod(vis, id, cdirs, attrs) => {