{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE StandaloneDeriving #-}

module Language.Ltml.Parser.Footnote
    ( FootnoteParser
    , FootnoteMap
    , FootnoteWriterT
    , runFootnoteWriterT
    , mapFootnoteWriterT
    , eitherMapFootnoteWriterT
    , footnoteP
    )
where

import Control.Applicative (Alternative)
import Control.Applicative.Combinators (choice)
import Control.Monad (MonadPlus)
import Control.Monad.Reader (ReaderT, ask, mapReaderT, runReaderT)
import Control.Monad.State (StateT, get, mapStateT, put, runStateT, state)
import Control.Monad.Trans.Class (MonadTrans, lift)
import Data.Map (Map)
import qualified Data.Map as Map (empty)
import Data.Map.Utils (insert')
import qualified Data.Set as Set (singleton)
import Data.Text (Text, unpack)
import Data.Void (Void)
import Language.Lsd.AST.Type.Footnote (FootnoteType (FootnoteType))
import Language.Ltml.AST.Footnote (Footnote (Footnote))
import Language.Ltml.AST.Label (Label (unLabel))
import Language.Ltml.Parser (Parser)
import Language.Ltml.Parser.Text (hangingTextP')
import Text.Megaparsec (MonadParsec, getOffset, parseError)
import Text.Megaparsec.Error
    ( ErrorFancy (ErrorFail)
    , ParseError (FancyError)
    )

type FootnoteMap = Map Label Footnote

newtype FootnoteWriterT m a
    = FootnoteWriterT (ReaderT [FootnoteType] (StateT FootnoteMap m) a)
    deriving ((forall a b.
 (a -> b) -> FootnoteWriterT m a -> FootnoteWriterT m b)
-> (forall a b. a -> FootnoteWriterT m b -> FootnoteWriterT m a)
-> Functor (FootnoteWriterT m)
forall a b. a -> FootnoteWriterT m b -> FootnoteWriterT m a
forall a b. (a -> b) -> FootnoteWriterT m a -> FootnoteWriterT m b
forall (m :: * -> *) a b.
Functor m =>
a -> FootnoteWriterT m b -> FootnoteWriterT m a
forall (m :: * -> *) a b.
Functor m =>
(a -> b) -> FootnoteWriterT m a -> FootnoteWriterT m b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
$cfmap :: forall (m :: * -> *) a b.
Functor m =>
(a -> b) -> FootnoteWriterT m a -> FootnoteWriterT m b
fmap :: forall a b. (a -> b) -> FootnoteWriterT m a -> FootnoteWriterT m b
$c<$ :: forall (m :: * -> *) a b.
Functor m =>
a -> FootnoteWriterT m b -> FootnoteWriterT m a
<$ :: forall a b. a -> FootnoteWriterT m b -> FootnoteWriterT m a
Functor, Functor (FootnoteWriterT m)
Functor (FootnoteWriterT m) =>
(forall a. a -> FootnoteWriterT m a)
-> (forall a b.
    FootnoteWriterT m (a -> b)
    -> FootnoteWriterT m a -> FootnoteWriterT m b)
-> (forall a b c.
    (a -> b -> c)
    -> FootnoteWriterT m a
    -> FootnoteWriterT m b
    -> FootnoteWriterT m c)
-> (forall a b.
    FootnoteWriterT m a -> FootnoteWriterT m b -> FootnoteWriterT m b)
-> (forall a b.
    FootnoteWriterT m a -> FootnoteWriterT m b -> FootnoteWriterT m a)
-> Applicative (FootnoteWriterT m)
forall a. a -> FootnoteWriterT m a
forall a b.
FootnoteWriterT m a -> FootnoteWriterT m b -> FootnoteWriterT m a
forall a b.
FootnoteWriterT m a -> FootnoteWriterT m b -> FootnoteWriterT m b
forall a b.
FootnoteWriterT m (a -> b)
-> FootnoteWriterT m a -> FootnoteWriterT m b
forall a b c.
(a -> b -> c)
-> FootnoteWriterT m a
-> FootnoteWriterT m b
-> FootnoteWriterT m c
forall (m :: * -> *). Monad m => Functor (FootnoteWriterT m)
forall (m :: * -> *) a. Monad m => a -> FootnoteWriterT m a
forall (m :: * -> *) a b.
Monad m =>
FootnoteWriterT m a -> FootnoteWriterT m b -> FootnoteWriterT m a
forall (m :: * -> *) a b.
Monad m =>
FootnoteWriterT m a -> FootnoteWriterT m b -> FootnoteWriterT m b
forall (m :: * -> *) a b.
Monad m =>
FootnoteWriterT m (a -> b)
-> FootnoteWriterT m a -> FootnoteWriterT m b
forall (m :: * -> *) a b c.
Monad m =>
(a -> b -> c)
-> FootnoteWriterT m a
-> FootnoteWriterT m b
-> FootnoteWriterT m c
forall (f :: * -> *).
Functor f =>
(forall a. a -> f a)
-> (forall a b. f (a -> b) -> f a -> f b)
-> (forall a b c. (a -> b -> c) -> f a -> f b -> f c)
-> (forall a b. f a -> f b -> f b)
-> (forall a b. f a -> f b -> f a)
-> Applicative f
$cpure :: forall (m :: * -> *) a. Monad m => a -> FootnoteWriterT m a
pure :: forall a. a -> FootnoteWriterT m a
$c<*> :: forall (m :: * -> *) a b.
Monad m =>
FootnoteWriterT m (a -> b)
-> FootnoteWriterT m a -> FootnoteWriterT m b
<*> :: forall a b.
FootnoteWriterT m (a -> b)
-> FootnoteWriterT m a -> FootnoteWriterT m b
$cliftA2 :: forall (m :: * -> *) a b c.
Monad m =>
(a -> b -> c)
-> FootnoteWriterT m a
-> FootnoteWriterT m b
-> FootnoteWriterT m c
liftA2 :: forall a b c.
(a -> b -> c)
-> FootnoteWriterT m a
-> FootnoteWriterT m b
-> FootnoteWriterT m c
$c*> :: forall (m :: * -> *) a b.
Monad m =>
FootnoteWriterT m a -> FootnoteWriterT m b -> FootnoteWriterT m b
*> :: forall a b.
FootnoteWriterT m a -> FootnoteWriterT m b -> FootnoteWriterT m b
$c<* :: forall (m :: * -> *) a b.
Monad m =>
FootnoteWriterT m a -> FootnoteWriterT m b -> FootnoteWriterT m a
<* :: forall a b.
FootnoteWriterT m a -> FootnoteWriterT m b -> FootnoteWriterT m a
Applicative, Applicative (FootnoteWriterT m)
Applicative (FootnoteWriterT m) =>
(forall a b.
 FootnoteWriterT m a
 -> (a -> FootnoteWriterT m b) -> FootnoteWriterT m b)
-> (forall a b.
    FootnoteWriterT m a -> FootnoteWriterT m b -> FootnoteWriterT m b)
-> (forall a. a -> FootnoteWriterT m a)
-> Monad (FootnoteWriterT m)
forall a. a -> FootnoteWriterT m a
forall a b.
FootnoteWriterT m a -> FootnoteWriterT m b -> FootnoteWriterT m b
forall a b.
FootnoteWriterT m a
-> (a -> FootnoteWriterT m b) -> FootnoteWriterT m b
forall (m :: * -> *). Monad m => Applicative (FootnoteWriterT m)
forall (m :: * -> *) a. Monad m => a -> FootnoteWriterT m a
forall (m :: * -> *) a b.
Monad m =>
FootnoteWriterT m a -> FootnoteWriterT m b -> FootnoteWriterT m b
forall (m :: * -> *) a b.
Monad m =>
FootnoteWriterT m a
-> (a -> FootnoteWriterT m b) -> FootnoteWriterT m b
forall (m :: * -> *).
Applicative m =>
(forall a b. m a -> (a -> m b) -> m b)
-> (forall a b. m a -> m b -> m b)
-> (forall a. a -> m a)
-> Monad m
$c>>= :: forall (m :: * -> *) a b.
Monad m =>
FootnoteWriterT m a
-> (a -> FootnoteWriterT m b) -> FootnoteWriterT m b
>>= :: forall a b.
FootnoteWriterT m a
-> (a -> FootnoteWriterT m b) -> FootnoteWriterT m b
$c>> :: forall (m :: * -> *) a b.
Monad m =>
FootnoteWriterT m a -> FootnoteWriterT m b -> FootnoteWriterT m b
>> :: forall a b.
FootnoteWriterT m a -> FootnoteWriterT m b -> FootnoteWriterT m b
$creturn :: forall (m :: * -> *) a. Monad m => a -> FootnoteWriterT m a
return :: forall a. a -> FootnoteWriterT m a
Monad, Monad (FootnoteWriterT m)
Monad (FootnoteWriterT m) =>
(forall a. String -> FootnoteWriterT m a)
-> MonadFail (FootnoteWriterT m)
forall a. String -> FootnoteWriterT m a
forall (m :: * -> *).
Monad m =>
(forall a. String -> m a) -> MonadFail m
forall (m :: * -> *). MonadFail m => Monad (FootnoteWriterT m)
forall (m :: * -> *) a.
MonadFail m =>
String -> FootnoteWriterT m a
$cfail :: forall (m :: * -> *) a.
MonadFail m =>
String -> FootnoteWriterT m a
fail :: forall a. String -> FootnoteWriterT m a
MonadFail, Applicative (FootnoteWriterT m)
Applicative (FootnoteWriterT m) =>
(forall a. FootnoteWriterT m a)
-> (forall a.
    FootnoteWriterT m a -> FootnoteWriterT m a -> FootnoteWriterT m a)
-> (forall a. FootnoteWriterT m a -> FootnoteWriterT m [a])
-> (forall a. FootnoteWriterT m a -> FootnoteWriterT m [a])
-> Alternative (FootnoteWriterT m)
forall a. FootnoteWriterT m a
forall a. FootnoteWriterT m a -> FootnoteWriterT m [a]
forall a.
FootnoteWriterT m a -> FootnoteWriterT m a -> FootnoteWriterT m a
forall (f :: * -> *).
Applicative f =>
(forall a. f a)
-> (forall a. f a -> f a -> f a)
-> (forall a. f a -> f [a])
-> (forall a. f a -> f [a])
-> Alternative f
forall (m :: * -> *).
MonadPlus m =>
Applicative (FootnoteWriterT m)
forall (m :: * -> *) a. MonadPlus m => FootnoteWriterT m a
forall (m :: * -> *) a.
MonadPlus m =>
FootnoteWriterT m a -> FootnoteWriterT m [a]
forall (m :: * -> *) a.
MonadPlus m =>
FootnoteWriterT m a -> FootnoteWriterT m a -> FootnoteWriterT m a
$cempty :: forall (m :: * -> *) a. MonadPlus m => FootnoteWriterT m a
empty :: forall a. FootnoteWriterT m a
$c<|> :: forall (m :: * -> *) a.
MonadPlus m =>
FootnoteWriterT m a -> FootnoteWriterT m a -> FootnoteWriterT m a
<|> :: forall a.
FootnoteWriterT m a -> FootnoteWriterT m a -> FootnoteWriterT m a
$csome :: forall (m :: * -> *) a.
MonadPlus m =>
FootnoteWriterT m a -> FootnoteWriterT m [a]
some :: forall a. FootnoteWriterT m a -> FootnoteWriterT m [a]
$cmany :: forall (m :: * -> *) a.
MonadPlus m =>
FootnoteWriterT m a -> FootnoteWriterT m [a]
many :: forall a. FootnoteWriterT m a -> FootnoteWriterT m [a]
Alternative, Monad (FootnoteWriterT m)
Alternative (FootnoteWriterT m)
(Alternative (FootnoteWriterT m), Monad (FootnoteWriterT m)) =>
(forall a. FootnoteWriterT m a)
-> (forall a.
    FootnoteWriterT m a -> FootnoteWriterT m a -> FootnoteWriterT m a)
-> MonadPlus (FootnoteWriterT m)
forall a. FootnoteWriterT m a
forall a.
FootnoteWriterT m a -> FootnoteWriterT m a -> FootnoteWriterT m a
forall (m :: * -> *).
(Alternative m, Monad m) =>
(forall a. m a) -> (forall a. m a -> m a -> m a) -> MonadPlus m
forall (m :: * -> *). MonadPlus m => Monad (FootnoteWriterT m)
forall (m :: * -> *).
MonadPlus m =>
Alternative (FootnoteWriterT m)
forall (m :: * -> *) a. MonadPlus m => FootnoteWriterT m a
forall (m :: * -> *) a.
MonadPlus m =>
FootnoteWriterT m a -> FootnoteWriterT m a -> FootnoteWriterT m a
$cmzero :: forall (m :: * -> *) a. MonadPlus m => FootnoteWriterT m a
mzero :: forall a. FootnoteWriterT m a
$cmplus :: forall (m :: * -> *) a.
MonadPlus m =>
FootnoteWriterT m a -> FootnoteWriterT m a -> FootnoteWriterT m a
mplus :: forall a.
FootnoteWriterT m a -> FootnoteWriterT m a -> FootnoteWriterT m a
MonadPlus)

instance MonadTrans FootnoteWriterT where
    lift :: forall (m :: * -> *) a. Monad m => m a -> FootnoteWriterT m a
lift = ReaderT [FootnoteType] (StateT (Map Label Footnote) m) a
-> FootnoteWriterT m a
forall (m :: * -> *) a.
ReaderT [FootnoteType] (StateT (Map Label Footnote) m) a
-> FootnoteWriterT m a
FootnoteWriterT (ReaderT [FootnoteType] (StateT (Map Label Footnote) m) a
 -> FootnoteWriterT m a)
-> (m a
    -> ReaderT [FootnoteType] (StateT (Map Label Footnote) m) a)
-> m a
-> FootnoteWriterT m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. StateT (Map Label Footnote) m a
-> ReaderT [FootnoteType] (StateT (Map Label Footnote) m) a
forall (m :: * -> *) a.
Monad m =>
m a -> ReaderT [FootnoteType] m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (StateT (Map Label Footnote) m a
 -> ReaderT [FootnoteType] (StateT (Map Label Footnote) m) a)
-> (m a -> StateT (Map Label Footnote) m a)
-> m a
-> ReaderT [FootnoteType] (StateT (Map Label Footnote) m) a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. m a -> StateT (Map Label Footnote) m a
forall (m :: * -> *) a.
Monad m =>
m a -> StateT (Map Label Footnote) m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift

type FootnoteParser = FootnoteWriterT Parser

deriving instance
    (MonadParsec Void Text m)
    => MonadParsec Void Text (FootnoteWriterT m)

mapFootnoteWriterT
    :: (m (a, FootnoteMap) -> n (b, FootnoteMap))
    -> FootnoteWriterT m a
    -> FootnoteWriterT n b
mapFootnoteWriterT :: forall (m :: * -> *) a (n :: * -> *) b.
(m (a, Map Label Footnote) -> n (b, Map Label Footnote))
-> FootnoteWriterT m a -> FootnoteWriterT n b
mapFootnoteWriterT m (a, Map Label Footnote) -> n (b, Map Label Footnote)
f (FootnoteWriterT ReaderT [FootnoteType] (StateT (Map Label Footnote) m) a
p) =
    ReaderT [FootnoteType] (StateT (Map Label Footnote) n) b
-> FootnoteWriterT n b
forall (m :: * -> *) a.
ReaderT [FootnoteType] (StateT (Map Label Footnote) m) a
-> FootnoteWriterT m a
FootnoteWriterT (ReaderT [FootnoteType] (StateT (Map Label Footnote) n) b
 -> FootnoteWriterT n b)
-> ReaderT [FootnoteType] (StateT (Map Label Footnote) n) b
-> FootnoteWriterT n b
forall a b. (a -> b) -> a -> b
$ (StateT (Map Label Footnote) m a
 -> StateT (Map Label Footnote) n b)
-> ReaderT [FootnoteType] (StateT (Map Label Footnote) m) a
-> ReaderT [FootnoteType] (StateT (Map Label Footnote) n) b
forall (m :: * -> *) a (n :: * -> *) b r.
(m a -> n b) -> ReaderT r m a -> ReaderT r n b
mapReaderT ((m (a, Map Label Footnote) -> n (b, Map Label Footnote))
-> StateT (Map Label Footnote) m a
-> StateT (Map Label Footnote) n b
forall (m :: * -> *) a s (n :: * -> *) b.
(m (a, s) -> n (b, s)) -> StateT s m a -> StateT s n b
mapStateT m (a, Map Label Footnote) -> n (b, Map Label Footnote)
f) ReaderT [FootnoteType] (StateT (Map Label Footnote) m) a
p

eitherMapFootnoteWriterT
    :: (Monad n)
    => (m (a, FootnoteMap) -> Either e (b, FootnoteMap))
    -> FootnoteWriterT m a
    -> FootnoteWriterT n (Either e b)
eitherMapFootnoteWriterT :: forall (n :: * -> *) (m :: * -> *) a e b.
Monad n =>
(m (a, Map Label Footnote) -> Either e (b, Map Label Footnote))
-> FootnoteWriterT m a -> FootnoteWriterT n (Either e b)
eitherMapFootnoteWriterT m (a, Map Label Footnote) -> Either e (b, Map Label Footnote)
f (FootnoteWriterT ReaderT [FootnoteType] (StateT (Map Label Footnote) m) a
m) =
    ReaderT [FootnoteType] (StateT (Map Label Footnote) n) (Either e b)
-> FootnoteWriterT n (Either e b)
forall (m :: * -> *) a.
ReaderT [FootnoteType] (StateT (Map Label Footnote) m) a
-> FootnoteWriterT m a
FootnoteWriterT (ReaderT
   [FootnoteType] (StateT (Map Label Footnote) n) (Either e b)
 -> FootnoteWriterT n (Either e b))
-> ReaderT
     [FootnoteType] (StateT (Map Label Footnote) n) (Either e b)
-> FootnoteWriterT n (Either e b)
forall a b. (a -> b) -> a -> b
$ (StateT (Map Label Footnote) m a
 -> StateT (Map Label Footnote) n (Either e b))
-> ReaderT [FootnoteType] (StateT (Map Label Footnote) m) a
-> ReaderT
     [FootnoteType] (StateT (Map Label Footnote) n) (Either e b)
forall (m :: * -> *) a (n :: * -> *) b r.
(m a -> n b) -> ReaderT r m a -> ReaderT r n b
mapReaderT ((m (a, Map Label Footnote) -> Either e (b, Map Label Footnote))
-> StateT (Map Label Footnote) m a
-> StateT (Map Label Footnote) n (Either e b)
forall (n :: * -> *) (m :: * -> *) a s e b.
Monad n =>
(m (a, s) -> Either e (b, s))
-> StateT s m a -> StateT s n (Either e b)
eitherMapStateT m (a, Map Label Footnote) -> Either e (b, Map Label Footnote)
f) ReaderT [FootnoteType] (StateT (Map Label Footnote) m) a
m

eitherMapStateT
    :: (Monad n)
    => (m (a, s) -> Either e (b, s))
    -> StateT s m a
    -> StateT s n (Either e b)
eitherMapStateT :: forall (n :: * -> *) (m :: * -> *) a s e b.
Monad n =>
(m (a, s) -> Either e (b, s))
-> StateT s m a -> StateT s n (Either e b)
eitherMapStateT m (a, s) -> Either e (b, s)
f StateT s m a
p = (s -> (Either e b, s)) -> StateT s n (Either e b)
forall a. (s -> (a, s)) -> StateT s n a
forall s (m :: * -> *) a. MonadState s m => (s -> (a, s)) -> m a
state ((s -> (Either e b, s)) -> StateT s n (Either e b))
-> (s -> (Either e b, s)) -> StateT s n (Either e b)
forall a b. (a -> b) -> a -> b
$ \s
s ->
    case m (a, s) -> Either e (b, s)
f (StateT s m a -> s -> m (a, s)
forall s (m :: * -> *) a. StateT s m a -> s -> m (a, s)
runStateT StateT s m a
p s
s) of
        Left e
e -> (e -> Either e b
forall a b. a -> Either a b
Left e
e, s
s)
        Right (b
y, s
s') -> (b -> Either e b
forall a b. b -> Either a b
Right b
y, s
s')

runFootnoteWriterT
    :: FootnoteWriterT m a
    -> [FootnoteType]
    -> m (a, FootnoteMap)
runFootnoteWriterT :: forall (m :: * -> *) a.
FootnoteWriterT m a -> [FootnoteType] -> m (a, Map Label Footnote)
runFootnoteWriterT (FootnoteWriterT ReaderT [FootnoteType] (StateT (Map Label Footnote) m) a
p) [FootnoteType]
ts =
    StateT (Map Label Footnote) m a
-> Map Label Footnote -> m (a, Map Label Footnote)
forall s (m :: * -> *) a. StateT s m a -> s -> m (a, s)
runStateT (ReaderT [FootnoteType] (StateT (Map Label Footnote) m) a
-> [FootnoteType] -> StateT (Map Label Footnote) m a
forall r (m :: * -> *) a. ReaderT r m a -> r -> m a
runReaderT ReaderT [FootnoteType] (StateT (Map Label Footnote) m) a
p [FootnoteType]
ts) Map Label Footnote
forall k a. Map k a
Map.empty

footnoteP :: FootnoteParser ()
footnoteP :: FootnoteParser ()
footnoteP = do
    Int
o <- FootnoteWriterT (ParsecT Void Text Identity) Int
forall e s (m :: * -> *). MonadParsec e s m => m Int
getOffset
    ReaderT
  [FootnoteType]
  (StateT (Map Label Footnote) (ParsecT Void Text Identity))
  [FootnoteType]
-> FootnoteWriterT (ParsecT Void Text Identity) [FootnoteType]
forall (m :: * -> *) a.
ReaderT [FootnoteType] (StateT (Map Label Footnote) m) a
-> FootnoteWriterT m a
FootnoteWriterT ReaderT
  [FootnoteType]
  (StateT (Map Label Footnote) (ParsecT Void Text Identity))
  [FootnoteType]
forall r (m :: * -> *). MonadReader r m => m r
ask FootnoteWriterT (ParsecT Void Text Identity) [FootnoteType]
-> ([FootnoteType]
    -> FootnoteWriterT (ParsecT Void Text Identity) (Label, Footnote))
-> FootnoteWriterT (ParsecT Void Text Identity) (Label, Footnote)
forall a b.
FootnoteWriterT (ParsecT Void Text Identity) a
-> (a -> FootnoteWriterT (ParsecT Void Text Identity) b)
-> FootnoteWriterT (ParsecT Void Text Identity) b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Parser (Label, Footnote)
-> FootnoteWriterT (ParsecT Void Text Identity) (Label, Footnote)
forall (m :: * -> *) a. Monad m => m a -> FootnoteWriterT m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (Parser (Label, Footnote)
 -> FootnoteWriterT (ParsecT Void Text Identity) (Label, Footnote))
-> ([FootnoteType] -> Parser (Label, Footnote))
-> [FootnoteType]
-> FootnoteWriterT (ParsecT Void Text Identity) (Label, Footnote)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Parser (Label, Footnote)] -> Parser (Label, Footnote)
forall (f :: * -> *) (m :: * -> *) a.
(Foldable f, Alternative m) =>
f (m a) -> m a
choice ([Parser (Label, Footnote)] -> Parser (Label, Footnote))
-> ([FootnoteType] -> [Parser (Label, Footnote)])
-> [FootnoteType]
-> Parser (Label, Footnote)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (FootnoteType -> Parser (Label, Footnote))
-> [FootnoteType] -> [Parser (Label, Footnote)]
forall a b. (a -> b) -> [a] -> [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap FootnoteType -> Parser (Label, Footnote)
footnoteP' FootnoteWriterT (ParsecT Void Text Identity) (Label, Footnote)
-> ((Label, Footnote) -> FootnoteParser ()) -> FootnoteParser ()
forall a b.
FootnoteWriterT (ParsecT Void Text Identity) a
-> (a -> FootnoteWriterT (ParsecT Void Text Identity) b)
-> FootnoteWriterT (ParsecT Void Text Identity) b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (Label -> Footnote -> FootnoteParser ())
-> (Label, Footnote) -> FootnoteParser ()
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry (Int -> Label -> Footnote -> FootnoteParser ()
add Int
o)
  where
    add :: Int -> Label -> Footnote -> FootnoteParser ()
    add :: Int -> Label -> Footnote -> FootnoteParser ()
add Int
offset Label
label Footnote
fn = do
        Map Label Footnote
fnMap <- ReaderT
  [FootnoteType]
  (StateT (Map Label Footnote) (ParsecT Void Text Identity))
  (Map Label Footnote)
-> FootnoteWriterT
     (ParsecT Void Text Identity) (Map Label Footnote)
forall (m :: * -> *) a.
ReaderT [FootnoteType] (StateT (Map Label Footnote) m) a
-> FootnoteWriterT m a
FootnoteWriterT ReaderT
  [FootnoteType]
  (StateT (Map Label Footnote) (ParsecT Void Text Identity))
  (Map Label Footnote)
forall s (m :: * -> *). MonadState s m => m s
get
        case Label
-> Footnote -> Map Label Footnote -> Maybe (Map Label Footnote)
forall k v. Ord k => k -> v -> Map k v -> Maybe (Map k v)
insert' Label
label Footnote
fn Map Label Footnote
fnMap of
            Maybe (Map Label Footnote)
Nothing ->
                String -> FootnoteParser ()
forall {a}.
String -> FootnoteWriterT (ParsecT Void Text Identity) a
fail' (String -> FootnoteParser ()) -> String -> FootnoteParser ()
forall a b. (a -> b) -> a -> b
$
                    String
"Footnote {"
                        String -> String -> String
forall a. [a] -> [a] -> [a]
++ Text -> String
unpack (Label -> Text
unLabel Label
label)
                        String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"} already defined."
            Just Map Label Footnote
fnMap' -> ReaderT
  [FootnoteType]
  (StateT (Map Label Footnote) (ParsecT Void Text Identity))
  ()
-> FootnoteParser ()
forall (m :: * -> *) a.
ReaderT [FootnoteType] (StateT (Map Label Footnote) m) a
-> FootnoteWriterT m a
FootnoteWriterT (ReaderT
   [FootnoteType]
   (StateT (Map Label Footnote) (ParsecT Void Text Identity))
   ()
 -> FootnoteParser ())
-> ReaderT
     [FootnoteType]
     (StateT (Map Label Footnote) (ParsecT Void Text Identity))
     ()
-> FootnoteParser ()
forall a b. (a -> b) -> a -> b
$ Map Label Footnote
-> ReaderT
     [FootnoteType]
     (StateT (Map Label Footnote) (ParsecT Void Text Identity))
     ()
forall s (m :: * -> *). MonadState s m => s -> m ()
put Map Label Footnote
fnMap'
      where
        fail' :: String -> FootnoteWriterT (ParsecT Void Text Identity) a
fail' = ParseError Text Void
-> FootnoteWriterT (ParsecT Void Text Identity) a
forall a.
ParseError Text Void
-> FootnoteWriterT (ParsecT Void Text Identity) a
forall e s (m :: * -> *) a.
MonadParsec e s m =>
ParseError s e -> m a
parseError (ParseError Text Void
 -> FootnoteWriterT (ParsecT Void Text Identity) a)
-> (String -> ParseError Text Void)
-> String
-> FootnoteWriterT (ParsecT Void Text Identity) a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Set (ErrorFancy Void) -> ParseError Text Void
forall s e. Int -> Set (ErrorFancy e) -> ParseError s e
FancyError Int
offset (Set (ErrorFancy Void) -> ParseError Text Void)
-> (String -> Set (ErrorFancy Void))
-> String
-> ParseError Text Void
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ErrorFancy Void -> Set (ErrorFancy Void)
forall a. a -> Set a
Set.singleton (ErrorFancy Void -> Set (ErrorFancy Void))
-> (String -> ErrorFancy Void) -> String -> Set (ErrorFancy Void)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> ErrorFancy Void
forall e. String -> ErrorFancy e
ErrorFail

footnoteP' :: FootnoteType -> Parser (Label, Footnote)
footnoteP' :: FootnoteType -> Parser (Label, Footnote)
footnoteP' (FootnoteType Keyword
kw FootnoteFormat
fmt TextType Void
tt) =
    ([FootnoteTextTree] -> Footnote)
-> (Label, [FootnoteTextTree]) -> (Label, Footnote)
forall a b. (a -> b) -> (Label, a) -> (Label, b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (FootnoteFormat -> [FootnoteTextTree] -> Footnote
Footnote FootnoteFormat
fmt) ((Label, [FootnoteTextTree]) -> (Label, Footnote))
-> ParsecT Void Text Identity (Label, [FootnoteTextTree])
-> Parser (Label, Footnote)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Keyword
-> TextType Void
-> ParsecT Void Text Identity (Label, [FootnoteTextTree])
forall (f :: * -> *) (m :: * -> *) lbrk fnref style enumType enum
       special.
(HangingTextP f, ParserWrapper m, LineBreakP lbrk,
 FootnoteRefP fnref, StyleP style, EnumP enumType enum,
 SpecialP m special) =>
Keyword
-> TextType enumType
-> m (f [TextTree lbrk fnref style enum special])
forall (m :: * -> *) lbrk fnref style enumType enum special.
(ParserWrapper m, LineBreakP lbrk, FootnoteRefP fnref,
 StyleP style, EnumP enumType enum, SpecialP m special) =>
Keyword
-> TextType enumType
-> m (Label, [TextTree lbrk fnref style enum special])
hangingTextP' Keyword
kw TextType Void
tt