Skip to content

Commit 5ffbcf1

Browse files
authored
Merge pull request #44 from purescript-contrib/hoist
Add `hoistParserT`
2 parents 3c931ed + 72d6293 commit 5ffbcf1

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

src/Text/Parsing/Parser.purs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,17 @@ module Text.Parsing.Parser
77
, Parser
88
, runParser
99
, runParserT
10+
, hoistParserT
1011
, consume
1112
, fail
1213
) where
1314

1415
import Prelude
1516
import Control.Alt (class Alt)
1617
import 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)
1819
import 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)
2021
import Control.Monad.Trans.Class (lift, class MonadTrans)
2122
import Control.MonadPlus (class Alternative, class MonadZero, class MonadPlus, class Plus)
2223
import 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.
6465
runParser :: forall s a. s -> Parser s a -> Either ParseError a
6566
runParser 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+
6771
instance lazyParserT :: Lazy (ParserT s m a) where
6872
defer f = ParserT (ExceptT (defer (runExceptT <<< unwrap <<< f)))
6973

0 commit comments

Comments
 (0)