@@ -7,16 +7,17 @@ module Text.Parsing.Parser
77 , Parser
88 , runParser
99 , runParserT
10+ , hoistParserT
1011 , consume
1112 , fail
1213 ) where
1314
1415import Prelude
1516import Control.Alt (class Alt )
1617import Control.Lazy (defer , class Lazy )
17- import Control.Monad.Except (class MonadError , ExceptT (..), throwError , runExceptT )
18+ import Control.Monad.Except (class MonadError , ExceptT (..), throwError , runExceptT , mapExceptT )
1819import Control.Monad.Rec.Class (class MonadRec )
19- import Control.Monad.State (runStateT , class MonadState , StateT (..), gets , evalStateT , modify )
20+ import Control.Monad.State (runStateT , class MonadState , StateT (..), gets , evalStateT , mapStateT , modify )
2021import Control.Monad.Trans.Class (lift , class MonadTrans )
2122import Control.MonadPlus (class Alternative , class MonadZero , class MonadPlus , class Plus )
2223import Data.Either (Either (..))
@@ -58,12 +59,15 @@ runParserT s p = evalStateT (runExceptT (unwrap p)) initialState where
5859 initialState = ParseState s initialPos false
5960
6061-- | The `Parser` monad is a synonym for the parser monad transformer applied to the `Identity` monad.
61- type Parser s a = ParserT s Identity a
62+ type Parser s = ParserT s Identity
6263
6364-- | Apply a parser, keeping only the parsed result.
6465runParser :: forall s a . s -> Parser s a -> Either ParseError a
6566runParser s = unwrap <<< runParserT s
6667
68+ hoistParserT :: forall s m n a . (m ~> n ) -> ParserT s m a -> ParserT s n a
69+ hoistParserT f (ParserT m) = ParserT (mapExceptT (mapStateT f) m)
70+
6771instance lazyParserT :: Lazy (ParserT s m a ) where
6872 defer f = ParserT (ExceptT (defer (runExceptT <<< unwrap <<< f)))
6973
0 commit comments