~jojo/Carth

ref: d09e75d2aee08fb372c5e5eba38fc16f308c2ec7 Carth/src/SrcPos.hs -rw-r--r-- 1.0 KiB
d09e75d2JoJo Add macro rules and ellipsis (...) macro operator 1 year, 8 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
{-# LANGUAGE DeriveDataTypeable #-}

module SrcPos where

import Text.Megaparsec.Pos
import Data.Data


data SrcPos = SrcPos
    { srcName :: FilePath
    , srcLine :: Word
    , srcColumn :: Word
    } deriving (Show, Eq, Ord, Data)

data WithPos a = WithPos SrcPos a deriving (Data)

class HasPos a where
    getPos :: a -> SrcPos


instance Show a => Show (WithPos a) where
    showsPrec p (WithPos _ a) = showsPrec p a
instance Eq a => Eq (WithPos a) where
    (WithPos _ a) == (WithPos _ b) = a == b
instance Ord a => Ord (WithPos a) where
    compare (WithPos _ a) (WithPos _ b) = compare a b

instance HasPos (WithPos a) where
    getPos (WithPos p _) = p

mapPosd :: (a -> b) -> WithPos a -> WithPos b
mapPosd f (WithPos p a) = WithPos p (f a)

mapPosdM :: Monad m => (a -> m b) -> WithPos a -> m (WithPos b)
mapPosdM f (WithPos p a) = fmap (WithPos p) (f a)

unpos :: WithPos a -> a
unpos (WithPos _ a) = a

prettySrcPos :: SrcPos -> String
prettySrcPos (SrcPos f l c) =
    sourcePosPretty (SourcePos f (mkPos (fromIntegral l)) (mkPos (fromIntegral c)))