Convert pretty-printer to istrs. Issue #855

This commit is contained in:
Brian Anderson 2011-08-27 01:16:40 -07:00
parent 675073c266
commit cbad23a747
18 changed files with 395 additions and 370 deletions

View file

@ -347,7 +347,7 @@ fn build_link_meta(sess: &session::session, c: &ast::crate, output: &istr,
}
fn len_and_str_lit(l: &ast::lit) -> istr {
ret len_and_str(istr::from_estr(pprust::lit_to_str(@l)));
ret len_and_str(pprust::lit_to_str(@l));
}
let cmh_items = attr::sort_meta_items(metas.cmh_items);

View file

@ -185,11 +185,11 @@ fn pretty_print_input(sess: session::session, cfg: ast::crate_cfg, input: str,
alt node {
pprust::node_expr(s, expr) {
pp::space(s.s);
pp::word(s.s, "as");
pp::word(s.s, ~"as");
pp::space(s.s);
pp::word(
s.s,
istr::to_estr(ppaux::ty_to_str(tcx, ty::expr_ty(tcx, expr))));
ppaux::ty_to_str(tcx, ty::expr_ty(tcx, expr)));
pprust::pclose(s);
}
_ { }
@ -200,17 +200,17 @@ fn pretty_print_input(sess: session::session, cfg: ast::crate_cfg, input: str,
pprust::node_item(s, item) {
pp::space(s.s);
pprust::synth_comment(
s, istr::to_estr(int::to_str(item.id, 10u)));
s, int::to_str(item.id, 10u));
}
pprust::node_block(s, blk) {
pp::space(s.s);
pprust::synth_comment(
s, istr::to_estr(~"block " + int::to_str(blk.node.id, 10u)));
s, ~"block " + int::to_str(blk.node.id, 10u));
}
pprust::node_expr(s, expr) {
pp::space(s.s);
pprust::synth_comment(
s, istr::to_estr(int::to_str(expr.id, 10u)));
s, int::to_str(expr.id, 10u));
pprust::pclose(s);
}
_ { }
@ -246,7 +246,8 @@ fn pretty_print_input(sess: session::session, cfg: ast::crate_cfg, input: str,
}
ppm_normal. { ann = pprust::no_ann(); }
}
pprust::print_crate(sess.get_codemap(), crate, input,
pprust::print_crate(sess.get_codemap(), crate,
istr::from_estr(input),
io::string_reader(istr::from_estr(src)),
io::stdout(), ann);
}

View file

@ -125,10 +125,12 @@ fn eq(a: @ast::meta_item, b: @ast::meta_item) -> bool {
fn contains(haystack: &[@ast::meta_item], needle: @ast::meta_item) -> bool {
log #fmt["looking for %s",
syntax::print::pprust::meta_item_to_str(*needle)];
istr::to_estr(
syntax::print::pprust::meta_item_to_str(*needle))];
for item: @ast::meta_item in haystack {
log #fmt["looking in %s",
syntax::print::pprust::meta_item_to_str(*item)];
istr::to_estr(
syntax::print::pprust::meta_item_to_str(*item))];
if eq(item, needle) { log "found it!"; ret true; }
}
log "found it not :(";

View file

@ -167,7 +167,8 @@ fn mk_test_module(cx: &test_ctxt) -> @ast::item {
node: item_,
span: dummy_sp()};
log #fmt["Synthetic test module:\n%s\n", pprust::item_to_str(@item)];
log #fmt["Synthetic test module:\n%s\n",
istr::to_estr(pprust::item_to_str(@item))];
ret @item;
}

View file

@ -105,7 +105,8 @@ fn metadata_matches(crate_data: &@[u8], metas: &[@ast::meta_item]) -> bool {
for needed: @ast::meta_item in metas {
if !attr::contains(linkage_metas, needed) {
log #fmt["missing %s", pprust::meta_item_to_str(*needed)];
log #fmt["missing %s",
istr::to_estr(pprust::meta_item_to_str(*needed))];
ret false;
}
}

View file

@ -349,7 +349,9 @@ fn get_attributes(md: &ebml::doc) -> [ast::attribute] {
fn list_meta_items(meta_items: &ebml::doc, out: io::writer) {
for mi: @ast::meta_item in get_meta_items(meta_items) {
out.write_str(
istr::from_estr(#fmt["%s\n", pprust::meta_item_to_str(*mi)]));
istr::from_estr(
#fmt["%s\n",
istr::to_estr(pprust::meta_item_to_str(*mi))]));
}
}
@ -358,7 +360,9 @@ fn list_crate_attributes(md: &ebml::doc, out: io::writer) {
for attr: ast::attribute in get_attributes(md) {
out.write_str(
istr::from_estr(#fmt["%s\n", pprust::attribute_to_str(attr)]));
istr::from_estr(
#fmt["%s\n",
istr::to_estr(pprust::attribute_to_str(attr))]));
}
out.write_str(~"\n\n");

View file

@ -235,7 +235,7 @@ fn enc_ty_fn(w: &io::writer, cx: &@ctxt, args: &[ty::arg], out: ty::t,
// FIXME less copy-and-paste
fn enc_constr(w: &io::writer, cx: &@ctxt, c: &@ty::constr) {
w.write_str(istr::from_estr(path_to_str(c.node.path)));
w.write_str(path_to_str(c.node.path));
w.write_char('(');
w.write_str(cx.ds(c.node.id));
w.write_char('|');
@ -246,7 +246,7 @@ fn enc_constr(w: &io::writer, cx: &@ctxt, c: &@ty::constr) {
carg_base. { w.write_char('*'); }
carg_ident(i) { w.write_uint(i); }
carg_lit(l) {
w.write_str(istr::from_estr(lit_to_str(l)));
w.write_str(lit_to_str(l));
}
}
}
@ -254,7 +254,7 @@ fn enc_constr(w: &io::writer, cx: &@ctxt, c: &@ty::constr) {
}
fn enc_ty_constr(w: &io::writer, cx: &@ctxt, c: &@ty::type_constr) {
w.write_str(istr::from_estr(path_to_str(c.node.path)));
w.write_str(path_to_str(c.node.path));
w.write_char('(');
w.write_str(cx.ds(c.node.id));
w.write_char('|');
@ -264,9 +264,9 @@ fn enc_ty_constr(w: &io::writer, cx: &@ctxt, c: &@ty::type_constr) {
alt a.node {
carg_base. { w.write_char('*'); }
carg_ident(p) {
w.write_str(istr::from_estr(path_to_str(p))); }
w.write_str(path_to_str(p)); }
carg_lit(l) {
w.write_str(istr::from_estr(lit_to_str(l))); }
w.write_str(lit_to_str(l)); }
}
}
w.write_char(')');

View file

@ -451,7 +451,7 @@ fn resolve_constr(e: @env, c: &@ast::constr, sc: &scopes, _v: &vt<scopes>) {
_ {
e.sess.span_err(c.span,
"Non-predicate in constraint: " +
path_to_str(c.node.path));
istr::to_estr(path_to_str(c.node.path)));
}
}
}

View file

