diff options
| author | Garming Sam <garming_sam@outlook.com> | 2015-08-06 19:55:27 +1200 |
|---|---|---|
| committer | Garming Sam <garming_sam@outlook.com> | 2015-08-09 21:16:21 +1200 |
| commit | c0c6af7f073620aacfb103c070e997a8d50b7201 (patch) | |
| tree | 83ad67bd244da073a54d5e6a46720d477a88c05f /src/libsyntax/print/pprust.rs | |
| parent | c67a34b9e509b76ff145526f28a20f9495c2ab61 (diff) | |
| download | rust-c0c6af7f073620aacfb103c070e997a8d50b7201.tar.gz rust-c0c6af7f073620aacfb103c070e997a8d50b7201.zip | |
Add operator precedence for pretty printer
Previously it just added parentheses in excess. e.g. ((1 + 2) + 3) + 4
Diffstat (limited to 'src/libsyntax/print/pprust.rs')
| -rw-r--r-- | src/libsyntax/print/pprust.rs | 27 |
1 files changed, 25 insertions, 2 deletions
diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs index 147314739f8..b3fd05325b1 100644 --- a/src/libsyntax/print/pprust.rs +++ b/src/libsyntax/print/pprust.rs @@ -1555,6 +1555,21 @@ impl<'a> State<'a> { self.pclose() } + pub fn check_expr_bin_needs_paren(&mut self, sub_expr: &ast::Expr, + binop: ast::BinOp) -> bool { + match sub_expr.node { + ast::ExprBinary(ref sub_op, _, _) => { + if ast_util::operator_prec(sub_op.node) < + ast_util::operator_prec(binop.node) { + true + } else { + false + } + } + _ => true + } + } + pub fn print_expr_maybe_paren(&mut self, expr: &ast::Expr) -> io::Result<()> { let needs_par = needs_parentheses(expr); if needs_par { @@ -1670,10 +1685,18 @@ impl<'a> State<'a> { op: ast::BinOp, lhs: &ast::Expr, rhs: &ast::Expr) -> io::Result<()> { - try!(self.print_expr_maybe_paren(lhs)); + if self.check_expr_bin_needs_paren(lhs, op) { + try!(self.print_expr_maybe_paren(lhs)); + } else { + try!(self.print_expr(lhs)); + } try!(space(&mut self.s)); try!(self.word_space(ast_util::binop_to_string(op.node))); - self.print_expr_maybe_paren(rhs) + if self.check_expr_bin_needs_paren(rhs, op) { + self.print_expr_maybe_paren(rhs) + } else { + self.print_expr(rhs) + } } fn print_expr_unary(&mut self, |
