packages feed

hfusion-0.0.4: HFusion/Internal/HsPrec.hs

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

module HFusion.Internal.HsPrec(
    Precedence,
    LeftParam,
    hpar,      -- :: Bool -> Doc -> Doc
    parentizar, -- ::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 an expression E2, with outermost operador O2 having 
-- precedence given in the third parameter, needs parenthesis
-- being the first parameter the precedence of operador O1 which
-- contains E2 as one of its arguments, and the second parameter
-- tells if E2 is left of O1.
-- parentizar (1,LeftAsoc) False (1,_) -> True
parentizar::Precedence -> LeftParam -> Precedence -> Bool
parentizar (p0,a0) left (p1,_) = (p0>p1)||
                                 (p0==p1)&&(if left then (a0==RightAsoc)
                                                    else (a0==LeftAsoc))