hw-string-parse (empty) → 0.0.0.1
raw patch · 6 files changed
+137/−0 lines, 6 filesdep +QuickCheckdep +basedep +bytestringsetup-changed
Dependencies added: QuickCheck, base, bytestring, hspec, hw-string-parse, vector
Files
- LICENSE +30/−0
- README.md +4/−0
- Setup.hs +2/−0
- hw-string-parse.cabal +41/−0
- src/HaskellWorks/Data/String/Parse.hs +59/−0
- test/Spec.hs +1/−0
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright John Ky (c) 2016++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 Author name here 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,4 @@+# hw-string-parse+[](https://circleci.com/gh/haskell-works/hw-string-parse/tree/0-branch)++Simple parser combinator library for strings.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ hw-string-parse.cabal view
@@ -0,0 +1,41 @@+name: hw-string-parse+version: 0.0.0.1+synopsis: Conduits for tokenizing streams.+description: Please see README.md+homepage: http://github.com/haskell-works/hw-string-parse#readme+license: MIT+license-file: LICENSE+author: John Ky+maintainer: newhoggy@gmail.com+copyright: 2016 John Ky+category: Data, Bit+stability: Experimental+build-type: Simple+extra-source-files: README.md+cabal-version: >= 1.22++library+ hs-source-dirs: src+ exposed-modules: HaskellWorks.Data.String.Parse+ build-depends: base >= 4 && < 5++ default-language: Haskell2010+ ghc-options: -Wall -O2 -msse4.2++test-suite hw-string-parse-test+ type: exitcode-stdio-1.0+ hs-source-dirs: test+ main-is: Spec.hs+ other-modules:+ build-depends: base >= 4 && < 5+ , bytestring+ , hspec+ , hw-string-parse+ , QuickCheck+ , vector+ ghc-options: -threaded -rtsopts -with-rtsopts=-N -Wall+ default-language: Haskell2010++source-repository head+ type: git+ location: https://github.com/haskell-works/hw-string-parse
+ src/HaskellWorks/Data/String/Parse.hs view
@@ -0,0 +1,59 @@+module HaskellWorks.Data.String.Parse where++import Control.Monad+import Control.Applicative++newtype Parser a = Parser { parse :: String -> [(a, String)] }++runParser :: Parser a -> String -> a+runParser m s =+ case parse m s of+ [(res, [])] -> res+ [(_, _)] -> error "Parser did not consume entire stream."+ _ -> error "Parser error."++item :: Parser Char+item = Parser $ \s ->+ case s of+ [] -> []+ (c:cs) -> [(c,cs)]++bind :: Parser a -> (a -> Parser b) -> Parser b+bind p f = Parser $ \s -> concatMap (\(a, s') -> parse (f a) s') $ parse p s++unit :: a -> Parser a+unit a = Parser (\s -> [(a,s)])++instance Functor Parser where+ fmap f (Parser cs) = Parser (\s -> [(f a, b) | (a, b) <- cs s])++instance Applicative Parser where+ pure = return+ (Parser cs1) <*> (Parser cs2) = Parser (\s -> [(f a, s2) | (f, s1) <- cs1 s, (a, s2) <- cs2 s1])++instance Monad Parser where+ return = unit+ (>>=) = bind++instance MonadPlus Parser where+ mzero = failure+ mplus = combine++instance Alternative Parser where+ empty = mzero+ (<|>) = option++combine :: Parser a -> Parser a -> Parser a+combine p q = Parser (\s -> parse p s ++ parse q s)++failure :: Parser a+failure = Parser (const [])++option :: Parser a -> Parser a -> Parser a+option p q = Parser $ \s ->+ case parse p s of+ [] -> parse q s+ res -> res++char :: Char -> Parser Char+char = return
+ test/Spec.hs view
@@ -0,0 +1,1 @@+{-# OPTIONS_GHC -F -pgmF hspec-discover #-}