diff --git a/parsley/ChangeLog.md b/parsley/ChangeLog.md index c121ee26..545ef0b8 100644 --- a/parsley/ChangeLog.md +++ b/parsley/ChangeLog.md @@ -36,4 +36,9 @@ ## 1.0.1.0 -- 2021-11-13 * Added `line` and `col` combinators. -* Added `pos` combinator. \ No newline at end of file +* Added `pos` combinator. + +## 1.0.2.0 -- 2021-11-14 + +* Added `local_` combinator to `Register`. +* Added `localModify` and `localModify_` combinators to `Register`. \ No newline at end of file diff --git a/parsley/parsley.cabal b/parsley/parsley.cabal index 32ece807..84b21c73 100644 --- a/parsley/parsley.cabal +++ b/parsley/parsley.cabal @@ -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 diff --git a/parsley/src/ghc/Parsley/Register.hs b/parsley/src/ghc/Parsley/Register.hs index 62dc9494..4faf737f 100644 --- a/parsley/src/ghc/Parsley/Register.hs +++ b/parsley/src/ghc/Parsley/Register.hs @@ -21,7 +21,9 @@ module Parsley.Register ( gets, gets_, modify, modify_, move, swap, - bind, local, rollback, + local, local_, + localModify, localModify_, + bind, rollback, for ) where @@ -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.