replace-megaparsec 1.1.0.0 → 1.1.1.0
raw patch · 5 files changed
+59/−72 lines, 5 files
Files
- CHANGELOG.md +14/−2
- LICENSE +6/−25
- README.md +11/−10
- replace-megaparsec.cabal +2/−2
- src/Replace/Megaparsec.hs +26/−33
CHANGELOG.md view
@@ -1,5 +1,17 @@ # Revision history for replace-megaparsec -## 1.1.0.0 -- 2019-08-24+## 1.0.0.0 -- 2019-08-24 -* First version. Megaparsec.+* First version.++## 1.0.1.0 -- 2019-08-28++* Add test suite.+* `sepCap` will treats `sep` as failing if it succeeds but consumes no input.++## 1.1.0.0 -- 2019-09-01++* Add benchmark suite.+* In `streamEditT`, replace `fold` with `mconcat`. The benchmarks now show+ linear scaling instead of quadratic.+
LICENSE view
@@ -1,30 +1,11 @@-Copyright (c) 2019, James Brock--All rights reserved.+BSD-2-Clause -Redistribution and use in source and binary forms, with or without-modification, are permitted provided that the following conditions are met:+Copyright (c) 2019, James Brock - * Redistributions of source code must retain the above copyright- notice, this list of conditions and the following disclaimer.+Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: - * Redistributions in binary form must reproduce the above- copyright notice, this list of conditions and the following- disclaimer in the documentation and/or other materials provided- with the distribution.+1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. - * Neither the name of James Brock nor the names of other- contributors may be used to endorse or promote products derived- from this software without specific prior written permission.+2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS-"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT-LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR-A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT-OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,-SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT-LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,-DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY-THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT-(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE-OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
README.md view
@@ -61,8 +61,8 @@ an example of lifting a `Parser` into a `State` monad for context-sensitive pattern-matching. - The replacement expression for a traditional regular expression-based- substitution command is usually just a simple string template in which+* The replacement expression for a traditional regular expression-based+ substitution command is usually just a string template in which the *Nth* “capture group” can be inserted with the syntax `\N`. With this library, instead of a template, we get an `editor` function which can perform any computation, including IO.@@ -246,15 +246,15 @@ We want to replace all the `"foo"` with `"bar"`. We would expect `sed` to be about at the upper bound of speed for this task, so here- are the `perf` results when we compare `sed`+ are the `perf` results when we compare `sed s/foo/bar/g` to __replace-megaparsec__ with some different stream types. - | Method | `perf task-clock` |- | :--- | ---: |- | `sed s/foo/bar/g` | 39 msec |- | `streamEdit String` | 793 msec |- | `streamEdit ByteString` | 513 msec |- | `streamEdit Text` | 428 msec |+ | Method | `perf task-clock` |+ | :--- | ---: |+ | `sed` | 39 msec |+ | `streamEdit String` | 793 msec |+ | `streamEdit ByteString` | 513 msec |+ | `streamEdit Text` | 428 msec | 2. *Could we write this library for __parsec__?* @@ -266,4 +266,5 @@ 3. *Could we write this library for __attoparsec__?* I think so, but I wouldn't expect much of a speed improvement, because- again, `sepCap` is a fundamentally slow activity.+ again, `sepCap` is a fundamentally slow activity, and anyway+ [__megaparsec__ is as fast as __attoparsec__](https://github.com/mrkkrp/megaparsec#performance).
replace-megaparsec.cabal view
@@ -1,10 +1,10 @@ name: replace-megaparsec-version: 1.1.0.0+version: 1.1.1.0 cabal-version: 1.18 synopsis: Stream editing with parsers homepage: https://github.com/jamesdbrock/replace-megaparsec bug-reports: https://github.com/jamesdbrock/replace-megaparsec/issues-license: BSD3+license: BSD2 license-file: LICENSE author: James Brock maintainer: jamesbrock@gmail.com
src/Replace/Megaparsec.hs view
@@ -41,10 +41,8 @@ , findAllCap -- * Running parser- --- -- Ways to run a parser- , streamEditT , streamEdit+ , streamEditT ) where @@ -169,21 +167,18 @@ -- -- If you want access to the matched string in the @editor@ function, -- then combine the pattern parser @sep@ with 'Text.Megaparsec.match'.--- This will effectively change the type of the @editor@--- to @(s,a) -> m s@.+-- This will effectively change the type of the @editor@ function+-- to @(s,a) -> s@. -- -- This allows us to write an @editor@ function which can choose to not--- edit the match and just leave it as it is. We can write an--- @editorId@ function such that @streamEditT@ changes nothing.------ @--- editorId (matchString, parseResult) = return matchString--- @+-- edit the match and just leave it as it is. If the @editor@ function+-- always returns the first item in the tuple, then @streamEdit@ changes+-- nothing. ----- implies that+-- So for all @sep@, -- -- @--- streamEditT ('Text.Megaparsec.match' sep) editorId ≡ 'Control.Monad.return'+-- streamEdit ('Text.Megaparsec.match' sep) 'Data.Tuple.fst' ≡ 'Data.Function.id' -- @ -- -- === Type constraints@@ -199,17 +194,32 @@ -- "Data.Bytestring.Lazy", -- and "Data.String". ----- We need the @Monoid s@ instance so that we can @mconcat@ the output+-- We need the @Monoid s@ instance so that we can 'Data.Monoid.mconcat' the output -- stream. -- -- We need @Typeable s@ and @Show s@ for 'Control.Exception.throw'. In theory -- this function should never throw an exception, because it only throws -- when the 'sepCap' parser fails, and the 'sepCap' parser -- can never fail. If this function ever throws, please report that as a bug.+{-# INLINABLE streamEdit #-}+streamEdit+ :: forall s a. (Stream s, Monoid s, Tokens s ~ s, Show s, Show (Token s), Typeable s)+ => Parsec Void s a+ -- ^ The parser @sep@ for the pattern of interest.+ -> (a -> s)+ -- ^ The @editor@ function. Takes a parsed result of @sep@+ -- and returns a new stream section for the replacement.+ -> s+ -- ^ The input stream of text to be edited.+ -> s+streamEdit sep editor = runIdentity . streamEditT sep (Identity . editor)++-- |+-- == Stream editor transformer ----- === Underlying monad context+-- Monad transformer version of 'streamEdit'. ----- Both the parser @sep@ and the @editor@ function are run in the underlying+-- Both the parser @sep@ and the @editor@ function run in the underlying -- monad context. -- -- If you want to do 'IO' operations in the @editor@ function or the@@ -232,21 +242,4 @@ runParserT (sepCap sep) "" input >>= \case (Left err) -> throw err -- sepCap can never fail, but if it does, throw (Right r) -> fmap mconcat $ traverse (either return editor) r---- |--- == Pure stream editor------ Pure version of 'streamEditT'.-{-# INLINABLE streamEdit #-}-streamEdit- :: forall s a. (Stream s, Monoid s, Tokens s ~ s, Show s, Show (Token s), Typeable s)- => Parsec Void s a- -- ^ The parser @sep@ for the pattern of interest.- -> (a -> s)- -- ^ The @editor@ function. Takes a parsed result of @sep@- -- and returns a new stream section for the replacement.- -> s- -- ^ The input stream of text to be edited.- -> s-streamEdit sep editor = runIdentity . streamEditT sep (Identity . editor)