Skip to content

feat: add MonadAsk & MonadReader instances #208

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Oct 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ Breaking changes:

New features:

- add `MonadAsk` and `MonadReader` instances (#208 by @bentongxyz)

Other improvements:

## [v10.0.0](https://github.com/purescript-contrib/purescript-parsing/releases/tag/v9.1.0) - 2022-07-18
Expand Down
10 changes: 10 additions & 0 deletions src/Parsing.purs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import Control.Alt (class Alt)
import Control.Apply (lift2)
import Control.Lazy (class Lazy)
import Control.Monad.Error.Class (class MonadError, class MonadThrow, catchError, throwError)
import Control.Monad.Reader.Class (class MonadAsk, class MonadReader, ask, local)
import Control.Monad.Rec.Class (class MonadRec, Step(..), tailRecM)
import Control.Monad.State.Class (class MonadState, state)
import Control.Monad.Trans.Class (class MonadTrans, lift)
Expand Down Expand Up @@ -278,6 +279,15 @@ instance MonadRec (ParserT s m) where
instance (MonadState t m) => MonadState t (ParserT s m) where
state k = lift (state k)

instance (MonadAsk r m) => MonadAsk r (ParserT s m) where
ask = lift ask

instance (MonadReader r m) => MonadReader r (ParserT s m) where
local f (ParserT k) = ParserT
( mkFn5 \state1 more lift throw done ->
runFn5 k state1 more (lift <<< local f) throw done
)

instance MonadThrow ParseError (ParserT s m) where
throwError err = ParserT
( mkFn5 \state1 _ _ throw _ ->
Expand Down