summaryrefslogtreecommitdiff
path: root/lib/Language/Elna/Location.hs
blob: 918ef4695a864040105dd9942d252814450ba42a (plain)
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
module Language.Elna.Location
    ( Identifier(..)
    , Location(..)
    , Node(..)
    , showArrayType
    ) where

import Data.Hashable (Hashable(..))
import Data.String (IsString(..))
import Data.Text (Text)
import qualified Data.Text as Text
import Data.Word (Word32)

data Location = Location
    { line :: Word32
    , column :: Word32
    } deriving (Eq, Show)

instance Semigroup Location
  where
    (Location thisLine thisColumn) <> (Location thatLine thatColumn) = Location
        { line = thisLine + thatLine
        , column = thisColumn + thatColumn
        }

instance Monoid Location
  where
   mempty = Location{ line = 1, column = 1 }

data Node a = Node a Location
    deriving (Eq, Show)

instance Functor Node
  where
    fmap f (Node node location) = Node (f node) location

newtype Identifier = Identifier { unIdentifier :: Text }
    deriving Eq

instance Show Identifier
  where
    show (Identifier identifier) = Text.unpack identifier

instance IsString Identifier
  where
    fromString = Identifier . Text.pack

instance Ord Identifier
  where
    compare (Identifier lhs) (Identifier rhs) = compare lhs rhs

instance Hashable Identifier
  where
    hashWithSalt salt (Identifier identifier) = hashWithSalt salt identifier

showArrayType :: Show a => Word32 -> a -> String
showArrayType elementCount typeName = concat
    ["array[", show elementCount, "] of ", show typeName]