Permalink
Browse files

Add pipes-attoparsec.

  • Loading branch information...
0 parents commit fd21a8e480df40d1f7b8e54ed057ad1c118648cc @pcapriotti committed Feb 8, 2012
Showing with 91 additions and 0 deletions.
  1. +43 −0 Control/Pipe/Attoparsec.hs
  2. +30 −0 LICENSE
  3. +2 −0 Setup.hs
  4. +16 −0 pipes-attoparsec.cabal
@@ -0,0 +1,43 @@
+{-# LANGUAGE DeriveDataTypeable #-}
+module Control.Pipe.Attoparsec (
+ ParseError(..),
+ pipeParser,
+ ) where
+
+import Control.Exception
+import Control.Pipe
+import Data.Attoparsec.Types
+import Data.Maybe
+import Data.Monoid
+import Data.Typeable
+
+-- | A parse error as returned by Attoparsec.
+data ParseError
+ = ParseError {
+ errorContexts :: [String], -- ^ Contexts where the error occurred.
+ errorMessage :: String -- ^ Error message.
+ }
+ | DivergentParser -- ^ Returned if a parser does not terminate
+ -- when its input is exhausted.
+ deriving (Show, Typeable)
+
+instance Exception ParseError
+
+-- | Convert a parser continuation into a Pipe.
+--
+-- To get a parser continuation from a 'Parser', use the parse function of the
+-- appropriate Attoparsec module.
+pipeParser :: (Monoid a, Monad m) => (a -> IResult a r) -> Pipe a x m (a, Either ParseError r)
+pipeParser p = go p
+ where
+ go p = do
+ chunk <- tryAwait
+ case p (maybe mempty id chunk) of
+ Fail leftover contexts msg ->
+ return (leftover, Left $ ParseError contexts msg)
+ Partial p' ->
+ if isNothing chunk
+ then return (mempty, Left DivergentParser)
+ else go p'
+ Done leftover result ->
+ return (leftover, Right result)
30 LICENSE
@@ -0,0 +1,30 @@
+Copyright (c)2012, Paolo Capriotti
+
+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.
+
+ * 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.
+
+ * Neither the name of Paolo Capriotti nor the names of other
+ contributors may be used to endorse or promote products derived
+ from this software without specific prior written permission.
+
+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.
@@ -0,0 +1,2 @@
+import Distribution.Simple
+main = defaultMain
@@ -0,0 +1,16 @@
+Name: pipes-attoparsec
+Version: 0.0.1
+License: BSD3
+License-file: LICENSE
+Author: Paolo Capriotti
+Maintainer: p.capriotti@gmail.com
+Category: Text
+Build-type: Simple
+Cabal-version: >=1.2
+
+Library
+ Exposed-modules: Control.Pipe.Attoparsec
+ Build-depends:
+ base (== 4.*),
+ pipes (== 1.1.*),
+ attoparsec (== 0.10.*)

0 comments on commit fd21a8e

Please sign in to comment.