(p *PrettyCfg)
| 471 | } |
| 472 | |
| 473 | func (node *BinaryExpr) doc(p *PrettyCfg) pretty.Doc { |
| 474 | // All the binary operators are at least left-associative. |
| 475 | // So we can always simplify "(a OP b) OP c" to "a OP b OP c". |
| 476 | parenPrio := binaryOpPrio[node.Operator.Symbol] |
| 477 | leftOperand := p.peelBinaryOperand(node.Left, true /*sameLevel*/, parenPrio) |
| 478 | // If the binary operator is also fully associative, |
| 479 | // we can also simplify "a OP (b OP c)" to "a OP b OP c". |
| 480 | opFullyAssoc := binaryOpFullyAssoc[node.Operator.Symbol] |
| 481 | rightOperand := p.peelBinaryOperand(node.Right, opFullyAssoc, parenPrio) |
| 482 | |
| 483 | opDoc := pretty.Text(node.Operator.String()) |
| 484 | var res pretty.Doc |
| 485 | if !node.Operator.Symbol.IsPadded() { |
| 486 | res = pretty.JoinDoc(opDoc, p.Doc(leftOperand), p.Doc(rightOperand)) |
| 487 | } else { |
| 488 | pred := func(e Expr, recurse func(e Expr)) bool { |
| 489 | if b, ok := e.(*BinaryExpr); ok && b.Operator == node.Operator { |
| 490 | leftSubOperand := p.peelBinaryOperand(b.Left, true /*sameLevel*/, parenPrio) |
| 491 | rightSubOperand := p.peelBinaryOperand(b.Right, opFullyAssoc, parenPrio) |
| 492 | recurse(leftSubOperand) |
| 493 | recurse(rightSubOperand) |
| 494 | return true |
| 495 | } |
| 496 | return false |
| 497 | } |
| 498 | formatOperand := func(e Expr) pretty.Doc { |
| 499 | return p.Doc(e) |
| 500 | } |
| 501 | operands := p.flattenOp(leftOperand, pred, formatOperand, nil) |
| 502 | operands = p.flattenOp(rightOperand, pred, formatOperand, operands) |
| 503 | res = pretty.JoinNestedRight( |
| 504 | opDoc, operands...) |
| 505 | } |
| 506 | return pretty.Group(res) |
| 507 | } |
| 508 | |
| 509 | func (node *ParenExpr) doc(p *PrettyCfg) pretty.Doc { |
| 510 | return p.bracket("(", p.Doc(node.Expr), ")") |
nothing calls this directly
no test coverage detected