Skip to content

Commit 3aa8d7f

Browse files
committed
rustc: Pretty-print ternary operator
1 parent b9fc4df commit 3aa8d7f

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

src/comp/front/parser.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1119,6 +1119,7 @@ fn parse_binops(&parser p) -> @ast::expr {
11191119
const int unop_prec = 100;
11201120

11211121
const int as_prec = 5;
1122+
const int ternary_prec = 0;
11221123

11231124
fn parse_more_binops(&parser p, @ast::expr lhs, int min_prec) -> @ast::expr {
11241125
auto peeked = p.peek();
@@ -1552,6 +1553,7 @@ fn stmt_ends_with_semi(&ast::stmt stmt) -> bool {
15521553
case (ast::expr_lit(_)) { true }
15531554
case (ast::expr_cast(_, _)) { true }
15541555
case (ast::expr_if(_, _, _)) { false }
1556+
case (ast::expr_ternary(_, _, _)) { true }
15551557
case (ast::expr_for(_, _, _)) { false }
15561558
case (ast::expr_for_each(_, _, _)) { false }
15571559
case (ast::expr_while(_, _)) { false }

src/comp/pretty/pprust.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -673,6 +673,15 @@ fn print_expr(&ps s, &@ast::expr expr) {
673673
case (ast::expr_if_check(?test, ?block, ?elseopt)) {
674674
print_if(s, test, block, elseopt, true);
675675
}
676+
case (ast::expr_ternary(?test, ?then, ?els)) {
677+
print_expr(s, test);
678+
space(s.s);
679+
word_space(s, "?");
680+
print_expr(s, then);
681+
space(s.s);
682+
word_space(s, ":");
683+
print_expr(s, els);
684+
}
676685
case (ast::expr_while(?test, ?block)) {
677686
head(s, "while");
678687
popen(s);
@@ -1113,6 +1122,9 @@ fn print_maybe_parens(&ps s, &@ast::expr expr, int outer_prec) {
11131122
case (ast::expr_cast(_, _)) {
11141123
add_them = front::parser::as_prec < outer_prec;
11151124
}
1125+
case (ast::expr_ternary(_, _, _)) {
1126+
add_them = front::parser::ternary_prec < outer_prec;
1127+
}
11161128
case (_) { add_them = false; }
11171129
}
11181130
if (add_them) { popen(s); }

0 commit comments

Comments
 (0)