2024-07-26 12:22:07 +02:00
|
|
|
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
|
|
|
|
|
2024-10-30 14:12:51 +01:00
|
|
|
showArrayType :: (Show a, Show b) => a -> b -> String
|
2024-07-26 12:22:07 +02:00
|
|
|
showArrayType elementCount typeName = concat
|
|
|
|
["array[", show elementCount, "] of ", show typeName]
|