{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE DeriveGeneric #-}

-- | Fortran version enum and tools for selecting version for a given file.

module Language.Fortran.Version
  ( FortranVersion(..)
  , fortranVersionAliases
  , selectFortranVersion
  , deduceFortranVersion
  , deduceVersion -- deprecated
  ) where

import           Data.Char (toLower)
import           Data.List (isInfixOf, isSuffixOf, find)

import Data.Data (Data, Typeable)
import GHC.Generics (Generic)

data FortranVersion = Fortran66
                    | Fortran77
                    | Fortran77Extended
                    | Fortran77Legacy
                    | Fortran90
                    | Fortran95
                    | Fortran2003
                    | Fortran2008
                    deriving (Eq FortranVersion
Eq FortranVersion
-> (FortranVersion -> FortranVersion -> Ordering)
-> (FortranVersion -> FortranVersion -> Bool)
-> (FortranVersion -> FortranVersion -> Bool)
-> (FortranVersion -> FortranVersion -> Bool)
-> (FortranVersion -> FortranVersion -> Bool)
-> (FortranVersion -> FortranVersion -> FortranVersion)
-> (FortranVersion -> FortranVersion -> FortranVersion)
-> Ord FortranVersion
FortranVersion -> FortranVersion -> Bool
FortranVersion -> FortranVersion -> Ordering
FortranVersion -> FortranVersion -> FortranVersion
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 :: FortranVersion -> FortranVersion -> FortranVersion
$cmin :: FortranVersion -> FortranVersion -> FortranVersion
max :: FortranVersion -> FortranVersion -> FortranVersion
$cmax :: FortranVersion -> FortranVersion -> FortranVersion
>= :: FortranVersion -> FortranVersion -> Bool
$c>= :: FortranVersion -> FortranVersion -> Bool
> :: FortranVersion -> FortranVersion -> Bool
$c> :: FortranVersion -> FortranVersion -> Bool
<= :: FortranVersion -> FortranVersion -> Bool
$c<= :: FortranVersion -> FortranVersion -> Bool
< :: FortranVersion -> FortranVersion -> Bool
$c< :: FortranVersion -> FortranVersion -> Bool
compare :: FortranVersion -> FortranVersion -> Ordering
$ccompare :: FortranVersion -> FortranVersion -> Ordering
$cp1Ord :: Eq FortranVersion
Ord, FortranVersion -> FortranVersion -> Bool
(FortranVersion -> FortranVersion -> Bool)
-> (FortranVersion -> FortranVersion -> Bool) -> Eq FortranVersion
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: FortranVersion -> FortranVersion -> Bool
$c/= :: FortranVersion -> FortranVersion -> Bool
== :: FortranVersion -> FortranVersion -> Bool
$c== :: FortranVersion -> FortranVersion -> Bool
Eq, Typeable FortranVersion
DataType
Constr
Typeable FortranVersion
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> FortranVersion -> c FortranVersion)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c FortranVersion)
-> (FortranVersion -> Constr)
-> (FortranVersion -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c FortranVersion))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e))
    -> Maybe (c FortranVersion))
-> ((forall b. Data b => b -> b)
    -> FortranVersion -> FortranVersion)
-> (forall r r'.
    (r -> r' -> r)
    -> r -> (forall d. Data d => d -> r') -> FortranVersion -> r)
-> (forall r r'.
    (r' -> r -> r)
    -> r -> (forall d. Data d => d -> r') -> FortranVersion -> r)
-> (forall u.
    (forall d. Data d => d -> u) -> FortranVersion -> [u])
-> (forall u.
    Int -> (forall d. Data d => d -> u) -> FortranVersion -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d)
    -> FortranVersion -> m FortranVersion)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d)
    -> FortranVersion -> m FortranVersion)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d)
    -> FortranVersion -> m FortranVersion)
-> Data FortranVersion
FortranVersion -> DataType
FortranVersion -> Constr
(forall b. Data b => b -> b) -> FortranVersion -> FortranVersion
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> FortranVersion -> c FortranVersion
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c FortranVersion
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) -> FortranVersion -> u
forall u. (forall d. Data d => d -> u) -> FortranVersion -> [u]
forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> FortranVersion -> r
forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> FortranVersion -> r
forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d)
-> FortranVersion -> m FortranVersion
forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> FortranVersion -> m FortranVersion
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c FortranVersion
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> FortranVersion -> c FortranVersion
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c FortranVersion)
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c FortranVersion)
$cFortran2008 :: Constr
$cFortran2003 :: Constr
$cFortran95 :: Constr
$cFortran90 :: Constr
$cFortran77Legacy :: Constr
$cFortran77Extended :: Constr
$cFortran77 :: Constr
$cFortran66 :: Constr
$tFortranVersion :: DataType
gmapMo :: (forall d. Data d => d -> m d)
-> FortranVersion -> m FortranVersion
$cgmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> FortranVersion -> m FortranVersion
gmapMp :: (forall d. Data d => d -> m d)
-> FortranVersion -> m FortranVersion
$cgmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> FortranVersion -> m FortranVersion
gmapM :: (forall d. Data d => d -> m d)
-> FortranVersion -> m FortranVersion
$cgmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d)
-> FortranVersion -> m FortranVersion
gmapQi :: Int -> (forall d. Data d => d -> u) -> FortranVersion -> u
$cgmapQi :: forall u.
Int -> (forall d. Data d => d -> u) -> FortranVersion -> u
gmapQ :: (forall d. Data d => d -> u) -> FortranVersion -> [u]
$cgmapQ :: forall u. (forall d. Data d => d -> u) -> FortranVersion -> [u]
gmapQr :: (r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> FortranVersion -> r
$cgmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> FortranVersion -> r
gmapQl :: (r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> FortranVersion -> r
$cgmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> FortranVersion -> r
gmapT :: (forall b. Data b => b -> b) -> FortranVersion -> FortranVersion
$cgmapT :: (forall b. Data b => b -> b) -> FortranVersion -> FortranVersion
dataCast2 :: (forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c FortranVersion)
$cdataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c FortranVersion)
dataCast1 :: (forall d. Data d => c (t d)) -> Maybe (c FortranVersion)
$cdataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c FortranVersion)
dataTypeOf :: FortranVersion -> DataType
$cdataTypeOf :: FortranVersion -> DataType
toConstr :: FortranVersion -> Constr
$ctoConstr :: FortranVersion -> Constr
gunfold :: (forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c FortranVersion
$cgunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c FortranVersion
gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> FortranVersion -> c FortranVersion
$cgfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> FortranVersion -> c FortranVersion
$cp1Data :: Typeable FortranVersion
Data, Typeable, (forall x. FortranVersion -> Rep FortranVersion x)
-> (forall x. Rep FortranVersion x -> FortranVersion)
-> Generic FortranVersion
forall x. Rep FortranVersion x -> FortranVersion
forall x. FortranVersion -> Rep FortranVersion x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep FortranVersion x -> FortranVersion
$cfrom :: forall x. FortranVersion -> Rep FortranVersion x
Generic)

