shrub/pkg/hs-urbit/lib/Noun/Convert.hs

196 lines
4.7 KiB
Haskell
Raw Normal View History

2019-07-12 04:16:40 +03:00
module Noun.Convert
( ToNoun(toNoun)
2019-07-16 03:01:45 +03:00
, FromNoun(parseNoun), fromNoun, fromNounErr, fromNounExn
2019-07-12 04:16:40 +03:00
, Parser(..)
, ParseStack
2019-07-22 21:10:27 +03:00
, parseNounUtf8Atom
, named
2019-07-12 04:16:40 +03:00
) where
import ClassyPrelude hiding (hash)
2019-07-12 22:24:44 +03:00
import Noun.Core
2019-07-12 04:16:40 +03:00
import qualified Control.Monad.Fail as Fail
-- Types -----------------------------------------------------------------------
type ParseStack = [Text]
2019-07-12 04:16:40 +03:00
-- IResult ---------------------------------------------------------------------
data IResult a = IError ParseStack String | ISuccess a
2019-07-12 04:16:40 +03:00
deriving (Eq, Show, Typeable, Functor, Foldable, Traversable)
instance Applicative IResult where
pure = ISuccess
(<*>) = ap
instance Fail.MonadFail IResult where
fail err = IError [] err
instance Monad IResult where
return = pure
fail = Fail.fail
ISuccess a >>= k = k a
IError path err >>= _ = IError path err
instance MonadPlus IResult where
mzero = fail "mzero"
mplus a@(ISuccess _) _ = a
mplus _ b = b
instance Alternative IResult where
empty = mzero
(<|>) = mplus
instance Semigroup (IResult a) where
(<>) = mplus
instance Monoid (IResult a) where
mempty = fail "mempty"
mappend = (<>)
-- Result ----------------------------------------------------------------------
data Result a = Error String | Success a
deriving (Eq, Show, Typeable, Functor, Foldable, Traversable)
instance Applicative Result where
pure = Success
(<*>) = ap
instance Fail.MonadFail Result where
fail err = Error err
instance Monad Result where
return = pure
fail = Fail.fail
Success a >>= k = k a
Error err >>= _ = Error err
instance MonadPlus Result where
mzero = fail "mzero"
mplus a@(Success _) _ = a
mplus _ b = b
instance Alternative Result where
empty = mzero
(<|>) = mplus
instance Semigroup (Result a) where
(<>) = mplus
{-# INLINE (<>) #-}
instance Monoid (Result a) where
mempty = fail "mempty"
mappend = (<>)
-- "Parser" --------------------------------------------------------------------
type Failure f r = ParseStack -> String -> f r
2019-07-12 04:16:40 +03:00
type Success a f r = a -> f r
newtype Parser a = Parser {
runParser :: forall f r. ParseStack -> Failure f r -> Success a f r -> f r
2019-07-12 04:16:40 +03:00
}
named :: Text -> Parser a -> Parser a
named nm (Parser cb) =
Parser $ \path kf ks -> cb (nm:path) kf ks
2019-07-12 04:16:40 +03:00
instance Monad Parser where
m >>= g = Parser $ \path kf ks -> let ks' a = runParser (g a) path kf ks
in runParser m path kf ks'
return = pure
fail = Fail.fail
instance Fail.MonadFail Parser where
fail msg = Parser $ \path kf _ks -> kf (reverse path) msg
instance Functor Parser where
fmap f m = Parser $ \path kf ks -> let ks' a = ks (f a)
in runParser m path kf ks'
apP :: Parser (a -> b) -> Parser a -> Parser b
apP d e = do
b <- d
b <$> e
instance Applicative Parser where
pure a = Parser $ \_path _kf ks -> ks a
(<*>) = apP
instance Alternative Parser where
empty = fail "empty"
(<|>) = mplus
instance MonadPlus Parser where
mzero = fail "mzero"
mplus a b = Parser $ \path kf ks -> let kf' _ _ = runParser b path kf ks
in runParser a path kf' ks
instance Semigroup (Parser a) where
(<>) = mplus
instance Monoid (Parser a) where
mempty = fail "mempty"
mappend = (<>)
-- Conversion ------------------------------------------------------------------
class FromNoun a where
parseNoun :: Noun -> Parser a
class ToNoun a where
toNoun :: a -> Noun
--------------------------------------------------------------------------------
fromNoun :: FromNoun a => Noun -> Maybe a
fromNoun n = runParser (parseNoun n) [] onFail onSuccess
where
onFail p m = Nothing
onSuccess x = Just x
fromNounErr :: FromNoun a => Noun -> Either ([Text], Text) a
2019-07-12 04:16:40 +03:00
fromNounErr n = runParser (parseNoun n) [] onFail onSuccess
where
onFail p m = Left (p, pack m)
2019-07-12 04:16:40 +03:00
onSuccess x = Right x
data BadNoun = BadNoun [Text] String
deriving (Eq, Ord)
instance Show BadNoun where
show (BadNoun pax msg) =
mconcat [ "(BadNoun "
, show (intercalate "." pax)
, " "
, show msg
, ")"
]
2019-07-16 03:01:45 +03:00
instance Exception BadNoun where
fromNounExn :: FromNoun a => Noun -> IO a
fromNounExn n = runParser (parseNoun n) [] onFail onSuccess
where
onFail p m = throwIO (BadNoun p m)
2019-07-16 03:01:45 +03:00
onSuccess x = pure x
2019-07-12 04:16:40 +03:00
-- Cord Conversions ------------------------------------------------------------
2019-07-22 21:10:27 +03:00
parseNounUtf8Atom :: Noun -> Parser Text
parseNounUtf8Atom n =
named "utf8-atom" $ do
case utf8AtomToText n of
Left err -> fail (unpack err)
Right tx -> pure tx