Skip to content

Commit d199706

Browse files
committed
Change MonadState instance
`state` refers to base monad instead of `ParseState`
1 parent 7e804ad commit d199706

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

src/Parsing.purs

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ module Parsing
2121
, failWithPosition
2222
, region
2323
, ParseState(..)
24+
, stateParserT
2425
, hoistParserT
2526
, mapParserT
2627
) where
@@ -32,8 +33,8 @@ import Control.Apply (lift2)
3233
import Control.Lazy (class Lazy)
3334
import Control.Monad.Error.Class (class MonadError, class MonadThrow, catchError, throwError)
3435
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)
3738
import Control.MonadPlus (class Alternative, class MonadPlus, class Plus)
3839
import Data.Either (Either(..))
3940
import Data.Function.Uncurried (Fn2, Fn5, mkFn2, mkFn3, mkFn5, runFn2, runFn3, runFn5)
@@ -273,12 +274,8 @@ instance MonadRec (ParserT s m) where
273274
runFn3 loop state1 initArg 30
274275
)
275276

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)
282279

283280
instance MonadThrow ParseError (ParserT s m) where
284281
throwError err = ParserT
@@ -360,18 +357,28 @@ instance MonadTrans (ParserT s) where
360357
lift' $ map (\a _ -> runFn2 done state1 a) m
361358
)
362359

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+
363371
-- | Set the consumed flag.
364372
-- |
365373
-- | Setting the consumed flag means that we're committed to this parsing branch
366374
-- | of an alternative (`<|>`), so that if this branch fails then we want to
367375
-- | fail the entire parse instead of trying the other alternative.
368376
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)
371378

372379
-- | Returns the current position in the stream.
373380
position :: forall s m. ParserT s m Position
374-
position = gets \(ParseState _ pos _) -> pos
381+
position = stateParserT \state1@(ParseState _ pos _) -> Tuple pos state1
375382

376383
-- | Fail with a message.
377384
fail :: forall m s a. String -> ParserT s m a

0 commit comments

Comments
 (0)