substring-parser 0.4.0.0 → 0.4.1.0
raw patch · 3 files changed
+31/−12 lines, 3 filesdep +parsers
Dependencies added: parsers
Files
- ChangeLog.md +4/−0
- src/Text/Parser/Substring.hs +23/−10
- substring-parser.cabal +4/−2
ChangeLog.md view
@@ -1,3 +1,7 @@+# 0.4.1.0++- Add: Make some of the APIs compatible with parser libraries other than attoparsec.+ # 0.4.0.0 - Add: `matchAll` function to make the given parser to collect all matching substrings.
src/Text/Parser/Substring.hs view
@@ -7,7 +7,7 @@ ) where import Control.Applicative ((<|>), optional)-import Data.Attoparsec.Text+import qualified Data.Attoparsec.Text as AT import qualified Data.DList as DList import Data.Maybe (isJust) import Data.Monoid ((<>))@@ -16,28 +16,30 @@ import qualified Data.Text.Lazy.Builder as TextBuilder import Data.Text.Lazy.Builder (Builder) import qualified Data.Text.IO as Text+import Text.Parser.Char (CharParsing, anyChar)+import Text.Parser.Combinators (eof) import Debug.NoTrace (trace, traceM) -replaceFileOnceWithParser :: Parser Text -> FilePath -> IO ()+replaceFileOnceWithParser :: AT.Parser Text -> FilePath -> IO () replaceFileOnceWithParser p filePath = Text.writeFile filePath =<< replaceOnceWithParser p <$> Text.readFile filePath -replaceOnceWithParser :: Parser Text -> Text -> Text+replaceOnceWithParser :: AT.Parser Text -> Text -> Text replaceOnceWithParser p t = maybe t (Text.toStrict . TextBuilder.toLazyText)- $ maybeResult+ $ AT.maybeResult $ traceId "fed"- $ flip feed ""+ $ flip AT.feed "" $ traceId "parsed"- $ parse (onceReplacify p) t+ $ AT.parse (onceReplacify p) t -- | ref: https://stackoverflow.com/questions/29549435/parsec-how-to-find-matches-within-a-string {-# INLINE matchAll #-}-matchAll :: Parser a -> Parser [a]+matchAll :: (Monad m, CharParsing m) => m a -> m [a] matchAll p = DList.toList <$> loop DList.empty where loop xs = do@@ -49,16 +51,27 @@ (anyChar *> loop xs) <|> pure xs -onceReplacify :: Parser Text -> Parser Builder+onceReplacify :: (Monad m, CharParsing m) => m Text -> m Builder onceReplacify p = let firstToMatched = (TextBuilder.fromText <$> p) <|> ((<>) <$> (TextBuilder.singleton <$> anyChar) <*> firstToMatched) in- (<>) <$> firstToMatched <*> (TextBuilder.fromText <$> takeText)+ (<>) <$> firstToMatched <*> takeBuilder -takeMatch :: Parser a -> Parser (Text, a)+{-# INLINE takeBuilder #-}+takeBuilder :: (Monad m, CharParsing m) => m Builder+takeBuilder = loop mempty+ where+ loop b = (eof *> pure b) <|> ( do+ c <- anyChar+ loop (b <> TextBuilder.singleton c)+ )+++{-# INLINE takeMatch #-}+takeMatch :: (Monad m, CharParsing m) => m a -> m (Text, a) takeMatch p = loop mempty where loop taken = do
substring-parser.cabal view
@@ -2,10 +2,10 @@ -- -- see: https://github.com/sol/hpack ----- hash: 2cb8b595a4b640dae99a8172a9c884e27964461afec3041f5cfbadfdf4d648d4+-- hash: 50b2b743e1e07990064a9b1b0e1708cf7ee567aed3e3fb3b9a5ec815cbf5d478 name: substring-parser-version: 0.4.0.0+version: 0.4.1.0 synopsis: Match / replace substrings with a parser combinators. description: See README.md category: Text, Parsing@@ -31,6 +31,7 @@ , attoparsec , base >=4.7 && <5 , dlist+ , parsers , text exposed-modules: Text.Parser.Substring@@ -50,6 +51,7 @@ , attoparsec , base >=4.7 && <5 , hspec+ , parsers , substring-parser , text other-modules: