{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE DeriveGeneric #-}

module Language.Fortran.AST.AList where

import Language.Fortran.Util.FirstParameter
import Language.Fortran.Util.SecondParameter
import Language.Fortran.Util.Position (Spanned, SrcSpan(..), getSpan)

import Data.Data    (Data, Typeable)
import GHC.Generics (Generic)
import Control.DeepSeq (NFData)
import Text.PrettyPrint.GenericPretty (Out)

-- | A location-tagged list of @t a@s (@t@ decorated with an @a@ annotation).
--
-- The AST is polymorphic on some type @a@, which is used for arbitrary
-- annotations. Since many AST nodes use lists (e.g. executable statements,
-- declarations), we define a dedicated annotated list type to reuse.
--
-- Note that the list itself also holds an @a@ annotation.
data AList t a = AList a SrcSpan [t a] deriving (AList t a -> AList t a -> Bool
(AList t a -> AList t a -> Bool)
-> (AList t a -> AList t a -> Bool) -> Eq (AList t a)
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
forall (t :: * -> *) a.
(Eq a, Eq (t a)) =>
AList t a -> AList t a -> Bool
/= :: AList t a -> AList t a -> Bool
$c/= :: forall (t :: * -> *) a.
(Eq a, Eq (t a)) =>
AList t a -> AList t a -> Bool
== :: AList t a -> AList t a -> Bool
$c== :: forall (t :: * -> *) a.
(Eq a, Eq (t a)) =>
AList t a -> AList t a -> Bool
Eq, Int -> AList t a -> ShowS
[AList t a] -> ShowS
AList t a -> String
(Int -> AList t a -> ShowS)
-> (AList t a -> String)
-> ([AList t a] -> ShowS)
-> Show (AList t a)
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
forall (t :: * -> *) a.
(Show a, Show (t a)) =>
Int -> AList t a -> ShowS
forall (t :: * -> *) a.
(Show a, Show (t a)) =>
[AList t a] -> ShowS
forall (t :: * -> *) a. (Show a, Show (t a)) => AList t a -> String
showList :: [AList t a] -> ShowS
$cshowList :: forall (t :: * -> *) a.
(Show a, Show (t a)) =>
[AList t a] -> ShowS
show :: AList t a -> String
$cshow :: forall (t :: * -> *) a. (Show a, Show (t a)) => AList t a -> String
showsPrec :: Int -> AList t a -> ShowS
$cshowsPrec :: forall (t :: * -> *) a.
(Show a, Show (t a)) =>
Int -> AList t a -> ShowS
Show, Typeable (AList t a)
DataType
Constr
Typeable (AList t a)
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> AList t a -> c (AList t a))
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c (AList t a))
-> (AList t a -> Constr)
-> (AList t a -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c (AList t a)))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e))
    -> Maybe (c (AList t a)))
