fourmolu-0.15.0.0: A formatter for Haskell source code

Safe Haskell | None
Language | GHC2021

Ormolu.Printer.Operators

Description

This module helps handle operator chains composed of different operators that may have different precedence and fixities.

Synopsis

- data OpTree ty op
  - = OpNode ty
  - | OpBranches (NonEmpty (OpTree ty op)) [op]
- pattern BinaryOpBranches :: OpTree ty op -> op -> OpTree ty op -> OpTree ty op
- data OpInfo op = OpInfo {
  - opiOp :: op
  - opiName :: Maybe RdrName
  - opiFixityApproximation :: FixityApproximation
  }
- opTreeLoc :: HasSrcSpan l => OpTree (GenLocated l a) b -> SrcSpan
- reassociateOpTree :: Bool -> (op -> Maybe RdrName) -> ModuleFixityMap -> OpTree ty op -> OpTree ty (OpInfo op)
- isHardSplitterOp :: FixityApproximation -> Bool

# Documentation

data OpTree ty op #

Intermediate representation of operator trees, where a branching is not just a binary branching (with a left node, right node, and operator like in the GHC's AST), but rather a n-ary branching, with n + 1 nodes and n operators (n >= 1).

This representation allows us to put all the operators with the same precedence level as direct siblings in this tree, to better represent the idea of a chain of operators.

Constructors

OpNode ty | A node which is not an operator application
OpBranches (NonEmpty (OpTree ty op)) [op] | A subtree of operator application(s); the invariant is: `length exprs == length ops + 1`. `OpBranches [x, y, z] [op1, op2]` represents the expression `x op1 y op2 z`.

#### Instances

Instances details

(Show ty, Show op) => Show (OpTree ty op) # |
Instance details Defined in Ormolu.Printer.Operators Methods showsPrec :: Int -> OpTree ty op -> ShowS # show :: OpTree ty op -> String # showList :: [OpTree ty op] -> ShowS #
(Eq ty, Eq op) => Eq (OpTree ty op) # |
Instance details Defined in Ormolu.Printer.Operators Methods (==) :: OpTree ty op -> OpTree ty op -> Bool # (/=) :: OpTree ty op -> OpTree ty op -> Bool #

pattern BinaryOpBranches :: OpTree ty op -> op -> OpTree ty op -> OpTree ty op #

data OpInfo op #

Wrapper for an operator, carrying information about its name and fixity.

Constructors

OpInfo |

Fields

- opiOp :: op

  The actual operator

- opiName :: Maybe RdrName

  Its name, if available. We use 'Maybe RdrName' here instead of `RdrName` because the name-fetching function received by `reassociateOpTree` returns a `Maybe`

- opiFixityApproximation :: FixityApproximation

  Information about the fixity direction and precedence level of the operator

#### Instances

Instances details

Eq op => Eq (OpInfo op) # |
Instance details Defined in Ormolu.Printer.Operators Methods (==) :: OpInfo op -> OpInfo op -> Bool # (/=) :: OpInfo op -> OpInfo op -> Bool #

opTreeLoc :: HasSrcSpan l => OpTree (GenLocated l a) b -> SrcSpan #

Return combined `SrcSpan`s of all elements in this `OpTree`.

reassociateOpTree #

Arguments

:: Bool | Whether to print debug info regarding fixity inference
-> (op -> Maybe RdrName) | How to get name of an operator
-> ModuleFixityMap | Fixity Map
-> OpTree ty op | Original `OpTree`
-> OpTree ty (OpInfo op) | Re-associated `OpTree`, with added context and info around operators

Re-associate an `OpTree` taking into account precedence of operators. Users are expected to first construct an initial `OpTree`, then re-associate it using this function before printing.

isHardSplitterOp :: FixityApproximation -> Bool #

Indicate if an operator has ``InfixR` 0` fixity. We special-case this class of operators because they often have, like (`$`), a specific “separator” use-case, and we sometimes format them differently than other operators.