packages feed

attoparsec-path (empty) → 0.0.0

raw patch · 6 files changed

+187/−0 lines, 6 filesdep +QuickCheckdep +attoparsecdep +attoparsec-pathsetup-changed

Dependencies added: QuickCheck, attoparsec, attoparsec-path, base, path, quickcheck-instances, text

Files

+ LICENSE view
@@ -0,0 +1,30 @@+Copyright Athan Clark (c) 2017++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 Athan Clark 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.
+ README.md view
@@ -0,0 +1,1 @@+# attoparsec-path
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ attoparsec-path.cabal view
@@ -0,0 +1,40 @@+name:                attoparsec-path+version:             0.0.0+synopsis:            Convenience bindings between path and attoparsec+-- description:+homepage:            https://github.com/athanclark/attoparsec-path#readme+license:             BSD3+license-file:        LICENSE+author:              Athan Clark+maintainer:          athan.clark@gmail.com+copyright:           BSD-3+category:            Web+build-type:          Simple+extra-source-files:  README.md+cabal-version:       >=1.10++library+  hs-source-dirs:      src+  exposed-modules:     Data.Attoparsec.Path+  build-depends:       base >= 4.7 && < 5+                     , attoparsec+                     , path+                     , text+  default-language:    Haskell2010++test-suite attoparsec-path-test+  type:                exitcode-stdio-1.0+  hs-source-dirs:      test+  main-is:             Spec.hs+  build-depends:       base+                     , attoparsec+                     , attoparsec-path+                     , text+                     , QuickCheck+                     , quickcheck-instances+  ghc-options:         -threaded -rtsopts -with-rtsopts=-N+  default-language:    Haskell2010++source-repository head+  type:     git+  location: https://github.com/athanclark/attoparsec-path
+ src/Data/Attoparsec/Path.hs view
@@ -0,0 +1,39 @@+module Data.Attoparsec.Path (rootPath, relFilePath, absFilePath, relDirPath, absDirPath) where++import Data.Attoparsec.Text (Parser, char, takeWhile1)+import Data.Functor.Identity (Identity (..))+import qualified Data.Text as T+import Control.Applicative (Alternative (..))+import Path (Path, File, Dir, Abs, Rel, (</>))+import Unsafe.Coerce (unsafeCoerce)++++rootPath :: Parser (Path Abs Dir)+rootPath =+  (unsafeCoerce . (:[])) <$> char '/'++chunkPath :: Parser (Path Rel File)+chunkPath =+  (unsafeCoerce . T.unpack) <$> takeWhile1 (/= '/')++relFilePath :: Parser (Path Rel File)+relFilePath =+  let parseComplex =+        (\head tail -> unsafeCoerce (unsafeCoerce head ++ unsafeCoerce tail))+          <$> chunkPath <*> absFilePath+      parseSimple = chunkPath+  in  parseComplex <|> parseSimple++absFilePath :: Parser (Path Abs File)+absFilePath =+  (</>) <$> rootPath <*> relFilePath++relDirPath :: Parser (Path Rel Dir)+relDirPath =+  (\files suffix -> unsafeCoerce (unsafeCoerce files ++ unsafeCoerce suffix))+    <$> relFilePath <*> rootPath++absDirPath :: Parser (Path Abs Dir)+absDirPath =+  (</>) <$> rootPath <*> relDirPath
+ test/Spec.hs view
@@ -0,0 +1,75 @@+{-# LANGUAGE+    OverloadedStrings+  #-}++module Main where++import Data.Attoparsec.Text+import Data.Attoparsec.Path+import qualified Data.Text as T+import Data.Monoid+import Test.QuickCheck+import Test.QuickCheck.Instances+import Unsafe.Coerce++newtype RelFile = RelFile String+  deriving (Show)++instance Arbitrary RelFile where+  arbitrary = do+    xs <- arbitrary+    pure $ RelFile $+      if T.length xs > 0+      then  let xs' = if T.last xs == '/'+                      then xs <> "a"+                      else xs+                xs'' = if T.head xs' == '/'+                       then "a" <> xs'+                       else xs'+            in  T.unpack xs''+      else "foo"++newtype AbsFile = AbsFile String+  deriving (Show)++instance Arbitrary AbsFile where+  arbitrary = do+    RelFile xs <- arbitrary+    pure $ AbsFile $ "/" ++ xs++newtype RelDir = RelDir String+  deriving (Show)++instance Arbitrary RelDir where+  arbitrary = do+    RelFile xs <- arbitrary+    pure $ RelDir $ xs ++ "/"++newtype AbsDir = AbsDir String+  deriving (Show)++instance Arbitrary AbsDir where+  arbitrary = do+    RelDir xs <- arbitrary+    pure $ AbsDir $ "/" ++ xs++++main :: IO ()+main = do+  quickCheck $ \(RelFile x) -> parseOnly (relFilePath <* endOfInput) (T.pack x) == Right (unsafeCoerce x)+  quickCheck $ \(AbsFile x) -> parseOnly (absFilePath <* endOfInput) (T.pack x) == Right (unsafeCoerce x)+  quickCheck $ \(RelDir x) -> parseOnly (relDirPath <* endOfInput) (T.pack x) == Right (unsafeCoerce x)+  quickCheck $ \(AbsDir x) -> parseOnly (absDirPath <* endOfInput) (T.pack x) == Right (unsafeCoerce x)+  quickCheck $ \(RelFile x) -> case parseOnly (relFilePath <* endOfInput) (T.pack x <> "/") of+    Left _ -> True+    _      -> False+  quickCheck $ \(RelFile x) -> case parseOnly (absFilePath <* endOfInput) (T.pack x <> "/") of+    Left _ -> True+    _      -> False+  quickCheck $ \(RelFile x) -> case parseOnly (relDirPath <* endOfInput) (T.pack x) of+    Left _ -> True+    _      -> False+  quickCheck $ \(RelFile x) -> case parseOnly (absDirPath <* endOfInput) (T.pack x) of+    Left _ -> True+    _      -> False