Add operator precedence for pretty printer
Previously it just added parentheses in excess. e.g. ((1 + 2) + 3) + 4
This commit is contained in:
@@ -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<()> {
|
||||
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)));
|
||||
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,
|
||||
|
||||
Reference in New Issue
Block a user