~jojo/Carth

ref: ee5251b9b8bf725c1d5499fedf9c9671308fa476 Carth/src/SrcPos.hs -rw-r--r-- 973 bytes
ee5251b9JoJo Glob exports in AST modules -- minor refactor / cleanup 2 years 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
43
44
module SrcPos
    ( SrcPos(..)
    , WithPos(..)
    , HasPos(..)
    , mapPos
    , unpos
    , prettySrcPos
    )
where

import Text.Megaparsec.Pos


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

data WithPos a = WithPos SrcPos a

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

mapPos :: (a -> b) -> WithPos a -> WithPos b
mapPos f (WithPos p a) = 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)))