{-# LANGUAGE DeriveFoldable    #-}
{-# LANGUAGE DeriveFunctor     #-}
{-# LANGUAGE DeriveTraversable #-}
module Zinza.Pos where

-- | Location, line and column.
data Loc = Loc !Int !Int
  deriving (Loc -> Loc -> Bool
(Loc -> Loc -> Bool) -> (Loc -> Loc -> Bool) -> Eq Loc
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Loc -> Loc -> Bool
== :: Loc -> Loc -> Bool
$c/= :: Loc -> Loc -> Bool
/= :: Loc -> Loc -> Bool
Eq, Int -> Loc -> ShowS
[Loc] -> ShowS
Loc -> String
(Int -> Loc -> ShowS)
-> (Loc -> String) -> ([Loc] -> ShowS) -> Show Loc
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Loc -> ShowS
showsPrec :: Int -> Loc -> ShowS
$cshow :: Loc -> String
show :: Loc -> String
$cshowList :: [Loc] -> ShowS
showList :: [Loc] -> ShowS
Show)

-- | /Unknown/ location.
zeroLoc :: Loc
zeroLoc :: Loc
zeroLoc = Int -> Int -> Loc
Loc Int
0 Int
0

-- | Pretty-print location.
displayLoc :: Loc -> String
displayLoc :: Loc -> String
displayLoc (Loc Int
l Int
c) = Int -> String
forall a. Show a => a -> String
show Int
l 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
c

-- | Located element.
data Located a = L {-# UNPACK #-} !Loc a
  deriving (Located a -> Located a -> Bool
(Located a -> Located a -> Bool)
-> (Located a -> Located a -> Bool) -> Eq (Located a)
forall a. Eq a => Located a -> Located a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: forall a. Eq a => Located a -> Located a -> Bool
== :: Located a -> Located a -> Bool
$c/= :: forall a. Eq a => Located a -> Located a -> Bool
/= :: Located a -> Located a -> Bool
Eq, Int -> Located a -> ShowS
[Located a] -> ShowS
Located a -> String
(Int -> Located a -> ShowS)
-> (Located a -> String)
-> ([Located a] -> ShowS)
-> Show (Located a)
forall a. Show a => Int -> Located a -> ShowS
forall a. Show a => [Located a] -> ShowS
forall a. Show a => Located a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: forall a. Show a => Int -> Located a -> ShowS
showsPrec :: Int -> Located a -> ShowS
$cshow :: forall a. Show a => Located a -> String
show :: Located a -> String
$cshowList :: forall a. Show a => [Located a] -> ShowS
showList :: [Located a] -> ShowS
Show, (forall a b. (a -> b) -> Located a -> Located b)
-> (forall a b. a -> Located b -> Located a) -> Functor Located
forall a b. a -> Located b -> Located a
forall a b. (a -> b) -> Located a -> Located b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
$cfmap :: forall a b. (a -> b) -> Located a -> Located b
fmap :: forall a b. (a -> b) -> Located a -> Located b
$c<$ :: forall a b. a -> Located b -> Located a
<$ :: forall a b. a -> Located b -> Located a
Functor, (forall m. Monoid m => Located m -> m)
-> (forall m a. Monoid m => (a -> m) -> Located a -> m)
-> (forall m a. Monoid m => (a -> m) -> Located a -> m)
-> (forall a b. (a -> b -> b) -> b -> Located a -> b)
-> (forall a b. (a -> b -> b) -> b -> Located a -> b)
-> (forall b a. (b -> a -> b) -> b -> Located a -> b)
-> (forall b a. (b -> a -> b) -> b -> Located a -> b)
-> (forall a. (a -> a -> a) -> Located a -> a)
-> (forall a. (a -> a -> a) -> Located a -> a)
-> (forall a. Located a -> [a])
-> (forall a. Located a -> Bool)
-> (forall a. Located a -> Int)
-> (forall a. Eq a => a -> Located a -> Bool)
-> (forall a. Ord a => Located a -> a)
-> (forall a. Ord a => Located a -> a)
-> (forall a. Num a => Located a -> a)
-> (forall a. Num a => Located a -> a)
-> Foldable Located
forall a. Eq a => a -> Located a -> Bool
forall a. Num a => Located a -> a
forall a. Ord a => Located a -> a
forall m. Monoid m => Located m -> m
forall a. Located a -> Bool
forall a. Located a -> Int
forall a. Located a -> [a]
forall a. (a -> a -> a) -> Located a -> a
forall m a. Monoid m => (a -> m) -> Located a -> m
forall b a. (b -> a -> b) -> b -> Located a -> b
forall a b. (a -> b -> b) -> b -> Located a -> b
forall (t :: * -> *).
(forall m. Monoid m => t m -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. t a -> [a])
-> (forall a. t a -> Bool)
-> (forall a. t a -> Int)
-> (forall a. Eq a => a -> t a -> Bool)
-> (forall a. Ord a => t a -> a)
-> (forall a. Ord a => t a -> a)
-> (forall a. Num a => t a -> a)
-> (forall a. Num a => t a -> a)
-> Foldable t
$cfold :: forall m. Monoid m => Located m -> m
fold :: forall m. Monoid m => Located m -> m
$cfoldMap :: forall m a. Monoid m => (a -> m) -> Located a -> m
foldMap :: forall m a. Monoid m => (a -> m) -> Located a -> m
$cfoldMap' :: forall m a. Monoid m => (a -> m) -> Located a -> m
foldMap' :: forall m a. Monoid m => (a -> m) -> Located a -> m
$cfoldr :: forall a b. (a -> b -> b) -> b -> Located a -> b
foldr :: forall a b. (a -> b -> b) -> b -> Located a -> b
$cfoldr' :: forall a b. (a -> b -> b) -> b -> Located a -> b
foldr' :: forall a b. (a -> b -> b) -> b -> Located a -> b
$cfoldl :: forall b a. (b -> a -> b) -> b -> Located a -> b
foldl :: forall b a. (b -> a -> b) -> b -> Located a -> b
$cfoldl' :: forall b a. (b -> a -> b) -> b -> Located a -> b
foldl' :: forall b a. (b -> a -> b) -> b -> Located a -> b
$cfoldr1 :: forall a. (a -> a -> a) -> Located a -> a
foldr1 :: forall a. (a -> a -> a) -> Located a -> a
$cfoldl1 :: forall a. (a -> a -> a) -> Located a -> a
foldl1 :: forall a. (a -> a -> a) -> Located a -> a
$ctoList :: forall a. Located a -> [a]
toList :: forall a. Located a -> [a]
$cnull :: forall a. Located a -> Bool
null :: forall a. Located a -> Bool
$clength :: forall a. Located a -> Int
length :: forall a. Located a -> Int
$celem :: forall a. Eq a => a -> Located a -> Bool
elem :: forall a. Eq a => a -> Located a -> Bool
$cmaximum :: forall a. Ord a => Located a -> a
maximum :: forall a. Ord a => Located a -> a
$cminimum :: forall a. Ord a => Located a -> a
minimum :: forall a. Ord a => Located a -> a
$csum :: forall a. Num a => Located a -> a
sum :: forall a. Num a => Located a -> a
$cproduct :: forall a. Num a => Located a -> a
product :: forall a. Num a => Located a -> a
Foldable, Functor Located
Foldable Located
(Functor Located, Foldable Located) =>
(forall (f :: * -> *) a b.
 Applicative f =>
 (a -> f b) -> Located a -> f (Located b))
-> (forall (f :: * -> *) a.
    Applicative f =>
    Located (f a) -> f (Located a))
-> (forall (m :: * -> *) a b.
    Monad m =>
    (a -> m b) -> Located a -> m (Located b))
-> (forall (m :: * -> *) a.
    Monad m =>
    Located (m a) -> m (Located a))
-> Traversable Located
forall (t :: * -> *).
(Functor t, Foldable t) =>
(forall (f :: * -> *) a b.
 Applicative f =>
 (a -> f b) -> t a -> f (t b))
-> (forall (f :: * -> *) a. Applicative f => t (f a) -> f (t a))
-> (forall (m :: * -> *) a b.
    Monad m =>
    (a -> m b) -> t a -> m (t b))
-> (forall (m :: * -> *) a. Monad m => t (m a) -> m (t a))
-> Traversable t
forall (m :: * -> *) a. Monad m => Located (m a) -> m (Located a)
forall (f :: * -> *) a.
Applicative f =>
Located (f a) -> f (Located a)
forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Located a -> m (Located b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Located a -> f (Located b)
$ctraverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Located a -> f (Located b)
traverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Located a -> f (Located b)
$csequenceA :: forall (f :: * -> *) a.
Applicative f =>
Located (f a) -> f (Located a)
sequenceA :: forall (f :: * -> *) a.
Applicative f =>
Located (f a) -> f (Located a)
$cmapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Located a -> m (Located b)
mapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Located a -> m (Located b)
$csequence :: forall (m :: * -> *) a. Monad m => Located (m a) -> m (Located a)
sequence :: forall (m :: * -> *) a. Monad m => Located (m a) -> m (Located a)
Traversable)

-- | Some containers have location for each element.
class Traversable t => TraversableWithLoc t where
    traverseWithLoc :: Applicative f => (Loc -> a -> f b) -> t a -> f (t b)