Skip to content

Commit

Permalink
Added local_ combinator fixing #29, as well as localModify and localM…
Browse files Browse the repository at this point in the history
…odify_ (#38)

fixes #29
  • Loading branch information
j-mie6 authored Nov 14, 2021
1 parent e784318 commit 91cd92d
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
7 changes: 6 additions & 1 deletion parsley/ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,9 @@
## 1.0.1.0 -- 2021-11-13

* Added `line` and `col` combinators.
* Added `pos` combinator.
* Added `pos` combinator.

## 1.0.2.0 -- 2021-11-14

* Added `local_` combinator to `Register`.
* Added `localModify` and `localModify_` combinators to `Register`.
2 changes: 1 addition & 1 deletion parsley/parsley.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ name: parsley
-- | +------- breaking API changes
-- | | +----- non-breaking API additions
-- | | | +--- code changes with no API change
version: 1.0.1.0
version: 1.0.2.0
synopsis: A fast parser combinator library backed by Typed Template Haskell
description: Parsley is a staged selective parser combinator library, which means
it does not support monadic operations, and relies on Typed Template
Expand Down
33 changes: 32 additions & 1 deletion parsley/src/ghc/Parsley/Register.hs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ module Parsley.Register (
gets, gets_,
modify, modify_,
move, swap,
bind, local, rollback,
local, local_,
localModify, localModify_,
bind, rollback,
for
) where

Expand Down Expand Up @@ -152,6 +154,35 @@ local r p q = bind (get r) $ \x -> put r p
*> q
<* put r x

{-|
@local_ reg x p@ stores @x@ in @reg@ for the /duration/ of parsing @p@.
If @p@ succeeds, @reg@ will be restored to its original state.
@since 1.0.2.0
-}
local_ :: ParserOps rep => Reg r a -> rep a -> Parser b -> Parser b
local_ r = local r . pure

{-|
@localModify reg p q@ first parses @p@ and @reg@ with its returned function for the /duration/ of parsing @q@.
If @q@ succeeds, @reg@ will be restored to its original state /before/ @p@ was parsed.
@since 1.0.2.0
-}
localModify :: Reg r a -> Parser (a -> a) -> Parser b -> Parser b
localModify r p q = bind (get r) $ \x -> modify r p
*> q
<* put r x

{-|
@localModify_ reg x p@ modifes @reg@ using @f@ for the /duration/ of parsing @p@.
If @p@ succeeds, @reg@ will be restored to its original state.
@since 1.0.2.0
-}
localModify_ :: ParserOps rep => Reg r a -> rep (a -> a) -> Parser b -> Parser b
localModify_ r = localModify r . pure

{-|
This combinator will swap the values contained in two registers.
Expand Down

0 comments on commit 91cd92d

Please sign in to comment.