@ -4602,7 +4602,7 @@ fn trans_log(lvl: int, cx: &@block_ctxt, e: &@ast::expr) -> result {
fn trans_check_expr(cx: &@block_ctxt, e: &@ast::expr, s: &istr) -> result {
let cond_res = trans_expr(cx, e);
let expr_str = s + ~" " + istr::from_estr(expr_to_str(e)) + ~" failed";
let expr_str = s + ~" " + expr_to_str(e) + ~" failed";
let fail_cx = new_sub_block_ctxt(cx, ~"fail");
trans_fail(fail_cx, some::<span>(e.span), expr_str);
let next_cx = new_sub_block_ctxt(cx, ~"next");

View file

@ -68,7 +68,7 @@ fn comma_str(args: &[@constr_arg_use]) -> istr {
alt a.node {
carg_base. { rslt += ~"*"; }
carg_ident(i) { rslt += i.ident; }
carg_lit(l) { rslt += istr::from_estr(lit_to_str(l)); }
carg_lit(l) { rslt += lit_to_str(l); }
}
}
ret rslt;
@ -81,7 +81,7 @@ fn constraint_to_str(tcx: &ty::ctxt, c: &sp_constr) -> istr {
istr::from_estr(tcx.sess.span_str(c.span)) + ~"])";
}
npred(p, _, args) {
ret istr::from_estr(path_to_str(p)) + ~"(" +
ret path_to_str(p) + ~"(" +
comma_str(args) + ~")" + ~"[" +
istr::from_estr(tcx.sess.span_str(c.span)) + ~"]";
}
@ -593,9 +593,9 @@ fn constraints(fcx: &fn_ctxt) -> [norm_constraint] {
// should freeze it at some earlier point.
fn match_args(fcx: &fn_ctxt, occs: &@mutable [pred_args],
occ: &[@constr_arg_use]) -> uint {
log "match_args: looking at " +
constr_args_to_str(fn (i: &inst) -> str {
ret istr::to_estr(i.ident);
log ~"match_args: looking at " +
constr_args_to_str(fn (i: &inst) -> istr {
ret i.ident;
}, occ);
for pd: pred_args in *occs {
log ~"match_args: candidate " + pred_args_to_str(pd);
@ -687,11 +687,11 @@ fn expr_to_constr(tcx: ty::ctxt, e: &@expr) -> sp_constr {
}
fn pred_args_to_str(p: &pred_args) -> istr {
istr::from_estr("<" + istr::to_estr(uint::str(p.node.bit_num)) + ", " +
constr_args_to_str(fn (i: &inst) -> str {
ret istr::to_estr(i.ident);
~"<" + uint::str(p.node.bit_num) + ~", " +
constr_args_to_str(fn (i: &inst) -> istr {
ret i.ident;
}, p.node.args)
+ ">")
+ ~">"
}
fn substitute_constr_args(cx: &ty::ctxt, actuals: &[@expr], c: &@ty::constr)

View file

@ -87,7 +87,7 @@ fn check_states_expr(e: &@expr, fcx: &fn_ctxt, v: &visit::vt<fn_ctxt>) {
s +=
~"Unsatisfied precondition constraint (for example, " + diff +
~") for expression:\n";
s += istr::from_estr(syntax::print::pprust::expr_to_str(e));
s += syntax::print::pprust::expr_to_str(e);
s += ~"\nPrecondition:\n";
s += tritv_to_str(fcx, prec);
s += ~"\nPrestate:\n";
@ -119,7 +119,7 @@ fn check_states_stmt(s: &@stmt, fcx: &fn_ctxt, v: &visit::vt<fn_ctxt>) {
ss +=
~"Unsatisfied precondition constraint (for example, " + diff +
~") for statement:\n";
ss += istr::from_estr(syntax::print::pprust::stmt_to_str(*s));
ss += syntax::print::pprust::stmt_to_str(*s);
ss += ~"\nPrecondition:\n";
ss += tritv_to_str(fcx, prec);
ss += ~"\nPrestate: \n";
@ -154,9 +154,10 @@ fn check_states_against_conditions(fcx: &fn_ctxt, f: &_fn,
istr::to_estr(fcx.name) +
", not all control paths \
return a value");
fcx.ccx.tcx.sess.span_fatal(f.decl.output.span,
fcx.ccx.tcx.sess.span_fatal(
f.decl.output.span,
"see declared return type of '" +
ty_to_str(f.decl.output) + "'");
istr::to_estr(ty_to_str(f.decl.output)) + "'");
} else if f.decl.cf == noreturn {
// check that this really always fails

View file

@ -2900,7 +2900,8 @@ fn ast_constr_to_constr<T>(tcx: ty::ctxt, c: &@ast::constr_general<T>) ->
}
_ {
tcx.sess.span_fatal(c.span,
"Predicate " + path_to_str(c.node.path) +
"Predicate " +
istr::to_estr(path_to_str(c.node.path)) +
" is unbound or bound to a non-function or an \
impure function");
}

View file

@ -315,7 +315,7 @@ fn ast_ty_to_ty(tcx: &ty::ctxt, getter: &ty_getter, ast_ty: &@ast::ty) ->
ret typ;
}
let typ;
let cname = none::<str>;
let cname = none::<istr>;
alt ast_ty.node {
ast::ty_nil. { typ = ty::mk_nil(tcx); }
ast::ty_bot. { typ = ty::mk_bot(tcx); }
@ -415,7 +415,9 @@ fn ast_ty_to_ty(tcx: &ty::ctxt, getter: &ty_getter, ast_ty: &@ast::ty) ->
}
alt cname {
none. {/* no-op */ }
some(cname_str) { typ = ty::rename(tcx, typ, cname_str); }
some(cname_str) {
typ = ty::rename(tcx, typ, istr::to_estr(cname_str));
}
}
tcx.ast_ty_to_ty_cache.insert(ast_ty, some(typ));
ret typ;
@ -2402,7 +2404,8 @@ fn check_expr_with_unifier(fcx: &@fn_ctxt, expr: &@ast::expr, unify: &unifier,
// The user is trying to extend a non-object.
tcx.sess.span_fatal(
e.span,
syntax::print::pprust::expr_to_str(e)
istr::to_estr(
syntax::print::pprust::expr_to_str(e))
+
" does not have object type");
}

View file

@ -8,7 +8,7 @@ fn expand_syntax_ext(cx: &ext_ctxt, sp: codemap::span, arg: @ast::expr,
cx.print_backtrace();
std::io::stdout().write_line(
istr::from_estr(print::pprust::expr_to_str(arg)));
print::pprust::expr_to_str(arg));
//trivial expression
ret @{id: cx.next_id(), node: ast::expr_rec([], option::none), span: sp};

View file

@ -62,33 +62,37 @@ type break_t = {offset: int, blank_space: int};
type begin_t = {offset: int, breaks: breaks};
tag token { STRING(str, int); BREAK(break_t); BEGIN(begin_t); END; EOF; }
tag token { STRING(istr, int); BREAK(break_t); BEGIN(begin_t); END; EOF; }
fn tok_str(t: token) -> str {
fn tok_str(t: token) -> istr {
alt t {
STRING(s, len) { ret #fmt["STR(%s,%d)", s, len]; }
BREAK(_) { ret "BREAK"; }
BEGIN(_) { ret "BEGIN"; }
END. { ret "END"; }
EOF. { ret "EOF"; }
STRING(s, len) {
ret istr::from_estr(
#fmt["STR(%s,%d)", istr::to_estr(s), len]);
}
BREAK(_) { ret ~"BREAK"; }
BEGIN(_) { ret ~"BEGIN"; }
END. { ret ~"END"; }
EOF. { ret ~"EOF"; }
}
}
fn buf_str(toks: &[mutable token], szs: &[mutable int], left: uint,
right: uint, lim: uint) -> str {
right: uint, lim: uint) -> istr {
let n = vec::len(toks);
assert (n == vec::len(szs));
let i = left;
let L = lim;
let s = "[";
let s = ~"[";
while i != right && L != 0u {
L -= 1u;
if i != left { s += ", "; }
s += #fmt["%d=%s", szs[i], tok_str(toks[i])];
if i != left { s += ~", "; }
s += istr::from_estr(
#fmt["%d=%s", szs[i], istr::to_estr(tok_str(toks[i]))]);
i += 1u;
i %= n;
}
s += "]";
s += ~"]";
ret s;
}
@ -405,15 +409,16 @@ obj printer(out: io::writer,
if n != 0u { top = print_stack[n - 1u]; }
ret top;
}
fn write_str(s: str) {
fn write_str(s: &istr) {
while pending_indentation > 0 {
out.write_str(~" ");
pending_indentation -= 1;
}
out.write_str(istr::from_estr(s));
out.write_str(s);
}
fn print(x: token, L: int) {
log #fmt["print %s %d (remaining line space=%d)", tok_str(x), L,
log #fmt["print %s %d (remaining line space=%d)",
istr::to_estr(tok_str(x)), L,
space];
log buf_str(token, size, left, right, 6u);
alt x {
@ -493,15 +498,15 @@ fn end(p: printer) { p.pretty_print(END); }
fn eof(p: printer) { p.pretty_print(EOF); }
fn word(p: printer, wrd: str) {
p.pretty_print(STRING(wrd, str::char_len(wrd) as int));
fn word(p: printer, wrd: &istr) {
p.pretty_print(STRING(wrd, istr::char_len(wrd) as int));
}
fn huge_word(p: printer, wrd: str) {
fn huge_word(p: printer, wrd: &istr) {
p.pretty_print(STRING(wrd, size_infinity));
}
fn zero_word(p: printer, wrd: str) { p.pretty_print(STRING(wrd, 0)); }
fn zero_word(p: printer, wrd: &istr) { p.pretty_print(STRING(wrd, 0)); }
fn spaces(p: printer, n: uint) { break_offset(p, n, 0); }

File diff suppressed because it is too large Load diff

View file

@ -62,7 +62,7 @@ fn ty_to_str(cx: &ctxt, typ: &t) -> istr {
fn fn_to_str(cx: &ctxt, proto: ast::proto, ident: option::t<ast::ident>,
inputs: &[arg], output: t, cf: ast::controlflow,
constrs: &[@constr]) -> istr {
let s = istr::from_estr(proto_to_str(proto));
let s = proto_to_str(proto);
alt ident {
some(i) {
s += ~" ";
@ -169,9 +169,8 @@ fn ty_to_short_str(cx: &ctxt, typ: t) -> istr {
}
fn constr_to_str(c: &@constr) -> istr {
ret istr::from_estr(path_to_str(c.node.path)) +
istr::from_estr(
pprust::constr_args_to_str(pprust::uint_to_str, c.node.args));
ret path_to_str(c.node.path) +
pprust::constr_args_to_str(pprust::uint_to_str, c.node.args);
}
fn constrs_str(constrs: &[@constr]) -> istr {
@ -186,9 +185,8 @@ fn constrs_str(constrs: &[@constr]) -> istr {
fn ty_constr_to_str<Q>(c: &@ast::spanned<ast::constr_general_<ast::path, Q>>)
-> istr {
ret istr::from_estr(path_to_str(c.node.path)) +
istr::from_estr(
constr_args_to_str::<ast::path>(path_to_str, c.node.args));
ret path_to_str(c.node.path) +
constr_args_to_str::<ast::path>(path_to_str, c.node.args);
}
// Local Variables:

View file

@ -163,14 +163,15 @@ fn check_variants_of_ast(crate: &ast::crate, codemap: &codemap::codemap,
let exprsL = vec::len(exprs);
if exprsL < 100u {
for each i: uint in under(uint::min(exprsL, 20u)) {
log_err "Replacing... " + pprust::expr_to_str(@exprs[i]);
log_err ~"Replacing... " + pprust::expr_to_str(@exprs[i]);
for each j: uint in under(uint::min(exprsL, 5u)) {
log_err "With... " + pprust::expr_to_str(@exprs[j]);
log_err ~"With... " + pprust::expr_to_str(@exprs[j]);
let crate2 = @replace_expr_in_crate(crate, i, exprs[j].node);
// It would be best to test the *crate* for stability, but testing the
// string for stability is easier and ok for now.
let str3 =
as_str(bind pprust::print_crate(codemap, crate2, filename,
as_str(bind pprust::print_crate(codemap, crate2,
istr::from_estr(filename),
io::string_reader(~""), _,
pprust::no_ann()));
// 1u would be sane here, but the pretty-printer currently has lots of whitespace and paren issues,
@ -254,7 +255,8 @@ fn parse_and_print(code: &str) -> str {
//write_file(filename, code);
let crate = parser::parse_crate_from_source_str(
istr::from_estr(filename), istr::from_estr(code), [], sess);
ret as_str(bind pprust::print_crate(sess.cm, crate, filename,
ret as_str(bind pprust::print_crate(sess.cm, crate,
istr::from_estr(filename),
io::string_reader(istr::from_estr(code)), _,
pprust::no_ann()));
}
@ -364,7 +366,8 @@ fn check_variants(files: &[str]) {
parser::parse_crate_from_source_str(
istr::from_estr(file),
istr::from_estr(s), [], sess);
log_err as_str(bind pprust::print_crate(sess.cm, crate, file,
log_err as_str(bind pprust::print_crate(sess.cm, crate,
istr::from_estr(file),
io::string_reader(istr::from_estr(s)), _,
pprust::no_ann()));
check_variants_of_ast(*crate, sess.cm, file);