{-# OPTIONS_GHC -Wno-unrecognised-pragmas #-}
{-# HLINT ignore "Use section" #-}
module Toml.Semantics (
Value, Value'(..),
Table, Table'(..),
semantics,
SemanticError(..), SemanticErrorKind(..),
forgetTableAnns,
forgetValueAnns,
valueAnn,
valueType,
) where
import Control.Monad (foldM)
import Data.List.NonEmpty (NonEmpty((:|)))
import Data.List.NonEmpty qualified as NonEmpty
import Data.Map (Map)
import Data.Map qualified as Map
import Data.Text (Text)
import Toml.Syntax.Types (SectionKind(..), Key, Val(..), Expr(..))
import Toml.Semantics.Types
data SemanticError a = SemanticError {
forall a. SemanticError a -> a
errorAnn :: a,
forall a. SemanticError a -> Text
errorKey :: Text,
forall a. SemanticError a -> SemanticErrorKind
errorKind :: SemanticErrorKind
} deriving (
ReadPrec [SemanticError a]
ReadPrec (SemanticError a)
Int -> ReadS (SemanticError a)
ReadS [SemanticError a]
(Int -> ReadS (SemanticError a))
-> ReadS [SemanticError a]
-> ReadPrec (SemanticError a)
-> ReadPrec [SemanticError a]
-> Read (SemanticError a)
forall a. Read a => ReadPrec [SemanticError a]
forall a. Read a => ReadPrec (SemanticError a)
forall a. Read a => Int -> ReadS (SemanticError a)
forall a. Read a => ReadS [SemanticError a]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
$creadsPrec :: forall a. Read a => Int -> ReadS (SemanticError a)
readsPrec :: Int -> ReadS (SemanticError a)
$creadList :: forall a. Read a => ReadS [SemanticError a]
readList :: ReadS [SemanticError a]
$creadPrec :: forall a. Read a => ReadPrec (SemanticError a)
readPrec :: ReadPrec (SemanticError a)
$creadListPrec :: forall a. Read a => ReadPrec [SemanticError a]
readListPrec :: ReadPrec [SemanticError a]
Read ,
Int -> SemanticError a -> ShowS
[SemanticError a] -> ShowS
SemanticError a -> String
(Int -> SemanticError a -> ShowS)
-> (SemanticError a -> String)
-> ([SemanticError a] -> ShowS)
-> Show (SemanticError a)
forall a. Show a => Int -> SemanticError a -> ShowS
forall a. Show a => [SemanticError a] -> ShowS
forall a. Show a => SemanticError a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: forall a. Show a => Int -> SemanticError a -> ShowS
showsPrec :: Int -> SemanticError a -> ShowS
$cshow :: forall a. Show a => SemanticError a -> String
show :: SemanticError a -> String
$cshowList :: forall a. Show a => [SemanticError a] -> ShowS
showList :: [SemanticError a] -> ShowS
Show ,
SemanticError a -> SemanticError a -> Bool
(SemanticError a -> SemanticError a -> Bool)
-> (SemanticError a -> SemanticError a -> Bool)
-> Eq (SemanticError a)
forall a. Eq a => SemanticError a -> SemanticError a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: forall a. Eq a => SemanticError a -> SemanticError a -> Bool
== :: SemanticError a -> SemanticError a -> Bool
$c/= :: forall a. Eq a => SemanticError a -> SemanticError a -> Bool
/= :: SemanticError a -> SemanticError a -> Bool
Eq ,
Eq (SemanticError a)
Eq (SemanticError a) =>
(SemanticError a -> SemanticError a -> Ordering)
-> (SemanticError a -> SemanticError a -> Bool)
-> (SemanticError a -> SemanticError a -> Bool)
-> (SemanticError a -> SemanticError a -> Bool)
-> (SemanticError a -> SemanticError a -> Bool)
-> (SemanticError a -> SemanticError a -> SemanticError a)
-> (SemanticError a -> SemanticError a -> SemanticError a)
-> Ord (SemanticError a)
SemanticError a -> SemanticError a -> Bool
SemanticError a -> SemanticError a -> Ordering
SemanticError a -> SemanticError a -> SemanticError a
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
forall a. Ord a => Eq (SemanticError a)
forall a. Ord a => SemanticError a -> SemanticError a -> Bool
forall a. Ord a => SemanticError a -> SemanticError a -> Ordering
forall a.
Ord a =>
SemanticError a -> SemanticError a -> SemanticError a
$ccompare :: forall a. Ord a => SemanticError a -> SemanticError a -> Ordering
compare :: SemanticError a -> SemanticError a -> Ordering
$c< :: forall a. Ord a => SemanticError a -> SemanticError a -> Bool
< :: SemanticError a -> SemanticError a -> Bool
$c<= :: forall a. Ord a => SemanticError a -> SemanticError a -> Bool
<= :: SemanticError a -> SemanticError a -> Bool
$c> :: forall a. Ord a => SemanticError a -> SemanticError a -> Bool
> :: SemanticError a -> SemanticError a -> Bool
$c>= :: forall a. Ord a => SemanticError a -> SemanticError a -> Bool
>= :: SemanticError a -> SemanticError a -> Bool
$cmax :: forall a.
Ord a =>
SemanticError a -> SemanticError a -> SemanticError a
max :: SemanticError a -> SemanticError a -> SemanticError a
$cmin :: forall a.
Ord a =>
SemanticError a -> SemanticError a -> SemanticError a
min :: SemanticError a -> SemanticError a -> SemanticError a
Ord ,
(forall a b. (a -> b) -> SemanticError a -> SemanticError b)
-> (forall a b. a -> SemanticError b -> SemanticError a)
-> Functor SemanticError
forall a b. a -> SemanticError b -> SemanticError a
forall a b. (a -> b) -> SemanticError a -> SemanticError b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
$cfmap :: forall a b. (a -> b) -> SemanticError a -> SemanticError b
fmap :: forall a b. (a -> b) -> SemanticError a -> SemanticError b
$c<$ :: forall a b. a -> SemanticError b -> SemanticError a
<$ :: forall a b. a -> SemanticError b -> SemanticError a
Functor, (forall m. Monoid m => SemanticError m -> m)
-> (forall m a. Monoid m => (a -> m) -> SemanticError a -> m)
-> (forall m a. Monoid m => (a -> m) -> SemanticError a -> m)
-> (forall a b. (a -> b -> b) -> b -> SemanticError a -> b)
-> (forall a b. (a -> b -> b) -> b -> SemanticError a -> b)
-> (forall b a. (b -> a -> b) -> b -> SemanticError a -> b)
-> (forall b a. (b -> a -> b) -> b -> SemanticError a -> b)
-> (forall a. (a -> a -> a) -> SemanticError a -> a)
-> (forall a. (a -> a -> a) -> SemanticError a -> a)
-> (forall a. SemanticError a -> [a])
-> (forall a. SemanticError a -> Bool)
-> (forall a. SemanticError a -> Int)
-> (forall a. Eq a => a -> SemanticError a -> Bool)
-> (forall a. Ord a => SemanticError a -> a)
-> (forall a. Ord a => SemanticError a -> a)
-> (forall a. Num a => SemanticError a -> a)
-> (forall a. Num a => SemanticError a -> a)
-> Foldable SemanticError
forall a. Eq a => a -> SemanticError a -> Bool
forall a. Num a => SemanticError a -> a
forall a. Ord a => SemanticError a -> a
forall m. Monoid m => SemanticError m -> m
forall a. SemanticError a -> Bool
forall a. SemanticError a -> Int
forall a. SemanticError a -> [a]
forall a. (a -> a -> a) -> SemanticError a -> a
forall m a. Monoid m => (a -> m) -> SemanticError a -> m
forall b a. (b -> a -> b) -> b -> SemanticError a -> b
forall a b. (a -> b -> b) -> b -> SemanticError a -> b
forall (t :: * -> *).
(forall m. Monoid m => t m -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. t a -> [a])
-> (forall a. t a -> Bool)
-> (forall a. t a -> Int)
-> (forall a. Eq a => a -> t a -> Bool)
-> (forall a. Ord a => t a -> a)
-> (forall a. Ord a => t a -> a)
-> (forall a. Num a => t a -> a)
-> (forall a. Num a => t a -> a)
-> Foldable t
$cfold :: forall m. Monoid m => SemanticError m -> m
fold :: forall m. Monoid m => SemanticError m -> m
$cfoldMap :: forall m a. Monoid m => (a -> m) -> SemanticError a -> m
foldMap :: forall m a. Monoid m => (a -> m) -> SemanticError a -> m
$cfoldMap' :: forall m a. Monoid m => (a -> m) -> SemanticError a -> m
foldMap' :: forall m a. Monoid m => (a -> m) -> SemanticError a -> m
$cfoldr :: forall a b. (a -> b -> b) -> b -> SemanticError a -> b
foldr :: forall a b. (a -> b -> b) -> b -> SemanticError a -> b
$cfoldr' :: forall a b. (a -> b -> b) -> b -> SemanticError a -> b
foldr' :: forall a b. (a -> b -> b) -> b -> SemanticError a -> b
$cfoldl :: forall b a. (b -> a -> b) -> b -> SemanticError a -> b
foldl :: forall b a. (b -> a -> b) -> b -> SemanticError a -> b
$cfoldl' :: forall b a. (b -> a -> b) -> b -> SemanticError a -> b
foldl' :: forall b a. (b -> a -> b) -> b -> SemanticError a -> b
$cfoldr1 :: forall a. (a -> a -> a) -> SemanticError a -> a
foldr1 :: forall a. (a -> a -> a) -> SemanticError a -> a
$cfoldl1 :: forall a. (a -> a -> a) -> SemanticError a -> a
foldl1 :: forall a. (a -> a -> a) -> SemanticError a -> a
$ctoList :: forall a. SemanticError a -> [a]
toList :: forall a. SemanticError a -> [a]
$cnull :: forall a. SemanticError a -> Bool
null :: forall a. SemanticError a -> Bool
$clength :: forall a. SemanticError a -> Int
length :: forall a. SemanticError a -> Int
$celem :: forall a. Eq a => a -> SemanticError a -> Bool
elem :: forall a. Eq a => a -> SemanticError a -> Bool
$cmaximum :: forall a. Ord a => SemanticError a -> a
maximum :: forall a. Ord a => SemanticError a -> a
$cminimum :: forall a. Ord a => SemanticError a -> a
minimum :: forall a. Ord a => SemanticError a -> a
$csum :: forall a. Num a => SemanticError a -> a
sum :: forall a. Num a => SemanticError a -> a
$cproduct :: forall a. Num a => SemanticError a -> a
product :: forall a. Num a => SemanticError a -> a
Foldable, Functor SemanticError
Foldable SemanticError
(Functor SemanticError, Foldable SemanticError) =>
(forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> SemanticError a -> f (SemanticError b))
-> (forall (f :: * -> *) a.
Applicative f =>
SemanticError (f a) -> f (SemanticError a))
-> (forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> SemanticError a -> m (SemanticError b))
-> (forall (m :: * -> *) a.
Monad m =>
SemanticError (m a) -> m (SemanticError a))
-> Traversable SemanticError
forall (t :: * -> *).
(Functor t, Foldable t) =>
(forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> t a -> f (t b))
-> (forall (f :: * -> *) a. Applicative f => t (f a) -> f (t a))
-> (forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> t a -> m (t b))
-> (forall (m :: * -> *) a. Monad m => t (m a) -> m (t a))
-> Traversable t
forall (m :: * -> *) a.
Monad m =>
SemanticError (m a) -> m (SemanticError a)
forall (f :: * -> *) a.
Applicative f =>
SemanticError (f a) -> f (SemanticError a)
forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> SemanticError a -> m (SemanticError b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> SemanticError a -> f (SemanticError b)
$ctraverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> SemanticError a -> f (SemanticError b)
traverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> SemanticError a -> f (SemanticError b)
$csequenceA :: forall (f :: * -> *) a.
Applicative f =>
SemanticError (f a) -> f (SemanticError a)
sequenceA :: forall (f :: * -> *) a.
Applicative f =>
SemanticError (f a) -> f (SemanticError a)
$cmapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> SemanticError a -> m (SemanticError b)
mapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> SemanticError a -> m (SemanticError b)
$csequence :: forall (m :: * -> *) a.
Monad m =>
SemanticError (m a) -> m (SemanticError a)
sequence :: forall (m :: * -> *) a.
Monad m =>
SemanticError (m a) -> m (SemanticError a)
Traversable)
data SemanticErrorKind
= AlreadyAssigned
| ClosedTable
| ImplicitlyTable
deriving (
ReadPrec [SemanticErrorKind]
ReadPrec SemanticErrorKind
Int -> ReadS SemanticErrorKind
ReadS [SemanticErrorKind]
(Int -> ReadS SemanticErrorKind)
-> ReadS [SemanticErrorKind]
-> ReadPrec SemanticErrorKind
-> ReadPrec [SemanticErrorKind]
-> Read SemanticErrorKind
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
$creadsPrec :: Int -> ReadS SemanticErrorKind
readsPrec :: Int -> ReadS SemanticErrorKind
$creadList :: ReadS [SemanticErrorKind]
readList :: ReadS [SemanticErrorKind]
$creadPrec :: ReadPrec SemanticErrorKind
readPrec :: ReadPrec SemanticErrorKind
$creadListPrec :: ReadPrec [SemanticErrorKind]
readListPrec :: ReadPrec [SemanticErrorKind]
Read ,
Int -> SemanticErrorKind -> ShowS
[SemanticErrorKind] -> ShowS
SemanticErrorKind -> String
(Int -> SemanticErrorKind -> ShowS)
-> (SemanticErrorKind -> String)
-> ([SemanticErrorKind] -> ShowS)
-> Show SemanticErrorKind
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> SemanticErrorKind -> ShowS
showsPrec :: Int -> SemanticErrorKind -> ShowS
$cshow :: SemanticErrorKind -> String
show :: SemanticErrorKind -> String
$cshowList :: [SemanticErrorKind] -> ShowS
showList :: [SemanticErrorKind] -> ShowS
Show ,
SemanticErrorKind -> SemanticErrorKind -> Bool
(SemanticErrorKind -> SemanticErrorKind -> Bool)
-> (SemanticErrorKind -> SemanticErrorKind -> Bool)
-> Eq SemanticErrorKind
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: SemanticErrorKind -> SemanticErrorKind -> Bool
== :: SemanticErrorKind -> SemanticErrorKind -> Bool
$c/= :: SemanticErrorKind -> SemanticErrorKind -> Bool
/= :: SemanticErrorKind -> SemanticErrorKind -> Bool
Eq ,
Eq SemanticErrorKind
Eq SemanticErrorKind =>
(SemanticErrorKind -> SemanticErrorKind -> Ordering)
-> (SemanticErrorKind -> SemanticErrorKind -> Bool)
-> (SemanticErrorKind -> SemanticErrorKind -> Bool)
-> (SemanticErrorKind -> SemanticErrorKind -> Bool)
-> (SemanticErrorKind -> SemanticErrorKind -> Bool)
-> (SemanticErrorKind -> SemanticErrorKind -> SemanticErrorKind)
-> (SemanticErrorKind -> SemanticErrorKind -> SemanticErrorKind)
-> Ord SemanticErrorKind
SemanticErrorKind -> SemanticErrorKind -> Bool
SemanticErrorKind -> SemanticErrorKind -> Ordering
SemanticErrorKind -> SemanticErrorKind -> SemanticErrorKind
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
$ccompare :: SemanticErrorKind -> SemanticErrorKind -> Ordering
compare :: SemanticErrorKind -> SemanticErrorKind -> Ordering
$c< :: SemanticErrorKind -> SemanticErrorKind -> Bool
< :: SemanticErrorKind -> SemanticErrorKind -> Bool
$c<= :: SemanticErrorKind -> SemanticErrorKind -> Bool
<= :: SemanticErrorKind -> SemanticErrorKind -> Bool
$c> :: SemanticErrorKind -> SemanticErrorKind -> Bool
> :: SemanticErrorKind -> SemanticErrorKind -> Bool
$c>= :: SemanticErrorKind -> SemanticErrorKind -> Bool
>= :: SemanticErrorKind -> SemanticErrorKind -> Bool
$cmax :: SemanticErrorKind -> SemanticErrorKind -> SemanticErrorKind
max :: SemanticErrorKind -> SemanticErrorKind -> SemanticErrorKind
$cmin :: SemanticErrorKind -> SemanticErrorKind -> SemanticErrorKind
min :: SemanticErrorKind -> SemanticErrorKind -> SemanticErrorKind
Ord )
semantics :: [Expr a] -> Either (SemanticError a) (Table' a)
semantics :: forall a. [Expr a] -> Either (SemanticError a) (Table' a)
semantics [Expr a]
exprs =
do [(Key a, Val a)] -> Either (SemanticError a) (FrameTable a)
f <- (([(Key a, Val a)] -> Either (SemanticError a) (FrameTable a))
-> Expr a
-> Either
(SemanticError a)
([(Key a, Val a)] -> Either (SemanticError a) (FrameTable a)))
-> ([(Key a, Val a)] -> Either (SemanticError a) (FrameTable a))
-> [Expr a]
-> Either
(SemanticError a)
([(Key a, Val a)] -> Either (SemanticError a) (FrameTable a))
forall (t :: * -> *) (m :: * -> *) b a.
(Foldable t, Monad m) =>
(b -> a -> m b) -> b -> t a -> m b
foldM ([(Key a, Val a)] -> Either (SemanticError a) (FrameTable a))
-> Expr a
-> Either
(SemanticError a)
([(Key a, Val a)] -> Either (SemanticError a) (FrameTable a))
forall {a}.
([(Key a, Val a)] -> Either (SemanticError a) (FrameTable a))
-> Expr a
-> Either
(SemanticError a)
([(Key a, Val a)] -> Either (SemanticError a) (FrameTable a))
processExpr (([(Key a, Val a)]
-> FrameTable a -> Either (SemanticError a) (FrameTable a))
-> FrameTable a
-> [(Key a, Val a)]
-> Either (SemanticError a) (FrameTable a)
forall a b c. (a -> b -> c) -> b -> a -> c
flip [(Key a, Val a)]
-> FrameTable a -> Either (SemanticError a) (FrameTable a)
forall a. [(Key a, Val a)] -> FrameTable a -> M a (FrameTable a)
assignKeyVals FrameTable a
forall k a. Map k a
Map.empty) [Expr a]
exprs
FrameTable a -> Table' a
forall a. FrameTable a -> Table' a
framesToTable (FrameTable a -> Table' a)
-> Either (SemanticError a) (FrameTable a)
-> Either (SemanticError a) (Table' a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [(Key a, Val a)] -> Either (SemanticError a) (FrameTable a)
f []
where
processExpr :: ([(Key a, Val a)] -> Either (SemanticError a) (FrameTable a))
-> Expr a
-> Either
(SemanticError a)
([(Key a, Val a)] -> Either (SemanticError a) (FrameTable a))
processExpr [(Key a, Val a)] -> Either (SemanticError a) (FrameTable a)
f = \case
KeyValExpr Key a
k Val a
v -> ([(Key a, Val a)] -> Either (SemanticError a) (FrameTable a))
-> Either
(SemanticError a)
([(Key a, Val a)] -> Either (SemanticError a) (FrameTable a))
forall a b. b -> Either a b
Right ([(Key a, Val a)] -> Either (SemanticError a) (FrameTable a)
f ([(Key a, Val a)] -> Either (SemanticError a) (FrameTable a))
-> ([(Key a, Val a)] -> [(Key a, Val a)])
-> [(Key a, Val a)]
-> Either (SemanticError a) (FrameTable a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((Key a
k,Val a
v)(Key a, Val a) -> [(Key a, Val a)] -> [(Key a, Val a)]
forall a. a -> [a] -> [a]
:))
TableExpr Key a
k -> SectionKind
-> Key a
-> Either
(SemanticError a)
([(Key a, Val a)] -> Either (SemanticError a) (FrameTable a))
processSection SectionKind
TableKind Key a
k
ArrayTableExpr Key a
k -> SectionKind
-> Key a
-> Either
(SemanticError a)
([(Key a, Val a)] -> Either (SemanticError a) (FrameTable a))
processSection SectionKind
ArrayTableKind Key a
k
where
processSection :: SectionKind
-> Key a
-> Either
(SemanticError a)
([(Key a, Val a)] -> Either (SemanticError a) (FrameTable a))
processSection SectionKind
kind Key a
k = ([(Key a, Val a)]
-> FrameTable a -> Either (SemanticError a) (FrameTable a))
-> FrameTable a
-> [(Key a, Val a)]
-> Either (SemanticError a) (FrameTable a)
forall a b c. (a -> b -> c) -> b -> a -> c
flip (SectionKind
-> Key a
-> [(Key a, Val a)]
-> FrameTable a
-> Either (SemanticError a) (FrameTable a)
forall a.
SectionKind
-> Key a -> [(Key a, Val a)] -> FrameTable a -> M a (FrameTable a)
addSection SectionKind
kind Key a
k) (FrameTable a
-> [(Key a, Val a)] -> Either (SemanticError a) (FrameTable a))
-> Either (SemanticError a) (FrameTable a)
-> Either
(SemanticError a)
([(Key a, Val a)] -> Either (SemanticError a) (FrameTable a))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [(Key a, Val a)] -> Either (SemanticError a) (FrameTable a)
f []
type FrameTable a = Map Text (a, Frame a)
type M a = Either (SemanticError a)
data Frame a
= FrameTable a FrameKind (FrameTable a)
| FrameArray (NonEmpty (a, FrameTable a))
| FrameValue (Value' a)
deriving Int -> Frame a -> ShowS
[Frame a] -> ShowS
Frame a -> String
(Int -> Frame a -> ShowS)
-> (Frame a -> String) -> ([Frame a] -> ShowS) -> Show (Frame a)
forall a. Show a => Int -> Frame a -> ShowS
forall a. Show a => [Frame a] -> ShowS
forall a. Show a => Frame a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: forall a. Show a => Int -> Frame a -> ShowS
showsPrec :: Int -> Frame a -> ShowS
$cshow :: forall a. Show a => Frame a -> String
show :: Frame a -> String
$cshowList :: forall a. Show a => [Frame a] -> ShowS
showList :: [Frame a] -> ShowS
Show
data FrameKind
= Open
| Dotted
| Closed
deriving Int -> FrameKind -> ShowS
[FrameKind] -> ShowS
FrameKind -> String
(Int -> FrameKind -> ShowS)
-> (FrameKind -> String)
-> ([FrameKind] -> ShowS)
-> Show FrameKind
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> FrameKind -> ShowS
showsPrec :: Int -> FrameKind -> ShowS
$cshow :: FrameKind -> String
show :: FrameKind -> String
$cshowList :: [FrameKind] -> ShowS
showList :: [FrameKind] -> ShowS
Show
framesToTable :: FrameTable a -> Table' a
framesToTable :: forall a. FrameTable a -> Table' a
framesToTable = (Map Text (a, Value' a) -> Table' a)
-> (FrameTable a -> Map Text (a, Value' a))
-> FrameTable a
-> Table' a
forall a b. (a -> b) -> (FrameTable a -> a) -> FrameTable a -> b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Map Text (a, Value' a) -> Table' a
forall a. Map Text (a, Value' a) -> Table' a
MkTable ((FrameTable a -> Map Text (a, Value' a))
-> FrameTable a -> Table' a)
-> (FrameTable a -> Map Text (a, Value' a))
-> FrameTable a
-> Table' a
forall a b. (a -> b) -> a -> b
$ ((a, Frame a) -> (a, Value' a))
-> FrameTable a -> Map Text (a, Value' a)
forall a b. (a -> b) -> Map Text a -> Map Text b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (((a, Frame a) -> (a, Value' a))
-> FrameTable a -> Map Text (a, Value' a))
-> ((a, Frame a) -> (a, Value' a))
-> FrameTable a
-> Map Text (a, Value' a)
forall a b. (a -> b) -> a -> b
$ (Frame a -> Value' a) -> (a, Frame a) -> (a, Value' a)
forall a b. (a -> b) -> (a, a) -> (a, b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap
\case
FrameTable a
a FrameKind
_kind FrameTable a
t -> a -> Table' a -> Value' a
forall a. a -> Table' a -> Value' a
Table' a
a (FrameTable a -> Table' a
forall a. FrameTable a -> Table' a
framesToTable FrameTable a
t)
FrameArray (NonEmpty (a, FrameTable a) -> NonEmpty (a, FrameTable a)
forall a. NonEmpty a -> NonEmpty a
NonEmpty.reverse -> (a, FrameTable a)
t :| [(a, FrameTable a)]
ts) ->
a -> [Value' a] -> Value' a
forall a. a -> [Value' a] -> Value' a
List' ((a, FrameTable a) -> a
forall a b. (a, b) -> a
fst (a, FrameTable a)
t) [a -> Table' a -> Value' a
forall a. a -> Table' a -> Value' a
Table' a
a (FrameTable a -> Table' a
forall a. FrameTable a -> Table' a
framesToTable FrameTable a
x) | (a
a, FrameTable a
x) <- (a, FrameTable a)
t (a, FrameTable a) -> [(a, FrameTable a)] -> [(a, FrameTable a)]
forall a. a -> [a] -> [a]
: [(a, FrameTable a)]
ts]
FrameValue Value' a
v -> Value' a
v
addSection ::
SectionKind ->
Key a ->
[(Key a, Val a)] ->
FrameTable a ->
M a (FrameTable a)
addSection :: forall a.
SectionKind
-> Key a -> [(Key a, Val a)] -> FrameTable a -> M a (FrameTable a)
addSection SectionKind
kind ((a, Text)
k :| []) [(NonEmpty (a, Text), Val a)]
kvs =
(a, Text)
-> M a (Frame a)
-> (Frame a -> M a (Frame a))
-> FrameTable a
-> M a (FrameTable a)
forall a.
(a, Text)
-> M a (Frame a)
-> (Frame a -> M a (Frame a))
-> FrameTable a
-> M a (FrameTable a)
alterFrame (a, Text)
k
(case SectionKind
kind of
SectionKind
TableKind -> a -> FrameKind -> FrameTable a -> Frame a
forall a. a -> FrameKind -> FrameTable a -> Frame a
FrameTable ((a, Text) -> a
forall a b. (a, b) -> a
fst (a, Text)
k) FrameKind
Closed (FrameTable a -> Frame a) -> M a (FrameTable a) -> M a (Frame a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> FrameTable a -> M a (FrameTable a)
go FrameTable a
forall a. Monoid a => a
mempty
SectionKind
ArrayTableKind -> NonEmpty (a, FrameTable a) -> Frame a
forall a. NonEmpty (a, FrameTable a) -> Frame a
FrameArray (NonEmpty (a, FrameTable a) -> Frame a)
-> (FrameTable a -> NonEmpty (a, FrameTable a))
-> FrameTable a
-> Frame a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((a, FrameTable a)
-> [(a, FrameTable a)] -> NonEmpty (a, FrameTable a)
forall a. a -> [a] -> NonEmpty a
:| []) ((a, FrameTable a) -> NonEmpty (a, FrameTable a))
-> (FrameTable a -> (a, FrameTable a))
-> FrameTable a
-> NonEmpty (a, FrameTable a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (,) ((a, Text) -> a
forall a b. (a, b) -> a
fst (a, Text)
k) (FrameTable a -> Frame a) -> M a (FrameTable a) -> M a (Frame a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> FrameTable a -> M a (FrameTable a)
go FrameTable a
forall a. Monoid a => a
mempty)
\case
FrameTable a
_ FrameKind
Open FrameTable a
t ->
case SectionKind
kind of
SectionKind
TableKind -> a -> FrameKind -> FrameTable a -> Frame a
forall a. a -> FrameKind -> FrameTable a -> Frame a
FrameTable ((a, Text) -> a
forall a b. (a, b) -> a
fst (a, Text)
k) FrameKind
Closed (FrameTable a -> Frame a) -> M a (FrameTable a) -> M a (Frame a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> FrameTable a -> M a (FrameTable a)
go FrameTable a
t
SectionKind
ArrayTableKind -> (a, Text) -> SemanticErrorKind -> M a (Frame a)
forall a b. (a, Text) -> SemanticErrorKind -> M a b
invalidKey (a, Text)
k SemanticErrorKind
ImplicitlyTable
FrameArray ((a, FrameTable a)
t :| [(a, FrameTable a)]
ts) ->
case SectionKind
kind of
SectionKind
TableKind -> (a, Text) -> SemanticErrorKind -> M a (Frame a)
forall a b. (a, Text) -> SemanticErrorKind -> M a b
invalidKey (a, Text)
k SemanticErrorKind
ClosedTable
SectionKind
ArrayTableKind -> NonEmpty (a, FrameTable a) -> Frame a
forall a. NonEmpty (a, FrameTable a) -> Frame a
FrameArray (NonEmpty (a, FrameTable a) -> Frame a)
-> (FrameTable a -> NonEmpty (a, FrameTable a))
-> FrameTable a
-> Frame a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((a, FrameTable a)
-> [(a, FrameTable a)] -> NonEmpty (a, FrameTable a)
forall a. a -> [a] -> NonEmpty a
:| (a, FrameTable a)
t (a, FrameTable a) -> [(a, FrameTable a)] -> [(a, FrameTable a)]
forall a. a -> [a] -> [a]
: [(a, FrameTable a)]
ts) ((a, FrameTable a) -> NonEmpty (a, FrameTable a))
-> (FrameTable a -> (a, FrameTable a))
-> FrameTable a
-> NonEmpty (a, FrameTable a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (,) ((a, Text) -> a
forall a b. (a, b) -> a
fst (a, Text)
k) (FrameTable a -> Frame a) -> M a (FrameTable a) -> M a (Frame a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> FrameTable a -> M a (FrameTable a)
go FrameTable a
forall a. Monoid a => a
mempty
FrameTable a
_ FrameKind
Closed FrameTable a
_ -> (a, Text) -> SemanticErrorKind -> M a (Frame a)
forall a b. (a, Text) -> SemanticErrorKind -> M a b
invalidKey (a, Text)
k SemanticErrorKind
ClosedTable
FrameTable a
_ FrameKind
Dotted FrameTable a
_ -> String -> M a (Frame a)
forall a. HasCallStack => String -> a
error String
"addSection: dotted table left unclosed"
FrameValue {} -> (a, Text) -> SemanticErrorKind -> M a (Frame a)
forall a b. (a, Text) -> SemanticErrorKind -> M a b
invalidKey (a, Text)
k SemanticErrorKind
AlreadyAssigned
where
go :: FrameTable a -> M a (FrameTable a)
go = [(NonEmpty (a, Text), Val a)] -> FrameTable a -> M a (FrameTable a)
forall a. [(Key a, Val a)] -> FrameTable a -> M a (FrameTable a)
assignKeyVals [(NonEmpty (a, Text), Val a)]
kvs
addSection SectionKind
kind ((a, Text)
k1 :| (a, Text)
k2 : [(a, Text)]
ks) [(NonEmpty (a, Text), Val a)]
kvs =
(a, Text)
-> M a (Frame a)
-> (Frame a -> M a (Frame a))
-> FrameTable a
-> M a (FrameTable a)
forall a.
(a, Text)
-> M a (Frame a)
-> (Frame a -> M a (Frame a))
-> FrameTable a
-> M a (FrameTable a)
alterFrame (a, Text)
k1
(a -> FrameKind -> FrameTable a -> Frame a
forall a. a -> FrameKind -> FrameTable a -> Frame a
FrameTable ((a, Text) -> a
forall a b. (a, b) -> a
fst (a, Text)
k1) FrameKind
Open (FrameTable a -> Frame a) -> M a (FrameTable a) -> M a (Frame a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> FrameTable a -> M a (FrameTable a)
go FrameTable a
forall a. Monoid a => a
mempty)
\case
FrameTable a
a FrameKind
tk FrameTable a
t -> a -> FrameKind -> FrameTable a -> Frame a
forall a. a -> FrameKind -> FrameTable a -> Frame a
FrameTable a
a FrameKind
tk (FrameTable a -> Frame a) -> M a (FrameTable a) -> M a (Frame a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> FrameTable a -> M a (FrameTable a)
go FrameTable a
t
FrameArray ((a, FrameTable a)
t :| [(a, FrameTable a)]
ts) -> NonEmpty (a, FrameTable a) -> Frame a
forall a. NonEmpty (a, FrameTable a) -> Frame a
FrameArray (NonEmpty (a, FrameTable a) -> Frame a)
-> ((a, FrameTable a) -> NonEmpty (a, FrameTable a))
-> (a, FrameTable a)
-> Frame a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((a, FrameTable a)
-> [(a, FrameTable a)] -> NonEmpty (a, FrameTable a)
forall a. a -> [a] -> NonEmpty a
:| [(a, FrameTable a)]
ts) ((a, FrameTable a) -> Frame a)
-> Either (SemanticError a) (a, FrameTable a) -> M a (Frame a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (FrameTable a -> M a (FrameTable a))
-> (a, FrameTable a) -> Either (SemanticError a) (a, FrameTable a)
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> (a, a) -> f (a, b)
traverse FrameTable a -> M a (FrameTable a)
go (a, FrameTable a)
t
FrameValue Value' a
_ -> (a, Text) -> SemanticErrorKind -> M a (Frame a)
forall a b. (a, Text) -> SemanticErrorKind -> M a b
invalidKey (a, Text)
k1 SemanticErrorKind
AlreadyAssigned
where
go :: FrameTable a -> M a (FrameTable a)
go = SectionKind
-> NonEmpty (a, Text)
-> [(NonEmpty (a, Text), Val a)]
-> FrameTable a
-> M a (FrameTable a)
forall a.
SectionKind
-> Key a -> [(Key a, Val a)] -> FrameTable a -> M a (FrameTable a)
addSection SectionKind
kind ((a, Text)
k2 (a, Text) -> [(a, Text)] -> NonEmpty (a, Text)
forall a. a -> [a] -> NonEmpty a
:| [(a, Text)]
ks) [(NonEmpty (a, Text), Val a)]
kvs
closeDots :: FrameTable a -> FrameTable a
closeDots :: forall a. FrameTable a -> FrameTable a
closeDots =
((a, Frame a) -> (a, Frame a))
-> Map Text (a, Frame a) -> Map Text (a, Frame a)
forall a b. (a -> b) -> Map Text a -> Map Text b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (((a, Frame a) -> (a, Frame a))
-> Map Text (a, Frame a) -> Map Text (a, Frame a))
-> ((a, Frame a) -> (a, Frame a))
-> Map Text (a, Frame a)
-> Map Text (a, Frame a)
forall a b. (a -> b) -> a -> b
$ (Frame a -> Frame a) -> (a, Frame a) -> (a, Frame a)
forall a b. (a -> b) -> (a, a) -> (a, b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap \case
FrameTable a
a FrameKind
Dotted Map Text (a, Frame a)
t -> a -> FrameKind -> Map Text (a, Frame a) -> Frame a
forall a. a -> FrameKind -> FrameTable a -> Frame a
FrameTable a
a FrameKind
Closed (Map Text (a, Frame a) -> Map Text (a, Frame a)
forall a. FrameTable a -> FrameTable a
closeDots Map Text (a, Frame a)
t)
Frame a
frame -> Frame a
frame
assignKeyVals :: [(Key a, Val a)] -> FrameTable a -> M a (FrameTable a)
assignKeyVals :: forall a. [(Key a, Val a)] -> FrameTable a -> M a (FrameTable a)
assignKeyVals [(Key a, Val a)]
kvs FrameTable a
t = FrameTable a -> FrameTable a
forall a. FrameTable a -> FrameTable a
closeDots (FrameTable a -> FrameTable a)
-> Either (SemanticError a) (FrameTable a)
-> Either (SemanticError a) (FrameTable a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (FrameTable a
-> (Key a, Val a) -> Either (SemanticError a) (FrameTable a))
-> FrameTable a
-> [(Key a, Val a)]
-> Either (SemanticError a) (FrameTable a)
forall (t :: * -> *) (m :: * -> *) b a.
(Foldable t, Monad m) =>
(b -> a -> m b) -> b -> t a -> m b
foldM FrameTable a
-> (Key a, Val a) -> Either (SemanticError a) (FrameTable a)
forall {a}. FrameTable a -> (Key a, Val a) -> M a (FrameTable a)
f FrameTable a
t [(Key a, Val a)]
kvs
where
f :: FrameTable a -> (Key a, Val a) -> M a (FrameTable a)
f FrameTable a
m (Key a
k,Val a
v) = Key a -> Val a -> FrameTable a -> M a (FrameTable a)
forall a. Key a -> Val a -> FrameTable a -> M a (FrameTable a)
assign Key a
k Val a
v FrameTable a
m
assign :: Key a -> Val a -> FrameTable a -> M a (FrameTable a)
assign :: forall a. Key a -> Val a -> FrameTable a -> M a (FrameTable a)
assign ((a, Text)
key :| []) Val a
val =
(a, Text)
-> M a (Frame a)
-> (Frame a -> M a (Frame a))
-> FrameTable a
-> M a (FrameTable a)
forall a.
(a, Text)
-> M a (Frame a)
-> (Frame a -> M a (Frame a))
-> FrameTable a
-> M a (FrameTable a)
alterFrame (a, Text)
key
(Value' a -> Frame a
forall a. Value' a -> Frame a
FrameValue (Value' a -> Frame a)
-> Either (SemanticError a) (Value' a) -> M a (Frame a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Val a -> Either (SemanticError a) (Value' a)
forall a. Val a -> M a (Value' a)
valToValue Val a
val)
(\Frame a
_ -> (a, Text) -> SemanticErrorKind -> M a (Frame a)
forall a b. (a, Text) -> SemanticErrorKind -> M a b
invalidKey (a, Text)
key SemanticErrorKind
AlreadyAssigned)
assign ((a, Text)
key :| (a, Text)
k1 : [(a, Text)]
keys) Val a
val =
(a, Text)
-> M a (Frame a)
-> (Frame a -> M a (Frame a))
-> FrameTable a
-> M a (FrameTable a)
forall a.
(a, Text)
-> M a (Frame a)
-> (Frame a -> M a (Frame a))
-> FrameTable a
-> M a (FrameTable a)
alterFrame (a, Text)
key (a -> FrameTable a -> M a (Frame a)
go ((a, Text) -> a
forall a b. (a, b) -> a
fst (a, Text)
key) FrameTable a
forall a. Monoid a => a
mempty)
\case
FrameTable a
a FrameKind
Open FrameTable a
t -> a -> FrameTable a -> M a (Frame a)
go a
a FrameTable a
t
FrameTable a
a FrameKind
Dotted FrameTable a
t -> a -> FrameTable a -> M a (Frame a)
go a
a FrameTable a
t
FrameTable a
_ FrameKind
Closed FrameTable a
_ -> (a, Text) -> SemanticErrorKind -> M a (Frame a)
forall a b. (a, Text) -> SemanticErrorKind -> M a b
invalidKey (a, Text)
key SemanticErrorKind
ClosedTable
FrameArray NonEmpty (a, FrameTable a)
_ -> (a, Text) -> SemanticErrorKind -> M a (Frame a)
forall a b. (a, Text) -> SemanticErrorKind -> M a b
invalidKey (a, Text)
key SemanticErrorKind
ClosedTable
FrameValue Value' a
_ -> (a, Text) -> SemanticErrorKind -> M a (Frame a)
forall a b. (a, Text) -> SemanticErrorKind -> M a b
invalidKey (a, Text)
key SemanticErrorKind
AlreadyAssigned
where
go :: a -> FrameTable a -> M a (Frame a)
go a
a FrameTable a
t = a -> FrameKind -> FrameTable a -> Frame a
forall a. a -> FrameKind -> FrameTable a -> Frame a
FrameTable a
a FrameKind
Dotted (FrameTable a -> Frame a) -> M a (FrameTable a) -> M a (Frame a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> NonEmpty (a, Text) -> Val a -> FrameTable a -> M a (FrameTable a)
forall a. Key a -> Val a -> FrameTable a -> M a (FrameTable a)
assign ((a, Text)
k1 (a, Text) -> [(a, Text)] -> NonEmpty (a, Text)
forall a. a -> [a] -> NonEmpty a
:| [(a, Text)]
keys) Val a
val FrameTable a
t
valToValue :: Val a -> M a (Value' a)
valToValue :: forall a. Val a -> M a (Value' a)
valToValue =
\case
ValInteger a
a Integer
x -> Value' a -> M a (Value' a)
forall a b. b -> Either a b
Right (a -> Integer -> Value' a
forall a. a -> Integer -> Value' a
Integer' a
a Integer
x)
ValFloat a
a Double
x -> Value' a -> M a (Value' a)
forall a b. b -> Either a b
Right (a -> Double -> Value' a
forall a. a -> Double -> Value' a
Double' a
a Double
x)
ValBool a
a Bool
x -> Value' a -> M a (Value' a)
forall a b. b -> Either a b
Right (a -> Bool -> Value' a
forall a. a -> Bool -> Value' a
Bool' a
a Bool
x)
ValString a
a Text
x -> Value' a -> M a (Value' a)
forall a b. b -> Either a b
Right (a -> Text -> Value' a
forall a. a -> Text -> Value' a
Text' a
a Text
x)
ValTimeOfDay a
a TimeOfDay
x -> Value' a -> M a (Value' a)
forall a b. b -> Either a b
Right (a -> TimeOfDay -> Value' a
forall a. a -> TimeOfDay -> Value' a
TimeOfDay' a
a TimeOfDay
x)
ValZonedTime a
a ZonedTime
x -> Value' a -> M a (Value' a)
forall a b. b -> Either a b
Right (a -> ZonedTime -> Value' a
forall a. a -> ZonedTime -> Value' a
ZonedTime' a
a ZonedTime
x)
ValLocalTime a
a LocalTime
x -> Value' a -> M a (Value' a)
forall a b. b -> Either a b
Right (a -> LocalTime -> Value' a
forall a. a -> LocalTime -> Value' a
LocalTime' a
a LocalTime
x)
ValDay a
a Day
x -> Value' a -> M a (Value' a)
forall a b. b -> Either a b
Right (a -> Day -> Value' a
forall a. a -> Day -> Value' a
Day' a
a Day
x)
ValArray a
a [Val a]
xs -> a -> [Value' a] -> Value' a
forall a. a -> [Value' a] -> Value' a
List' a
a ([Value' a] -> Value' a)
-> Either (SemanticError a) [Value' a] -> M a (Value' a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Val a -> M a (Value' a))
-> [Val a] -> Either (SemanticError a) [Value' a]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> [a] -> f [b]
traverse Val a -> M a (Value' a)
forall a. Val a -> M a (Value' a)
valToValue [Val a]
xs
ValTable a
a [(Key a, Val a)]
kvs -> a -> Table' a -> Value' a
forall a. a -> Table' a -> Value' a
Table' a
a (Table' a -> Value' a)
-> (FrameTable a -> Table' a) -> FrameTable a -> Value' a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FrameTable a -> Table' a
forall a. FrameTable a -> Table' a
framesToTable (FrameTable a -> Value' a)
-> Either (SemanticError a) (FrameTable a) -> M a (Value' a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [(Key a, Val a)]
-> FrameTable a -> Either (SemanticError a) (FrameTable a)
forall a. [(Key a, Val a)] -> FrameTable a -> M a (FrameTable a)
assignKeyVals [(Key a, Val a)]
kvs FrameTable a
forall a. Monoid a => a
mempty
invalidKey ::
(a, Text) ->
SemanticErrorKind ->
M a b
invalidKey :: forall a b. (a, Text) -> SemanticErrorKind -> M a b
invalidKey (a
a, Text
key) SemanticErrorKind
kind = SemanticError a -> Either (SemanticError a) b
forall a b. a -> Either a b
Left (a -> Text -> SemanticErrorKind -> SemanticError a
forall a. a -> Text -> SemanticErrorKind -> SemanticError a
SemanticError a
a Text
key SemanticErrorKind
kind)
alterFrame ::
(a, Text) ->
M a (Frame a) ->
(Frame a -> M a (Frame a)) ->
FrameTable a -> M a (FrameTable a)
alterFrame :: forall a.
(a, Text)
-> M a (Frame a)
-> (Frame a -> M a (Frame a))
-> FrameTable a
-> M a (FrameTable a)
alterFrame (a
a, Text
k) M a (Frame a)
create Frame a -> M a (Frame a)
update = (Maybe (a, Frame a)
-> Either (SemanticError a) (Maybe (a, Frame a)))
-> Text
-> Map Text (a, Frame a)
-> Either (SemanticError a) (Map Text (a, Frame a))
forall (f :: * -> *) k a.
(Functor f, Ord k) =>
(Maybe a -> f (Maybe a)) -> k -> Map k a -> f (Map k a)
Map.alterF Maybe (a, Frame a) -> Either (SemanticError a) (Maybe (a, Frame a))
g Text
k
where
g :: Maybe (a, Frame a) -> Either (SemanticError a) (Maybe (a, Frame a))
g Maybe (a, Frame a)
Nothing =
do Frame a
lf <- M a (Frame a)
create
Maybe (a, Frame a) -> Either (SemanticError a) (Maybe (a, Frame a))
forall a. a -> Either (SemanticError a) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ((a, Frame a) -> Maybe (a, Frame a)
forall a. a -> Maybe a
Just (a
a, Frame a
lf))
g (Just (a
op, Frame a
ov)) =
do Frame a
lf <- Frame a -> M a (Frame a)
update Frame a
ov
Maybe (a, Frame a) -> Either (SemanticError a) (Maybe (a, Frame a))
forall a. a -> Either (SemanticError a) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ((a, Frame a) -> Maybe (a, Frame a)
forall a. a -> Maybe a
Just (a
op, Frame a
lf))