From 2de1a4bb1b5acc63a280a18701cdfd14168d143e Mon Sep 17 00:00:00 2001 From: Graydon Hoare Date: Tue, 31 May 2011 12:57:56 -0700 Subject: [PATCH] Handle pretty-printing else-if properly. --- src/comp/pretty/pprust.rs | 38 ++++++++++++++++++++++++++------------ 1 file changed, 26 insertions(+), 12 deletions(-) diff --git a/src/comp/pretty/pprust.rs b/src/comp/pretty/pprust.rs index 104beee7db4..dfe6cc3a75a 100644 --- a/src/comp/pretty/pprust.rs +++ b/src/comp/pretty/pprust.rs @@ -587,28 +587,42 @@ fn print_expr(&ps s, &@ast::expr expr) { print_type(s, *ty); } case (ast::expr_if(?test,?block,?elseopt,_)) { + head(s, "if"); popen(s); print_expr(s, test); pclose(s); space(s.s); print_block(s, block); - alt (elseopt) { - case (option::some[@ast::expr](?_else)) { - // NB: we can't use 'head' here since - // it builds a block that starts in the - // wrong column. - cbox(s.s, indent_unit-1u); - ibox(s.s, 0u); - word(s.s, " else "); - alt (_else.node) { - case (ast::expr_block(?b, _)) { - print_block(s, block); + fn do_else(&ps s, option::t[@ast::expr] els) { + alt (els) { + case (option::some[@ast::expr](?_else)) { + alt (_else.node) { + // "another else-if" + case (ast::expr_if(?i,?t,?e,_)) { + cbox(s.s, indent_unit-1u); + ibox(s.s, 0u); + word(s.s, " else if "); + popen(s); + print_expr(s, i); + pclose(s); + space(s.s); + print_block(s, t); + do_else(s, e); + } + // "final else" + case (ast::expr_block(?b, _)) { + cbox(s.s, indent_unit-1u); + ibox(s.s, 0u); + word(s.s, " else "); + print_block(s, b); + } } } + case (_) { /* fall through */ } } - case (_) { /* fall through */ } } + do_else(s, elseopt); } case (ast::expr_while(?test,?block,_)) { head(s, "while");