+ bool needs_parentheses = (inner_precedence>=outer_precedence);
+
+ // Omit parentheses if the outer operator encloses this operand.
+ if(outer_oper && outer_oper->type==Operator::BINARY && outer_oper->token2[0] && on_rhs)
+ needs_parentheses = false;
+
+ if(expr.oper)
+ {
+ /* Omit parentheses if the inner expression's operator sits between the
+ expression and the outer operator. */
+ bool oper_on_left = expr.oper->type==Operator::PREFIX;
+ bool oper_on_right = expr.oper->type==Operator::POSTFIX || (expr.oper->type==Operator::BINARY && expr.oper->token2[0]);
+ if(expr.oper && ((oper_on_left && on_rhs) || (oper_on_right && !on_rhs)))
+ needs_parentheses = false;
+
+ // Omit parentheses if the operator's natural grouping works out.
+ if(expr.oper==outer_oper)
+ needs_parentheses = (expr.oper->assoc!=Operator::ASSOCIATIVE && on_rhs!=(expr.oper->assoc==Operator::RIGHT_TO_LEFT));
+ }
+
+ if(needs_parentheses)
+ append('(');
+ expr.visit(*this);
+ if(needs_parentheses)
+ append(')');