replace-megaparsec 1.1.1.0 → 1.1.2.0
raw patch · 3 files changed
+44/−7 lines, 3 filesPVP ok
version bump matches the API change (PVP)
API changes (from Hackage documentation)
Files
- README.md +32/−1
- replace-megaparsec.cabal +1/−1
- src/Replace/Megaparsec.hs +11/−5
README.md view
@@ -130,7 +130,7 @@ #### Pattern match, capture only the locations of the matched patterns Find all of the sections of the stream which match-the `Text.Megaparsec.Char.space1` parser (a string of whitespace).+a string of spaces. Print a list of the offsets of the beginning of every pattern match. ```haskell@@ -142,6 +142,27 @@ [0,2,5] ``` +#### Pattern match balanced parentheses++Find the outer parentheses of all balanced nested parentheses.+Here's an example of matching a pattern that can't be expressed by a regular+expression. We can express the pattern with a recursive parser.++```haskell+let parens :: Parsec Void String ()+ parens = do+ char '('+ manyTill+ (void (noneOf "()") <|> void parens)+ (char ')')+ return ()++parseTest (findAll parens) "(()) (()())"+```+```haskell+[Right "(())",Left " ",Right "(()())"]+```+ ### Edit text strings by running parsers with `streamEdit` The following examples show how to search for a pattern in a string of text@@ -184,6 +205,16 @@ ``` ```haskell "10 000 0xFFFF"+```++#### Pattern match and edit the matches with IO++```haskell+import System.Environment+streamEditT (char '{' *> manyTill anySingle (char '}')) getEnv "- {HOME} -"+```+```haskell+"- /home/jbrock -" ``` #### Context-sensitive pattern match and edit the matches
replace-megaparsec.cabal view
@@ -1,5 +1,5 @@ name: replace-megaparsec-version: 1.1.1.0+version: 1.1.2.0 cabal-version: 1.18 synopsis: Stream editing with parsers homepage: https://github.com/jamesdbrock/replace-megaparsec
src/Replace/Megaparsec.hs view
@@ -122,8 +122,10 @@ -- the text which matched the pattern parser @sep@ will be returned in -- the 'Right' sections, along with the result of the parse of @sep@. --+-- Definition:+-- -- @--- findAllCap sep = 'sepCap' ('Text.Megaparsec.match' sep)+-- findAllCap sep = 'sepCap' ('Text.Megaparsec.match' sep) -- @ {-# INLINABLE findAllCap #-} findAllCap@@ -141,8 +143,10 @@ -- return the text which matched the pattern parser @sep@ in -- the 'Right' sections. --+-- Definition:+-- -- @--- findAll sep = (fmap.fmap) ('Data.Bifunctor.second' fst) $ 'sepCap' ('Text.Megaparsec.match' sep)+-- findAll sep = (fmap.fmap) ('Data.Bifunctor.second' fst) $ 'sepCap' ('Text.Megaparsec.match' sep) -- @ {-# INLINABLE findAll #-} findAll@@ -175,10 +179,10 @@ -- always returns the first item in the tuple, then @streamEdit@ changes -- nothing. ----- So for all @sep@,+-- So, for all @sep@: -- -- @--- streamEdit ('Text.Megaparsec.match' sep) 'Data.Tuple.fst' ≡ 'Data.Function.id'+-- streamEdit ('Text.Megaparsec.match' sep) 'Data.Tuple.fst' ≡ 'Data.Function.id' -- @ -- -- === Type constraints@@ -240,6 +244,8 @@ -> m s streamEditT sep editor input = do runParserT (sepCap sep) "" input >>= \case- (Left err) -> throw err -- sepCap can never fail, but if it does, throw+ (Left err) -> throw err+ -- sepCap can never fail, but if it does, throw.+ -- Don't use MonadFail because Identity is not a MonadFail. (Right r) -> fmap mconcat $ traverse (either return editor) r