2016-01-20 22:39:17 +00:00
|
|
|
{-# LANGUAGE CPP, TemplateHaskell #-}
|
2015-02-08 13:17:53 +00:00
|
|
|
module NotCPP.Utils where
|
|
|
|
|
|
|
|
import Control.Applicative ((<$>))
|
|
|
|
import Language.Haskell.TH
|
|
|
|
|
|
|
|
-- | Turns 'Nothing' into an expression representing 'Nothing', and
|
|
|
|
-- @'Just' x@ into an expression representing 'Just' applied to the
|
|
|
|
-- expression in @x@.
|
|
|
|
liftMaybe :: Maybe Exp -> Exp
|
|
|
|
liftMaybe = maybe (ConE 'Nothing) (AppE (ConE 'Just))
|
|
|
|
|
|
|
|
-- | A useful variant of 'reify' that returns 'Nothing' instead of
|
|
|
|
-- halting compilation when an error occurs (e.g. because the given
|
|
|
|
-- name was not in scope).
|
|
|
|
maybeReify :: Name -> Q (Maybe Info)
|
|
|
|
maybeReify = recoverMaybe . reify
|
|
|
|
|
|
|
|
-- | Turns a possibly-failing 'Q' action into one returning a 'Maybe'
|
|
|
|
-- value.
|
|
|
|
recoverMaybe :: Q a -> Q (Maybe a)
|
|
|
|
recoverMaybe q = recover (return Nothing) (Just <$> q)
|
|
|
|
|
|
|
|
-- | Returns @'Just' ('VarE' n)@ if the info relates to a value called
|
|
|
|
-- @n@, or 'Nothing' if it relates to a different sort of thing.
|
|
|
|
infoToExp :: Info -> Maybe Exp
|
2016-01-20 22:39:17 +00:00
|
|
|
|
|
|
|
#if __GLASGOW_HASKELL__ >= 800
|
|
|
|
infoToExp (VarI n _ _) =
|
|
|
|
#else
|
|
|
|
infoToExp (VarI n _ _ _) =
|
|
|
|
#endif
|
|
|
|
Just (VarE n)
|
|
|
|
|
|
|
|
#if __GLASGOW_HASKELL__ >= 800
|
|
|
|
infoToExp (DataConI n _ _) =
|
|
|
|
#else
|
|
|
|
infoToExp (DataConI n _ _ _) =
|
|
|
|
#endif
|
|
|
|
Just (ConE n)
|
|
|
|
|
2015-02-08 13:17:53 +00:00
|
|
|
infoToExp _ = Nothing
|