diff options
author | Albert Krewinkel <albert@zeitkraut.de> | 2017-04-27 21:48:32 +0200 |
---|---|---|
committer | Albert Krewinkel <albert+github@zeitkraut.de> | 2017-04-30 10:59:20 +0200 |
commit | 31caa616a9353e073eb86be7889b7087e14a48ac (patch) | |
tree | 44be7b17210655bfc307e7b276e1b7829ce314ab /src/Text/Pandoc/Parsing.hs | |
parent | 97addc2a17266b7d1c6cc712244f675bc0263595 (diff) |
Provide shared F monad functions for Markdown and Org readers
The `F` monads used for delayed evaluation of certain values in the
Markdown and Org readers are based on a shared data type capturing the
common pattern of both `F` types.
Diffstat (limited to 'src/Text/Pandoc/Parsing.hs')
-rw-r--r-- | src/Text/Pandoc/Parsing.hs | 35 |
1 files changed, 25 insertions, 10 deletions
diff --git a/src/Text/Pandoc/Parsing.hs b/src/Text/Pandoc/Parsing.hs index e985f3d32..a6d3cd46a 100644 --- a/src/Text/Pandoc/Parsing.hs +++ b/src/Text/Pandoc/Parsing.hs @@ -108,10 +108,13 @@ module Text.Pandoc.Parsing ( anyLine, applyMacros', Parser, ParserT, - F(..), + F, + Future(..), runF, askF, asksF, + returnF, + trimInlinesF, token, (<+?>), extractIdClass, @@ -175,7 +178,7 @@ where import Text.Pandoc.Definition import Text.Pandoc.Options -import Text.Pandoc.Builder (Blocks, Inlines, rawBlock, HasMeta(..)) +import Text.Pandoc.Builder (Blocks, Inlines, rawBlock, HasMeta(..), trimInlines) import qualified Text.Pandoc.Builder as B import Text.Pandoc.XML (fromEntities) import qualified Text.Pandoc.UTF8 as UTF8 (putStrLn) @@ -205,18 +208,30 @@ type Parser t s = Parsec t s type ParserT = ParsecT -newtype F a = F { unF :: Reader ParserState a } deriving (Monad, Applicative, Functor) +-- | Reader monad wrapping the parser state. This is used to possibly delay +-- evaluation until all relevant information has been parsed and made available +-- in the parser state. +newtype Future s a = Future { runDelayed :: Reader s a } + deriving (Monad, Applicative, Functor) -runF :: F a -> ParserState -> a -runF = runReader . unF +type F = Future ParserState -askF :: F ParserState -askF = F ask +runF :: Future s a -> s -> a +runF = runReader . runDelayed -asksF :: (ParserState -> a) -> F a -asksF f = F $ asks f +askF :: Future s s +askF = Future ask -instance Monoid a => Monoid (F a) where +asksF :: (s -> a) -> Future s a +asksF f = Future $ asks f + +returnF :: Monad m => a -> m (Future s a) +returnF = return . return + +trimInlinesF :: Future s Inlines -> Future s Inlines +trimInlinesF = liftM trimInlines + +instance Monoid a => Monoid (Future s a) where mempty = return mempty mappend = liftM2 mappend mconcat = liftM mconcat . sequence |