{-# LANGUAGE DeriveGeneric #-}
module Distribution.Parsec.Position (
    Position (..),
    incPos,
    retPos,
    showPos,
    zeroPos,
    positionCol,
    positionRow,
    ) where

import Distribution.Compat.Prelude
import Prelude ()

-- | 1-indexed row and column positions in a file.
data Position = Position
    {-# UNPACK #-}  !Int           -- row
    {-# UNPACK #-}  !Int           -- column
  deriving (Position -> Position -> Bool
(Position -> Position -> Bool)
-> (Position -> Position -> Bool) -> Eq Position
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Position -> Position -> Bool
== :: Position -> Position -> Bool
$c/= :: Position -> Position -> Bool
/= :: Position -> Position -> Bool
Eq, Eq Position
Eq Position
-> (Position -> Position -> Ordering)
-> (Position -> Position -> Bool)
-> (Position -> Position -> Bool)
-> (Position -> Position -> Bool)
-> (Position -> Position -> Bool)
-> (Position -> Position -> Position)
-> (Position -> Position -> Position)
-> Ord Position
Position -> Position -> Bool
Position -> Position -> Ordering
Position -> Position -> Position
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
$ccompare :: Position -> Position -> Ordering
compare :: Position -> Position -> Ordering
$c< :: Position -> Position -> Bool
< :: Position -> Position -> Bool
$c<= :: Position -> Position -> Bool
<= :: Position -> Position -> Bool
$c> :: Position -> Position -> Bool
> :: Position -> Position -> Bool
$c>= :: Position -> Position -> Bool
>= :: Position -> Position -> Bool
$cmax :: Position -> Position -> Position
max :: Position -> Position -> Position
$cmin :: Position -> Position -> Position
min :: Position -> Position -> Position
Ord, Int -> Position -> ShowS
[Position] -> ShowS
Position -> String
(Int -> Position -> ShowS)
-> (Position -> String) -> ([Position] -> ShowS) -> Show Position
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Position -> ShowS
showsPrec :: Int -> Position -> ShowS
$cshow :: Position -> String
show :: Position -> String
$cshowList :: [Position] -> ShowS
showList :: [Position] -> ShowS
Show, (forall x. Position -> Rep Position x)
-> (forall x. Rep Position x -> Position) -> Generic Position
forall x. Rep Position x -> Position
forall x. Position -> Rep Position x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. Position -> Rep Position x
$cto :: forall x. Rep Position x -> Position
Generic)

instance Binary Position
instance NFData Position where rnf :: Position -> ()
rnf = Position -> ()
forall a. (Generic a, GNFData (Rep a)) => a -> ()
genericRnf

-- | Shift position by n columns to the right.
incPos :: Int -> Position -> Position
incPos :: Int -> Position -> Position
incPos Int
n (Position Int
row Int
col) = Int -> Int -> Position
Position Int
row (Int
col Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
n)

-- | Shift position to beginning of next row.
retPos :: Position -> Position
retPos :: Position -> Position
retPos (Position Int
row Int
_col) = Int -> Int -> Position
Position (Int
row Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) Int
1

showPos :: Position -> String
showPos :: Position -> String
showPos (Position Int
row Int
col) = Int -> String
forall a. Show a => a -> String
show Int
row String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
":" String -> ShowS
forall a. [a] -> [a] -> [a]
++ Int -> String
forall a. Show a => a -> String
show Int
col

zeroPos :: Position
zeroPos :: Position
zeroPos = Int -> Int -> Position
Position Int
0 Int
0

-- | @since 3.0.0.0
positionCol :: Position -> Int
positionCol :: Position -> Int
positionCol (Position Int
_ Int
c) = Int
c

-- | @since 3.0.0.0
positionRow :: Position -> Int
positionRow :: Position -> Int
positionRow (Position Int
r Int
_) = Int
r