attoparsec-conduit 1.0.1 → 1.0.1.1
raw patch · 4 files changed
+63/−52 lines, 4 filesPVP ok
version bump matches the API change (PVP)
API changes (from Hackage documentation)
Files
- Data/Conduit/Attoparsec.hs +17/−23
- LICENSE +17/−27
- attoparsec-conduit.cabal +2/−2
- test/main.hs +27/−0
Data/Conduit/Attoparsec.hs view
@@ -32,6 +32,7 @@ import qualified Data.ByteString.Char8 as B8 import Data.Maybe (fromMaybe) import qualified Data.Text as T+import qualified Data.Text.Internal as TI import Data.Typeable (Typeable) import Prelude hiding (lines) @@ -77,25 +78,23 @@ isNull :: a -> Bool notEmpty :: [a] -> [a] getLinesCols :: a -> (Int, Int)- take' :: Int -> a -> a- length' :: a -> Int + -- | Return the beginning of the first input with the length of+ -- the second input removed. Assumes the second string is shorter+ -- than the first.+ stripFromEnd :: a -> a -> a+ instance AttoparsecInput B.ByteString where parseA = Data.Attoparsec.ByteString.parse feedA = Data.Attoparsec.ByteString.feed empty = B.empty isNull = B.null notEmpty = filter (not . B.null)- getLinesCols b =- (lines, cols)+ getLinesCols = B.foldl' f (0, 0) where- lines = B.count 10 b- cols =- case B8.lines b of- [] -> 0- ls -> B.length $ last ls- take' = B.take- length' = B.length+ f (!line, !col) ch | ch == 10 = (line + 1, 0)+ | otherwise = (line, col + 1)+ stripFromEnd b1 b2 = B.take (B.length b1 - B.length b2) b1 instance AttoparsecInput T.Text where parseA = Data.Attoparsec.Text.parse@@ -103,17 +102,12 @@ empty = T.empty isNull = T.null notEmpty = filter (not . T.null)- getLinesCols t =- (lines, cols)+ getLinesCols = T.foldl' f (0, 0) where- lines = T.count (T.pack "\n") t- cols =- case T.lines t of- [] -> 0- ls -> T.length $ last ls- take' = T.take- length' = T.length-+ f (!line, !col) ch | ch == '\n' = (line + 1, 0)+ | otherwise = (line, col + 1)+ stripFromEnd (TI.Text arr1 off1 len1) (TI.Text _ _ len2) =+ TI.textP arr1 off1 (len1 - len2) -- | Convert an Attoparsec 'A.Parser' into a 'Sink'. The parser will -- be streamed bytes until it returns 'A.Done' or 'A.Fail'.@@ -198,12 +192,12 @@ let pos' | end = pos | otherwise = addLinesCols prev pos- y = take' (length' c - length' lo) c+ y = stripFromEnd c lo pos'' = addLinesCols y pos' unless (isNull lo) $ leftover lo pos'' `seq` return $! Right (pos'', x) go end c (A.Fail rest contexts msg) =- let x = take' (length' c - length' rest) c+ let x = stripFromEnd c rest pos' | end = pos | otherwise = addLinesCols prev pos
LICENSE view
@@ -1,30 +1,20 @@-Copyright (c)2011, Michael Snoyman--All rights reserved.--Redistribution and use in source and binary forms, with or without-modification, are permitted provided that the following conditions are met:-- * Redistributions of source code must retain the above copyright- notice, this list of conditions and the following disclaimer.+Copyright (c) 2012 Michael Snoyman, http://www.yesodweb.com/ - * 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.+Permission is hereby granted, free of charge, to any person obtaining+a copy of this software and associated documentation files (the+"Software"), to deal in the Software without restriction, including+without limitation the rights to use, copy, modify, merge, publish,+distribute, sublicense, and/or sell copies of the Software, and to+permit persons to whom the Software is furnished to do so, subject to+the following conditions: - * Neither the name of Michael Snoyman nor the names of other- contributors may be used to endorse or promote products derived- from this software without specific prior written permission.+The above copyright notice and this permission notice shall be+included in all copies or substantial portions of the Software. -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.+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
attoparsec-conduit.cabal view
@@ -1,8 +1,8 @@ Name: attoparsec-conduit-Version: 1.0.1+Version: 1.0.1.1 Synopsis: Consume attoparsec parsers via conduit. Description: Consume attoparsec parsers via conduit.-License: BSD3+License: MIT License-file: LICENSE Author: Michael Snoyman Maintainer: michael@snoyman.com
test/main.hs view
@@ -64,6 +64,30 @@ case fromException e of Just pe -> do errorPosition pe `shouldBe` Position badLine badCol+ it "works after new line in text" $ do+ let input = ["aaa\n", "aaa\n\n", "aaa", "aa\nb\naaaa"]+ badLine = 5+ badCol = 1+ parser = Data.Attoparsec.Text.endOfInput <|> (Data.Attoparsec.Text.notChar 'b' >> parser)+ sink = sinkParser parser+ ea <- runExceptionT $ CL.sourceList input $$ sink+ case ea of+ Left e ->+ case fromException e of+ Just pe -> do+ errorPosition pe `shouldBe` Position badLine badCol+ it "works after new line in bytestring" $ do+ let input = ["aaa\n", "aaa\n\n", "aaa", "aa\nb\naaaa"]+ badLine = 5+ badCol = 1+ parser = Data.Attoparsec.ByteString.Char8.endOfInput <|> (Data.Attoparsec.ByteString.Char8.notChar 'b' >> parser)+ sink = sinkParser parser+ ea <- runExceptionT $ CL.sourceList input $$ sink+ case ea of+ Left e ->+ case fromException e of+ Just pe -> do+ errorPosition pe `shouldBe` Position badLine badCol describe "conduitParser" $ do it "parses a repeated stream" $ do@@ -74,7 +98,10 @@ let chk a = case a of Left{} -> False Right (_, xs) -> xs == "aaa"+ chkp 1 = (PositionRange (Position 1 0) (Position 2 1))+ chkp l = (PositionRange (Position l 1) (Position (l+1) 1)) forM_ ea $ \ a -> a `shouldSatisfy` chk :: Expectation+ forM_ (zip ea [1..]) $ \ (Right (pos, _), l) -> pos `shouldBe` chkp l length ea `shouldBe` 4