constaparser (empty) → 0.1.0.0
raw patch · 6 files changed
+146/−0 lines, 6 filesdep +attoparsecdep +basedep +bytestringsetup-changed
Dependencies added: attoparsec, base, bytestring, vector
Files
- ChangeLog.md +2/−0
- Data/Constaparser.hs +12/−0
- Data/Constaparser/Internal.hs +92/−0
- LICENSE +11/−0
- Setup.hs +2/−0
- constaparser.cabal +27/−0
+ ChangeLog.md view
@@ -0,0 +1,2 @@+# 0.1.0.0+Initial version.
+ Data/Constaparser.hs view
@@ -0,0 +1,12 @@+{-# OPTIONS_GHC -O2 -Wall #-}++module Data.Constaparser + ( Constaparser+ , ConstaparserST+ , constaparserToAttoparsec+ , withMutableVector+ , cpInt + , dropTrailingSpace + ) where++import Data.Constaparser.Internal
+ Data/Constaparser/Internal.hs view
@@ -0,0 +1,92 @@+{-# LANGUAGE BangPatterns #-}+{-# LANGUAGE DeriveFunctor #-}+{-# LANGUAGE RankNTypes #-}++{-# OPTIONS_GHC -O2 -Wall #-}++module Data.Constaparser.Internal + ( Constaparser(..)+ , ConstaparserST(..)+ , constaparserToAttoparsec+ , withMutableVector+ , cpInt + , dropTrailingSpace + ) where++import Control.Monad.ST+import Data.Attoparsec.ByteString.Char8 (Parser)+import Data.ByteString.Char8 (ByteString)+import Data.Vector (Vector)+import Data.Vector.Mutable (MVector)++import qualified Data.Attoparsec.ByteString.Char8 as AB+import qualified Data.ByteString.Char8 as BC+import qualified Data.Vector as V+import qualified Data.Vector.Mutable as MV++data Constaparser a = Constaparser+ {-# UNPACK #-} !Int+ (ByteString -> Either String a)+ deriving (Functor)++instance Applicative Constaparser where+ pure a = Constaparser 0 (const (Right a))+ Constaparser n f1 <*> Constaparser m f2 = Constaparser (n + m) $+ \bs -> do+ let (bs1,bs2) = BC.splitAt n bs+ func <- f1 bs1+ val <- f2 bs2+ Right (func val)++data ConstaparserST s e a = ConstaparserST+ {-# UNPACK #-} !Int+ (e -> ByteString -> ST s (Either String a))+ deriving (Functor)++instance Applicative (ConstaparserST s e) where+ pure a = ConstaparserST 0 (\_ _ -> pure (Right a))+ ConstaparserST n f1 <*> ConstaparserST m f2 = ConstaparserST (n + m) $+ \e bs -> do+ let (bs1,bs2) = BC.splitAt n bs+ efunc <- f1 e bs1+ case efunc of+ Left err -> pure (Left err)+ Right func -> do+ eval <- f2 e bs2+ case eval of+ Left err -> pure (Left err)+ Right val -> pure (Right (func val))++constaparserToAttoparsec :: Constaparser a -> Parser a+constaparserToAttoparsec (Constaparser n f) = do+ bs <- AB.take n+ case f bs of+ Left err -> fail ("constaparser failed: " ++ err)+ Right a -> pure a++withMutableVector+ :: Int + -> (forall s. ConstaparserST s (MVector s a) ())+ -> Constaparser (Vector a)+withMutableVector sz cp@(ConstaparserST n _) = Constaparser n $+ \bs -> runST $ do+ case cp of+ ConstaparserST _ f -> do+ mv <- MV.new sz+ e <- f mv bs+ case e of+ Left err -> pure (Left err)+ Right _ -> do+ v <- V.unsafeFreeze mv+ pure (Right v)++cpInt :: String -> Int -> Constaparser Int+cpInt note n = Constaparser n $+ \bs -> case BC.readInt (dropTrailingSpace bs) of+ Nothing -> Left (note ++ " string was: " ++ BC.unpack bs)+ Just (i, remaining) -> if BC.null remaining+ then Right i+ else Left note++dropTrailingSpace :: ByteString -> ByteString+dropTrailingSpace = fst . BC.spanEnd (== ' ')
+ LICENSE view
@@ -0,0 +1,11 @@+Copyright 2018 Andrew Martin++Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:++1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.++2. 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.++3. Neither the name of the copyright holder nor the names of its 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 HOLDER 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.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ constaparser.cabal view
@@ -0,0 +1,27 @@+name: constaparser+version: 0.1.0.0+synopsis: Parse ByteStrings of a constant length. +description: Parse constant-length ByteStrings, which allows+ for some performance optimisations.+ You can also convert from a 'Constaparser' to a+ 'Parser' from Attoparsec.+homepage: https://github.com/chessai/constaparser+license: BSD3+license-file: LICENSE+author: Andrew Martin+maintainer: chessai1996@gmail.com+copyright: 2018 Andrew Martin +category: Data+build-type: Simple+extra-source-files: ChangeLog.md+cabal-version: >=1.10++library+ exposed-modules: Data.Constaparser+ other-modules: Data.Constaparser.Internal + build-depends: base >=4.10 && <4.11, attoparsec, bytestring >=0.10 && <0.11, vector+ default-language: Haskell2010++source-repository head+ type: git+ location: https://github.com/chessai/constaparser