instance Show FortranVersion where
  show :: FortranVersion -> String
show FortranVersion
Fortran66         = String
"Fortran 66"
  show FortranVersion
Fortran77         = String
"Fortran 77"
  show FortranVersion
Fortran77Extended = String
"Fortran 77 Extended"
  show FortranVersion
Fortran77Legacy   = String
"Fortran 77 Legacy"
  show FortranVersion
Fortran90         = String
"Fortran 90"
  show FortranVersion
Fortran95         = String
"Fortran 95"
  show FortranVersion
Fortran2003       = String
"Fortran 2003"
  show FortranVersion
Fortran2008       = String
"Fortran 2008"

fortranVersionAliases :: [(String, FortranVersion)]
fortranVersionAliases :: [(String, FortranVersion)]
fortranVersionAliases = [ (String
"66" , FortranVersion
Fortran66)
                        , (String
"77e", FortranVersion
Fortran77Extended)
                        , (String
"77l", FortranVersion
Fortran77Legacy)
                        , (String
"77" , FortranVersion
Fortran77)
                        , (String
"90" , FortranVersion
Fortran90)
                        , (String
"95" , FortranVersion
Fortran95)
                        , (String
"03" , FortranVersion
Fortran2003)
                        , (String
"08" , FortranVersion
Fortran2008) ]

selectFortranVersion :: String -> Maybe FortranVersion
selectFortranVersion :: String -> Maybe FortranVersion
selectFortranVersion String
alias = (String, FortranVersion) -> FortranVersion
forall a b. (a, b) -> b
snd ((String, FortranVersion) -> FortranVersion)
-> Maybe (String, FortranVersion) -> Maybe FortranVersion
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ((String, FortranVersion) -> Bool)
-> [(String, FortranVersion)] -> Maybe (String, FortranVersion)
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Maybe a
find (\ (String, FortranVersion)
entry -> (String, FortranVersion) -> String
forall a b. (a, b) -> a
fst (String, FortranVersion)
entry String -> String -> Bool
forall a. Eq a => [a] -> [a] -> Bool
`isInfixOf` (Char -> Char) -> ShowS
forall a b. (a -> b) -> [a] -> [b]
map Char -> Char
toLower String
alias) [(String, FortranVersion)]
fortranVersionAliases

-- | Deduce the 'FortranVersion' from a 'FilePath' using extension.
--
-- Defaults to Fortran 90 if suffix is unrecognized.
deduceFortranVersion :: FilePath -> FortranVersion
deduceFortranVersion :: String -> FortranVersion
deduceFortranVersion String
path
  | String -> Bool
isExtensionOf String
".f"      = FortranVersion
Fortran77Extended
  | String -> Bool
isExtensionOf String
".for"    = FortranVersion
Fortran77
  | String -> Bool
isExtensionOf String
".fpp"    = FortranVersion
Fortran77
  | String -> Bool
isExtensionOf String
".ftn"    = FortranVersion
Fortran77
  | String -> Bool
isExtensionOf String
".f90"    = FortranVersion
Fortran90
  | String -> Bool
isExtensionOf String
".f95"    = FortranVersion
Fortran95
  | String -> Bool
isExtensionOf String
".f03"    = FortranVersion
Fortran2003
  | String -> Bool
isExtensionOf String
".f2003"  = FortranVersion
Fortran2003
  | String -> Bool
isExtensionOf String
".f08"    = FortranVersion
Fortran2008
  | String -> Bool
isExtensionOf String
".f2008"  = FortranVersion
Fortran2008
  | Bool
otherwise               = FortranVersion
Fortran90         -- unrecognized, default to F90
  where
    isExtensionOf :: String -> Bool
isExtensionOf = (String -> String -> Bool) -> String -> String -> Bool
forall a b c. (a -> b -> c) -> b -> a -> c
flip String -> String -> Bool
forall a. Eq a => [a] -> [a] -> Bool
isSuffixOf (String -> String -> Bool) -> String -> String -> Bool
forall a b. (a -> b) -> a -> b
$ (Char -> Char) -> ShowS
forall a b. (a -> b) -> [a] -> [b]
map Char -> Char
toLower String
path

-- | Alias for previous function name. TODO: deprecate eventually.
deduceVersion :: FilePath -> FortranVersion
deduceVersion :: String -> FortranVersion
deduceVersion = String -> FortranVersion
deduceFortranVersion