module Language.Haskell.Exts.SrcLoc where
#ifdef __GLASGOW_HASKELL__
#ifdef BASE4
import Data.Data
#else
import Data.Generics (Data(..),Typeable(..))
#endif
#endif
data SrcLoc = SrcLoc
{ srcFilename :: String
, srcLine :: Int
, srcColumn :: Int
}
#ifdef __GLASGOW_HASKELL__
deriving (Eq,Ord,Show,Typeable,Data)
#else
deriving (Eq,Ord,Show)
#endif
noLoc :: SrcLoc
noLoc = SrcLoc "" (1) (1)
data SrcSpan = SrcSpan
{ srcSpanFilename :: String
, srcSpanStartLine :: Int
, srcSpanStartColumn :: Int
, srcSpanEndLine :: Int
, srcSpanEndColumn :: Int
}
#ifdef __GLASGOW_HASKELL__
deriving (Eq,Ord,Show,Typeable,Data)
#else
deriving (Eq,Ord,Show)
#endif
srcSpanStart :: SrcSpan -> (Int,Int)
srcSpanStart x = (srcSpanStartLine x, srcSpanStartColumn x)
srcSpanEnd :: SrcSpan -> (Int,Int)
srcSpanEnd x = (srcSpanEndLine x, srcSpanEndColumn x)
mkSrcSpan :: SrcLoc -> SrcLoc -> SrcSpan
mkSrcSpan (SrcLoc fn sl sc) (SrcLoc _ el ec) = SrcSpan fn sl sc el ec
mergeSrcSpan :: SrcSpan -> SrcSpan -> SrcSpan
mergeSrcSpan (SrcSpan fn sl1 sc1 el1 ec1) (SrcSpan _ sl2 sc2 el2 ec2) =
let (sl,sc) = min (sl1,sc1) (sl2,sc2)
(el,ec) = max (el1,ec1) (el2,ec2)
in SrcSpan fn sl sc el ec
isNullSpan :: SrcSpan -> Bool
isNullSpan ss = spanSize ss == (0,0)
spanSize :: SrcSpan -> (Int, Int)
spanSize ss = (srcSpanEndLine ss srcSpanStartLine ss, max 0 (srcSpanEndColumn ss srcSpanStartColumn ss))
data Loc a = Loc
{ loc :: SrcSpan
, unLoc :: a
}
deriving (Eq,Ord,Show)
data SrcSpanInfo = SrcSpanInfo
{ srcInfoSpan :: SrcSpan
, srcInfoPoints :: [SrcSpan]
}
#ifdef __GLASGOW_HASKELL__
deriving (Eq,Ord,Show,Typeable,Data)
#else
deriving (Eq,Ord,Show)
#endif
noInfoSpan :: SrcSpan -> SrcSpanInfo
noInfoSpan ss = SrcSpanInfo ss []
infoSpan :: SrcSpan -> [SrcSpan] -> SrcSpanInfo
infoSpan = SrcSpanInfo
combSpanInfo :: SrcSpanInfo -> SrcSpanInfo -> SrcSpanInfo
combSpanInfo s1 s2 = SrcSpanInfo
(mergeSrcSpan (srcInfoSpan s1) (srcInfoSpan s2))
[]
(<++>) :: SrcSpanInfo -> SrcSpanInfo -> SrcSpanInfo
(<++>) = combSpanInfo
(<+?>) :: SrcSpanInfo -> Maybe SrcSpanInfo -> SrcSpanInfo
a <+?> b = case b of {Nothing -> a; Just b -> a <++> b}
(<?+>) :: Maybe SrcSpanInfo -> SrcSpanInfo -> SrcSpanInfo
a <?+> b = case a of {Nothing -> b; Just a -> a <++> b}
(<**) :: SrcSpanInfo -> [SrcSpan] -> SrcSpanInfo
ss@(SrcSpanInfo {srcInfoPoints = ps}) <** xs = ss {srcInfoPoints = ps ++ xs}
(<^^>) :: SrcSpan -> SrcSpan -> SrcSpanInfo
a <^^> b = noInfoSpan (mergeSrcSpan a b)
infixl 6 <^^>
infixl 5 <++>
infixl 4 <**, <+?>, <?+>
class SrcInfo si where
toSrcInfo :: SrcLoc -> [SrcSpan] -> SrcLoc -> si
fromSrcInfo :: SrcSpanInfo -> si
getPointLoc :: si -> SrcLoc
fileName :: si -> String
startLine :: si -> Int
startColumn :: si -> Int
getPointLoc si = SrcLoc (fileName si) (startLine si) (startColumn si)
instance SrcInfo SrcLoc where
toSrcInfo s _ _ = s
fromSrcInfo si = SrcLoc (fileName si) (startLine si) (startColumn si)
fileName = srcFilename
startLine = srcLine
startColumn = srcColumn
instance SrcInfo SrcSpan where
toSrcInfo st _ end = mkSrcSpan st end
fromSrcInfo = srcInfoSpan
fileName = srcSpanFilename
startLine = srcSpanStartLine
startColumn = srcSpanStartColumn
instance SrcInfo SrcSpanInfo where
toSrcInfo st pts end = SrcSpanInfo (mkSrcSpan st end) pts
fromSrcInfo = id
fileName = fileName . srcInfoSpan
startLine = startLine . srcInfoSpan
startColumn = startColumn . srcInfoSpan