packages feed

substring-parser 0.2.0.0 → 0.3.0.0

raw patch · 4 files changed

+49/−7 lines, 4 filesdep +containers

Dependencies added: containers

Files

ChangeLog.md view
@@ -1,3 +1,7 @@+# 0.3.0.0++- Add: `takeMatch` function to make the given parser match partially, by returning the prefixed string with parsed value+ # 0.2.0.0  - Fix: forgot to specify exposed-modules.
src/Text/Parser/Substring.hs view
@@ -2,10 +2,12 @@   ( replaceFileOnceWithParser   , replaceOnceWithParser   , onceReplacify+  , takeMatch   ) where -import           Control.Applicative ((<|>))+import           Control.Applicative ((<|>), optional) import           Data.Attoparsec.Text+import           Data.Maybe (isJust) import           Data.Monoid ((<>)) import           Data.Text (Text) import qualified Data.Text.Lazy as Text@@ -13,7 +15,7 @@ import           Data.Text.Lazy.Builder (Builder) import qualified Data.Text.IO as Text -import           Debug.NoTrace (trace)+import           Debug.NoTrace (trace, traceM)   replaceFileOnceWithParser :: Parser Text -> FilePath -> IO ()@@ -40,8 +42,26 @@     (<>) <$> firstToMatched <*> (TextBuilder.fromText <$> takeText)  +takeMatch :: Parser a -> Parser (Text, a)+takeMatch p = loop mempty+  where+    loop taken = do+      mMatched <- optional p+      traceM $ "isJust mMatched: " ++ show (isJust mMatched)+      case mMatched of+          Just matched ->+            return (Text.toStrict $ TextBuilder.toLazyText taken, matched)+          _ -> do+            newChar <- (TextBuilder.singleton <$> anyChar) <|> pure mempty+            traceM $ "newChar: " ++ show newChar+            loop (taken <> newChar)+++{-# INLINE traceIdVia #-} traceIdVia :: Show b => (a -> b) -> String -> a -> a traceIdVia via prefix x = trace (prefix ++ ": " ++ show (via x)) x ++{-# INLINE traceId #-} traceId :: Show a => String -> a -> a traceId = traceIdVia id
substring-parser.cabal view
@@ -3,7 +3,7 @@ -- see: https://github.com/sol/hpack  name:           substring-parser-version:        0.2.0.0+version:        0.3.0.0 synopsis:       Match / replace substrings with a parser combinators. description:    See README.md category:       Text, Parsing@@ -35,7 +35,7 @@       Paths_substring_parser   default-language: Haskell2010 -test-suite add-env-config-test+test-suite substring-parser-test   type: exitcode-stdio-1.0   main-is: Spec.hs   hs-source-dirs:@@ -47,5 +47,6 @@     , attoparsec     , text     , substring-parser+    , containers     , hspec   default-language: Haskell2010
test/Spec.hs view
@@ -1,15 +1,18 @@-{-# LANGUAGE OverloadedStrings #-}- import           Test.Hspec+import           Test.Hspec.QuickCheck  import           Text.Parser.Substring  import           Control.Applicative ((*>)) import           Data.Attoparsec.Text+import           Data.Monoid ((<>))+import qualified Data.Set as Set+import           Data.Set ((\\))+import qualified Data.Text as Text   main :: IO ()-main = hspec $+main = hspec $ do   describe "replaceWithParser" $ do     it "replace first matching text by parser" $       replaceOnceWithParser (string "abc" *> pure "def") "--abc-abc-"@@ -18,3 +21,17 @@     it "returns the source text if parser doesn't match" $ do       let src = "--abc-abc-"       replaceOnceWithParser mempty src `shouldBe` src++  describe "takeMatch" $+    prop "take any string before the given parser and parse the data with the parser." $+      \(cs1, i, cs2) -> do+        let s1 = Text.pack $ Set.toList $ cs1 \\ Set.fromList ['0'..'9']+            s2 = Text.pack $ Set.toList $ cs2 \\ Set.fromList ['0'..'9']+            ds = show (abs i :: Integer)+            input = s1 <> Text.pack ds <> s2+        case feed (parse (takeMatch $ many1 digit) input) "" of+             Done left result -> do+               result `shouldBe` (s1, ds)+               left `shouldBe` s2+             other ->+               fail $ "Not done: " <> show other