@@ -21,6 +21,8 @@ module Parsing
2121 , failWithPosition
2222 , region
2323 , ParseState (..)
24+ , stateParserT
25+ , getParserT
2426 , hoistParserT
2527 , mapParserT
2628 ) where
@@ -32,8 +34,8 @@ import Control.Apply (lift2)
3234import Control.Lazy (class Lazy )
3335import Control.Monad.Error.Class (class MonadError , class MonadThrow , catchError , throwError )
3436import Control.Monad.Rec.Class (class MonadRec , Step (..), tailRecM )
35- import Control.Monad.State.Class (class MonadState , gets , modify_ )
36- import Control.Monad.Trans.Class (class MonadTrans )
37+ import Control.Monad.State.Class (class MonadState , state )
38+ import Control.Monad.Trans.Class (class MonadTrans , lift )
3739import Control.MonadPlus (class Alternative , class MonadPlus , class Plus )
3840import Data.Either (Either (..))
3941import Data.Function.Uncurried (Fn2 , Fn5 , mkFn2 , mkFn3 , mkFn5 , runFn2 , runFn3 , runFn5 )
@@ -273,12 +275,8 @@ instance MonadRec (ParserT s m) where
273275 runFn3 loop state1 initArg 30
274276 )
275277
276- instance MonadState (ParseState s ) (ParserT s m ) where
277- state k = ParserT
278- ( mkFn5 \state1 _ _ _ done -> do
279- let (Tuple a state2) = k state1
280- runFn2 done state2 a
281- )
278+ instance (MonadState t m ) => MonadState t (ParserT s m ) where
279+ state k = lift (state k)
282280
283281instance MonadThrow ParseError (ParserT s m ) where
284282 throwError err = ParserT
@@ -360,18 +358,35 @@ instance MonadTrans (ParserT s) where
360358 lift' $ map (\a _ -> runFn2 done state1 a) m
361359 )
362360
361+ -- | Query and modify the `ParserT` internal state.
362+ -- |
363+ -- | Like the `state` member of `MonadState`.
364+ stateParserT :: forall s m a . (ParseState s -> Tuple a (ParseState s )) -> ParserT s m a
365+ stateParserT k = ParserT
366+ ( mkFn5 \state1 _ _ _ done -> do
367+ let (Tuple a state2) = k state1
368+ runFn2 done state2 a
369+ )
370+
371+ -- | Query the `ParserT` internal state.
372+ -- |
373+ -- | Like the `get` member of `MonadState`.
374+ getParserT :: forall s m . ParserT s m (ParseState s )
375+ getParserT = ParserT
376+ ( mkFn5 \state1 _ _ _ done -> runFn2 done state1 state1
377+ )
378+
363379-- | Set the consumed flag.
364380-- |
365381-- | Setting the consumed flag means that we're committed to this parsing branch
366382-- | of an alternative (`<|>`), so that if this branch fails then we want to
367383-- | fail the entire parse instead of trying the other alternative.
368384consume :: forall s m . ParserT s m Unit
369- consume = modify_ \(ParseState input pos _) ->
370- ParseState input pos true
385+ consume = stateParserT \(ParseState input pos _) -> Tuple unit (ParseState input pos true )
371386
372387-- | Returns the current position in the stream.
373388position :: forall s m . ParserT s m Position
374- position = gets \ (ParseState _ pos _) -> pos
389+ position = stateParserT \state1@ (ParseState _ pos _) -> Tuple pos state1
375390
376391-- | Fail with a message.
377392fail :: forall m s a . String -> ParserT s m a
0 commit comments