packages feed

hfusion-0.0.5: HFusion/Internal/HsPrec.hs

-- Please, see the file LICENSE for copyright and license information.

module HFusion.Internal.HsPrec(
    Precedence,
    LeftParam,
    hpar,      -- :: Bool -> Doc -> Doc
    parenthesize, -- ::Precedence -> LeftParam -> Precedence -> Bool
    Asoc(LeftAsoc,RightAsoc,None)
) where

import Text.PrettyPrint

type Precedence = (PrecValue,Asoc) -- Characterizes an operator.
type LeftParam = Bool   

type PrecValue = Int
data Asoc = LeftAsoc | RightAsoc | None
           deriving (Eq,Show)

-- Inserts parethesis according to the value of the first parameter,
hpar:: Bool -> Doc -> Doc
hpar paren d = if paren then char '(' <> d <> char ')'
                        else d

-- | Decides if a subexpression must be parenthesized. 
parenthesize :: Precedence -- ^ Precendence of the containing operator
                -> LeftParam -- ^ Tells if the contained expression is left or right of the containing operator
				-> Precedence -- ^ Precedence of the contained operator
				-> Bool
parenthesize (p0,a0) left (p1,_) = p0>p1||
                                   p0==p1 && (if left then a0==RightAsoc
                                                else a0==LeftAsoc)