{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE DeriveGeneric #-}
module Distribution.Simple.InstallDirs.Internal
  ( PathComponent(..)
  , PathTemplateVariable(..)
  ) where

import Prelude ()
import Distribution.Compat.Prelude

data PathComponent =
       Ordinary FilePath
     | Variable PathTemplateVariable
     deriving (PathComponent -> PathComponent -> Bool
(PathComponent -> PathComponent -> Bool)
-> (PathComponent -> PathComponent -> Bool) -> Eq PathComponent
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: PathComponent -> PathComponent -> Bool
$c/= :: PathComponent -> PathComponent -> Bool
== :: PathComponent -> PathComponent -> Bool
$c== :: PathComponent -> PathComponent -> Bool
Eq, Eq PathComponent
Eq PathComponent
-> (PathComponent -> PathComponent -> Ordering)
-> (PathComponent -> PathComponent -> Bool)
-> (PathComponent -> PathComponent -> Bool)
-> (PathComponent -> PathComponent -> Bool)
-> (PathComponent -> PathComponent -> Bool)
-> (PathComponent -> PathComponent -> PathComponent)
-> (PathComponent -> PathComponent -> PathComponent)
-> Ord PathComponent
PathComponent -> PathComponent -> Bool
PathComponent -> PathComponent -> Ordering
PathComponent -> PathComponent -> PathComponent
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 :: PathComponent -> PathComponent -> PathComponent
$cmin :: PathComponent -> PathComponent -> PathComponent
max :: PathComponent -> PathComponent -> PathComponent
$cmax :: PathComponent -> PathComponent -> PathComponent
>= :: PathComponent -> PathComponent -> Bool
$c>= :: PathComponent -> PathComponent -> Bool
> :: PathComponent -> PathComponent -> Bool
$c> :: PathComponent -> PathComponent -> Bool
<= :: PathComponent -> PathComponent -> Bool
$c<= :: PathComponent -> PathComponent -> Bool
< :: PathComponent -> PathComponent -> Bool
$c< :: PathComponent -> PathComponent -> Bool
compare :: PathComponent -> PathComponent -> Ordering
$ccompare :: PathComponent -> PathComponent -> Ordering
$cp1Ord :: Eq PathComponent
Ord, (forall x. PathComponent -> Rep PathComponent x)
-> (forall x. Rep PathComponent x -> PathComponent)
-> Generic PathComponent
forall x. Rep PathComponent x -> PathComponent
forall x. PathComponent -> Rep PathComponent x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep PathComponent x -> PathComponent
$cfrom :: forall x. PathComponent -> Rep PathComponent x
Generic, Typeable)

instance Binary PathComponent
instance Structured PathComponent

data PathTemplateVariable =
       PrefixVar     -- ^ The @$prefix@ path variable
     | BindirVar     -- ^ The @$bindir@ path variable
     | LibdirVar     -- ^ The @$libdir@ path variable
     | LibsubdirVar  -- ^ The @$libsubdir@ path variable
     | DynlibdirVar  -- ^ The @$dynlibdir@ path variable
     | DatadirVar    -- ^ The @$datadir@ path variable
     | DatasubdirVar -- ^ The @$datasubdir@ path variable
     | DocdirVar     -- ^ The @$docdir@ path variable
     | HtmldirVar    -- ^ The @$htmldir@ path variable
     | PkgNameVar    -- ^ The @$pkg@ package name path variable
     | PkgVerVar     -- ^ The @$version@ package version path variable
     | PkgIdVar      -- ^ The @$pkgid@ package Id path variable, eg @foo-1.0@
     | LibNameVar    -- ^ The @$libname@ path variable
     | CompilerVar   -- ^ The compiler name and version, eg @ghc-6.6.1@
     | OSVar         -- ^ The operating system name, eg @windows@ or @linux@
     | ArchVar       -- ^ The CPU architecture name, eg @i386@ or @x86_64@
     | AbiVar        -- ^ The compiler's ABI identifier,
                     ---  $arch-$os-$compiler-$abitag
     | AbiTagVar     -- ^ The optional ABI tag for the compiler
     | ExecutableNameVar -- ^ The executable name; used in shell wrappers
     | TestSuiteNameVar   -- ^ The name of the test suite being run
     | TestSuiteResultVar -- ^ The result of the test suite being run, eg
                          -- @pass@, @fail@, or @error@.
     | BenchmarkNameVar   -- ^ The name of the benchmark being run
  deriving (PathTemplateVariable -> PathTemplateVariable -> Bool
(PathTemplateVariable -> PathTemplateVariable -> Bool)
-> (PathTemplateVariable -> PathTemplateVariable -> Bool)
-> Eq PathTemplateVariable
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: PathTemplateVariable -> PathTemplateVariable -> Bool
$c/= :: PathTemplateVariable -> PathTemplateVariable -> Bool
== :: PathTemplateVariable -> PathTemplateVariable -> Bool
$c== :: PathTemplateVariable -> PathTemplateVariable -> Bool
Eq, Eq PathTemplateVariable
Eq PathTemplateVariable
-> (PathTemplateVariable -> PathTemplateVariable -> Ordering)
-> (PathTemplateVariable -> PathTemplateVariable -> Bool)
-> (PathTemplateVariable -> PathTemplateVariable -> Bool)
-> (PathTemplateVariable -> PathTemplateVariable -> Bool)
-> (PathTemplateVariable -> PathTemplateVariable -> Bool)
-> (PathTemplateVariable
    -> PathTemplateVariable -> PathTemplateVariable)
-> (PathTemplateVariable
    -> PathTemplateVariable -> PathTemplateVariable)
-> Ord PathTemplateVariable
PathTemplateVariable -> PathTemplateVariable -> Bool
PathTemplateVariable -> PathTemplateVariable -> Ordering
PathTemplateVariable
-> PathTemplateVariable -> PathTemplateVariable
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 :: PathTemplateVariable
-> PathTemplateVariable -> PathTemplateVariable
$cmin :: PathTemplateVariable
-> PathTemplateVariable -> PathTemplateVariable
max :: PathTemplateVariable
-> PathTemplateVariable -> PathTemplateVariable
$cmax :: PathTemplateVariable
-> PathTemplateVariable -> PathTemplateVariable
>= :: PathTemplateVariable -> PathTemplateVariable -> Bool
$c>= :: PathTemplateVariable -> PathTemplateVariable -> Bool
> :: PathTemplateVariable -> PathTemplateVariable -> Bool
$c> :: PathTemplateVariable -> PathTemplateVariable -> Bool
<= :: PathTemplateVariable -> PathTemplateVariable -> Bool
$c<= :: PathTemplateVariable -> PathTemplateVariable -> Bool
< :: PathTemplateVariable -> PathTemplateVariable -> Bool
$c< :: PathTemplateVariable -> PathTemplateVariable -> Bool
compare :: PathTemplateVariable -> PathTemplateVariable -> Ordering
$ccompare :: PathTemplateVariable -> PathTemplateVariable -> Ordering
$cp1Ord :: Eq PathTemplateVariable
Ord, (forall x. PathTemplateVariable -> Rep PathTemplateVariable x)
-> (forall x. Rep PathTemplateVariable x -> PathTemplateVariable)
-> Generic PathTemplateVariable
forall x. Rep PathTemplateVariable x -> PathTemplateVariable
forall x. PathTemplateVariable -> Rep PathTemplateVariable x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep PathTemplateVariable x -> PathTemplateVariable
$cfrom :: forall x. PathTemplateVariable -> Rep PathTemplateVariable x
Generic, Typeable)

instance Binary PathTemplateVariable
instance Structured PathTemplateVariable

instance Show PathTemplateVariable where
  show :: PathTemplateVariable -> String
show PathTemplateVariable
PrefixVar     = String
"prefix"
  show PathTemplateVariable
LibNameVar    = String
"libname"
  show PathTemplateVariable
BindirVar     = String
"bindir"
  show PathTemplateVariable
LibdirVar     = String
"libdir"
  show PathTemplateVariable
LibsubdirVar  = String
"libsubdir"
  show PathTemplateVariable
DynlibdirVar  = String
"dynlibdir"
  show PathTemplateVariable
DatadirVar    = String
"datadir"
  show PathTemplateVariable
DatasubdirVar = String
"datasubdir"
  show PathTemplateVariable
DocdirVar     = String
"docdir"
  show PathTemplateVariable
HtmldirVar    = String
"htmldir"
  show PathTemplateVariable
PkgNameVar    = String
"pkg"
  show PathTemplateVariable
PkgVerVar     = String
"version"
  show PathTemplateVariable
PkgIdVar      = String
"pkgid"
  show PathTemplateVariable
CompilerVar   = String
"compiler"
  show PathTemplateVariable
OSVar         = String
"os"
  show PathTemplateVariable
ArchVar       = String
"arch"
  show PathTemplateVariable
AbiTagVar     = String
"abitag"
  show PathTemplateVariable
AbiVar        = String
"abi"
  show PathTemplateVariable
ExecutableNameVar = String
"executablename"
  show PathTemplateVariable
TestSuiteNameVar   = String
"test-suite"
  show PathTemplateVariable
TestSuiteResultVar = String
"result"
  show PathTemplateVariable
BenchmarkNameVar   = String
"benchmark"

instance Read PathTemplateVariable where
  readsPrec :: Int -> ReadS PathTemplateVariable
readsPrec Int
_ String
s =
    Int
-> [(PathTemplateVariable, String)]
-> [(PathTemplateVariable, String)]
forall a. Int -> [a] -> [a]
take Int
1
    [ (PathTemplateVariable
var, Int -> ShowS
forall a. Int -> [a] -> [a]
drop (String -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length String
varStr) String
s)
    | (String
varStr, PathTemplateVariable
var) <- [(String, PathTemplateVariable)]
vars
    , String
varStr String -> String -> Bool
forall a. Eq a => [a] -> [a] -> Bool
`isPrefixOf` String
s ]
    -- NB: order matters! Longer strings first
    where vars :: [(String, PathTemplateVariable)]
vars = [(String
"prefix",     PathTemplateVariable
PrefixVar)
                 ,(String
"bindir",     PathTemplateVariable
BindirVar)
                 ,(String
"libdir",     PathTemplateVariable
LibdirVar)
                 ,(String
"libsubdir",  PathTemplateVariable
LibsubdirVar)
                 ,(String
"dynlibdir",  PathTemplateVariable
DynlibdirVar)
                 ,(String
"datadir",    PathTemplateVariable
DatadirVar)
                 ,(String
"datasubdir", PathTemplateVariable
DatasubdirVar)
                 ,(String
"docdir",     PathTemplateVariable
DocdirVar)
                 ,(String
"htmldir",    PathTemplateVariable
HtmldirVar)
                 ,(String
"pkgid",      PathTemplateVariable
PkgIdVar)
                 ,(String
"libname",    PathTemplateVariable
LibNameVar)
                 ,(String
"pkgkey",     PathTemplateVariable
LibNameVar) -- backwards compatibility
                 ,(String
"pkg",        PathTemplateVariable
PkgNameVar)
                 ,(String
"version",    PathTemplateVariable
PkgVerVar)
                 ,(String
"compiler",   PathTemplateVariable
CompilerVar)
                 ,(String
"os",         PathTemplateVariable
OSVar)
                 ,(String
"arch",       PathTemplateVariable
ArchVar)
                 ,(String
"abitag",     PathTemplateVariable
AbiTagVar)
                 ,(String
"abi",        PathTemplateVariable
AbiVar)
                 ,(String
"executablename", PathTemplateVariable
ExecutableNameVar)
                 ,(String
"test-suite", PathTemplateVariable
TestSuiteNameVar)
                 ,(String
"result", PathTemplateVariable
TestSuiteResultVar)
                 ,(String
"benchmark", PathTemplateVariable
BenchmarkNameVar)]

instance Show PathComponent where
  show :: PathComponent -> String
show (Ordinary String
path) = String
path
  show (Variable PathTemplateVariable
var)  = Char
'$'Char -> ShowS
forall a. a -> [a] -> [a]
:PathTemplateVariable -> String
forall a. Show a => a -> String
show PathTemplateVariable
var
  showList :: [PathComponent] -> ShowS
showList = (PathComponent -> ShowS -> ShowS)
-> ShowS -> [PathComponent] -> ShowS
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (\PathComponent
x -> (PathComponent -> ShowS
forall a. Show a => a -> ShowS
shows PathComponent
x ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.)) ShowS
forall a. a -> a
id

instance Read PathComponent where
  -- for some reason we collapse multiple $ symbols here
  readsPrec :: Int -> ReadS PathComponent
readsPrec Int
_ = ReadS PathComponent
lex0
    where lex0 :: ReadS PathComponent
lex0 [] = []
          lex0 (Char
'$':Char
'$':String
s') = ReadS PathComponent
lex0 (Char
'$'Char -> ShowS
forall a. a -> [a] -> [a]
:String
s')
          lex0 (Char
'$':String
s') = case [ (PathTemplateVariable -> PathComponent
Variable PathTemplateVariable
var, String
s'')
                               | (PathTemplateVariable
var, String
s'') <- ReadS PathTemplateVariable
forall a. Read a => ReadS a
reads String
s' ] of
                            [] -> String -> ReadS PathComponent
lex1 String
"$" String
s'
                            [(PathComponent, String)]
ok -> [(PathComponent, String)]
ok
          lex0 String
s' = String -> ReadS PathComponent
lex1 [] String
s'
          lex1 :: String -> ReadS PathComponent
lex1 String
""  String
""      = []
          lex1 String
acc String
""      = [(String -> PathComponent
Ordinary (ShowS
forall a. [a] -> [a]
reverse String
acc), String
"")]
          lex1 String
acc (Char
'$':Char
'$':String
s) = String -> ReadS PathComponent
lex1 String
acc (Char
'$'Char -> ShowS
forall a. a -> [a] -> [a]
:String
s)
          lex1 String
acc (Char
'$':String
s) = [(String -> PathComponent
Ordinary (ShowS
forall a. [a] -> [a]
reverse String
acc), Char
'$'Char -> ShowS
forall a. a -> [a] -> [a]
:String
s)]
          lex1 String
acc (Char
c:String
s)   = String -> ReadS PathComponent
lex1 (Char
cChar -> ShowS
forall a. a -> [a] -> [a]
:String
acc) String
s
  readList :: ReadS [PathComponent]
readList [] = [([],String
"")]
  readList String
s  = [ (PathComponent
componentPathComponent -> [PathComponent] -> [PathComponent]
forall a. a -> [a] -> [a]
:[PathComponent]
components, String
s'')
                | (PathComponent
component, String
s') <- ReadS PathComponent
forall a. Read a => ReadS a
reads String
s
                , ([PathComponent]
components, String
s'') <- ReadS [PathComponent]
forall a. Read a => ReadS [a]
readList String
s' ]