regex-tdfa-pipes (empty) → 0.1.0.0
raw patch · 4 files changed
+308/−0 lines, 4 filesdep +arraydep +basedep +lenssetup-changed
Dependencies added: array, base, lens, monads-tf, pipes, regex-base, regex-tdfa
Files
- LICENSE +30/−0
- Setup.hs +2/−0
- Text/Regex/TDFA/Pipes.hs +239/−0
- regex-tdfa-pipes.cabal +37/−0
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright (c) 2015, Eric Brisco + +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 Eric Brisco 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.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple +main = defaultMain
+ Text/Regex/TDFA/Pipes.hs view
@@ -0,0 +1,239 @@+{-# LANGUAGE MultiParamTypeClasses, FlexibleInstances #-} +module Text.Regex.TDFA.Pipes +( Regex +, CompOption +, ExecOption +, compile +, execute +, regexec +, ProjectChar(..) +) where + +import Prelude + ( Monad(..) + , Functor(..) + , String + , Either(..) + , (.) + , (++) + , Maybe(..) + , show + , Char + , fst + , tail + , map + , id + , either + ) +-- + +import Text.Regex.TDFA.NewDFA.Uncons + ( Uncons(..) + ) +-- + +import Text.Regex.TDFA.NewDFA.Engine + ( execMatch + ) +-- + +import Text.Regex.Base.Impl + ( polymatch + , polymatchM + ) +-- + +import Text.Regex.TDFA.Common + ( CompOption + , ExecOption + ) +-- + +import Text.Regex.TDFA.TDFA + ( patternToRegex + ) +-- + +import Text.Regex.Base + ( RegexLike(..) + , RegexOptions + , Extract(..) + , RegexContext(..) + , MatchArray + , matchOnce + ) +-- + +import Text.Regex.TDFA + ( Regex + ) +-- + +import Text.Regex.TDFA.ReadRegex + ( parseRegex + ) +-- + +import Pipes + ( (<-<) + ) +-- + +import Pipes as Pipe + ( next + ) +-- + +import qualified Pipes.Prelude as Pipe + ( take + , drop + , toListM + ) +-- + +import Pipes.Core + ( Producer + ) +-- + +import Control.Lens + ( (^?) + , over + , _Just + , _1 + , _Right + , each + , firstOf + ) +-- + +import Data.Foldable + ( Foldable + , asum + ) +-- + +import Data.Array.IArray + ( (!) + , elems + , amap + ) +-- + +import Control.Monad.Identity + ( Identity(..) + ) +-- + +------------------------------------------------------------------------ +-- ProjectChar +------------------------------------------------------------------------ + +-- | Types which can have a Char projected from them. +-- +class ProjectChar a where + -- | Project a Char from the type. + -- + projectChar :: a -> Char +-- + +instance ProjectChar Char where + projectChar = id +-- + +instance (ProjectChar a, ProjectChar b) => + ProjectChar (Either a b) + where + projectChar = either projectChar projectChar +-- + +instance (ProjectChar a) => ProjectChar (Identity a) where + projectChar = projectChar . runIdentity +-- + +instance (ProjectChar a, Monad m, Foldable m, Functor m) => + Uncons (Producer a m r) + where + uncons = over (_Just . _1) projectChar + . asum + . fmap (^? _Right) + . Pipe.next +-- + +------------------------------------------------------------------------ +-- Adapting Pipes to TDFA +------------------------------------------------------------------------ + +instance (Monad m) => Extract (Producer a m ()) where + before = (<-<) . Pipe.take + after = (<-<) . Pipe.drop + empty = return () + extract (off, len) = before len . after off +-- + +instance (ProjectChar a, Monad m, Foldable m, Functor m) => + RegexLike Regex (Producer a m ()) + where + matchOnce r p = firstOf each (matchAll r p) + matchAll r p = execMatch r 0 '\n' p +-- + +instance (ProjectChar a, Monad m, Foldable m, Functor m) => + RegexContext Regex (Producer a m ()) (Producer a m ()) + where + match = polymatch + matchM = polymatchM +-- + +------------------------------------------------------------------------ +-- Exported API +------------------------------------------------------------------------ + +-- | Takes the output of a Producer to form a regular expression. This +-- expression is then compiled using the options provided. If +-- compilation fails an error message is returned. +-- +-- If your regular expression is expressed as a String literal or +-- some other "Data.Foldable" then "Pipes.each" is useful. +-- +compile :: (ProjectChar a, Monad m, Foldable m, Functor m) => + CompOption + -> ExecOption + -> Producer a m () + -> Either String Regex +compile compOpt execOpt regexp = + case parseRegex (map projectChar (asum (Pipe.toListM regexp))) of + Left err -> Left ( "parseRegex for Text.Regex.TDFA.Pipes failed: " + ++ show err + ) + Right pat -> Right (patternToRegex pat compOpt execOpt) +-- + +-- | Execute a compiled regular expression on the output of a Producer. +-- +execute :: (ProjectChar a, Monad m, Foldable m, Functor m) => + Regex -> Producer a m () -> Either a (Maybe MatchArray) +execute r p = Right (matchOnce r p) + + +regexec :: (ProjectChar a, Monad m, Foldable m, Functor m) => + Regex + -> Producer a m () + -> Either String ( Maybe ( Producer a m () + , Producer a m () + , Producer a m () + , [Producer a m ()] + ) + ) +regexec r bs = + case matchOnceText r bs of + Nothing -> Right Nothing + Just (pre,mt,post) -> + let main = fst (mt!0) + rest = map fst (tail (elems mt)) -- will be [] + in Right (Just (pre,main,post,rest)) +-- + + + +
+ regex-tdfa-pipes.cabal view
@@ -0,0 +1,37 @@+name: regex-tdfa-pipes +version: 0.1.0.0 +synopsis: Parse with regular expressions on Producers. +description: Parse with regular expressions on Producers. +homepage: http://github.com/erisco/regex-tdfa-pipes +license: BSD3 +license-file: LICENSE +author: Eric Brisco +maintainer: eric.brisco@gmail.com +category: Text +build-type: Simple +cabal-version: >=1.10 + +source-repository this + type: git + location: http://github.com/erisco/regex-tdfa-pipes.git + tag: 0.1.0.0 + + +library + + exposed-modules: Text.Regex.TDFA.Pipes + + other-extensions: MultiParamTypeClasses + FlexibleInstances + + build-depends: base >=4.7 && <4.8 + , regex-tdfa >=1.2 && <1.3 + , regex-base >=0.93 && <0.94 + , pipes >=4.1 && <4.2 + , lens >=4.12 && <4.13 + , array >=0.5 && <0.6 + , monads-tf >=0.1 && <0.2 + + + default-language: Haskell2010 +