ormolu-0.5.3.0: A formatter for Haskell source code

Safe Haskell | Safe-Inferred
Language | Haskell2010

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 [OpTree ty op] [op]
- data OpInfo op = OpInfo {
  - opiOp :: op
  - opiName :: Maybe OpName
  - opiFix :: FixityInfo
  }
- opTreeLoc :: HasSrcSpan l => OpTree (GenLocated l a) b -> SrcSpan
- reassociateOpTree :: (op -> Maybe RdrName) -> FixityMap -> LazyFixityMap -> OpTree ty op -> OpTree ty (OpInfo op)
- isHardSplitterOp :: FixityInfo -> Bool

# Documentation

data OpTree ty op Source #

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

data OpInfo op Source #

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

Constructors

OpInfo |

Fields

- opiOp :: op

  The actual operator

- opiName :: Maybe OpName

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

- opiFix :: FixityInfo

  Information about the fixity direction and precedence level of the operator

#### Instances

Instances details

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

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

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

reassociateOpTree Source #

Arguments

:: (op -> Maybe RdrName) | How to get name of an operator
-> FixityMap | Fixity overrides
-> LazyFixityMap | 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 :: FixityInfo -> Bool Source #

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.