{-# LANGUAGE DeriveDataTypeable, TypeFamilies #-}
module Text.Boomerang.Pos
    ( Pos
    , InitialPosition(..)
    , ErrorPosition(..)
    , MajorMinorPos(..)
    , incMajor, incMinor
    )
    where

import Data.Data (Data, Typeable)

-- | type synonym family that maps an error type to its position type
type family Pos err :: *

-- | extract the position information from an error
class ErrorPosition err where
    getPosition :: err -> Maybe (Pos err)

-- | the initial position for a position type
class InitialPosition e where
    initialPos :: Maybe e -> Pos e

-- | A basic 2-axis position type (e.g. line, character)
data MajorMinorPos = MajorMinorPos
    { MajorMinorPos -> Integer
major :: Integer
    , MajorMinorPos -> Integer
minor :: Integer
    }
    deriving (MajorMinorPos -> MajorMinorPos -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: MajorMinorPos -> MajorMinorPos -> Bool
$c/= :: MajorMinorPos -> MajorMinorPos -> Bool
== :: MajorMinorPos -> MajorMinorPos -> Bool
$c== :: MajorMinorPos -> MajorMinorPos -> Bool
Eq, Eq MajorMinorPos
MajorMinorPos -> MajorMinorPos -> Bool
MajorMinorPos -> MajorMinorPos -> Ordering
MajorMinorPos -> MajorMinorPos -> MajorMinorPos
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: MajorMinorPos -> MajorMinorPos -> MajorMinorPos
$cmin :: MajorMinorPos -> MajorMinorPos -> MajorMinorPos
max :: MajorMinorPos -> MajorMinorPos -> MajorMinorPos
$cmax :: MajorMinorPos -> MajorMinorPos -> MajorMinorPos
>= :: MajorMinorPos -> MajorMinorPos -> Bool
$c>= :: MajorMinorPos -> MajorMinorPos -> Bool
> :: MajorMinorPos -> MajorMinorPos -> Bool
$c> :: MajorMinorPos -> MajorMinorPos -> Bool
<= :: MajorMinorPos -> MajorMinorPos -> Bool
$c<= :: MajorMinorPos -> MajorMinorPos -> Bool
< :: MajorMinorPos -> MajorMinorPos -> Bool
$c< :: MajorMinorPos -> MajorMinorPos -> Bool
compare :: MajorMinorPos -> MajorMinorPos -> Ordering
$ccompare :: MajorMinorPos -> MajorMinorPos -> Ordering
Ord, Typeable, Typeable MajorMinorPos
MajorMinorPos -> DataType
MajorMinorPos -> Constr
(forall b. Data b => b -> b) -> MajorMinorPos -> MajorMinorPos
forall a.
Typeable a
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> a -> c a)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c a)
-> (a -> Constr)
-> (a -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c a))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c a))
-> ((forall b. Data b => b -> b) -> a -> a)
-> (forall r r'.
    (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall r r'.
    (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall u. (forall d. Data d => d -> u) -> a -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> a -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> Data a
forall u. Int -> (forall d. Data d => d -> u) -> MajorMinorPos -> u
forall u. (forall d. Data d => d -> u) -> MajorMinorPos -> [u]
forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> MajorMinorPos -> r
forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> MajorMinorPos -> r
forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> MajorMinorPos -> m MajorMinorPos
forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> MajorMinorPos -> m MajorMinorPos
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c MajorMinorPos
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> MajorMinorPos -> c MajorMinorPos
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c MajorMinorPos)
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c MajorMinorPos)
gmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> MajorMinorPos -> m MajorMinorPos
$cgmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> MajorMinorPos -> m MajorMinorPos
gmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> MajorMinorPos -> m MajorMinorPos
$cgmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> MajorMinorPos -> m MajorMinorPos
gmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> MajorMinorPos -> m MajorMinorPos
$cgmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> MajorMinorPos -> m MajorMinorPos
gmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> MajorMinorPos -> u
$cgmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> MajorMinorPos -> u
gmapQ :: forall u. (forall d. Data d => d -> u) -> MajorMinorPos -> [u]
$cgmapQ :: forall u. (forall d. Data d => d -> u) -> MajorMinorPos -> [u]
gmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> MajorMinorPos -> r
$cgmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> MajorMinorPos -> r
gmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> MajorMinorPos -> r
$cgmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> MajorMinorPos -> r
gmapT :: (forall b. Data b => b -> b) -> MajorMinorPos -> MajorMinorPos
$cgmapT :: (forall b. Data b => b -> b) -> MajorMinorPos -> MajorMinorPos
dataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c MajorMinorPos)
$cdataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c MajorMinorPos)
dataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c MajorMinorPos)
$cdataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c MajorMinorPos)
dataTypeOf :: MajorMinorPos -> DataType
$cdataTypeOf :: MajorMinorPos -> DataType
toConstr :: MajorMinorPos -> Constr
$ctoConstr :: MajorMinorPos -> Constr
gunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c MajorMinorPos
$cgunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c MajorMinorPos
gfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> MajorMinorPos -> c MajorMinorPos
$cgfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> MajorMinorPos -> c MajorMinorPos
Data)

-- | increment major position by 'i', reset minor position to 0..
-- if you wanted something else.. too bad.
incMajor :: (Integral i) => i -> MajorMinorPos -> MajorMinorPos
incMajor :: forall i. Integral i => i -> MajorMinorPos -> MajorMinorPos
incMajor i
i (MajorMinorPos Integer
maj Integer
min) = Integer -> Integer -> MajorMinorPos
MajorMinorPos (Integer
maj forall a. Num a => a -> a -> a
+ (forall a b. (Integral a, Num b) => a -> b
fromIntegral i
i)) Integer
0

-- | increment minor position by 'i'
incMinor :: (Integral i) => i -> MajorMinorPos -> MajorMinorPos
incMinor :: forall i. Integral i => i -> MajorMinorPos -> MajorMinorPos
incMinor i
i (MajorMinorPos Integer
maj Integer
min) = Integer -> Integer -> MajorMinorPos
MajorMinorPos Integer
maj (Integer
min forall a. Num a => a -> a -> a
+ (forall a b. (Integral a, Num b) => a -> b
fromIntegral i
i))

instance Show MajorMinorPos where
    show :: MajorMinorPos -> String
show (MajorMinorPos Integer
s Integer
c) = String
"(" forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show Integer
s forall a. [a] -> [a] -> [a]
++ String
", " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show Integer
c forall a. [a] -> [a] -> [a]
++ String
")"