{-# LANGUAGE CPP #-}
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE ViewPatterns #-}
module Env.Internal.Parser
( Parser(..)
, VarF(..)
, parsePure
, eachUnsetVar
, Mod(..)
, prefixed
, var
, Var(..)
, defaultVar
, Reader
, str
, nonempty
, splitOn
, auto
, def
, helpDef
, showDef
, flag
, switch
, Flag
, HasHelp
, help
, HasKeep
, keep
) where
import Control.Applicative
import Control.Arrow (left)
import Control.Monad ((<=<))
import Data.Foldable (for_)
import Data.Map (Map)
import qualified Data.Map as Map
import qualified Data.Set as Set
#if __GLASGOW_HASKELL__ < 710
import Data.Monoid (Monoid(..))
#endif
#if MIN_VERSION_base(4,9,0)
import Data.Semigroup (Semigroup(..))
#endif
import Data.String (IsString(..))
import Env.Internal.Free
import qualified Env.Internal.Error as Error
import Env.Internal.Val
parsePure :: Parser e a -> [(String, String)] -> Either [(String, e)] a
parsePure (Parser p) (Map.fromList -> env) =
toEither (runAlt go p)
where
go v = maybe id (\d x -> x <|> pure d) (varfDef v) (fromEither (readVar v env))
eachUnsetVar :: Applicative m => Parser e a -> (String -> m b) -> m ()
eachUnsetVar Parser {unParser} =
for_ (foldAlt (\VarF {varfKeep, varfName} -> if varfKeep then Set.empty else Set.singleton varfName) unParser)
readVar :: VarF e a -> Map String String -> Either [(String, e)] a
readVar VarF {varfName, varfReader} =
left (pure . (\err -> (varfName, err))) . varfReader varfName
newtype Parser e a = Parser { unParser :: Alt (VarF e) a }
deriving (Functor)
instance Applicative (Parser e) where
pure =
Parser . pure
Parser f <*> Parser x =
Parser (f <*> x)
instance Alternative (Parser e) where
empty =
Parser empty
Parser f <|> Parser x =
Parser (f <|> x)
prefixed :: String -> Parser e a -> Parser e a
prefixed pre =
Parser . hoistAlt (\v -> v {varfName=pre ++ varfName v}) . unParser
data VarF e a = VarF
{ varfName :: String
, varfReader :: String -> Map String String -> Either e a
, varfHelp :: Maybe String
, varfDef :: Maybe a
, varfHelpDef :: Maybe String
, varfKeep :: Bool
} deriving (Functor)
liftVarF :: VarF e a -> Parser e a
liftVarF =
Parser . liftAlt
type Reader e a = String -> Either e a
lookupVar :: Error.AsUnset e => String -> Map String String -> Either e String
lookupVar name =
maybe (Left Error.unset) Right . Map.lookup name
var :: Error.AsUnset e => Reader e a -> String -> Mod Var a -> Parser e a
var r n (Mod f) =
liftVarF $ VarF
{ varfName = n
, varfReader = \name -> r <=< lookupVar name
, varfHelp = varHelp
, varfDef = varDef
, varfHelpDef = varHelpDef <*> varDef
, varfKeep = varKeep
}
where
Var {varHelp, varDef, varHelpDef, varKeep} = f defaultVar
flag
:: a
-> a
-> String -> Mod Flag a -> Parser e a
flag f t n (Mod g) =
liftVarF $ VarF
{ varfName = n
, varfReader = \name env ->
pure $ case (nonempty :: Reader Error.Error String) =<< lookupVar name env of
Left _ -> f
Right _ -> t
, varfHelp = flagHelp
, varfDef = Just f
, varfHelpDef = Nothing
, varfKeep = flagKeep
}
where
Flag {flagHelp, flagKeep} = g defaultFlag
switch :: String -> Mod Flag Bool -> Parser e Bool
switch =
flag False True
str :: IsString s => Reader e s
str =
Right . fromString
nonempty :: (Error.AsEmpty e, IsString s) => Reader e s
nonempty =
fmap fromString . go where go [] = Left Error.empty; go xs = Right xs
auto :: (Error.AsUnread e, Read a) => Reader e a
auto s =
case reads s of [(v, "")] -> Right v; _ -> Left (Error.unread (show s))
splitOn :: Char -> Reader e [String]
splitOn sep = Right . go
where
go [] = []
go xs = go' xs
go' xs =
case break (== sep) xs of
(ys, []) ->
ys : []
(ys, _ : zs) ->
ys : go' zs
newtype Mod t a = Mod (t a -> t a)
#if MIN_VERSION_base(4,9,0)
instance Semigroup (Mod t a) where
Mod f <> Mod g = Mod (g . f)
#endif
instance Monoid (Mod t a) where
mempty = Mod id
#if MIN_VERSION_base(4,11,0)
#elif MIN_VERSION_base(4,9,0)
mappend = (<>)
#else
mappend (Mod f) (Mod g) = Mod (g . f)
#endif
data Var a = Var
{ varHelp :: Maybe String
, varHelpDef :: Maybe (a -> String)
, varDef :: Maybe a
, varKeep :: Bool
}
defaultVar :: Var a
defaultVar = Var
{ varHelp = Nothing
, varDef = Nothing
, varHelpDef = Nothing
, varKeep = defaultKeep
}
defaultKeep :: Bool
defaultKeep = False
def :: a -> Mod Var a
def d =
Mod (\v -> v {varDef=Just d})
data Flag a = Flag
{ flagHelp :: Maybe String
, flagKeep :: Bool
}
defaultFlag :: Flag a
defaultFlag = Flag
{ flagHelp = Nothing
, flagKeep = defaultKeep
}
helpDef :: (a -> String) -> Mod Var a
helpDef d =
Mod (\v -> v {varHelpDef=Just d})
showDef :: Show a => Mod Var a
showDef =
helpDef show
class HasHelp t where
setHelp :: String -> t a -> t a
instance HasHelp Var where
setHelp h v = v {varHelp=Just h}
instance HasHelp Flag where
setHelp h v = v {flagHelp=Just h}
help :: HasHelp t => String -> Mod t a
help =
Mod . setHelp
class HasKeep t where
setKeep :: t a -> t a
instance HasKeep Var where
setKeep v = v {varKeep=True}
instance HasKeep Flag where
setKeep v = v {flagKeep=True}
keep :: HasKeep t => Mod t a
keep =
Mod setKeep