@@ -21,6 +21,7 @@ module Parsing
21
21
, failWithPosition
22
22
, region
23
23
, ParseState (..)
24
+ , stateParserT
24
25
, hoistParserT
25
26
, mapParserT
26
27
) where
@@ -32,8 +33,8 @@ import Control.Apply (lift2)
32
33
import Control.Lazy (class Lazy )
33
34
import Control.Monad.Error.Class (class MonadError , class MonadThrow , catchError , throwError )
34
35
import 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 )
36
+ import Control.Monad.State.Class (class MonadState , state )
37
+ import Control.Monad.Trans.Class (class MonadTrans , lift )
37
38
import Control.MonadPlus (class Alternative , class MonadPlus , class Plus )
38
39
import Data.Either (Either (..))
39
40
import Data.Function.Uncurried (Fn2 , Fn5 , mkFn2 , mkFn3 , mkFn5 , runFn2 , runFn3 , runFn5 )
@@ -273,12 +274,8 @@ instance MonadRec (ParserT s m) where
273
274
runFn3 loop state1 initArg 30
274
275
)
275
276
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
- )
277
+ instance (MonadState t m ) => MonadState t (ParserT s m ) where
278
+ state k = lift (state k)
282
279
283
280
instance MonadThrow ParseError (ParserT s m ) where
284
281
throwError err = ParserT
@@ -360,18 +357,28 @@ instance MonadTrans (ParserT s) where
360
357
lift' $ map (\a _ -> runFn2 done state1 a) m
361
358
)
362
359
360
+
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
+
363
371
-- | Set the consumed flag.
364
372
-- |
365
373
-- | Setting the consumed flag means that we're committed to this parsing branch
366
374
-- | of an alternative (`<|>`), so that if this branch fails then we want to
367
375
-- | fail the entire parse instead of trying the other alternative.
368
376
consume :: forall s m . ParserT s m Unit
369
- consume = modify_ \(ParseState input pos _) ->
370
- ParseState input pos true
377
+ consume = stateParserT \(ParseState input pos _) -> Tuple unit (ParseState input pos true )
371
378
372
379
-- | Returns the current position in the stream.
373
380
position :: forall s m . ParserT s m Position
374
- position = gets \ (ParseState _ pos _) -> pos
381
+ position = stateParserT \state1@ (ParseState _ pos _) -> Tuple pos state1
375
382
376
383
-- | Fail with a message.
377
384
fail :: forall m s a . String -> ParserT s m a
0 commit comments