-> ((forall b. Data b => b -> b) -> AList t a -> AList t a)
-> (forall r r'.
    (r -> r' -> r)
    -> r -> (forall d. Data d => d -> r') -> AList t a -> r)
-> (forall r r'.
    (r' -> r -> r)
    -> r -> (forall d. Data d => d -> r') -> AList t a -> r)
-> (forall u. (forall d. Data d => d -> u) -> AList t a -> [u])
-> (forall u.
    Int -> (forall d. Data d => d -> u) -> AList t a -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> AList t a -> m (AList t a))
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> AList t a -> m (AList t a))
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> AList t a -> m (AList t a))
-> Data (AList t a)
AList t a -> DataType
AList t a -> Constr
(forall b. Data b => b -> b) -> AList t a -> AList t a
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> AList t a -> c (AList t a)
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (AList t a)
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) -> AList t a -> u
forall u. (forall d. Data d => d -> u) -> AList t a -> [u]
forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> AList t a -> r
forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> AList t a -> r
forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> AList t a -> m (AList t a)
forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> AList t a -> m (AList t a)
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (AList t a)
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> AList t a -> c (AList t a)
forall (t :: * -> *) a.
(Typeable t, Data a, Data (t a)) =>
Typeable (AList t a)
forall (t :: * -> *) a.
(Typeable t, Data a, Data (t a)) =>
AList t a -> DataType
forall (t :: * -> *) a.
(Typeable t, Data a, Data (t a)) =>
AList t a -> Constr
forall (t :: * -> *) a.
(Typeable t, Data a, Data (t a)) =>
(forall b. Data b => b -> b) -> AList t a -> AList t a
forall (t :: * -> *) a u.
(Typeable t, Data a, Data (t a)) =>
Int -> (forall d. Data d => d -> u) -> AList t a -> u
forall (t :: * -> *) a u.
(Typeable t, Data a, Data (t a)) =>
(forall d. Data d => d -> u) -> AList t a -> [u]
forall (t :: * -> *) a r r'.
(Typeable t, Data a, Data (t a)) =>
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> AList t a -> r
forall (t :: * -> *) a r r'.
(Typeable t, Data a, Data (t a)) =>
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> AList t a -> r
forall (t :: * -> *) a (m :: * -> *).
(Typeable t, Data a, Data (t a), Monad m) =>
(forall d. Data d => d -> m d) -> AList t a -> m (AList t a)
forall (t :: * -> *) a (m :: * -> *).
(Typeable t, Data a, Data (t a), MonadPlus m) =>
(forall d. Data d => d -> m d) -> AList t a -> m (AList t a)
forall (t :: * -> *) a (c :: * -> *).
(Typeable t, Data a, Data (t a)) =>
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (AList t a)
forall (t :: * -> *) a (c :: * -> *).
(Typeable t, Data a, Data (t a)) =>
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> AList t a -> c (AList t a)
forall (t :: * -> *) a (t :: * -> *) (c :: * -> *).
(Typeable t, Data a, Data (t a), Typeable t) =>
(forall d. Data d => c (t d)) -> Maybe (c (AList t a))
forall (t :: * -> *) a (t :: * -> * -> *) (c :: * -> *).
(Typeable t, Data a, Data (t a), Typeable t) =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (AList t a))
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c (AList t a))
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (AList t a))
$cAList :: Constr
$tAList :: DataType
gmapMo :: (forall d. Data d => d -> m d) -> AList t a -> m (AList t a)
$cgmapMo :: forall (t :: * -> *) a (m :: * -> *).
(Typeable t, Data a, Data (t a), MonadPlus m) =>
(forall d. Data d => d -> m d) -> AList t a -> m (AList t a)
gmapMp :: (forall d. Data d => d -> m d) -> AList t a -> m (AList t a)
$cgmapMp :: forall (t :: * -> *) a (m :: * -> *).
(Typeable t, Data a, Data (t a), MonadPlus m) =>
(forall d. Data d => d -> m d) -> AList t a -> m (AList t a)
gmapM :: (forall d. Data d => d -> m d) -> AList t a -> m (AList t a)
$cgmapM :: forall (t :: * -> *) a (m :: * -> *).
(Typeable t, Data a, Data (t a), Monad m) =>
(forall d. Data d => d -> m d) -> AList t a -> m (AList t a)
gmapQi :: Int -> (forall d. Data d => d -> u) -> AList t a -> u
$cgmapQi :: forall (t :: * -> *) a u.
(Typeable t, Data a, Data (t a)) =>
Int -> (forall d. Data d => d -> u) -> AList t a -> u
gmapQ :: (forall d. Data d => d -> u) -> AList t a -> [u]
$cgmapQ :: forall (t :: * -> *) a u.
(Typeable t, Data a, Data (t a)) =>
(forall d. Data d => d -> u) -> AList t a -> [u]
gmapQr :: (r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> AList t a -> r
$cgmapQr :: forall (t :: * -> *) a r r'.
(Typeable t, Data a, Data (t a)) =>
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> AList t a -> r
gmapQl :: (r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> AList t a -> r
$cgmapQl :: forall (t :: * -> *) a r r'.
(Typeable t, Data a, Data (t a)) =>
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> AList t a -> r
gmapT :: (forall b. Data b => b -> b) -> AList t a -> AList t a
$cgmapT :: forall (t :: * -> *) a.
(Typeable t, Data a, Data (t a)) =>
(forall b. Data b => b -> b) -> AList t a -> AList t a
dataCast2 :: (forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (AList t a))
$cdataCast2 :: forall (t :: * -> *) a (t :: * -> * -> *) (c :: * -> *).
(Typeable t, Data a, Data (t a), Typeable t) =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (AList t a))
dataCast1 :: (forall d. Data d => c (t d)) -> Maybe (c (AList t a))
$cdataCast1 :: forall (t :: * -> *) a (t :: * -> *) (c :: * -> *).
(Typeable t, Data a, Data (t a), Typeable t) =>
(forall d. Data d => c (t d)) -> Maybe (c (AList t a))
dataTypeOf :: AList t a -> DataType
$cdataTypeOf :: forall (t :: * -> *) a.
(Typeable t, Data a, Data (t a)) =>
AList t a -> DataType
toConstr :: AList t a -> Constr
$ctoConstr :: forall (t :: * -> *) a.
(Typeable t, Data a, Data (t a)) =>
AList t a -> Constr
gunfold :: (forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (AList t a)
$cgunfold :: forall (t :: * -> *) a (c :: * -> *).
(Typeable t, Data a, Data (t a)) =>
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (AList t a)
gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> AList t a -> c (AList t a)
$cgfoldl :: forall (t :: * -> *) a (c :: * -> *).
(Typeable t, Data a, Data (t a)) =>
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> AList t a -> c (AList t a)
$cp1Data :: forall (t :: * -> *) a.
(Typeable t, Data a, Data (t a)) =>
Typeable (AList t a)
Data, Typeable, (forall x. AList t a -> Rep (AList t a) x)
-> (forall x. Rep (AList t a) x -> AList t a)
-> Generic (AList t a)
forall x. Rep (AList t a) x -> AList t a
forall x. AList t a -> Rep (AList t a) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall (t :: * -> *) a x. Rep (AList t a) x -> AList t a
forall (t :: * -> *) a x. AList t a -> Rep (AList t a) x
$cto :: forall (t :: * -> *) a x. Rep (AList t a) x -> AList t a
$cfrom :: forall (t :: * -> *) a x. AList t a -> Rep (AList t a) x
Generic)
instance Functor t => Functor (AList t) where
  fmap :: (a -> b) -> AList t a -> AList t b
fmap a -> b
f (AList a
a SrcSpan
s [t a]
xs) = b -> SrcSpan -> [t b] -> AList t b
forall (t :: * -> *) a. a -> SrcSpan -> [t a] -> AList t a
AList (a -> b
f a
a) SrcSpan
s ((t a -> t b) -> [t a] -> [t b]
forall a b. (a -> b) -> [a] -> [b]
map ((a -> b) -> t a -> t b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f) [t a]
xs)

instance FirstParameter (AList t a) a
instance SecondParameter (AList t a) SrcSpan
instance Spanned (AList t a)
instance (Out a, Out (t a)) => Out (AList t a)
instance (NFData a, NFData (t a)) => NFData (AList t a)

-- | Convert a non-empty list to an 'AList'.
fromList :: Spanned (t a) => a -> [ t a ] -> AList t a
fromList :: a -> [t a] -> AList t a
fromList a
a [t a]
xs = a -> SrcSpan -> [t a] -> AList t a
forall (t :: * -> *) a. a -> SrcSpan -> [t a] -> AList t a
AList a
a ([t a] -> SrcSpan
forall a. Spanned a => a -> SrcSpan
getSpan [t a]
xs) [t a]
xs

-- | Convert a list to an 'AList', returning Nothing iff the list is empty.
fromList' :: Spanned (t a) => a -> [ t a ] -> Maybe (AList t a)
fromList' :: a -> [t a] -> Maybe (AList t a)
fromList' a
_ [] = Maybe (AList t a)
forall a. Maybe a
Nothing
fromList' a
a [t a]
xs = AList t a -> Maybe (AList t a)
forall a. a -> Maybe a
Just (AList t a -> Maybe (AList t a)) -> AList t a -> Maybe (AList t a)
forall a b. (a -> b) -> a -> b
$ a -> [t a] -> AList t a
forall (t :: * -> *) a. Spanned (t a) => a -> [t a] -> AList t a
fromList a
a [t a]
xs

fromReverseList :: Spanned (t ()) => [ t () ] -> AList t ()
fromReverseList :: [t ()] -> AList t ()
fromReverseList = () -> [t ()] -> AList t ()
forall (t :: * -> *) a. Spanned (t a) => a -> [t a] -> AList t a
fromList () ([t ()] -> AList t ())
-> ([t ()] -> [t ()]) -> [t ()] -> AList t ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [t ()] -> [t ()]
forall a. [a] -> [a]
reverse

fromReverseList' :: Spanned (t ()) => [ t () ] -> Maybe (AList t ())
fromReverseList' :: [t ()] -> Maybe (AList t ())
fromReverseList' = () -> [t ()] -> Maybe (AList t ())
forall (t :: * -> *) a.
Spanned (t a) =>
a -> [t a] -> Maybe (AList t a)
fromList' () ([t ()] -> Maybe (AList t ()))
-> ([t ()] -> [t ()]) -> [t ()] -> Maybe (AList t ())
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [t ()] -> [t ()]
forall a. [a] -> [a]
reverse

aCons :: t a -> AList t a -> AList t a
aCons :: t a -> AList t a -> AList t a
aCons t a
x (AList a
a SrcSpan
s [t a]
xs) = a -> SrcSpan -> [t a] -> AList t a
forall (t :: * -> *) a. a -> SrcSpan -> [t a] -> AList t a
AList a
a SrcSpan
s ([t a] -> AList t a) -> [t a] -> AList t a
forall a b. (a -> b) -> a -> b
$ t a
xt a -> [t a] -> [t a]
forall a. a -> [a] -> [a]
:[t a]
xs

infixr 5 `aCons`

aReverse :: AList t a -> AList t a
aReverse :: AList t a -> AList t a
aReverse (AList a
a SrcSpan
s [t a]
xs) = a -> SrcSpan -> [t a] -> AList t a
forall (t :: * -> *) a. a -> SrcSpan -> [t a] -> AList t a
AList a
a SrcSpan
s ([t a] -> AList t a) -> [t a] -> AList t a
forall a b. (a -> b) -> a -> b
$ [t a] -> [t a]
forall a. [a] -> [a]
reverse [t a]
xs

aStrip :: AList t a -> [t a]
aStrip :: AList t a -> [t a]
aStrip (AList a
_ SrcSpan
_ [t a]
l) = [t a]
l

aStrip' :: Maybe (AList t a) -> [t a]
aStrip' :: Maybe (AList t a) -> [t a]
aStrip' Maybe (AList t a)
Nothing = []
aStrip' (Just AList t a
a) = AList t a -> [t a]
forall (t :: * -> *) a. AList t a -> [t a]
aStrip AList t a
a

aMap :: (t a -> r a) -> AList t a -> AList r a
aMap :: (t a -> r a) -> AList t a -> AList r a
aMap t a -> r a
f (AList a
a SrcSpan
s [t a]
xs) = a -> SrcSpan -> [r a] -> AList r a
forall (t :: * -> *) a. a -> SrcSpan -> [t a] -> AList t a
AList a
a SrcSpan
s ((t a -> r a) -> [t a] -> [r a]
forall a b. (a -> b) -> [a] -> [b]
map t a -> r a
f [t a]
xs)