binary-parser (empty) → 0.5
raw patch · 5 files changed
+253/−0 lines, 5 filesdep +basedep +base-preludedep +bytestringsetup-changed
Dependencies added: base, base-prelude, bytestring, success, text, transformers
Files
- LICENSE +22/−0
- Setup.hs +2/−0
- binary-parser.cabal +55/−0
- library/BinaryParser.hs +114/−0
- library/BinaryParser/Prelude.hs +60/−0
+ LICENSE view
@@ -0,0 +1,22 @@+Copyright (c) 2015, Nikita Volkov++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:++The above copyright notice and this permission notice shall be+included in all copies or substantial portions of the Software.++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.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ binary-parser.cabal view
@@ -0,0 +1,55 @@+name:+ binary-parser+version:+ 0.5+synopsis:+ A highly-efficient but limited parser API specialised for bytestrings+category:+ Parser, Binary+homepage:+ https://github.com/nikita-volkov/binary-parser +bug-reports:+ https://github.com/nikita-volkov/binary-parser/issues +author:+ Nikita Volkov <nikita.y.volkov@mail.ru>+maintainer:+ Nikita Volkov <nikita.y.volkov@mail.ru>+copyright:+ (c) 2015, Nikita Volkov+license:+ MIT+license-file:+ LICENSE+build-type:+ Simple+cabal-version:+ >=1.10+++source-repository head+ type:+ git+ location:+ git://github.com/nikita-volkov/binary-parser.git+++library+ hs-source-dirs:+ library+ default-extensions:+ Arrows, BangPatterns, ConstraintKinds, DataKinds, DefaultSignatures, DeriveDataTypeable, DeriveFunctor, DeriveGeneric, EmptyDataDecls, FlexibleContexts, FlexibleInstances, FunctionalDependencies, GADTs, GeneralizedNewtypeDeriving, ImpredicativeTypes, LambdaCase, LiberalTypeSynonyms, MagicHash, MultiParamTypeClasses, MultiWayIf, NoImplicitPrelude, NoMonomorphismRestriction, OverloadedStrings, PatternGuards, ParallelListComp, QuasiQuotes, RankNTypes, RecordWildCards, ScopedTypeVariables, StandaloneDeriving, TemplateHaskell, TupleSections, TypeFamilies, TypeOperators, UnboxedTuples+ default-language:+ Haskell2010+ other-modules:+ BinaryParser.Prelude+ exposed-modules:+ BinaryParser+ build-depends:+ -- data:+ success >= 0.2 && < 0.3,+ bytestring >= 0.10 && < 0.11,+ text >= 1 && < 2,+ -- general:+ transformers >= 0.3 && < 0.5,+ base-prelude >= 0.1.19 && < 0.2+
+ library/BinaryParser.hs view
@@ -0,0 +1,114 @@+module BinaryParser+(+ BinaryParser,+ run,+ failure,+ byte,+ bytesOfSize,+ unitOfSize,+ unitOfBytes,+ remainders,+ endOfInput,+ sized,+)+where++import BinaryParser.Prelude+import qualified Data.ByteString as ByteString+import qualified Data.ByteString.Unsafe as ByteString+import qualified Success.Pure as Success+++-- |+-- A highly-efficient parser specialised for strict 'ByteString's.+-- +-- Supports the roll-back and alternative branching+-- on the basis of the 'Alternative' interface.+-- +-- Does not generate fancy error-messages,+-- which contributes to its efficiency.+newtype BinaryParser a =+ BinaryParser ( StateT ByteString ( Success.Success Text ) a )+ deriving ( Functor , Applicative , Alternative , Monad , MonadPlus )++-- |+-- Apply a parser to bytes.+{-# INLINE run #-}+run :: BinaryParser a -> ByteString -> Either Text a+run (BinaryParser parser) input =+ mapLeft fold (Success.asEither (evalStateT parser input))++-- |+-- Fail with a message.+{-# INLINE failure #-}+failure :: Text -> BinaryParser a+failure text =+ BinaryParser (lift (Success.failure text))++-- |+-- Consume a single byte.+{-# INLINE byte #-}+byte :: BinaryParser Word8+byte =+ BinaryParser $ StateT $ \remainders ->+ if ByteString.null remainders+ then Success.failure "End of input"+ else pure (ByteString.unsafeHead remainders, ByteString.unsafeDrop 1 remainders)++-- |+-- Consume an amount of bytes.+{-# INLINE bytesOfSize #-}+bytesOfSize :: Int -> BinaryParser ByteString+bytesOfSize size =+ BinaryParser $ StateT $ \remainders ->+ if ByteString.length remainders >= size+ then return (ByteString.unsafeTake size remainders, ByteString.unsafeDrop size remainders)+ else Success.failure "End of input"++-- |+-- Skip an amount of bytes.+{-# INLINE unitOfSize #-}+unitOfSize :: Int -> BinaryParser ()+unitOfSize size =+ BinaryParser $ StateT $ \remainders ->+ if ByteString.length remainders >= size+ then return ((), ByteString.unsafeDrop size remainders)+ else Success.failure "End of input"++-- |+-- Skip specific bytes, while failing if they don't match.+{-# INLINE unitOfBytes #-}+unitOfBytes :: ByteString -> BinaryParser ()+unitOfBytes bytes =+ BinaryParser $ StateT $ \remainders ->+ if ByteString.isPrefixOf bytes remainders+ then return ((), ByteString.unsafeDrop (ByteString.length bytes) remainders)+ else Success.failure "Bytes don't match"++-- |+-- Consume all the remaining bytes.+{-# INLINE remainders #-}+remainders :: BinaryParser ByteString+remainders =+ BinaryParser $ StateT $ \remainders -> return (remainders, ByteString.empty)++-- |+-- Fail if the input hasn't ended.+{-# INLINE endOfInput #-}+endOfInput :: BinaryParser ()+endOfInput =+ BinaryParser $ StateT $ \case+ "" -> return ((), ByteString.empty)+ _ -> Success.failure "Not the end of input"++-- |+-- Run a subparser passing it a chunk of the current input of the specified size.+{-# INLINE sized #-}+sized :: Int -> BinaryParser a -> BinaryParser a+sized size (BinaryParser stateT) =+ BinaryParser $ StateT $ \remainders ->+ if ByteString.length remainders >= size+ then + evalStateT stateT (ByteString.unsafeTake size remainders) &+ fmap (\result -> (result, ByteString.unsafeDrop size remainders))+ else Success.failure "End of input"
+ library/BinaryParser/Prelude.hs view
@@ -0,0 +1,60 @@+module BinaryParser.Prelude+( + module Exports,+ LazyByteString,+ ByteStringBuilder,+ LazyText,+ TextBuilder,+ mapLeft,+ joinMap,+)+where+++-- base-prelude+-------------------------+import BasePrelude as Exports hiding (fail)++-- transformers+-------------------------+import Control.Monad.Trans.State.Strict as Exports hiding (liftCallCC, liftCatch)+import Control.Monad.Trans.Reader as Exports hiding (liftCallCC, liftCatch)+import Control.Monad.Trans.Class as Exports++-- bytestring+-------------------------+import Data.ByteString as Exports (ByteString)++-- text+-------------------------+import Data.Text as Exports (Text)++-- custom+-------------------------+import qualified Data.ByteString.Lazy+import qualified Data.ByteString.Builder+import qualified Data.Text.Lazy+import qualified Data.Text.Lazy.Builder+++type LazyByteString =+ Data.ByteString.Lazy.ByteString++type ByteStringBuilder =+ Data.ByteString.Builder.Builder++type LazyText =+ Data.Text.Lazy.Text++type TextBuilder =+ Data.Text.Lazy.Builder.Builder+++{-# INLINE mapLeft #-}+mapLeft :: (a -> b) -> Either a x -> Either b x+mapLeft f =+ either (Left . f) Right++joinMap :: Monad m => (a -> m b) -> m a -> m b+joinMap f =+ join . fmap f