Permalink
Browse files

Renamed library from sea to ceason

  • Loading branch information...
1 parent 819e89d commit e1dcbca088431a946c8db0f9eb307c7981c813dc @tibbe committed Mar 23, 2012
Showing with 122 additions and 5 deletions.
  1. +2 −2 Data/{Sea.hs → Ceason.hs}
  2. +117 −0 Data/Ceason/Types.hs
  3. +3 −3 sea.cabal → ceason.cabal
@@ -1,7 +1,7 @@
{-# LANGUAGE BangPatterns, Rank2Types #-}
-- | A RFC 4180 compliant CSV parsing and encodig module.
-module Data.Sea
+module Data.Ceason
(
-- * Encoding and decoding
decode
@@ -36,7 +36,7 @@ import qualified Data.Vector as V
import Data.Word
import Prelude hiding (takeWhile)
-import Data.Sea.Types
+import Data.Ceason.Types
-- | Efficiently deserialize CSV records from a lazy
-- 'L.ByteString'. If this fails due to incomplete or invalid input,
View
@@ -0,0 +1,117 @@
+{-# LANGUAGE Rank2Types #-}
+
+module Data.Ceason.Types
+ ( Parser
+ , Result(..)
+ , parse
+ ) where
+
+import Control.Applicative
+import Control.Monad
+import Data.Monoid
+
+-- | The result of running a 'Parser'.
+data Result a = Error String
+ | Success a
+ deriving (Eq, Show)
+
+instance Functor Result where
+ fmap f (Success a) = Success (f a)
+ fmap _ (Error err) = Error err
+ {-# INLINE fmap #-}
+
+instance Monad Result where
+ return = Success
+ {-# INLINE return #-}
+ Success a >>= k = k a
+ Error err >>= _ = Error err
+ {-# INLINE (>>=) #-}
+
+instance Applicative Result where
+ pure = return
+ {-# INLINE pure #-}
+ (<*>) = ap
+ {-# INLINE (<*>) #-}
+
+instance MonadPlus Result where
+ mzero = fail "mzero"
+ {-# INLINE mzero #-}
+ mplus a@(Success _) _ = a
+ mplus _ b = b
+ {-# INLINE mplus #-}
+
+instance Alternative Result where
+ empty = mzero
+ {-# INLINE empty #-}
+ (<|>) = mplus
+ {-# INLINE (<|>) #-}
+
+instance Monoid (Result a) where
+ mempty = fail "mempty"
+ {-# INLINE mempty #-}
+ mappend = mplus
+ {-# INLINE mappend #-}
+
+-- | Failure continuation.
+type Failure f r = String -> f r
+-- | Success continuation.
+type Success a f r = a -> f r
+
+-- | A continuation-based parser type.
+newtype Parser a = Parser {
+ runParser :: forall f r.
+ Failure f r
+ -> Success a f r
+ -> f r
+ }
+
+instance Monad Parser where
+ m >>= g = Parser $ \kf ks -> let ks' a = runParser (g a) kf ks
+ in runParser m kf ks'
+ {-# INLINE (>>=) #-}
+ return a = Parser $ \_kf ks -> ks a
+ {-# INLINE return #-}
+ fail msg = Parser $ \kf _ks -> kf msg
+ {-# INLINE fail #-}
+
+instance Functor Parser where
+ fmap f m = Parser $ \kf ks -> let ks' a = ks (f a)
+ in runParser m kf ks'
+ {-# INLINE fmap #-}
+
+instance Applicative Parser where
+ pure = return
+ {-# INLINE pure #-}
+ (<*>) = apP
+ {-# INLINE (<*>) #-}
+
+instance Alternative Parser where
+ empty = fail "empty"
+ {-# INLINE empty #-}
+ (<|>) = mplus
+ {-# INLINE (<|>) #-}
+
+instance MonadPlus Parser where
+ mzero = fail "mzero"
+ {-# INLINE mzero #-}
+ mplus a b = Parser $ \kf ks -> let kf' _ = runParser b kf ks
+ in runParser a kf' ks
+ {-# INLINE mplus #-}
+
+instance Monoid (Parser a) where
+ mempty = fail "mempty"
+ {-# INLINE mempty #-}
+ mappend = mplus
+ {-# INLINE mappend #-}
+
+apP :: Parser (a -> b) -> Parser a -> Parser b
+apP d e = do
+ b <- d
+ a <- e
+ return (b a)
+{-# INLINE apP #-}
+
+-- | Run a 'Parser'.
+parse :: Parser a -> Result a
+parse p = runParser p Error Success
+{-# INLINE parse #-}
@@ -1,4 +1,4 @@
-Name: sea
+Name: ceason
Version: 0.1.0.0
Synopsis: A CSV parsing and encoding library
Description:
@@ -15,9 +15,9 @@ Cabal-version: >=1.2
Library
- Exposed-modules: Data.Sea
+ Exposed-modules: Data.Ceason
- Other-modules: Data.Sea.Types
+ Other-modules: Data.Ceason.Types
Build-depends: attoparsec,
base,

0 comments on commit e1dcbca

Please sign in to comment.