(this: &pr::Expr, opt: &WriteOpt)
| 49 | } |
| 50 | |
| 51 | fn needs_parenthesis(this: &pr::Expr, opt: &WriteOpt) -> bool { |
| 52 | if opt.unbound_expr && can_bind_left(&this.kind) { |
| 53 | return true; |
| 54 | } |
| 55 | |
| 56 | let binding_strength = binding_strength(&this.kind); |
| 57 | if opt.context_strength > binding_strength { |
| 58 | // parent has higher binding strength, which means it would "steal" operand of this expr |
| 59 | // => parenthesis are needed |
| 60 | return true; |
| 61 | } |
| 62 | |
| 63 | if opt.context_strength < binding_strength { |
| 64 | // parent has lower binding strength, so it won't "steal" operand of this expr |
| 65 | // => no parenthesis needed |
| 66 | return false; |
| 67 | } |
| 68 | |
| 69 | // parent has equal binding strength, which means that now associativity of this expr counts |
| 70 | // for example: |
| 71 | // this=(a + b), parent=(a + b) + c |
| 72 | // asoc of + is left |
| 73 | // this is the left operand of parent |
| 74 | // => assoc_matches=true => we don't need parenthesis |
| 75 | |
| 76 | // this=(a + b), parent=c + (a + b) |
| 77 | // asoc of + is left |
| 78 | // this is the right operand of parent |
| 79 | // => assoc_matches=false => we need parenthesis |
| 80 | let assoc_matches = match opt.binary_position { |
| 81 | super::Position::Left => associativity(&this.kind) == super::Position::Left, |
| 82 | super::Position::Right => associativity(&this.kind) == super::Position::Right, |
| 83 | super::Position::Unspecified => false, |
| 84 | }; |
| 85 | |
| 86 | !assoc_matches |
| 87 | } |
| 88 | |
| 89 | impl WriteSource for pr::ExprKind { |
| 90 | fn write(&self, mut opt: WriteOpt) -> Option<String> { |
no test coverage detected