diff --git a/src/comp/front/parser.rs b/src/comp/front/parser.rs index 80a21639338..bcf97a786e6 100644 --- a/src/comp/front/parser.rs +++ b/src/comp/front/parser.rs @@ -1119,6 +1119,7 @@ fn parse_binops(&parser p) -> @ast::expr { const int unop_prec = 100; const int as_prec = 5; +const int ternary_prec = 0; fn parse_more_binops(&parser p, @ast::expr lhs, int min_prec) -> @ast::expr { auto peeked = p.peek(); @@ -1552,6 +1553,7 @@ fn stmt_ends_with_semi(&ast::stmt stmt) -> bool { case (ast::expr_lit(_)) { true } case (ast::expr_cast(_, _)) { true } case (ast::expr_if(_, _, _)) { false } + case (ast::expr_ternary(_, _, _)) { true } case (ast::expr_for(_, _, _)) { false } case (ast::expr_for_each(_, _, _)) { false } case (ast::expr_while(_, _)) { false } diff --git a/src/comp/pretty/pprust.rs b/src/comp/pretty/pprust.rs index b9fa08fa854..00985834620 100644 --- a/src/comp/pretty/pprust.rs +++ b/src/comp/pretty/pprust.rs @@ -673,6 +673,15 @@ fn print_expr(&ps s, &@ast::expr expr) { case (ast::expr_if_check(?test, ?block, ?elseopt)) { print_if(s, test, block, elseopt, true); } + case (ast::expr_ternary(?test, ?then, ?els)) { + print_expr(s, test); + space(s.s); + word_space(s, "?"); + print_expr(s, then); + space(s.s); + word_space(s, ":"); + print_expr(s, els); + } case (ast::expr_while(?test, ?block)) { head(s, "while"); popen(s); @@ -1113,6 +1122,9 @@ fn print_maybe_parens(&ps s, &@ast::expr expr, int outer_prec) { case (ast::expr_cast(_, _)) { add_them = front::parser::as_prec < outer_prec; } + case (ast::expr_ternary(_, _, _)) { + add_them = front::parser::ternary_prec < outer_prec; + } case (_) { add_them = false; } } if (add_them) { popen(s); }