Be more careful about pretty-printing literals

Before, literal printing would basically get derailed completely when
a literal was encountered that did not end up being printed. This
caused the strangeness seen in #1532.

Also cleans up pretty-printing of discriminants a little.

Closes #1510
Closes #1532
This commit is contained in:
Marijn Haverbeke 2012-01-16 10:50:34 +01:00
parent 2d36a71aee
commit 96f1eda6d0

View File

@ -419,6 +419,7 @@ fn print_item(s: ps, &&item: @ast::item) {
for v: ast::variant in variants { for v: ast::variant in variants {
space_if_not_bol(s); space_if_not_bol(s);
maybe_print_comment(s, v.span.lo); maybe_print_comment(s, v.span.lo);
ibox(s, indent_unit);
word(s.s, v.node.name); word(s.s, v.node.name);
if vec::len(v.node.args) > 0u { if vec::len(v.node.args) > 0u {
popen(s); popen(s);
@ -429,14 +430,15 @@ fn print_item(s: ps, &&item: @ast::item) {
pclose(s); pclose(s);
} }
alt v.node.disr_expr { alt v.node.disr_expr {
some(expr) { some(d) {
nbsp(s); space(s.s);
word_nbsp(s, "="); word_space(s, "=");
print_expr(s, expr); print_expr(s, d);
} }
_ {} _ {}
} }
word(s.s, ";"); word(s.s, ";");
end(s);
maybe_print_trailing_comment(s, v.span, none::<uint>); maybe_print_trailing_comment(s, v.span, none::<uint>);
} }
bclose(s, item.span); bclose(s, item.span);
@ -1414,11 +1416,12 @@ fn in_cbox(s: ps) -> bool {
fn print_literal(s: ps, &&lit: @ast::lit) { fn print_literal(s: ps, &&lit: @ast::lit) {
maybe_print_comment(s, lit.span.lo); maybe_print_comment(s, lit.span.lo);
alt next_lit(s) { alt next_lit(s, lit.span.lo) {
some(lt) { some(lt) {
if lt.pos == lit.span.lo { word(s.s, lt.lit); s.cur_lit += 1u; ret; } word(s.s, lt.lit);
ret;
} }
_ { } _ {}
} }
alt lit.node { alt lit.node {
ast::lit_str(st) { print_string(s, st); } ast::lit_str(st) { print_string(s, st); }
@ -1443,14 +1446,18 @@ fn print_literal(s: ps, &&lit: @ast::lit) {
fn lit_to_str(l: @ast::lit) -> str { be to_str(l, print_literal); } fn lit_to_str(l: @ast::lit) -> str { be to_str(l, print_literal); }
fn next_lit(s: ps) -> option::t<lexer::lit> { fn next_lit(s: ps, pos: uint) -> option::t<lexer::lit> {
alt s.literals { alt s.literals {
some(lits) { some(lits) {
if s.cur_lit < vec::len(lits) { while s.cur_lit < vec::len(lits) {
ret some(lits[s.cur_lit]); let lt = lits[s.cur_lit];
} else { ret none::<lexer::lit>; } if lt.pos > pos { ret none; }
s.cur_lit += 1u;
if lt.pos == pos { ret some(lt); }
}
ret none;
} }
_ { ret none::<lexer::lit>; } _ { ret none; }
} }
} }