(p *PrettyCfg)
| 430 | } |
| 431 | |
| 432 | func (node *BinaryExpr) doc(p *PrettyCfg) pretty.Doc { |
| 433 | // All the binary operators are at least left-associative. |
| 434 | // So we can always simplify "(a OP b) OP c" to "a OP b OP c". |
| 435 | parenPrio := binaryOpPrio[node.Operator] |
| 436 | leftOperand := p.peelBinaryOperand(node.Left, true /*sameLevel*/, parenPrio) |
| 437 | // If the binary operator is also fully associative, |
| 438 | // we can also simplify "a OP (b OP c)" to "a OP b OP c". |
| 439 | opFullyAssoc := binaryOpFullyAssoc[node.Operator] |
| 440 | rightOperand := p.peelBinaryOperand(node.Right, opFullyAssoc, parenPrio) |
| 441 | |
| 442 | opDoc := pretty.Text(node.Operator.String()) |
| 443 | var res pretty.Doc |
| 444 | if !node.Operator.isPadded() { |
| 445 | res = pretty.JoinDoc(opDoc, p.Doc(leftOperand), p.Doc(rightOperand)) |
| 446 | } else { |
| 447 | pred := func(e Expr, recurse func(e Expr)) bool { |
| 448 | if b, ok := e.(*BinaryExpr); ok && b.Operator == node.Operator { |
| 449 | leftSubOperand := p.peelBinaryOperand(b.Left, true /*sameLevel*/, parenPrio) |
| 450 | rightSubOperand := p.peelBinaryOperand(b.Right, opFullyAssoc, parenPrio) |
| 451 | recurse(leftSubOperand) |
| 452 | recurse(rightSubOperand) |
| 453 | return true |
| 454 | } |
| 455 | return false |
| 456 | } |
| 457 | formatOperand := func(e Expr) pretty.Doc { |
| 458 | return p.Doc(e) |
| 459 | } |
| 460 | operands := p.flattenOp(leftOperand, pred, formatOperand, nil) |
| 461 | operands = p.flattenOp(rightOperand, pred, formatOperand, operands) |
| 462 | res = pretty.JoinNestedRight( |
| 463 | opDoc, operands...) |
| 464 | } |
| 465 | return pretty.Group(res) |
| 466 | } |
| 467 | |
| 468 | func (node *ParenExpr) doc(p *PrettyCfg) pretty.Doc { |
| 469 | return p.bracket("(", p.Doc(node.Expr), ")") |
nothing calls this directly
no test coverage detected