Safe Haskell | Safe-Inferred |
---|---|
Language | Haskell2010 |
Text.Fixity.Internal
Description
Precedence-general machinery for deciding whether an expression needs to be wrapped in parentheses or not. Source code has comments on the approach used and how it compares to some other known approaches.
Synopsis
- data Associativity
- data FixityOver prec = Fixity {
- _fixityAssociativity :: !Associativity
- _fixityPrecedence :: !prec
- data Direction
- data RenderContextOver prec = RenderContext {
- _renderContextDirection :: !Direction
- _renderContextFixity :: !(FixityOver prec)
- encloseIn :: Ord prec => (a -> a) -> RenderContextOver prec -> FixityOver prec -> a -> a
Documentation
data Associativity Source #
Associativity of an operator.
Constructors
LeftAssociative | |
RightAssociative | |
NonAssociative |
Instances
Show Associativity Source # | |
Defined in Text.Fixity.Internal Methods showsPrec :: Int -> Associativity -> ShowS # show :: Associativity -> String # showList :: [Associativity] -> ShowS # | |
Eq Associativity Source # | |
Defined in Text.Fixity.Internal Methods (==) :: Associativity -> Associativity -> Bool # (/=) :: Associativity -> Associativity -> Bool # |
data FixityOver prec Source #
Fixity of an operator.
We allow unary operators to have associativity, because it's useful to distinguish
between an expression like -(-x)
(unary minus, left-associative) and ~~b
(boolean NOT, right-associative).
Associativity of unary operators also matters when pretty-printing expressions like (-x) + y
,
which is pretty-printed as -x + y
, assuming unary minus has the same fixity as +
(and both
the operators are left-associative). I.e. unary minus is handled just like the binary one:
(0 - x) + y
is pretty-printed as 0 - x + y
.
Postfix operators are handled similarly. E.g. if !
is left-associative, then (x!)!
is
pretty-printed as x!!
and if it's right-associative -- (x!)!
.
The data type is parameterized, so that the user can choose precedence to be integer/fractional,
bounded/unbounded, etc (we could also allows operators to be partially or totally ordered, but
at the moment prec
is required to implement Ord
, i.e. it has to be totally ordered).
By default we go with bounded fractional precedence, see the main Text.Fixity module.
Constructors
Fixity | |
Fields
|
Instances
Show prec => Show (FixityOver prec) Source # | |
Defined in Text.Fixity.Internal Methods showsPrec :: Int -> FixityOver prec -> ShowS # show :: FixityOver prec -> String # showList :: [FixityOver prec] -> ShowS # | |
Eq prec => Eq (FixityOver prec) Source # | |
Defined in Text.Fixity.Internal Methods (==) :: FixityOver prec -> FixityOver prec -> Bool # (/=) :: FixityOver prec -> FixityOver prec -> Bool # |
Direction in which pretty-printing goes. For example in x + y
x
is pretty-printed to the
left of +
and y
is pretty-printed to the right of +
.
Constructors
ToTheLeft | |
ToTheRight |
data RenderContextOver prec Source #
A context that an expression is being rendered in.
Constructors
RenderContext | |
Fields
|
Instances
HasRenderContext RenderContext Source # | |
Defined in Text.PrettyBy.Fixity Methods | |
Show prec => Show (RenderContextOver prec) Source # | |
Defined in Text.Fixity.Internal Methods showsPrec :: Int -> RenderContextOver prec -> ShowS # show :: RenderContextOver prec -> String # showList :: [RenderContextOver prec] -> ShowS # | |
Eq prec => Eq (RenderContextOver prec) Source # | |
Defined in Text.Fixity.Internal Methods (==) :: RenderContextOver prec -> RenderContextOver prec -> Bool # (/=) :: RenderContextOver prec -> RenderContextOver prec -> Bool # |
Arguments
:: Ord prec | |
=> (a -> a) | Enclose a value of type |
-> RenderContextOver prec | An outer context. |
-> FixityOver prec | An inner fixity. |
-> a | |
-> a |
Enclose an a
(using the provided function) if required or leave it as is.
The need for enclosing is determined from an outer RenderContext
and the inner fixity.