regex-examples 0.2.0.0 → 0.2.0.1
raw patch · 36 files changed
+38/−4600 lines, 36 filesdep ~regex
Dependency ranges changed: regex
Files
- Text/RE.hs +0/−199
- Text/RE/Capture.lhs +0/−290
- Text/RE/CaptureID.hs +0/−39
- Text/RE/Edit.lhs +0/−98
- Text/RE/Internal/AddCaptureNames.hs +0/−11
- Text/RE/Internal/NamedCaptures.lhs +0/−205
- Text/RE/Internal/PreludeMacros.hs +0/−907
- Text/RE/Internal/QQ.hs +0/−26
- Text/RE/IsRegex.lhs +0/−17
- Text/RE/LineNo.hs +0/−18
- Text/RE/Options.lhs +0/−77
- Text/RE/PCRE.hs +0/−61
- Text/RE/PCRE/ByteString.hs +0/−61
- Text/RE/PCRE/ByteString/Lazy.hs +0/−61
- Text/RE/PCRE/RE.hs +0/−249
- Text/RE/PCRE/Sequence.hs +0/−61
- Text/RE/PCRE/String.hs +0/−61
- Text/RE/Parsers.hs +0/−166
- Text/RE/Replace.lhs +0/−498
- Text/RE/TDFA.hs +0/−64
- Text/RE/TDFA/ByteString.hs +0/−61
- Text/RE/TDFA/ByteString/Lazy.hs +0/−61
- Text/RE/TDFA/RE.hs +0/−245
- Text/RE/TDFA/Sequence.hs +0/−61
- Text/RE/TDFA/String.hs +0/−61
- Text/RE/TDFA/Text.hs +0/−61
- Text/RE/TDFA/Text/Lazy.hs +0/−61
- Text/RE/TestBench.lhs +0/−564
- Text/RE/Tools/Grep.lhs +0/−62
- Text/RE/Tools/Lex.lhs +0/−39
- Text/RE/Tools/Sed.lhs +0/−55
- changelog +3/−0
- lib/cabal-masters/regex-examples.cabal +0/−2
- lib/mega-regex.cabal +17/−17
- lib/version.txt +1/−1
- regex-examples.cabal +17/−80
− Text/RE.hs
@@ -1,199 +0,0 @@-{-# OPTIONS_GHC -fno-warn-dodgy-exports #-}--- |--- Module : Text.RE--- Copyright : (C) 2016-17 Chris Dornan--- License : BSD3 (see the LICENSE file)--- Maintainer : Chris Dornan <chris.dornan@irisconnect.com>--- Stability : RFC--- Portability : portable--module Text.RE- (- -- * Tutorial- -- $tutorial-- -- * How to use this library- -- $use-- -- ** The Match Operators- -- $operators-- -- * Matches, Match, Capture Types and Functions- Matches(..)- , Match(..)- , Capture(..)- , noMatch- -- ** Matches functions- , anyMatches- , countMatches- , matches- , mainCaptures- -- ** Match functions- , matched- , matchedText- , matchCapture- , matchCaptures- , (!$$)- , captureText- , (!$$?)- , captureTextMaybe- , (!$)- , capture- , (!$?)- , captureMaybe- -- ** Capture functions- , hasCaptured- , capturePrefix- , captureSuffix- -- * IsRegex- , IsRegex(..)- -- * Options- , Options_(..)- , IsOption(..)- , Mode(..)- , MacroID(..)- , Macros- , emptyMacros- , SimpleRegexOptions(..)- -- * CaptureID- , CaptureID(..)- , CaptureNames- , noCaptureNames- , CaptureName(..)- , CaptureOrdinal(..)- , findCaptureID- -- * Edit- , Edits(..)- , Edit(..)- , LineEdit(..)- , applyEdits- , applyEdit- , applyLineEdit- -- * LineNo- , LineNo(..)- , firstLine- , getLineNo- , lineNo- -- * Parsers- , parseInteger- , parseHex- , parseDouble- , parseString- , parseSimpleString- , parseDate- , parseSlashesDate- , parseTimeOfDay- , parseTimeZone- , parseDateTime- , parseDateTime8601- , parseDateTimeCLF- , parseShortMonth- , shortMonthArray- , IPV4Address- , parseIPv4Address- , Severity(..)- , parseSeverity- , severityKeywords- -- * Replace- , Replace(..)- , Replace_(..)- , replace_- , Phi(..)- , Context(..)- , Location(..)- , isTopLocation- , replace- , replaceAll- , replaceAllCaptures- , replaceAllCaptures'- , replaceAllCaptures_- , replaceAllCapturesM- , replaceCaptures- , replaceCaptures'- , replaceCaptures_- , replaceCapturesM- , expandMacros- , expandMacros'- -- * Tools- -- ** Grep- , Line(..)- , grep- , grepLines- , GrepScript- , grepScript- , linesMatched- -- ** Lex- , alex- , alex'- -- ** Sed- , SedScript- , sed- , sed'- ) where--import Text.RE.Capture-import Text.RE.CaptureID-import Text.RE.Edit-import Text.RE.IsRegex-import Text.RE.LineNo-import Text.RE.Options-import Text.RE.Parsers-import Text.RE.Replace-import Text.RE.Tools.Grep-import Text.RE.Tools.Lex-import Text.RE.Tools.Sed---- $tutorial--- We have a regex tutorial at <http://tutorial.regex.uk>. These API--- docs are mainly for reference.---- $use------ This module won't provide any operators to match a regular expression--- against text as it merely provides the toolkit for working with the--- output of the match operators. You probably won't import it directly--- but import one of the modules that provides the match operators,--- which will in tuen re-export this module.------ The module that you choose to import will depend upon two factors:------ * Which flavour of regular expression do you want to use? If you want--- Posix flavour REs then you want the TDFA modules, otherwise its--- PCRE for Perl-style REs.------ * What type of text do you want to match: (slow) @String@s, @ByteString@,--- @ByteString.Lazy@, @Text@, @Text.Lazy@ or the anachronistic @Seq Char@--- or indeed a good old-fashioned polymorphic operators?------ While we aim to provide all combinations of these choices, some of them--- are currently not available. We have:------ * "Text.RE.PCRE"--- * "Text.RE.PCRE.ByteString"--- * "Text.RE.PCRE.ByteString.Lazy"--- * "Text.RE.PCRE.RE"--- * "Text.RE.PCRE.Sequence"--- * "Text.RE.PCRE.String"--- * "Text.RE.TDFA"--- * "Text.RE.TDFA.ByteString"--- * "Text.RE.TDFA.ByteString.Lazy"--- * "Text.RE.TDFA.RE"--- * "Text.RE.TDFA.Sequence"--- * "Text.RE.TDFA.String"--- * "Text.RE.TDFA.Text"--- * "Text.RE.TDFA.Text.Lazy"---- $operators------ The traditional @=~@ and @=~~@ operators are exported by the @regex@,--- but we recommend that you use the two new operators, especially if--- you are not familiar with the old operators. We have:------ * @txt ?=~ re@ searches for a single match yielding a value of type--- 'Match' @a@ where @a@ is the type of the text you are searching.------ * @txt *=~ re@ searches for all non-overlapping matches in @txt@,--- returning a value of type 'Matches' @a@.------ See the sections below for more information on these @Matches@ and--- @Match@ result types.
− Text/RE/Capture.lhs
@@ -1,290 +0,0 @@-\begin{code}-{-# LANGUAGE RecordWildCards #-}-{-# LANGUAGE FlexibleInstances #-}-{-# LANGUAGE UndecidableInstances #-}-{-# LANGUAGE MultiParamTypeClasses #-}-\end{code}--\begin{code}-module Text.RE.Capture- ( Matches(..)- , Match(..)- , Capture(..)- , noMatch- , emptyMatchArray- -- Matches functions- , anyMatches- , countMatches- , matches- , mainCaptures- -- Match functions- , matched- , matchedText- , matchCapture- , matchCaptures- , (!$$)- , captureText- , (!$$?)- , captureTextMaybe- , (!$)- , capture- , (!$?)- , captureMaybe- -- Capture functions- , hasCaptured- , capturePrefix- , captureSuffix- ) where-\end{code}--\begin{code}-import Data.Array-import Data.Maybe-import Text.Regex.Base-import Text.RE.CaptureID--infixl 9 !$, !$$-\end{code}----\begin{code}--- | the result type to use when every match is needed, not just the--- first match of the RE against the source-data Matches a =- Matches- { matchesSource :: !a -- ^ the source text being matched- , allMatches :: ![Match a] -- ^ all captures found, left to right- }- deriving (Show,Eq)-\end{code}--\begin{code}--- | the result of matching a RE to a text once, listing the text that--- was matched and the named captures in the RE and all of the substrings--- matched, with the text captured by the whole RE; a complete failure--- to match will be represented with an empty array (with bounds (0,-1))-data Match a =- Match- { matchSource :: !a -- ^ the whole source text- , captureNames :: !CaptureNames -- ^ the RE's capture names- , matchArray :: !(Array CaptureOrdinal (Capture a))- -- ^ 0..n-1 captures,- -- starting with the- -- text matched by the- -- whole RE- }- deriving (Show,Eq)-\end{code}--\begin{code}--- | the matching of a single sub-expression against part of the source--- text-data Capture a =- Capture- { captureSource :: !a -- ^ the whole text that was searched- , capturedText :: !a -- ^ the text that was matched- , captureOffset :: !Int -- ^ the number of characters preceding the- -- match with -1 used if no text was captured- -- by the RE (not even the empty string)- , captureLength :: !Int -- ^ the number of chacter in the captured- -- sub-string- }- deriving (Show,Eq)-\end{code}--\begin{code}--- | Construct a Match that does not match anything.-noMatch :: a -> Match a-noMatch t = Match t noCaptureNames emptyMatchArray---- | an empty array of Capture-emptyMatchArray :: Array CaptureOrdinal (Capture a)-emptyMatchArray = listArray (CaptureOrdinal 0,CaptureOrdinal $ -1) []-\end{code}--\begin{code}-instance Functor Matches where- fmap f Matches{..} =- Matches- { matchesSource = f matchesSource- , allMatches = map (fmap f) allMatches- }--instance Functor Match where- fmap f Match{..} =- Match- { matchSource = f matchSource- , captureNames = captureNames- , matchArray = fmap (fmap f) matchArray- }--instance Functor Capture where- fmap f c@Capture{..} =- c- { captureSource = f captureSource- , capturedText = f capturedText- }-\end{code}--\begin{code}--- | tests whether the RE matched the source text at all-anyMatches :: Matches a -> Bool-anyMatches = not . null . allMatches---- | count the matches-countMatches :: Matches a -> Int-countMatches = length . allMatches--matches :: Matches a -> [a]-matches = map capturedText . mainCaptures---- | extract the main capture from each match-mainCaptures :: Matches a -> [Capture a]-mainCaptures ac = [ capture c0 cs | cs<-allMatches ac ]- where- c0 = CID_ordinal $ CaptureOrdinal 0-\end{code}----\begin{code}--- | tests whether the RE matched the source text at all-matched :: Match a -> Bool-matched = isJust . matchCapture---- | tests whether the RE matched the source text at all-matchedText :: Match a -> Maybe a-matchedText = fmap capturedText . matchCapture---- | the top-level capture if the source text matched the RE,--- Nothing otherwise-matchCapture :: Match a -> Maybe (Capture a)-matchCapture = fmap fst . matchCaptures---- | the top-level capture and the sub captures if the text matched--- the RE, Nothing otherwise-matchCaptures :: Match a -> Maybe (Capture a,[Capture a])-matchCaptures Match{..} = case rangeSize (bounds matchArray) == 0 of- True -> Nothing- False -> Just (matchArray!0,drop 1 $ elems matchArray)---- | an alternative for captureText-(!$$) :: Match a -> CaptureID -> a-(!$$) = flip captureText---- | look up the text of the nth capture, 0 being the match of the whole--- RE against the source text, 1, the first bracketed sub-expression to--- be matched and so on-captureText :: CaptureID -> Match a -> a-captureText cid mtch = capturedText $ capture cid mtch---- | an alternative for captureTextMaybe-(!$$?) :: Match a -> CaptureID -> Maybe a-(!$$?) = flip captureTextMaybe---- | look up the text of the nth capture (0 being the match of the--- whole), returning Nothing if the Match doesn't contain the capture-captureTextMaybe :: CaptureID -> Match a -> Maybe a-captureTextMaybe cid mtch = do- cap <- mtch !$? cid- case hasCaptured cap of- True -> Just $ capturedText cap- False -> Nothing---- | an alternative for capture-(!$) :: Match a -> CaptureID -> Capture a-(!$) = flip capture---- | look up the nth capture, 0 being the match of the whole RE against--- the source text, 1, the first bracketed sub-expression to be matched--- and so on-capture :: CaptureID -> Match a -> Capture a-capture cid mtch = fromMaybe oops $ mtch !$? cid- where- oops = error $ "capture: out of bounds (" ++ show cid ++ ")"---- | an alternative for capture captureMaybe-(!$?) :: Match a -> CaptureID -> Maybe (Capture a)-(!$?) = flip captureMaybe---- | look up the nth capture, 0 being the match of the whole RE against--- the source text, 1, the first bracketed sub-expression to be matched--- and so on, returning Nothing if there is no such capture, or if the--- capture failed to capture anything (being in a failed alternate)-captureMaybe :: CaptureID -> Match a -> Maybe (Capture a)-captureMaybe cid mtch@Match{..} = do- cap <- case bounds matchArray `inRange` CaptureOrdinal i of- True -> Just $ matchArray ! CaptureOrdinal i- False -> Nothing- case hasCaptured cap of- True -> Just cap- False -> Nothing- where- i = lookupCaptureID cid mtch--lookupCaptureID :: CaptureID -> Match a -> Int-lookupCaptureID cid Match{..} = findCaptureID cid captureNames-\end{code}---\begin{code}--- | test if the capture has matched any text-hasCaptured :: Capture a -> Bool-hasCaptured = (>=0) . captureOffset---- | returns the text preceding the match-capturePrefix :: Extract a => Capture a -> a-capturePrefix Capture{..} = before captureOffset captureSource---- | returns the text after the match-captureSuffix :: Extract a => Capture a -> a-captureSuffix Capture{..} = after (captureOffset+captureLength) captureSource-\end{code}---\begin{code}--- | for matching just the first RE against the source text-instance- ( RegexContext regex source (AllTextSubmatches (Array Int) (source,(Int,Int)))- , RegexLike regex source- ) =>- RegexContext regex source (Match source) where- match r s = cvt s $ getAllTextSubmatches $ match r s- matchM r s = do- y <- matchM r s- return $ cvt s $ getAllTextSubmatches y---- | for matching all REs against the source text-instance- ( RegexContext regex source [MatchText source]- , RegexLike regex source- ) =>- RegexContext regex source (Matches source) where- match r s = Matches s $ map (cvt s) $ match r s- matchM r s = do- y <- matchM r s- return $ Matches s $ map (cvt s) y-\end{code}--\begin{code}-cvt :: source -> MatchText source -> Match source-cvt hay arr =- Match- { matchSource = hay- , captureNames = noCaptureNames- , matchArray =- ixmap (CaptureOrdinal lo,CaptureOrdinal hi) getCaptureOrdinal $- fmap f arr- }- where- (lo,hi) = bounds arr-- f (ndl,(off,len)) =- Capture- { captureSource = hay- , capturedText = ndl- , captureOffset = off- , captureLength = len- }-\end{code}
− Text/RE/CaptureID.hs
@@ -1,39 +0,0 @@-{-# LANGUAGE GeneralizedNewtypeDeriving #-}--module Text.RE.CaptureID where--import Data.Ix-import Data.Hashable-import qualified Data.HashMap.Strict as HMS-import Data.Maybe-import qualified Data.Text as T---data CaptureID- = CID_ordinal CaptureOrdinal- | CID_name CaptureName- deriving (Show,Ord,Eq)--type CaptureNames = HMS.HashMap CaptureName CaptureOrdinal--noCaptureNames :: CaptureNames-noCaptureNames = HMS.empty--newtype CaptureName = CaptureName { getCaptureName :: T.Text }- deriving (Show,Ord,Eq)--instance Hashable CaptureName where- hashWithSalt i = hashWithSalt i . getCaptureName--newtype CaptureOrdinal = CaptureOrdinal { getCaptureOrdinal :: Int }- deriving (Show,Ord,Eq,Enum,Ix,Num)--findCaptureID :: CaptureID -> CaptureNames -> Int-findCaptureID (CID_ordinal o) _ = getCaptureOrdinal o-findCaptureID (CID_name n) hms =- getCaptureOrdinal $ fromMaybe oops $ HMS.lookup n hms- where- oops = error $ unlines $- ("lookupCaptureID: " ++ T.unpack t ++ " not found in:") :- [ " "++T.unpack (getCaptureName nm) | nm <- HMS.keys hms ]- t = getCaptureName n
− Text/RE/Edit.lhs
@@ -1,98 +0,0 @@-\begin{code}-{-# LANGUAGE NoImplicitPrelude #-}-{-# LANGUAGE RecordWildCards #-}-{-# LANGUAGE CPP #-}-#if __GLASGOW_HASKELL__ >= 800-{-# OPTIONS_GHC -fno-warn-redundant-constraints #-}-#endif--module Text.RE.Edit- ( LineNo- , Edits(..)- , Edit(..)- , LineEdit(..)- , applyEdits- , applyEdit- , applyLineEdit- ) where--import Data.Maybe-import Prelude.Compat-import Text.RE.Capture-import Text.RE.IsRegex-import Text.RE.LineNo-import Text.RE.Replace---data Edits m re s- = Select [(re,Edit m s)]- | Pipe [(re,Edit m s)]--data Edit m s- = EDIT_tpl s- | EDIT_phi (Phi s)- | EDIT_fun Context (LineNo->Match s->Location->Capture s->m (Maybe s))- | EDIT_gen (LineNo->Matches s->m (LineEdit s))--data LineEdit s- = NoEdit- | ReplaceWith s- | Delete- deriving (Show)--applyEdits :: (IsRegex re s,Monad m,Functor m)- => LineNo- -> Edits m re s- -> s- -> m s-applyEdits lno ez0 s0 = case ez0 of- Select ez -> select_edit_scripts lno ez s0- Pipe ez -> pipe_edit_scripts lno ez s0--applyEdit :: (IsRegex re s,Monad m,Functor m)- => (s->s)- -> LineNo- -> re- -> Edit m s- -> s- -> m (Maybe s)-applyEdit anl lno re edit s =- case allMatches acs of- [] -> return Nothing- _ -> fmap Just $ case edit of- EDIT_tpl tpl -> return $ anl $ replaceAll tpl acs- EDIT_phi phi -> return $ anl $ replaceAllCaptures phi acs- EDIT_fun ctx f -> anl <$> replaceAllCapturesM replace_ ctx (f lno) acs- EDIT_gen g -> fromMaybe s' . applyLineEdit anl <$> g lno acs- where- s' = anl s- acs = matchMany re s--applyLineEdit :: Monoid s => (s->s) -> LineEdit s -> Maybe s-applyLineEdit _ NoEdit = Nothing-applyLineEdit anl (ReplaceWith s) = Just $ anl s-applyLineEdit _ Delete = Just $ mempty--select_edit_scripts :: (IsRegex re s,Monad m,Functor m)- => LineNo- -> [(re,Edit m s)]- -> s- -> m s-select_edit_scripts lno ps0 s = select ps0- where- select [] = return $ appendNewline s- select ((re,es):ps) =- applyEdit appendNewline lno re es s >>= maybe (select ps) return--pipe_edit_scripts :: (IsRegex re s,Monad m,Functor m)- => LineNo- -> [(re,Edit m s)]- -> s- -> m s-pipe_edit_scripts lno ez s0 =- appendNewline <$> foldr f (return s0) ez- where- f (re,es) act = do- s <- act- fromMaybe s <$> applyEdit id lno re es s-\end{code}
− Text/RE/Internal/AddCaptureNames.hs
@@ -1,11 +0,0 @@-module Text.RE.Internal.AddCaptureNames where--import Text.RE---addCaptureNamesToMatches :: CaptureNames -> Matches a -> Matches a-addCaptureNamesToMatches cnms mtchs =- mtchs { allMatches = map (addCaptureNamesToMatch cnms) $ allMatches mtchs }--addCaptureNamesToMatch :: CaptureNames -> Match a -> Match a-addCaptureNamesToMatch cnms mtch = mtch { captureNames = cnms }
− Text/RE/Internal/NamedCaptures.lhs
@@ -1,205 +0,0 @@-\begin{code}-{-# LANGUAGE QuasiQuotes #-}-{-# LANGUAGE TemplateHaskell #-}-{-# LANGUAGE DeriveGeneric #-}-{-# LANGUAGE RecordWildCards #-}--module Text.RE.Internal.NamedCaptures- ( cp- , extractNamedCaptures- , idFormatTokenOptions- , Token(..)- , validToken- , formatTokens- , formatTokens'- , formatTokens0- , scan- ) where--import Data.Char-import qualified Data.HashMap.Strict as HM-import qualified Data.Text as T-import GHC.Generics-import qualified Language.Haskell.TH as TH-import Language.Haskell.TH.Quote-import Text.Heredoc-import Text.RE-import Text.RE.Internal.PreludeMacros-import Text.RE.Internal.QQ-import Text.Regex.PCRE---cp :: QuasiQuoter-cp =- (qq0 "re_")- { quoteExp = parse_capture- }--extractNamedCaptures :: String -> Either String (CaptureNames,String)-extractNamedCaptures s = Right (analyseTokens tks,formatTokens tks)- where- tks = scan s-\end{code}---Token--------\begin{code}-data Token- = ECap (Maybe String)- | PGrp- | PCap- | Bra- | BS Char- | Other Char- deriving (Show,Generic,Eq)--validToken :: Token -> Bool-validToken tkn = case tkn of- ECap mb -> maybe True check_ecap mb- PGrp -> True- PCap -> True- Bra -> True- BS c -> is_dot c- Other c -> is_dot c- where- check_ecap s = not (null s) && all not_br s- is_dot c = c/='\n'- not_br c = not $ c `elem` "{}\n"--\end{code}---Analysing [Token] -> CaptureNames------------------------------------\begin{code}-analyseTokens :: [Token] -> CaptureNames-analyseTokens = HM.fromList . count_em 1- where- count_em _ [] = []- count_em n (tk:tks) = bd ++ count_em (n `seq` n+d) tks- where- (d,bd) = case tk of- ECap (Just nm) -> (,) 1 [(CaptureName $ T.pack nm,CaptureOrdinal n)]- ECap Nothing -> (,) 1 []- PGrp -> (,) 0 []- PCap -> (,) 1 []- Bra -> (,) 1 []- BS _ -> (,) 0 []- Other _ -> (,) 0 []-\end{code}---Scanning Regex Strings-------------------------\begin{code}-scan :: String -> [Token]-scan = alex' match al oops- where- al :: [(Regex,Match String->Maybe Token)]- al =- [ mk [here|\$\{([^{}]+)\}\(|] $ ECap . Just . x_1- , mk [here|\$\(|] $ const $ ECap Nothing- , mk [here|\(\?:|] $ const PGrp- , mk [here|\(\?|] $ const PCap- , mk [here|\(|] $ const Bra- , mk [here|\\(.)|] $ BS . s2c . x_1- , mk [here|(.)|] $ Other . s2c . x_1- ]-- x_1 = captureText $ CID_ordinal $ CaptureOrdinal 1-- s2c [c] = c- s2c _ = error "scan:s2c:internal error"-- mk s f = (either error id $ makeRegexM s,Just . f)-- oops = error "reScanner"-\end{code}---Parsing captures-------------------\begin{code}-parse_capture :: String -> TH.Q TH.Exp-parse_capture s = case all isDigit s of- True -> [|CID_ordinal $ CaptureOrdinal $ read s|]- False -> [|CID_name $ CaptureName $ T.pack s|]-\end{code}---Formatting [Token]---------------------\begin{code}-formatTokens :: [Token] -> String-formatTokens = formatTokens' defFormatTokenOptions--data FormatTokenOptions =- FormatTokenOptions- { _fto_regex_type :: Maybe RegexType- , _fto_min_caps :: Bool- , _fto_incl_caps :: Bool- }- deriving (Show)--defFormatTokenOptions :: FormatTokenOptions-defFormatTokenOptions =- FormatTokenOptions- { _fto_regex_type = Nothing- , _fto_min_caps = False- , _fto_incl_caps = False- }--idFormatTokenOptions :: FormatTokenOptions-idFormatTokenOptions =- FormatTokenOptions- { _fto_regex_type = Nothing- , _fto_min_caps = False- , _fto_incl_caps = True- }--formatTokens' :: FormatTokenOptions -> [Token] -> String-formatTokens' FormatTokenOptions{..} = foldr f ""- where- f tk tl = t_s ++ tl- where- t_s = case tk of- ECap mb -> ecap mb- PGrp -> if _fto_regex_type == Just TDFA then "(" else "(?:"- PCap -> "(?"- Bra -> bra _fto_min_caps- BS c -> "\\" ++ [c]- Other c -> [c]-- ecap mb = case _fto_incl_caps of- True -> case mb of- Nothing -> "$("- Just nm -> "${"++nm++"}("- False -> bra _fto_min_caps-- bra mc = case mc && _fto_regex_type == Just PCRE of- True -> "(?:"- False -> "("-\end{code}--\begin{code}--- this is a reference of formatTokens defFormatTokenOptions,--- used for testing the latter-formatTokens0 :: [Token] -> String-formatTokens0 = foldr f ""- where- f tk tl = t_s ++ tl- where- t_s = case tk of- ECap _ -> "("- PGrp -> "(?:"- PCap -> "(?"- Bra -> "("- BS c -> "\\" ++ [c]- Other c -> [c]-\end{code}
− Text/RE/Internal/PreludeMacros.hs
@@ -1,907 +0,0 @@-{-# LANGUAGE NoImplicitPrelude #-}-{-# LANGUAGE RecordWildCards #-}-{-# LANGUAGE GeneralizedNewtypeDeriving #-}-{-# LANGUAGE OverloadedStrings #-}-{-# LANGUAGE CPP #-}-#if __GLASGOW_HASKELL__ >= 800-{-# OPTIONS_GHC -fno-warn-redundant-constraints #-}-#endif--module Text.RE.Internal.PreludeMacros- ( RegexType(..)- , WithCaptures(..)- , MacroDescriptor(..)- , RegexSource(..)- , PreludeMacro(..)- , presentPreludeMacro- , preludeMacros- , preludeMacroTable- , preludeMacroSummary- , preludeMacroSources- , preludeMacroSource- , preludeMacroEnv- , preludeMacroDescriptor- ) where--import Data.Array-import qualified Data.HashMap.Lazy as HML-import Data.List-import Data.Maybe-import qualified Data.Text as T-import Data.Time-import Prelude.Compat-import Text.RE.Options-import Text.RE.Parsers-import Text.RE.TestBench---preludeMacros :: (Monad m,Functor m)- => (String->m r)- -> RegexType- -> WithCaptures- -> m (Macros r)-preludeMacros prs rty wc = mkMacros prs rty wc $ preludeMacroEnv rty--preludeMacroTable :: RegexType -> String-preludeMacroTable rty = formatMacroTable rty $ preludeMacroEnv rty--preludeMacroSummary :: RegexType -> PreludeMacro -> String-preludeMacroSummary rty =- formatMacroSummary rty (preludeMacroEnv rty) . prelude_macro_id--preludeMacroSources :: RegexType -> String-preludeMacroSources rty =- formatMacroSources rty ExclCaptures $ preludeMacroEnv rty--preludeMacroSource :: RegexType -> PreludeMacro -> String-preludeMacroSource rty =- formatMacroSource rty ExclCaptures (preludeMacroEnv rty) . prelude_macro_id--preludeMacroEnv :: RegexType -> MacroEnv-preludeMacroEnv TDFA = prelude_macro_env_tdfa-preludeMacroEnv PCRE = prelude_macro_env_pcre--prelude_macro_env_pcre :: MacroEnv-prelude_macro_env_pcre = fix $ prelude_macro_env PCRE--prelude_macro_env_tdfa :: MacroEnv-prelude_macro_env_tdfa = fix $ prelude_macro_env TDFA--prelude_macro_env :: RegexType -> MacroEnv -> MacroEnv-prelude_macro_env rty env = HML.fromList $ catMaybes- [ (,) (prelude_macro_id pm) <$> preludeMacroDescriptor rty env pm- | pm<-[minBound..maxBound]- ]--preludeMacroDescriptor :: RegexType- -> MacroEnv- -> PreludeMacro- -> Maybe MacroDescriptor-preludeMacroDescriptor rty env pm = case pm of- PM_nat -> natural_macro rty env pm- PM_hex -> natural_hex_macro rty env pm- PM_int -> integer_macro rty env pm- PM_frac -> decimal_macro rty env pm- PM_string -> string_macro rty env pm- PM_string_simple -> string_simple_macro rty env pm- PM_id -> id_macro rty env pm- PM_id' -> id'_macro rty env pm- PM_id_ -> id__macro rty env pm- PM_date -> date_macro rty env pm- PM_date_slashes -> date_slashes_macro rty env pm- PM_time -> time_macro rty env pm- PM_timezone -> timezone_macro rty env pm- PM_datetime -> datetime_macro rty env pm- PM_datetime_8601 -> datetime_8601_macro rty env pm- PM_datetime_clf -> datetime_clf_macro rty env pm- PM_shortmonth -> shortmonth_macro rty env pm- PM_address_ipv4 -> address_ipv4_macros rty env pm- PM_email_simple -> email_simple_macro rty env pm- PM_url -> url_macro rty env pm- PM_syslog_severity -> syslog_severity_macro rty env pm---- | an enumeration of all of the prelude macros-data PreludeMacro- -- numbers- = PM_nat- | PM_hex- | PM_int- | PM_frac- -- strings- | PM_string- | PM_string_simple- -- identifiers- | PM_id- | PM_id'- | PM_id_- -- dates & times- | PM_date- | PM_date_slashes- | PM_time- | PM_timezone- | PM_datetime- | PM_datetime_8601- | PM_datetime_clf- | PM_shortmonth- -- addresses- | PM_address_ipv4- | PM_email_simple- | PM_url- -- syslog- | PM_syslog_severity- deriving (Bounded,Enum,Ord,Eq,Show)---- | naming the macros-presentPreludeMacro :: PreludeMacro -> String-presentPreludeMacro pm = case pm of- PM_id_ -> prelude_prefix++"id-"- _ -> fmt pm- where- fmt = (prelude_prefix++) . map tr . drop 3 . show-- tr '_' = '.'- tr c = c---- | all prelude macros are prefixed with this-prelude_prefix :: String-prelude_prefix = "%"--prelude_macro_id :: PreludeMacro -> MacroID-prelude_macro_id = MacroID . presentPreludeMacro--natural_macro :: RegexType -> MacroEnv -> PreludeMacro -> Maybe MacroDescriptor-natural_macro rty env pm = Just $ run_tests rty parseInteger samples env pm- MacroDescriptor- { _md_source = "[0-9]+"- , _md_samples = map fst samples- , _md_counter_samples = counter_samples- , _md_test_results = []- , _md_parser = Just "parseInteger"- , _md_description = "a string of one or more decimal digits"- }- where- samples :: [(String,Int)]- samples =- [ (,) "0" 0- , (,) "1234567890" 1234567890- , (,) "00" 0- , (,) "01" 1- ]-- counter_samples =- [ ""- , "0A"- , "-1"- ]--natural_hex_macro :: RegexType- -> MacroEnv- -> PreludeMacro- -> Maybe MacroDescriptor-natural_hex_macro rty env pm = Just $ run_tests rty parseHex samples env pm- MacroDescriptor- { _md_source = "[0-9a-fA-F]+"- , _md_samples = map fst samples- , _md_counter_samples = counter_samples- , _md_test_results = []- , _md_parser = Just "parseHex"- , _md_description = "a string of one or more hexadecimal digits"- }- where- samples :: [(String,Int)]- samples =- [ (,) "0" 0x0- , (,) "12345678" 0x12345678- , (,) "90abcdef" 0x90abcdef- , (,) "90ABCDEF" 0x90abcdef- , (,) "00" 0x0- , (,) "010" 0x10- ]-- counter_samples =- [ ""- , "0x10"- , "0z"- , "-1a"- ]--integer_macro :: RegexType -> MacroEnv -> PreludeMacro -> Maybe MacroDescriptor-integer_macro rty env pm = Just $ run_tests rty parseInteger samples env pm- MacroDescriptor- { _md_source = "-?[0-9]+"- , _md_samples = map fst samples- , _md_counter_samples = counter_samples- , _md_test_results = []- , _md_parser = Just "parseInteger"- , _md_description = "a decimal integer"- }- where- samples :: [(String,Int)]- samples =- [ (,) "0" 0- , (,) "1234567890" 1234567890- , (,) "00" 0- , (,) "01" 1- , (,) "-1" $ -1- , (,) "-0" 0- ]-- counter_samples =- [ ""- , "0A"- , "+0"- ]---- | a digit string macro-decimal_macro :: RegexType -> MacroEnv -> PreludeMacro -> Maybe MacroDescriptor-decimal_macro rty env pm = Just $ run_tests rty parseDouble samples env pm- MacroDescriptor- { _md_source = "-?[0-9]+(?:\\.[0-9]+)?"- , _md_samples = map fst samples- , _md_counter_samples = counter_samples- , _md_test_results = []- , _md_parser = Just "parseInteger"- , _md_description = "a decimal integer"- }- where- samples :: [(String,Double)]- samples =- [ (,) "0" 0- , (,) "1234567890" 1234567890- , (,) "00" 0- , (,) "01" 1- , (,) "-1" $ -1- , (,) "-0" 0- , (,) "0.1234567890" 0.1234567890- , (,) "-1.0" $ -1.0- ]-- counter_samples =- [ ""- , "0A"- , "+0"- , "0."- , ".0"- , "."- , "-"- , "-."- , "-1."- , "-.1"- ]--string_macro :: RegexType- -> MacroEnv- -> PreludeMacro- -> Maybe MacroDescriptor-string_macro PCRE _ _ = Nothing-string_macro rty@TDFA env pm =- Just $ run_tests rty (fmap T.unpack . parseString) samples env pm- MacroDescriptor- { _md_source = "\"(?:[^\"\\]+|\\\\[\\\"])*\""- , _md_samples = map fst samples- , _md_counter_samples = counter_samples- , _md_test_results = []- , _md_parser = Just "parseString"- , _md_description = "a double-quote string, with simple \\ escapes for \\s and \"s"- }- where- samples :: [(String,String)]- samples =- [ (,) "\"\"" ""- , (,) "\"foo\"" "foo"- , (,) "\"\\\"\"" "\""- , (,) "\"\\\"\\\"\"" "\"\""- , (,) "\"\\\"\\\\\\\"\"" "\"\\\""- , (,) "\"\\\"foo\\\"\"" "\"foo\""- , (,) "\"\"" ""- ]-- counter_samples =- [ "\""- , "\"aa"- ]--string_simple_macro :: RegexType- -> MacroEnv- -> PreludeMacro- -> Maybe MacroDescriptor-string_simple_macro rty env pm =- Just $ run_tests rty (fmap T.unpack . parseSimpleString) samples env pm- MacroDescriptor- { _md_source = "\"[^\"[:cntrl:]]*\""- , _md_samples = map fst samples- , _md_counter_samples = counter_samples- , _md_test_results = []- , _md_parser = Just "parseSimpleString"- , _md_description = "a decimal integer"- }- where- samples :: [(String,String)]- samples =- [ (,) "\"\"" ""- , (,) "\"foo\"" "foo"- , (,) "\"\\\"" "\\"- , (,) "\"\"" ""- ]-- counter_samples =- [ ""- , "\""- , "\"\\\"\""- , "\"\\\"\\\"\""- , "\"\\\"\\\\\\\"\""- , "\"\\\"foo\\\"\""- , "\"aa"- ]--id_macro :: RegexType- -> MacroEnv- -> PreludeMacro- -> Maybe MacroDescriptor-id_macro rty env pm =- Just $ run_tests rty Just samples env pm- MacroDescriptor- { _md_source = "_*[a-zA-Z][a-zA-Z0-9_]*"- , _md_samples = map fst samples- , _md_counter_samples = counter_samples- , _md_test_results = []- , _md_parser = Nothing- , _md_description = "a standard C-style alphanumeric identifier (with _s)"- }- where- samples :: [(String,String)]- samples =- [ f "a"- , f "A"- , f "A1"- , f "a_"- , f "a1_B2"- , f "_abc"- , f "__abc"- ]- where- f s = (s,s)-- counter_samples =- [ ""- , "1"- , "_"- , "__"- , "__1"- , "1a"- , "a'"- ]--id'_macro :: RegexType- -> MacroEnv- -> PreludeMacro- -> Maybe MacroDescriptor-id'_macro rty env pm =- Just $ run_tests rty Just samples env pm- MacroDescriptor- { _md_source = "_*[a-zA-Z][a-zA-Z0-9_']*"- , _md_samples = map fst samples- , _md_counter_samples = counter_samples- , _md_test_results = []- , _md_parser = Nothing- , _md_description = "a standard Haskell-style alphanumeric identifier (with '_'s and '''s)"- }- where- samples :: [(String,String)]- samples =- [ f "a"- , f "A"- , f "A1"- , f "a_"- , f "a1_B2"- , f "_abc"- , f "__abc"- , f "a'"- , f "_a'"- , f "a'b"- ]- where- f s = (s,s)-- counter_samples =- [ ""- , "1"- , "_"- , "__"- , "__1"- , "1a"- , "'"- , "'a"- , "_'"- , "_1'"- ]--id__macro :: RegexType- -> MacroEnv- -> PreludeMacro- -> Maybe MacroDescriptor-id__macro rty env pm =- Just $ run_tests rty Just samples env pm- MacroDescriptor- { _md_source = "_*[a-zA-Z][a-zA-Z0-9_'-]*"- , _md_samples = map fst samples- , _md_counter_samples = counter_samples- , _md_test_results = []- , _md_parser = Nothing- , _md_description = "an identifier with -s"- }- where- samples :: [(String,String)]- samples =- [ f "a"- , f "A"- , f "A1"- , f "a_"- , f "a1_B2"- , f "_abc"- , f "__abc"- , f "a'"- , f "_a'"- , f "a'b"- , f "a-"- , f "a1-B2"- , f "a1-B2-"- ]- where- f s = (s,s)-- counter_samples =- [ ""- , "1"- , "_"- , "__"- , "__1"- , "1a"- , "'"- , "'a"- , "_'"- , "_1'"- ]--date_macro :: RegexType -> MacroEnv -> PreludeMacro -> Maybe MacroDescriptor-date_macro rty env pm =- Just $ run_tests rty parseDate samples env pm- MacroDescriptor- { _md_source = "[0-9]{4}-[0-9]{2}-[0-9]{2}"- , _md_samples = map fst samples- , _md_counter_samples = counter_samples- , _md_test_results = []- , _md_parser = Just "parseDate"- , _md_description = "a YYYY-MM-DD format date"- }- where- samples :: [(String,Day)]- samples =- [ f "2016-12-31"- , f "0001-01-01"- , f "1000-01-01"- ]- where- f s = (s,read s)-- counter_samples =- [ ""- , "2016/01/31"- , "2016-1-31"- , "2016-01-1"- , "2016-001-01"- ]--date_slashes_macro :: RegexType- -> MacroEnv- -> PreludeMacro- -> Maybe MacroDescriptor-date_slashes_macro rty env pm =- Just $ run_tests rty parseSlashesDate samples env pm- MacroDescriptor- { _md_source = "[0-9]{4}/[0-9]{2}/[0-9]{2}"- , _md_samples = map fst samples- , _md_counter_samples = counter_samples- , _md_test_results = []- , _md_parser = Just "parseSlashesDate"- , _md_description = "a YYYY/MM/DD format date"- }- where- samples :: [(String,Day)]- samples =- [ f "2016/12/31"- , f "0001/01/01"- , f "1000/01/01"- ]- where- f s = (s,read $ map tr s)- where- tr '/' = '-'- tr c = c-- counter_samples =- [ ""- , "2016-01-31"- , "2016/1/31"- , "2016/01/1"- , "2016/001/01"- ]--time_macro :: RegexType -> MacroEnv -> PreludeMacro -> Maybe MacroDescriptor-time_macro rty env pm =- Just $ run_tests rty parseTimeOfDay samples env pm- MacroDescriptor- { _md_source = "[0-9]{2}:[0-9]{2}:[0-9]{2}(?:[.][0-9]+)?"- , _md_samples = map fst samples- , _md_counter_samples = counter_samples- , _md_test_results = []- , _md_parser = Just "parseTimeOfDay"- , _md_description = "a HH:MM:SS[.Q+]"- }- where- samples :: [(String,TimeOfDay)]- samples =- [ f "00:00:00" 00 00 0- , f "23:59:59" 23 59 59- , f "00:00:00.1234567890" 00 00 $ 123456789 / 1000000000- ]- where- f s h m ps = (s,TimeOfDay h m ps)-- counter_samples =- [ ""- , "235959"- , "10:20"- , "A00:00:00"- , "00:00:00A"- , "23:59:59."- ]--timezone_macro :: RegexType- -> MacroEnv- -> PreludeMacro- -> Maybe MacroDescriptor-timezone_macro rty env pm =- Just $ run_tests rty parseTimeZone samples env pm- MacroDescriptor- { _md_source = "(?:Z|[+-][0-9]{2}:?[0-9]{2})"- , _md_samples = map fst samples- , _md_counter_samples = counter_samples- , _md_test_results = []- , _md_parser = Just "parseTimeZone"- , _md_description = "an IOS-8601 TZ specification"- }- where- samples :: [(String,TimeZone)]- samples =- [ f "Z" $ minutesToTimeZone 0- , f "+00:00" $ minutesToTimeZone 0- , f "+0000" $ minutesToTimeZone 0- , f "+0200" $ minutesToTimeZone 120- , f "-0100" $ minutesToTimeZone $ -60- ]- where- f = (,)-- counter_samples =- [ ""- , "00"- , "A00:00"- , "UTC"- , "EST"- , " EST"- ]--datetime_macro :: RegexType -> MacroEnv -> PreludeMacro -> Maybe MacroDescriptor-datetime_macro rty env pm = Just $ run_tests rty parseDateTime samples env pm- MacroDescriptor- { _md_source = "@{%date}[ T]@{%time}(?:@{%timezone}| UTC)?"- , _md_samples = map fst samples- , _md_counter_samples = counter_samples- , _md_test_results = []- , _md_parser = Just "parseDateTime"- , _md_description = "ISO-8601 format date and time + simple variants"- }- where- samples :: [(String,UTCTime)]- samples =- [ f "2016-12-31 23:37:22.525343 UTC" "2016-12-31 23:37:22.525343Z"- , f "2016-12-31 23:37:22.525343" "2016-12-31 23:37:22.525343Z"- , f "2016-12-31 23:37:22" "2016-12-31 23:37:22Z"- , f "2016-12-31T23:37:22+0100" "2016-12-31 23:37:22+0100"- , f "2016-12-31T23:37:22-01:00" "2016-12-31 23:37:22-0100"- , f "2016-12-31T23:37:22-23:59" "2016-12-31 23:37:22-2359"- , f "2016-12-31T23:37:22Z" "2016-12-31 23:37:22Z"- ]- where- f :: String -> String -> (String,UTCTime)- f s r_s = (s,read r_s)-- counter_samples =- [ ""- , "2016-12-31 23:37:22.525343 EST"- ]--datetime_8601_macro :: RegexType- -> MacroEnv- -> PreludeMacro- -> Maybe MacroDescriptor-datetime_8601_macro rty env pm =- Just $ run_tests rty parseDateTime samples env pm- MacroDescriptor- { _md_source = "@{%date}T@{%time}@{%timezone}"- , _md_samples = map fst samples- , _md_counter_samples = counter_samples- , _md_test_results = []- , _md_parser = Just "parseDateTime8601"- , _md_description = "YYYY-MM-DDTHH:MM:SS[.Q*](Z|[+-]HHMM) format date and time"- }- where- samples :: [(String,UTCTime)]- samples =- [ f "2016-12-31T23:37:22.343Z" "2016-12-31 23:37:22.343Z"- , f "2016-12-31T23:37:22-0100" "2016-12-31 23:37:22-0100"- , f "2016-12-31T23:37:22+23:59" "2016-12-31 23:37:22+2359"- ]- where- f :: String -> String -> (String,UTCTime)- f s r_s = (s,read r_s)-- counter_samples =- [ ""- , "2016-12-31 23:37:22.525343 EST"- ]--datetime_clf_macro :: RegexType -> MacroEnv -> PreludeMacro -> Maybe MacroDescriptor-datetime_clf_macro rty env pm =- Just $ run_tests rty parseDateTimeCLF samples env pm- MacroDescriptor- { _md_source = re- , _md_samples = map fst samples- , _md_counter_samples = counter_samples- , _md_test_results = []- , _md_parser = Just "parseDateTimeCLF"- , _md_description = "Common Log Format date+time: %d/%b/%Y:%H:%M:%S %z"- }- where- samples :: [(String,UTCTime)]- samples =- [ f "10/Oct/2000:13:55:36 -0700" "2000-10-10 13:55:36-0700"- , f "10/Oct/2000:13:55:36 +07:00" "2000-10-10 13:55:36+0700"- ]- where- f :: String -> String -> (String,UTCTime)- f s r_s = (s,read r_s)-- counter_samples =- [ ""- , "2016-12-31T23:37+0100"- , "10/Oct/2000:13:55:36-0700"- , "10/OCT/2000:13:55:36 -0700"- , "10/Oct/2000:13:55 -0700"- , "10/Oct/2000:13:55Z"- ]-- re = RegexSource $ unwords- [ "[0-9]{2}/@{%shortmonth}/[0-9]{4}:[0-9]{2}:[0-9]{2}:[0-9]{2}"- , "[+-][0-9]{2}:?[0-9]{2}"- ]--shortmonth_macro :: RegexType- -> MacroEnv- -> PreludeMacro- -> Maybe MacroDescriptor-shortmonth_macro rty env pm =- Just $ run_tests rty parseShortMonth samples env pm- MacroDescriptor- { _md_source = bracketedRegexSource $- intercalate "|" $ map T.unpack $ elems shortMonthArray- , _md_samples = map fst samples- , _md_counter_samples = counter_samples- , _md_test_results = []- , _md_parser = Just "parseShortMonth"- , _md_description = "three letter month name: Jan-Dec"- }- where- samples :: [(String,Int)]- samples =- [ f "Jan" 1- , f "Feb" 2- , f "Dec" 12- ]- where- f = (,)-- counter_samples =- [ ""- , "jan"- , "DEC"- , "January"- , "01"- , "1"- ]--address_ipv4_macros :: RegexType- -> MacroEnv- -> PreludeMacro- -> Maybe MacroDescriptor-address_ipv4_macros rty env pm =- Just $ run_tests rty parseIPv4Address samples env pm- MacroDescriptor- { _md_source = "[0-9]{1,3}[.][0-9]{1,3}[.][0-9]{1,3}[.][0-9]{1,3}"- , _md_samples = map fst samples- , _md_counter_samples = counter_samples- , _md_test_results = []- , _md_parser = Just "parseSeverity"- , _md_description = "an a.b.c.d IPv4 address"- }- where- samples :: [(String,IPV4Address)]- samples =- [ f "0.0.0.0" ( 0, 0, 0, 0)- , f "123.45.6.78" (123, 45, 6, 78)- , f "9.9.9.9" ( 9, 9, 9, 9)- , f "255.255.255.255" (255,255,255,255)- ]- where- f = (,)-- counter_samples =- [ ""- , "foo"- , "1234.0.0.0"- , "1.2.3"- , "1.2.3."- , "1.2..4"- , "www.example.com"- , "2001:0db8:85a3:0000:0000:8a2e:0370:7334"- ]--syslog_severity_macro :: RegexType- -> MacroEnv- -> PreludeMacro- -> Maybe MacroDescriptor-syslog_severity_macro rty env pm =- Just $ run_tests rty parseSeverity samples env pm- MacroDescriptor- { _md_source = re- , _md_samples = map fst samples- , _md_counter_samples = counter_samples- , _md_test_results = []- , _md_parser = Just "parseSeverity"- , _md_description = "syslog severity keyword (debug-emerg)"- }- where- samples :: [(String,Severity)]- samples =- [ f "emerg" Emerg- , f "panic" Emerg- , f "alert" Alert- , f "crit" Crit- , f "err" Err- , f "error" Err- , f "warn" Warning- , f "warning" Warning- , f "notice" Notice- , f "info" Info- , f "debug" Debug- ]- where- f = (,)-- counter_samples =- [ ""- , "Emergency"- , "ALERT"- ]-- re = case rty of- PCRE -> re_pcre- TDFA -> re_tdfa-- re_tdfa = bracketedRegexSource $- intercalate "|" $- [ T.unpack kw- | (kw0,kws) <- map severityKeywords [minBound..maxBound]- , kw <- kw0:kws- ]-- re_pcre = bracketedRegexSource $- intercalate "|" $- [ T.unpack kw- | (kw0,kws) <- map severityKeywords $- filter (/=Err) [minBound..maxBound]- , kw <- kw0:kws- ] ++ ["err(?:or)?"]--email_simple_macro :: RegexType- -> MacroEnv- -> PreludeMacro- -> Maybe MacroDescriptor-email_simple_macro rty env pm =- Just $ run_tests rty Just samples env pm- MacroDescriptor- { _md_source = "[a-zA-Z0-9%_.+-]+@[a-zA-Z0-9-]+\\.[a-zA-Z0-9.-]+"- , _md_samples = map fst samples- , _md_counter_samples = counter_samples- , _md_test_results = []- , _md_parser = Nothing- , _md_description = "an email address"- }- where- samples :: [(String,String)]- samples =- [ f "user-name%foo.bar.com@an-example.com"- ]- where- f s = (s,s)-- counter_samples =- [ ""- , "not-an-email-address"- , "@not-an-email-address"- ]--- | see https://mathiasbynens.be/demo/url-regex--- (based on @stephenhay URL)-url_macro :: RegexType- -> MacroEnv- -> PreludeMacro- -> Maybe MacroDescriptor-url_macro rty env pm =- Just $ run_tests rty Just samples env pm- MacroDescriptor- { _md_source = "([hH][tT][tT][pP][sS]?|[fF][tT][pP])://[^[:space:]/$.?#].[^[:space:]]*"- , _md_samples = map fst samples- , _md_counter_samples = counter_samples- , _md_test_results = []- , _md_parser = Nothing- , _md_description = "a URL"- }- where- samples :: [(String,String)]- samples =- [ f "https://mathiasbynens.be/demo/url-regex"- , f "http://foo.com/blah_blah"- , f "http://foo.com/blah_blah/"- , f "http://foo.com/blah_blah_(wikipedia)"- , f "http://foo.com/blah_blah_(wikipedia)_(again)"- , f "http://www.example.com/wpstyle/?p=364"- , f "HTTPS://foo.bar/?q=Test%20URL-encoded%20stuff"- , f "HTTP://223.255.255.254"- , f "ftp://223.255.255.254"- , f "FTP://223.255.255.254"- ]- where- f s = (s,s)-- counter_samples =- [ ""- , "http://"- , "http://."- , "http://.."- , "http://../"- , "http://?"- , "http://??"- , "http://foo.bar?q=Spaces should be encoded"- , "//"- , "http://##/"- , "http://##"- , "http://##/"- ]--run_tests :: (Eq a,Show a)- => RegexType- -> (String->Maybe a)- -> [(String,a)]- -> MacroEnv- -> PreludeMacro- -> MacroDescriptor- -> MacroDescriptor-run_tests rty parser vector env =- runTests rty parser vector env . prelude_macro_id--bracketedRegexSource :: String -> RegexSource-bracketedRegexSource re_s = RegexSource $ "(?:" ++ re_s ++ ")"--fix :: (a->a) -> a-fix f = f (fix f)
− Text/RE/Internal/QQ.hs
@@ -1,26 +0,0 @@-{-# LANGUAGE DeriveDataTypeable #-}--module Text.RE.Internal.QQ where--import Control.Exception-import Data.Typeable-import Language.Haskell.TH.Quote---data QQFailure =- QQFailure- { _qqf_context :: String- , _qqf_component :: String- }- deriving (Show,Typeable)--instance Exception QQFailure where--qq0 :: String -> QuasiQuoter-qq0 ctx =- QuasiQuoter- { quoteExp = const $ throw $ QQFailure ctx "expression"- , quotePat = const $ throw $ QQFailure ctx "pattern"- , quoteType = const $ throw $ QQFailure ctx "type"- , quoteDec = const $ throw $ QQFailure ctx "declaration"- }
− Text/RE/IsRegex.lhs
@@ -1,17 +0,0 @@-\begin{code}-{-# LANGUAGE MultiParamTypeClasses #-}-{-# LANGUAGE AllowAmbiguousTypes #-}--module Text.RE.IsRegex where--import Text.RE.Capture-import Text.RE.Replace-\end{code}---\begin{code}-class Replace s => IsRegex re s where- matchOnce :: re -> s -> Match s- matchMany :: re -> s -> Matches s- regexSource :: re -> String-\end{code}
− Text/RE/LineNo.hs
@@ -1,18 +0,0 @@-{-# LANGUAGE GeneralizedNewtypeDeriving #-}--module Text.RE.LineNo where---newtype LineNo =- ZeroBasedLineNo { getZeroBasedLineNo :: Int }- deriving (Show,Enum)---firstLine :: LineNo-firstLine = ZeroBasedLineNo 0--getLineNo :: LineNo -> Int-getLineNo = (+1) . getZeroBasedLineNo--lineNo :: Int -> LineNo-lineNo = ZeroBasedLineNo . (\x->x-1)
− Text/RE/Options.lhs
@@ -1,77 +0,0 @@-\begin{code}-{-# LANGUAGE GeneralizedNewtypeDeriving #-}-{-# LANGUAGE TemplateHaskell #-}-{-# LANGUAGE QuasiQuotes #-}-{-# LANGUAGE MultiParamTypeClasses #-}-{-# LANGUAGE FunctionalDependencies #-}--module Text.RE.Options where--import Data.Hashable-import qualified Data.HashMap.Strict as HM-import Data.String-import Language.Haskell.TH-import Language.Haskell.TH.Syntax-\end{code}--\begin{code}-data Options_ r c e =- Options- { _options_mode :: !Mode- , _options_macs :: !(Macros r)- , _options_comp :: !c- , _options_exec :: !e- }- deriving (Show)-\end{code}--\begin{code}-class IsOption o r c e |- e -> r, c -> e , e -> c, r -> c, c -> r, r -> e where- makeOptions :: o -> Options_ r c e-\end{code}--\begin{code}-data Mode- = Simple- | Block- deriving (Bounded,Enum,Ord,Eq,Show)-\end{code}--\begin{code}-newtype MacroID =- MacroID { _MacroID :: String }- deriving (IsString,Ord,Eq,Show)-\end{code}--\begin{code}-instance Hashable MacroID where- hashWithSalt i = hashWithSalt i . _MacroID-\end{code}--\begin{code}-type Macros r = HM.HashMap MacroID r-\end{code}--\begin{code}-emptyMacros :: Macros r-emptyMacros = HM.empty-\end{code}--\begin{code}-data SimpleRegexOptions- = MultilineSensitive- | MultilineInsensitive- | BlockSensitive- | BlockInsensitive- deriving (Bounded,Enum,Eq,Ord,Show)-\end{code}--\begin{code}-instance Lift SimpleRegexOptions where- lift sro = case sro of- MultilineSensitive -> conE 'MultilineSensitive- MultilineInsensitive -> conE 'MultilineInsensitive- BlockSensitive -> conE 'BlockSensitive- BlockInsensitive -> conE 'BlockInsensitive-\end{code}
− Text/RE/PCRE.hs
@@ -1,61 +0,0 @@-{-# LANGUAGE FlexibleContexts #-}-{-# LANGUAGE CPP #-}-#if __GLASGOW_HASKELL__ >= 800-{-# OPTIONS_GHC -fno-warn-redundant-constraints #-}-#endif-{-# OPTIONS_GHC -fno-warn-dodgy-exports #-}--module Text.RE.PCRE- ( (*=~)- , (?=~)- , (=~)- , (=~~)- , module Text.RE- , module Text.RE.PCRE.RE- , module Text.RE.PCRE.ByteString- , module Text.RE.PCRE.ByteString.Lazy- , module Text.RE.PCRE.Sequence- , module Text.RE.PCRE.String-- ) where---import qualified Text.Regex.Base as B-import Text.RE-import Text.RE.Internal.AddCaptureNames-import Text.RE.PCRE.RE-import qualified Text.Regex.PCRE as PCRE-import Text.RE.PCRE.ByteString()-import Text.RE.PCRE.ByteString.Lazy()-import Text.RE.PCRE.Sequence()-import Text.RE.PCRE.String()---(*=~) :: IsRegex RE s- => s- -> RE- -> Matches s-(*=~) bs rex = addCaptureNamesToMatches (reCaptureNames rex) $ matchMany rex bs--(?=~) :: IsRegex RE s- => s- -> RE- -> Match s-(?=~) bs rex = addCaptureNamesToMatch (reCaptureNames rex) $ matchOnce rex bs--(=~) :: ( B.RegexContext PCRE.Regex s a- , B.RegexMaker PCRE.Regex PCRE.CompOption PCRE.ExecOption s- )- => s- -> RE- -> a-(=~) bs rex = B.match (reRegex rex) bs--(=~~) :: ( Monad m- , B.RegexContext PCRE.Regex s a- , B.RegexMaker PCRE.Regex PCRE.CompOption PCRE.ExecOption s- )- => s- -> RE- -> m a-(=~~) bs rex = B.matchM (reRegex rex) bs
− Text/RE/PCRE/ByteString.hs
@@ -1,61 +0,0 @@-{-# LANGUAGE MultiParamTypeClasses #-}-{-# LANGUAGE FlexibleContexts #-}-{-# LANGUAGE FlexibleInstances #-}-{-# OPTIONS_GHC -fno-warn-orphans #-}-{-# LANGUAGE CPP #-}-#if __GLASGOW_HASKELL__ >= 800-{-# OPTIONS_GHC -fno-warn-redundant-constraints #-}-#endif--module Text.RE.PCRE.ByteString- ( (*=~)- , (?=~)- , (=~)- , (=~~)- , module Text.RE- , module Text.RE.PCRE.RE- ) where--import qualified Data.ByteString as B-import Text.Regex.Base-import Text.RE-import Text.RE.Internal.AddCaptureNames-import Text.RE.PCRE.RE-import qualified Text.Regex.PCRE as PCRE----- | find all matches in text-(*=~) :: B.ByteString- -> RE- -> Matches B.ByteString-(*=~) bs rex = addCaptureNamesToMatches (reCaptureNames rex) $ match (reRegex rex) bs---- | find first matches in text-(?=~) :: B.ByteString- -> RE- -> Match B.ByteString-(?=~) bs rex = addCaptureNamesToMatch (reCaptureNames rex) $ match (reRegex rex) bs---- | regex-base polymorphic match operator-(=~) :: ( RegexContext PCRE.Regex B.ByteString a- , RegexMaker PCRE.Regex PCRE.CompOption PCRE.ExecOption String- )- => B.ByteString- -> RE- -> a-(=~) bs rex = match (reRegex rex) bs---- | regex-base monadic, polymorphic match operator-(=~~) :: ( Monad m- , RegexContext PCRE.Regex B.ByteString a- , RegexMaker PCRE.Regex PCRE.CompOption PCRE.ExecOption String- )- => B.ByteString- -> RE- -> m a-(=~~) bs rex = matchM (reRegex rex) bs--instance IsRegex RE B.ByteString where- matchOnce = flip (?=~)- matchMany = flip (*=~)- regexSource = reSource
− Text/RE/PCRE/ByteString/Lazy.hs
@@ -1,61 +0,0 @@-{-# LANGUAGE MultiParamTypeClasses #-}-{-# LANGUAGE FlexibleContexts #-}-{-# LANGUAGE FlexibleInstances #-}-{-# OPTIONS_GHC -fno-warn-orphans #-}-{-# LANGUAGE CPP #-}-#if __GLASGOW_HASKELL__ >= 800-{-# OPTIONS_GHC -fno-warn-redundant-constraints #-}-#endif--module Text.RE.PCRE.ByteString.Lazy- ( (*=~)- , (?=~)- , (=~)- , (=~~)- , module Text.RE- , module Text.RE.PCRE.RE- ) where--import qualified Data.ByteString.Lazy as LBS-import Text.Regex.Base-import Text.RE-import Text.RE.Internal.AddCaptureNames-import Text.RE.PCRE.RE-import qualified Text.Regex.PCRE as PCRE----- | find all matches in text-(*=~) :: LBS.ByteString- -> RE- -> Matches LBS.ByteString-(*=~) bs rex = addCaptureNamesToMatches (reCaptureNames rex) $ match (reRegex rex) bs---- | find first matches in text-(?=~) :: LBS.ByteString- -> RE- -> Match LBS.ByteString-(?=~) bs rex = addCaptureNamesToMatch (reCaptureNames rex) $ match (reRegex rex) bs---- | regex-base polymorphic match operator-(=~) :: ( RegexContext PCRE.Regex LBS.ByteString a- , RegexMaker PCRE.Regex PCRE.CompOption PCRE.ExecOption String- )- => LBS.ByteString- -> RE- -> a-(=~) bs rex = match (reRegex rex) bs---- | regex-base monadic, polymorphic match operator-(=~~) :: ( Monad m- , RegexContext PCRE.Regex LBS.ByteString a- , RegexMaker PCRE.Regex PCRE.CompOption PCRE.ExecOption String- )- => LBS.ByteString- -> RE- -> m a-(=~~) bs rex = matchM (reRegex rex) bs--instance IsRegex RE LBS.ByteString where- matchOnce = flip (?=~)- matchMany = flip (*=~)- regexSource = reSource
− Text/RE/PCRE/RE.hs
@@ -1,249 +0,0 @@-{-# LANGUAGE NoImplicitPrelude #-}-{-# LANGUAGE RecordWildCards #-}-{-# LANGUAGE QuasiQuotes #-}-{-# LANGUAGE TemplateHaskell #-}-{-# LANGUAGE MultiParamTypeClasses #-}-{-# LANGUAGE TypeSynonymInstances #-}-{-# LANGUAGE FlexibleInstances #-}-{-# LANGUAGE FlexibleContexts #-}-{-# LANGUAGE CPP #-}-#if __GLASGOW_HASKELL__ >= 800-{-# OPTIONS_GHC -fno-warn-redundant-constraints #-}-#endif-{-# OPTIONS_GHC -fno-warn-orphans #-}--module Text.RE.PCRE.RE- ( re- , reMS- , reMI- , reBS- , reBI- , reMultilineSensitive- , reMultilineInsensitive- , reBlockSensitive- , reBlockInsensitive- , re_- , cp- , regexType- , RE- , reOptions- , reSource- , reCaptureNames- , reRegex- , Options- , prelude- , preludeEnv- , preludeTestsFailing- , preludeTable- , preludeSummary- , preludeSources- , preludeSource- , noPreludeOptions- , defaultOptions- , unpackSimpleRegexOptions- , compileRegex- ) where--import Data.Bits-import Data.Functor.Identity-import Language.Haskell.TH-import Language.Haskell.TH.Quote-import Prelude.Compat-import Text.RE-import Text.RE.Internal.NamedCaptures-import Text.RE.Internal.PreludeMacros-import Text.RE.Internal.QQ-import Text.RE.TestBench-import Text.Regex.PCRE---re- , reMS- , reMI- , reBS- , reBI- , reMultilineSensitive- , reMultilineInsensitive- , reBlockSensitive- , reBlockInsensitive- , re_ :: QuasiQuoter--re = re' $ Just minBound-reMS = reMultilineSensitive-reMI = reMultilineInsensitive-reBS = reBlockSensitive-reBI = reBlockInsensitive-reMultilineSensitive = re' $ Just MultilineSensitive-reMultilineInsensitive = re' $ Just MultilineInsensitive-reBlockSensitive = re' $ Just BlockSensitive-reBlockInsensitive = re' $ Just BlockInsensitive-re_ = re' Nothing--regexType :: RegexType-regexType = PCRE--data RE =- RE- { _re_options :: !Options- , _re_source :: !String- , _re_cnames :: !CaptureNames- , _re_regex :: !Regex- }--reOptions :: RE -> Options-reOptions = _re_options--reSource :: RE -> String-reSource = _re_source--reCaptureNames :: RE -> CaptureNames-reCaptureNames = _re_cnames--reRegex :: RE -> Regex-reRegex = _re_regex--type Options = Options_ RE CompOption ExecOption--instance IsOption SimpleRegexOptions RE CompOption ExecOption where- makeOptions = unpackSimpleRegexOptions--instance IsOption Mode RE CompOption ExecOption where- makeOptions md = Options md prelude def_comp_option def_exec_option--instance IsOption (Macros RE) RE CompOption ExecOption where- makeOptions ms = Options minBound ms def_comp_option def_exec_option--instance IsOption CompOption RE CompOption ExecOption where- makeOptions co = Options minBound prelude co def_exec_option--instance IsOption ExecOption RE CompOption ExecOption where- makeOptions eo = Options minBound prelude def_comp_option eo--instance IsOption Options RE CompOption ExecOption where- makeOptions = id--instance IsOption () RE CompOption ExecOption where- makeOptions _ = unpackSimpleRegexOptions minBound--def_comp_option :: CompOption-def_comp_option = _options_comp defaultOptions--def_exec_option :: ExecOption-def_exec_option = _options_exec defaultOptions--noPreludeOptions :: Options-noPreludeOptions = defaultOptions { _options_macs = emptyMacros }--defaultOptions :: Options-defaultOptions = makeOptions (minBound::SimpleRegexOptions)--unpackSimpleRegexOptions :: SimpleRegexOptions -> Options-unpackSimpleRegexOptions sro =- Options- { _options_mode = minBound- , _options_macs = prelude- , _options_comp = comp- , _options_exec = defaultExecOpt- }- where- comp =- wiggle ml compMultiline $- wiggle ci compCaseless- defaultCompOpt-- wiggle True m v = v .|. m- wiggle False m v = v .&. complement m-- (ml,ci) = case sro of- MultilineSensitive -> (,) True False- MultilineInsensitive -> (,) True True- BlockSensitive -> (,) False False- BlockInsensitive -> (,) False True--compileRegex :: ( IsOption o RE CompOption ExecOption- , Functor m- , Monad m- )- => o- -> String- -> m RE-compileRegex = compileRegex_ . makeOptions--compileRegex_ :: ( Functor m , Monad m )- => Options- -> String- -> m RE-compileRegex_ os re_s = uncurry mk <$> compileRegex' os re_s- where- mk cnms rex =- RE- { _re_options = os- , _re_source = re_s- , _re_cnames = cnms- , _re_regex = rex- }--re' :: Maybe SimpleRegexOptions -> QuasiQuoter-re' mb = case mb of- Nothing ->- (qq0 "re_")- { quoteExp = parse minBound (\rs->[|flip unsafeCompileRegex rs|])- }- Just sro ->- (qq0 "re")- { quoteExp = parse sro (\rs->[|unsafeCompileRegexSimple sro rs|])- }- where- parse :: SimpleRegexOptions -> (String->Q Exp) -> String -> Q Exp- parse sro mk rs = either error (\_->mk rs) $ compileRegex_ os rs- where- os = unpackSimpleRegexOptions sro--unsafeCompileRegexSimple :: SimpleRegexOptions -> String -> RE-unsafeCompileRegexSimple sro re_s = unsafeCompileRegex os re_s- where- os = unpackSimpleRegexOptions sro--unsafeCompileRegex :: IsOption o RE CompOption ExecOption- => o- -> String- -> RE-unsafeCompileRegex = unsafeCompileRegex_ . makeOptions--unsafeCompileRegex_ :: Options -> String -> RE-unsafeCompileRegex_ os = either oops id . compileRegex os- where- oops = error . ("unsafeCompileRegex: " ++)--compileRegex' :: (Functor m,Monad m)- => Options- -> String- -> m (CaptureNames,Regex)-compileRegex' Options{..} s0 = do- (cnms,s2) <- either fail return $ extractNamedCaptures s1- (,) cnms <$> makeRegexOptsM _options_comp _options_exec s2- where- s1 = expandMacros reSource _options_mode _options_macs s0--prelude :: Macros RE-prelude = runIdentity $ preludeMacros mk PCRE ExclCaptures- where- mk = Identity . unsafeCompileRegex_ noPreludeOptions--preludeTestsFailing :: [MacroID]-preludeTestsFailing = badMacros preludeEnv--preludeEnv :: MacroEnv-preludeEnv = preludeMacroEnv PCRE--preludeTable :: String-preludeTable = preludeMacroTable PCRE--preludeSummary :: PreludeMacro -> String-preludeSummary = preludeMacroSummary PCRE--preludeSources :: String-preludeSources = preludeMacroSources PCRE--preludeSource :: PreludeMacro -> String-preludeSource = preludeMacroSource PCRE
− Text/RE/PCRE/Sequence.hs
@@ -1,61 +0,0 @@-{-# LANGUAGE MultiParamTypeClasses #-}-{-# LANGUAGE FlexibleContexts #-}-{-# LANGUAGE FlexibleInstances #-}-{-# OPTIONS_GHC -fno-warn-orphans #-}-{-# LANGUAGE CPP #-}-#if __GLASGOW_HASKELL__ >= 800-{-# OPTIONS_GHC -fno-warn-redundant-constraints #-}-#endif--module Text.RE.PCRE.Sequence- ( (*=~)- , (?=~)- , (=~)- , (=~~)- , module Text.RE- , module Text.RE.PCRE.RE- ) where--import qualified Data.Sequence as S-import Text.Regex.Base-import Text.RE-import Text.RE.Internal.AddCaptureNames-import Text.RE.PCRE.RE-import qualified Text.Regex.PCRE as PCRE----- | find all matches in text-(*=~) :: (S.Seq Char)- -> RE- -> Matches (S.Seq Char)-(*=~) bs rex = addCaptureNamesToMatches (reCaptureNames rex) $ match (reRegex rex) bs---- | find first matches in text-(?=~) :: (S.Seq Char)- -> RE- -> Match (S.Seq Char)-(?=~) bs rex = addCaptureNamesToMatch (reCaptureNames rex) $ match (reRegex rex) bs---- | regex-base polymorphic match operator-(=~) :: ( RegexContext PCRE.Regex (S.Seq Char) a- , RegexMaker PCRE.Regex PCRE.CompOption PCRE.ExecOption String- )- => (S.Seq Char)- -> RE- -> a-(=~) bs rex = match (reRegex rex) bs---- | regex-base monadic, polymorphic match operator-(=~~) :: ( Monad m- , RegexContext PCRE.Regex (S.Seq Char) a- , RegexMaker PCRE.Regex PCRE.CompOption PCRE.ExecOption String- )- => (S.Seq Char)- -> RE- -> m a-(=~~) bs rex = matchM (reRegex rex) bs--instance IsRegex RE (S.Seq Char) where- matchOnce = flip (?=~)- matchMany = flip (*=~)- regexSource = reSource
− Text/RE/PCRE/String.hs
@@ -1,61 +0,0 @@-{-# LANGUAGE MultiParamTypeClasses #-}-{-# LANGUAGE FlexibleContexts #-}-{-# LANGUAGE FlexibleInstances #-}-{-# OPTIONS_GHC -fno-warn-orphans #-}-{-# LANGUAGE CPP #-}-#if __GLASGOW_HASKELL__ >= 800-{-# OPTIONS_GHC -fno-warn-redundant-constraints #-}-#endif--module Text.RE.PCRE.String- ( (*=~)- , (?=~)- , (=~)- , (=~~)- , module Text.RE- , module Text.RE.PCRE.RE- ) where---import Text.Regex.Base-import Text.RE-import Text.RE.Internal.AddCaptureNames-import Text.RE.PCRE.RE-import qualified Text.Regex.PCRE as PCRE----- | find all matches in text-(*=~) :: String- -> RE- -> Matches String-(*=~) bs rex = addCaptureNamesToMatches (reCaptureNames rex) $ match (reRegex rex) bs---- | find first matches in text-(?=~) :: String- -> RE- -> Match String-(?=~) bs rex = addCaptureNamesToMatch (reCaptureNames rex) $ match (reRegex rex) bs---- | regex-base polymorphic match operator-(=~) :: ( RegexContext PCRE.Regex String a- , RegexMaker PCRE.Regex PCRE.CompOption PCRE.ExecOption String- )- => String- -> RE- -> a-(=~) bs rex = match (reRegex rex) bs---- | regex-base monadic, polymorphic match operator-(=~~) :: ( Monad m- , RegexContext PCRE.Regex String a- , RegexMaker PCRE.Regex PCRE.CompOption PCRE.ExecOption String- )- => String- -> RE- -> m a-(=~~) bs rex = matchM (reRegex rex) bs--instance IsRegex RE String where- matchOnce = flip (?=~)- matchMany = flip (*=~)- regexSource = reSource
− Text/RE/Parsers.hs
@@ -1,166 +0,0 @@-{-# OPTIONS_GHC -fno-warn-warnings-deprecations #-}-{-# LANGUAGE OverloadedStrings #-}--module Text.RE.Parsers- ( parseInteger- , parseHex- , parseDouble- , parseString- , parseSimpleString- , parseDate- , parseSlashesDate- , parseTimeOfDay- , parseTimeZone- , parseDateTime- , parseDateTime8601- , parseDateTimeCLF- , parseShortMonth- , shortMonthArray- , IPV4Address- , parseIPv4Address- , Severity(..)- , parseSeverity- , severityKeywords- ) where--import Data.Array-import qualified Data.HashMap.Strict as HM-import Data.Maybe-import qualified Data.Text as T-import Data.Time-import qualified Data.Time.Locale.Compat as LC-import Data.Word-import Text.Printf-import Text.Read-import Text.RE.Replace---parseInteger :: Replace a => a -> Maybe Int-parseInteger = readMaybe . unpack_--parseHex :: Replace a => a -> Maybe Int-parseHex = readMaybe . ("0x"++) . unpack_--parseDouble :: Replace a => a -> Maybe Double-parseDouble = readMaybe . unpack_--parseString :: Replace a => a -> Maybe T.Text-parseString = readMaybe . unpack_--parseSimpleString :: Replace a => a -> Maybe T.Text-parseSimpleString = Just . T.dropEnd 1 . T.drop 1 . textify--date_templates, time_templates, timezone_templates,- date_time_8601_templates, date_time_templates :: [String]-date_templates = ["%F"]-time_templates = ["%H:%M:%S","%H:%M:%S%Q","%H:%M"]-timezone_templates = ["Z","%z"]-date_time_8601_templates =- [ printf "%sT%s%s" dt tm tz- | dt <- date_templates- , tm <- time_templates- , tz <- timezone_templates- ]-date_time_templates =- [ printf "%s%c%s%s" dt sc tm tz- | dt <- date_templates- , sc <- ['T',' ']- , tm <- time_templates- , tz <- timezone_templates ++ [" UTC",""]- ]--parseDate :: Replace a => a -> Maybe Day-parseDate = parse_time date_templates--parseSlashesDate :: Replace a => a -> Maybe Day-parseSlashesDate = parse_time ["%Y/%m/%d"]--parseTimeOfDay :: Replace a => a -> Maybe TimeOfDay-parseTimeOfDay = parse_time time_templates--parseTimeZone :: Replace a => a -> Maybe TimeZone-parseTimeZone = parse_time timezone_templates--parseDateTime :: Replace a => a -> Maybe UTCTime-parseDateTime = parse_time date_time_templates--parseDateTime8601 :: Replace a => a -> Maybe UTCTime-parseDateTime8601 = parse_time date_time_8601_templates--parseDateTimeCLF :: Replace a => a -> Maybe UTCTime-parseDateTimeCLF = parse_time ["%d/%b/%Y:%H:%M:%S %z"]--parseShortMonth :: Replace a => a -> Maybe Int-parseShortMonth = flip HM.lookup short_month_hm . unpack_--parse_time :: (ParseTime t,Replace s) => [String] -> s -> Maybe t-parse_time tpls = prs . unpack_- where- prs s = listToMaybe $ catMaybes- [ parseTime LC.defaultTimeLocale fmt s- | fmt<-tpls- ]--short_month_hm :: HM.HashMap String Int-short_month_hm = HM.fromList [ (T.unpack $ shortMonthArray!i,i) | i<-[1..12] ]--shortMonthArray :: Array Int T.Text-shortMonthArray = listArray (1,12) $- T.words "Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec"--type IPV4Address = (Word8,Word8,Word8,Word8)--parseIPv4Address :: Replace a => a -> Maybe IPV4Address-parseIPv4Address = prs . words_by (=='.') . unpack_- where- prs [a_s,b_s,c_s,d_s] = do- a <- readMaybe a_s- b <- readMaybe b_s- c <- readMaybe c_s- d <- readMaybe d_s- case all is_o [a,b,c,d] of- True -> Just (toEnum a,toEnum b,toEnum c,toEnum d)- False -> Nothing- prs _ = Nothing-- is_o x = 0 <= x && x <= 255--data Severity- = Emerg- | Alert- | Crit- | Err- | Warning- | Notice- | Info- | Debug- deriving (Bounded,Enum,Ord,Eq,Show)--parseSeverity :: Replace a => a -> Maybe Severity-parseSeverity = flip HM.lookup severity_hm . textify--severity_hm :: HM.HashMap T.Text Severity-severity_hm = HM.fromList- [ (kw,pri)- | pri<-[minBound..maxBound]- , let (kw0,kws) = severityKeywords pri- , kw <- kw0:kws- ]--severityKeywords :: Severity -> (T.Text,[T.Text])-severityKeywords pri = case pri of- Emerg -> (,) "emerg" ["panic"]- Alert -> (,) "alert" []- Crit -> (,) "crit" []- Err -> (,) "err" ["error"]- Warning -> (,) "warning" ["warn"]- Notice -> (,) "notice" []- Info -> (,) "info" []- Debug -> (,) "debug" []--words_by :: (Char->Bool) -> String -> [String]-words_by f s = case dropWhile f s of- "" -> []- s' -> w : words_by f s''- where- (w, s'') = break f s'
− Text/RE/Replace.lhs
@@ -1,498 +0,0 @@-\begin{code}-{-# LANGUAGE NoImplicitPrelude #-}-{-# LANGUAGE QuasiQuotes #-}-{-# LANGUAGE OverloadedStrings #-}-{-# LANGUAGE RecordWildCards #-}-{-# LANGUAGE FlexibleContexts #-}-{-# LANGUAGE FlexibleInstances #-}--module Text.RE.Replace- ( Replace(..)- , Replace_(..)- , replace_- , Phi(..)- , Context(..)- , Location(..)- , isTopLocation- , replace- , replaceAll- , replaceAllCaptures- , replaceAllCaptures'- , replaceAllCaptures_- , replaceAllCapturesM- , replaceCaptures- , replaceCaptures'- , replaceCaptures_- , replaceCapturesM- , expandMacros- , expandMacros'- ) where--import Control.Applicative-import Data.Array-import qualified Data.ByteString.Char8 as B-import qualified Data.ByteString.Lazy.Char8 as LBS-import Data.Char-import qualified Data.Foldable as F-import Data.Functor.Identity-import qualified Data.HashMap.Strict as HM-import Data.Maybe-import Data.Monoid-import qualified Data.Sequence as S-import qualified Data.Text as T-import qualified Data.Text.Encoding as TE-import qualified Data.Text.Lazy as LT-import Prelude.Compat-import Text.Heredoc-import Text.RE.Capture-import Text.RE.CaptureID-import Text.RE.Options-import Text.Regex.TDFA-import Text.Regex.TDFA.Text()-import Text.Regex.TDFA.Text.Lazy()-\end{code}--\begin{code}--- | Replace provides the missing methods needed to replace the matched--- text; length_ is the minimum implementation-class (Extract a,Monoid a) => Replace a where- -- | length function for a- length_ :: a -> Int- -- | inject String into a- pack_ :: String -> a- -- | project a onto a String- unpack_ :: a -> String- -- | inject into Text- textify :: a -> T.Text- -- | project Text onto a- detextify :: T.Text -> a- -- | append a newline- appendNewline :: a -> a- -- | apply a substitution function to a Capture- subst :: (a->a) -> Capture a -> a- -- | convert a template containing $0, $1, etc., in the first- -- argument, into a 'phi' replacement function for use with- -- replaceAllCaptures' and replaceCaptures'- parse_tpl :: a -> Match a -> Location -> Capture a -> Maybe a-- textify = T.pack . unpack_- detextify = pack_ . T.unpack- appendNewline = (<> pack_ "\n")-- subst f m@Capture{..} =- capturePrefix m <> f capturedText <> captureSuffix m-\end{code}--\begin{code}--- | a selction of the Replace methods can be encapsulated with Replace_--- for the higher-order replacement functions-data Replace_ a =- Replace_- { _r_length :: a -> Int- , _r_subst :: (a->a) -> Capture a -> a- }---- | replace_ encapsulates Replace_ a from a Replace a context-replace_ :: Replace a => Replace_ a-replace_ =- Replace_- { _r_length = length_- , _r_subst = subst- }-\end{code}--\begin{code}--- | @Phi@ specifies the substitution function for procesing the substrings--- captured by the regular expression.-data Phi a =- Phi- { _phi_context :: Context -- ^ the context for applying- -- the substitution- , _phi_phi :: Location -> a -> a -- ^ the substitution function- -- takes the location and- -- the text to be replaced and- -- returns the replacement- -- text to be substituted- }---- | @Context@ specifies which contexts the substitutions should be applied-data Context- = TOP -- ^ substitutions should be applied to the top-level only,- -- the text that matched the whole RE- | SUB -- ^ substitutions should only be applied to the text- -- captured by bracketed sub-REs- | ALL -- ^ the substitution function should be applied to all- -- captures, the top level and the sub-expression captures- deriving (Show)---- | the @Location@ information passed into the substitution function--- specifies which sub-expression is being substituted-data Location =- Location- { _loc_match :: Int -- ^ the zero-based, i-th string to be- -- matched, when matching all strings,- -- zero when only the first string is- -- being matched- , _loc_capture :: CaptureOrdinal- -- ^ 0, when matching the top-level- -- string matched by the whole RE, 1- -- for the top-most, left-most redex- -- captured by bracketed sub-REs, etc.- }- deriving (Show)-\end{code}--\begin{code}--- | True iff the location references a complete match--- (i.e., not a bracketed capture)-isTopLocation :: Location -> Bool-isTopLocation = (==0) . _loc_capture-\end{code}--\begin{code}--- | replace all with a template, $0 for whole text, $1 for first--- capture, etc.-replaceAll :: Replace a- => a- -> Matches a- -> a-replaceAll tpl ac = replaceAllCaptures' TOP (parse_tpl tpl) ac-\end{code}--\begin{code}--- | substitutes the PHI substitutions through the Matches-replaceAllCaptures :: Replace a- => Phi a- -> Matches a- -> a-replaceAllCaptures = mk_phi replaceAllCaptures'-\end{code}--\begin{code}--- | substitutes using a function that takes the full Match--- context and returns the same replacement text as the _phi_phi--- context.-replaceAllCaptures' :: Replace a- => Context- -> (Match a->Location->Capture a->Maybe a)- -> Matches a- -> a-\end{code}--\begin{code}-replaceAllCaptures' = replaceAllCaptures_ replace_-\end{code}--\begin{code}--- | replaceAllCaptures_ is like like replaceAllCaptures' but takes the--- Replace methods through the Replace_ argument-replaceAllCaptures_ :: Extract a- => Replace_ a- -> Context- -> (Match a->Location->Capture a->Maybe a)- -> Matches a- -> a-replaceAllCaptures_ s ctx phi ac =- runIdentity $ replaceAllCapturesM s ctx (lift_phi phi) ac-\end{code}--\begin{code}--- | replaceAllCapturesM is just a monadically generalised version of--- replaceAllCaptures_-replaceAllCapturesM :: (Extract a,Monad m)- => Replace_ a- -> Context- -> (Match a->Location->Capture a->m (Maybe a))- -> Matches a- -> m a-replaceAllCapturesM r ctx phi_ Matches{..} =- replaceCapturesM r ALL phi $ Match matchesSource cnms arr- where- phi _ (Location _ i) = case arr_c!i of- Just caps -> phi_ caps . uncurry Location $ arr_i ! i- Nothing -> const $ return Nothing-- arr_c = listArray bds $- concat $- [ repl (rangeSize $ bounds $ matchArray cs) cs- | cs <- allMatches- ]-- arr_i = listArray bds j_ks-- arr = listArray bds $- [ arr_ ! k- | arr_ <- map matchArray allMatches- , k <- indices arr_- ]-- bds = (0,CaptureOrdinal $ length j_ks-1)-- j_ks =- [ (j,k)- | (j,arr_) <- zip [0..] $ map matchArray allMatches- , k <- indices arr_- ]-- repl 0 _ = []- repl n x = case ctx of- TOP -> Just x : replicate (n-1) Nothing- SUB -> Nothing : replicate (n-1) (Just x)- ALL -> replicate n $ Just x-- cnms = fromMaybe noCaptureNames $ listToMaybe $ map captureNames allMatches-\end{code}--\begin{code}--- | replace with a template containing $0 for whole text,--- $1 for first capture, etc.-replace :: Replace a- => Match a- -> a- -> a-replace c tpl = replaceCaptures' TOP (parse_tpl tpl) c-\end{code}--\begin{code}--- | substitutes the PHI substitutions through the Match-replaceCaptures :: Replace a- => Phi a- -> Match a- -> a-replaceCaptures = mk_phi replaceCaptures'-\end{code}--\begin{code}--- | substitutes using a function that takes the full Match--- context and returns the same replacement text as the _phi_phi--- context.-replaceCaptures' :: Replace a- => Context- -> (Match a->Location->Capture a->Maybe a)- -> Match a- -> a-replaceCaptures' = replaceCaptures_ replace_-\end{code}--\begin{code}--- | replaceCaptures_ is like replaceCaptures' but takes the Replace methods--- through the Replace_ argument-replaceCaptures_ :: Extract a- => Replace_ a- -> Context- -> (Match a->Location->Capture a->Maybe a)- -> Match a- -> a-replaceCaptures_ s ctx phi caps =- runIdentity $ replaceCapturesM s ctx (lift_phi phi) caps-\end{code}--\begin{code}--- | replaceCapturesM is just a monadically generalised version of--- replaceCaptures_-replaceCapturesM :: (Monad m,Extract a)- => Replace_ a- -> Context- -> (Match a->Location->Capture a->m (Maybe a))- -> Match a- -> m a-replaceCapturesM Replace_{..} ctx phi_ caps@Match{..} = do- (hay',_) <- foldr sc (return (matchSource,[])) $- zip [0..] $ elems matchArray- return hay'- where- sc (i,cap0) act = do- (hay,ds) <- act- let ndl = capturedText cap- cap = adj hay ds cap0- mb <- phi i cap- case mb of- Nothing -> return (hay,ds)- Just ndl' ->- return- ( _r_subst (const ndl') cap- , (captureOffset cap,len'-len) : ds- )- where- len' = _r_length ndl'- len = _r_length ndl-- adj hay ds cap =- Capture- { captureSource = hay- , capturedText = before len $ after off0 hay- , captureOffset = off0- , captureLength = len- }- where- len = len0 + sum- [ delta- | (off,delta) <- ds- , off < off0 + len0- ]- len0 = captureLength cap- off0 = captureOffset cap-- phi i cap = case ctx of- TOP | i/=0 -> return Nothing- SUB | i==0 ->return Nothing- _ ->- case not $ hasCaptured cap of- True -> return Nothing- False -> phi_ caps (Location 0 i) cap-\end{code}--\begin{code}--- the Replace instances--instance Replace [Char] where- length_ = length- pack_ = id- unpack_ = id- textify = T.pack- detextify = T.unpack- appendNewline = (<>"\n")- parse_tpl = parse_tpl_ id--instance Replace B.ByteString where- length_ = B.length- pack_ = B.pack- unpack_ = B.unpack- textify = TE.decodeUtf8- detextify = TE.encodeUtf8- appendNewline = (<>"\n")- parse_tpl = parse_tpl_ B.unpack--instance Replace LBS.ByteString where- length_ = fromEnum . LBS.length- pack_ = LBS.pack- unpack_ = LBS.unpack- textify = TE.decodeUtf8 . LBS.toStrict- detextify = LBS.fromStrict . TE.encodeUtf8- appendNewline = (<>"\n")- parse_tpl = parse_tpl_ LBS.unpack--instance Replace (S.Seq Char) where- length_ = S.length- pack_ = S.fromList- unpack_ = F.toList- parse_tpl = parse_tpl_ F.toList--instance Replace T.Text where- length_ = T.length- pack_ = T.pack- unpack_ = T.unpack- textify = id- detextify = id- appendNewline = (<>"\n")- parse_tpl = parse_tpl_ T.unpack--instance Replace LT.Text where- length_ = fromEnum . LT.length- pack_ = LT.pack- unpack_ = LT.unpack- textify = LT.toStrict- detextify = LT.fromStrict- appendNewline = (<>"\n")- parse_tpl = parse_tpl_ LT.unpack-\end{code}--\begin{code}--- | expand all of the @{..} macros in the RE in the argument String--- according to the Macros argument, preprocessing the RE String--- according to the Mode argument (used internally)-expandMacros :: (r->String) -> Mode -> Macros r -> String -> String-expandMacros x_src md hm s0 =- case HM.null hm of- True -> s- False -> expandMacros' (fmap x_src . flip HM.lookup hm) s- where- s = case md of- Simple -> s0- Block -> concat $ map clean $ lines s0-- clean = reverse . dropWhile isSpace . reverse . dropWhile isSpace-\end{code}--\begin{code}--- | expand the @{..} macos in the argument string using the given--- function-expandMacros' :: (MacroID->Maybe String) -> String -> String-expandMacros' lu = fixpoint e_m- where- e_m re_s = replaceAllCaptures' TOP phi $ re_s $=~ [here|@(@|\{([^{}]+)\})|]- where- phi mtch _ cap = case txt == "@@" of- True -> Just "@"- False -> Just $ fromMaybe txt $ lu ide- where- txt = capturedText cap- ide = MacroID $ capturedText $ capture c2 mtch- c2 = CID_ordinal $ CaptureOrdinal 2-\end{code}--\begin{code}-lift_phi :: Monad m- => (Match a->Location->Capture a->Maybe a)- -> (Match a->Location->Capture a->m (Maybe a))-lift_phi phi_ = phi- where- phi caps' loc' cap' = return $ phi_ caps' loc' cap'--mk_phi :: (Context->(Match a->Location->Capture a->Maybe a)->b)- -> Phi a- -> b-mk_phi f phi@Phi{..} = f _phi_context $ mk_phi' phi--mk_phi' :: Phi a -> Match a -> Location -> Capture a -> Maybe a-mk_phi' Phi{..} _ loc = Just . _phi_phi loc . capturedText-\end{code}--\begin{code}-parse_tpl_ :: ( Replace a- , RegexContext Regex a (Matches a)- , RegexMaker Regex CompOption ExecOption String- )- => (a->String)- -> a- -> Match a- -> Location- -> Capture a- -> Maybe a-parse_tpl_ unpack tpl mtch _ _ =- Just $ replaceAllCaptures' TOP phi $- tpl $=~ [here|\$(\$|[0-9]+|\{([^{}]+)\})|]- where- phi t_mtch _ _ = case t_mtch !$? c2 of- Just cap -> this $ CID_name $ CaptureName txt- where- txt = T.pack $ unpack $ capturedText cap- Nothing -> case s == "$" of- True -> Just t- False -> this $ CID_ordinal $ CaptureOrdinal $ read s- where- s = unpack t- t = capturedText $ capture c1 t_mtch-- this cid = capturedText <$> mtch !$? cid-- c1 = CID_ordinal $ CaptureOrdinal 1- c2 = CID_ordinal $ CaptureOrdinal 2-\end{code}--\begin{code}-fixpoint :: (Eq a) => (a->a) -> a -> a-fixpoint f = chk . iterate f- where- chk (x:x':_) | x==x' = x- chk xs = chk $ tail xs-\end{code}--\begin{code}-($=~) :: ( RegexContext Regex source target- , RegexMaker Regex CompOption ExecOption String- )- => source -> String -> target-($=~) = (=~)--\end{code}
− Text/RE/TDFA.hs
@@ -1,64 +0,0 @@-{-# LANGUAGE FlexibleContexts #-}-{-# LANGUAGE CPP #-}-#if __GLASGOW_HASKELL__ >= 800-{-# OPTIONS_GHC -fno-warn-redundant-constraints #-}-#endif-{-# OPTIONS_GHC -fno-warn-dodgy-exports #-}--module Text.RE.TDFA- ( (*=~)- , (?=~)- , (=~)- , (=~~)- , module Text.RE- , module Text.RE.TDFA.RE- , module Text.RE.TDFA.ByteString- , module Text.RE.TDFA.ByteString.Lazy- , module Text.RE.TDFA.Sequence- , module Text.RE.TDFA.String- , module Text.RE.TDFA.Text- , module Text.RE.TDFA.Text.Lazy- ) where---import qualified Text.Regex.Base as B-import Text.RE-import Text.RE.Internal.AddCaptureNames-import Text.RE.TDFA.RE-import qualified Text.Regex.TDFA as TDFA-import Text.RE.TDFA.ByteString()-import Text.RE.TDFA.ByteString.Lazy()-import Text.RE.TDFA.Sequence()-import Text.RE.TDFA.String()-import Text.RE.TDFA.Text()-import Text.RE.TDFA.Text.Lazy()---(*=~) :: IsRegex RE s- => s- -> RE- -> Matches s-(*=~) bs rex = addCaptureNamesToMatches (reCaptureNames rex) $ matchMany rex bs--(?=~) :: IsRegex RE s- => s- -> RE- -> Match s-(?=~) bs rex = addCaptureNamesToMatch (reCaptureNames rex) $ matchOnce rex bs--(=~) :: ( B.RegexContext TDFA.Regex s a- , B.RegexMaker TDFA.Regex TDFA.CompOption TDFA.ExecOption s- )- => s- -> RE- -> a-(=~) bs rex = B.match (reRegex rex) bs--(=~~) :: ( Monad m- , B.RegexContext TDFA.Regex s a- , B.RegexMaker TDFA.Regex TDFA.CompOption TDFA.ExecOption s- )- => s- -> RE- -> m a-(=~~) bs rex = B.matchM (reRegex rex) bs
− Text/RE/TDFA/ByteString.hs
@@ -1,61 +0,0 @@-{-# LANGUAGE MultiParamTypeClasses #-}-{-# LANGUAGE FlexibleContexts #-}-{-# LANGUAGE FlexibleInstances #-}-{-# OPTIONS_GHC -fno-warn-orphans #-}-{-# LANGUAGE CPP #-}-#if __GLASGOW_HASKELL__ >= 800-{-# OPTIONS_GHC -fno-warn-redundant-constraints #-}-#endif--module Text.RE.TDFA.ByteString- ( (*=~)- , (?=~)- , (=~)- , (=~~)- , module Text.RE- , module Text.RE.TDFA.RE- ) where--import qualified Data.ByteString as B-import Text.Regex.Base-import Text.RE-import Text.RE.Internal.AddCaptureNames-import Text.RE.TDFA.RE-import qualified Text.Regex.TDFA as TDFA----- | find all matches in text-(*=~) :: B.ByteString- -> RE- -> Matches B.ByteString-(*=~) bs rex = addCaptureNamesToMatches (reCaptureNames rex) $ match (reRegex rex) bs---- | find first matches in text-(?=~) :: B.ByteString- -> RE- -> Match B.ByteString-(?=~) bs rex = addCaptureNamesToMatch (reCaptureNames rex) $ match (reRegex rex) bs---- | regex-base polymorphic match operator-(=~) :: ( RegexContext TDFA.Regex B.ByteString a- , RegexMaker TDFA.Regex TDFA.CompOption TDFA.ExecOption String- )- => B.ByteString- -> RE- -> a-(=~) bs rex = match (reRegex rex) bs---- | regex-base monadic, polymorphic match operator-(=~~) :: ( Monad m- , RegexContext TDFA.Regex B.ByteString a- , RegexMaker TDFA.Regex TDFA.CompOption TDFA.ExecOption String- )- => B.ByteString- -> RE- -> m a-(=~~) bs rex = matchM (reRegex rex) bs--instance IsRegex RE B.ByteString where- matchOnce = flip (?=~)- matchMany = flip (*=~)- regexSource = reSource
− Text/RE/TDFA/ByteString/Lazy.hs
@@ -1,61 +0,0 @@-{-# LANGUAGE MultiParamTypeClasses #-}-{-# LANGUAGE FlexibleContexts #-}-{-# LANGUAGE FlexibleInstances #-}-{-# OPTIONS_GHC -fno-warn-orphans #-}-{-# LANGUAGE CPP #-}-#if __GLASGOW_HASKELL__ >= 800-{-# OPTIONS_GHC -fno-warn-redundant-constraints #-}-#endif--module Text.RE.TDFA.ByteString.Lazy- ( (*=~)- , (?=~)- , (=~)- , (=~~)- , module Text.RE- , module Text.RE.TDFA.RE- ) where--import qualified Data.ByteString.Lazy.Char8 as LBS-import Text.Regex.Base-import Text.RE-import Text.RE.Internal.AddCaptureNames-import Text.RE.TDFA.RE-import qualified Text.Regex.TDFA as TDFA----- | find all matches in text-(*=~) :: LBS.ByteString- -> RE- -> Matches LBS.ByteString-(*=~) bs rex = addCaptureNamesToMatches (reCaptureNames rex) $ match (reRegex rex) bs---- | find first matches in text-(?=~) :: LBS.ByteString- -> RE- -> Match LBS.ByteString-(?=~) bs rex = addCaptureNamesToMatch (reCaptureNames rex) $ match (reRegex rex) bs---- | regex-base polymorphic match operator-(=~) :: ( RegexContext TDFA.Regex LBS.ByteString a- , RegexMaker TDFA.Regex TDFA.CompOption TDFA.ExecOption String- )- => LBS.ByteString- -> RE- -> a-(=~) bs rex = match (reRegex rex) bs---- | regex-base monadic, polymorphic match operator-(=~~) :: ( Monad m- , RegexContext TDFA.Regex LBS.ByteString a- , RegexMaker TDFA.Regex TDFA.CompOption TDFA.ExecOption String- )- => LBS.ByteString- -> RE- -> m a-(=~~) bs rex = matchM (reRegex rex) bs--instance IsRegex RE LBS.ByteString where- matchOnce = flip (?=~)- matchMany = flip (*=~)- regexSource = reSource
− Text/RE/TDFA/RE.hs
@@ -1,245 +0,0 @@-{-# LANGUAGE NoImplicitPrelude #-}-{-# LANGUAGE RecordWildCards #-}-{-# LANGUAGE QuasiQuotes #-}-{-# LANGUAGE TemplateHaskell #-}-{-# LANGUAGE MultiParamTypeClasses #-}-{-# LANGUAGE TypeSynonymInstances #-}-{-# LANGUAGE FlexibleInstances #-}-{-# LANGUAGE FlexibleContexts #-}-{-# LANGUAGE CPP #-}-#if __GLASGOW_HASKELL__ >= 800-{-# OPTIONS_GHC -fno-warn-redundant-constraints #-}-#endif-{-# OPTIONS_GHC -fno-warn-orphans #-}--module Text.RE.TDFA.RE- ( re- , reMS- , reMI- , reBS- , reBI- , reMultilineSensitive- , reMultilineInsensitive- , reBlockSensitive- , reBlockInsensitive- , re_- , cp- , regexType- , RE- , reOptions- , reSource- , reCaptureNames- , reRegex- , Options- , noPreludeOptions- , defaultOptions- , prelude- , preludeEnv- , preludeTestsFailing- , preludeTable- , preludeSummary- , preludeSources- , preludeSource- , unpackSimpleRegexOptions- , compileRegex- ) where--import Data.Functor.Identity-import Language.Haskell.TH-import Language.Haskell.TH.Quote-import Prelude.Compat-import Text.RE-import Text.RE.Internal.NamedCaptures-import Text.RE.Internal.PreludeMacros-import Text.RE.Internal.QQ-import Text.RE.TestBench-import Text.Regex.TDFA---re- , reMS- , reMI- , reBS- , reBI- , reMultilineSensitive- , reMultilineInsensitive- , reBlockSensitive- , reBlockInsensitive- , re_ :: QuasiQuoter--re = re' $ Just minBound-reMS = reMultilineSensitive-reMI = reMultilineInsensitive-reBS = reBlockSensitive-reBI = reBlockInsensitive-reMultilineSensitive = re' $ Just MultilineSensitive-reMultilineInsensitive = re' $ Just MultilineInsensitive-reBlockSensitive = re' $ Just BlockSensitive-reBlockInsensitive = re' $ Just BlockInsensitive-re_ = re' Nothing--regexType :: RegexType-regexType = TDFA--data RE =- RE- { _re_options :: !Options- , _re_source :: !String- , _re_cnames :: !CaptureNames- , _re_regex :: !Regex- }--reOptions :: RE -> Options-reOptions = _re_options--reSource :: RE -> String-reSource = _re_source--reCaptureNames :: RE -> CaptureNames-reCaptureNames = _re_cnames--reRegex :: RE -> Regex-reRegex = _re_regex--type Options = Options_ RE CompOption ExecOption--instance IsOption SimpleRegexOptions RE CompOption ExecOption where- makeOptions = unpackSimpleRegexOptions--instance IsOption Mode RE CompOption ExecOption where- makeOptions md = Options md prelude def_comp_option def_exec_option--instance IsOption (Macros RE) RE CompOption ExecOption where- makeOptions ms = Options minBound ms def_comp_option def_exec_option--instance IsOption CompOption RE CompOption ExecOption where- makeOptions co = Options minBound prelude co def_exec_option--instance IsOption ExecOption RE CompOption ExecOption where- makeOptions eo = Options minBound prelude def_comp_option eo--instance IsOption Options RE CompOption ExecOption where- makeOptions = id--instance IsOption () RE CompOption ExecOption where- makeOptions _ = unpackSimpleRegexOptions minBound--def_comp_option :: CompOption-def_comp_option = _options_comp defaultOptions--def_exec_option :: ExecOption-def_exec_option = _options_exec defaultOptions--noPreludeOptions :: Options-noPreludeOptions = defaultOptions { _options_macs = emptyMacros }--defaultOptions :: Options-defaultOptions = makeOptions (minBound::SimpleRegexOptions)--unpackSimpleRegexOptions :: SimpleRegexOptions -> Options-unpackSimpleRegexOptions sro =- Options- { _options_mode = minBound- , _options_macs = prelude- , _options_comp = comp- , _options_exec = defaultExecOpt- }- where- comp = defaultCompOpt- { caseSensitive = cs- , multiline = ml- }-- (ml,cs) = case sro of- MultilineSensitive -> (,) True True- MultilineInsensitive -> (,) True False- BlockSensitive -> (,) False True- BlockInsensitive -> (,) False False--compileRegex :: ( IsOption o RE CompOption ExecOption- , Functor m- , Monad m- )- => o- -> String- -> m RE-compileRegex = compileRegex_ . makeOptions--compileRegex_ :: (Functor m,Monad m)- => Options- -> String- -> m RE-compileRegex_ os re_s = uncurry mk <$> compileRegex' os re_s- where- mk cnms rx =- RE- { _re_options = os- , _re_source = re_s- , _re_cnames = cnms- , _re_regex = rx- }--re' :: Maybe SimpleRegexOptions -> QuasiQuoter-re' mb = case mb of- Nothing ->- (qq0 "re_")- { quoteExp = parse minBound (\rs->[|flip unsafeCompileRegex rs|])- }- Just sro ->- (qq0 "re")- { quoteExp = parse sro (\rs->[|unsafeCompileRegexSimple sro rs|])- }- where- parse :: SimpleRegexOptions -> (String->Q Exp) -> String -> Q Exp- parse sro mk rs = either error (\_->mk rs) $ compileRegex_ os rs- where- os = unpackSimpleRegexOptions sro--unsafeCompileRegexSimple :: SimpleRegexOptions -> String -> RE-unsafeCompileRegexSimple sro re_s = unsafeCompileRegex_ os re_s- where- os = unpackSimpleRegexOptions sro--unsafeCompileRegex :: IsOption o RE CompOption ExecOption- => o- -> String- -> RE-unsafeCompileRegex = unsafeCompileRegex_ . makeOptions--unsafeCompileRegex_ :: Options -> String -> RE-unsafeCompileRegex_ os = either oops id . compileRegex_ os- where- oops = error . ("unsafeCompileRegex: " ++)--compileRegex' :: (Functor m,Monad m)- => Options- -> String- -> m (CaptureNames,Regex)-compileRegex' Options{..} s0 = do- (cnms,s2) <- either fail return $ extractNamedCaptures s1- (,) cnms <$> makeRegexOptsM _options_comp _options_exec s2- where- s1 = expandMacros reSource _options_mode _options_macs s0--prelude :: Macros RE-prelude = runIdentity $ preludeMacros mk TDFA ExclCaptures- where- mk = Identity . unsafeCompileRegex_ noPreludeOptions--preludeEnv :: MacroEnv-preludeEnv = preludeMacroEnv TDFA--preludeTestsFailing :: [MacroID]-preludeTestsFailing = badMacros $ preludeMacroEnv TDFA--preludeTable :: String-preludeTable = preludeMacroTable TDFA--preludeSummary :: PreludeMacro -> String-preludeSummary = preludeMacroSummary TDFA--preludeSources :: String-preludeSources = preludeMacroSources TDFA--preludeSource :: PreludeMacro -> String-preludeSource = preludeMacroSource TDFA
− Text/RE/TDFA/Sequence.hs
@@ -1,61 +0,0 @@-{-# LANGUAGE MultiParamTypeClasses #-}-{-# LANGUAGE FlexibleContexts #-}-{-# LANGUAGE FlexibleInstances #-}-{-# OPTIONS_GHC -fno-warn-orphans #-}-{-# LANGUAGE CPP #-}-#if __GLASGOW_HASKELL__ >= 800-{-# OPTIONS_GHC -fno-warn-redundant-constraints #-}-#endif--module Text.RE.TDFA.Sequence- ( (*=~)- , (?=~)- , (=~)- , (=~~)- , module Text.RE- , module Text.RE.TDFA.RE- ) where--import qualified Data.Sequence as S-import Text.Regex.Base-import Text.RE-import Text.RE.Internal.AddCaptureNames-import Text.RE.TDFA.RE-import qualified Text.Regex.TDFA as TDFA----- | find all matches in text-(*=~) :: (S.Seq Char)- -> RE- -> Matches (S.Seq Char)-(*=~) bs rex = addCaptureNamesToMatches (reCaptureNames rex) $ match (reRegex rex) bs---- | find first matches in text-(?=~) :: (S.Seq Char)- -> RE- -> Match (S.Seq Char)-(?=~) bs rex = addCaptureNamesToMatch (reCaptureNames rex) $ match (reRegex rex) bs---- | regex-base polymorphic match operator-(=~) :: ( RegexContext TDFA.Regex (S.Seq Char) a- , RegexMaker TDFA.Regex TDFA.CompOption TDFA.ExecOption String- )- => (S.Seq Char)- -> RE- -> a-(=~) bs rex = match (reRegex rex) bs---- | regex-base monadic, polymorphic match operator-(=~~) :: ( Monad m- , RegexContext TDFA.Regex (S.Seq Char) a- , RegexMaker TDFA.Regex TDFA.CompOption TDFA.ExecOption String- )- => (S.Seq Char)- -> RE- -> m a-(=~~) bs rex = matchM (reRegex rex) bs--instance IsRegex RE (S.Seq Char) where- matchOnce = flip (?=~)- matchMany = flip (*=~)- regexSource = reSource
− Text/RE/TDFA/String.hs
@@ -1,61 +0,0 @@-{-# LANGUAGE MultiParamTypeClasses #-}-{-# LANGUAGE FlexibleContexts #-}-{-# LANGUAGE FlexibleInstances #-}-{-# OPTIONS_GHC -fno-warn-orphans #-}-{-# LANGUAGE CPP #-}-#if __GLASGOW_HASKELL__ >= 800-{-# OPTIONS_GHC -fno-warn-redundant-constraints #-}-#endif--module Text.RE.TDFA.String- ( (*=~)- , (?=~)- , (=~)- , (=~~)- , module Text.RE- , module Text.RE.TDFA.RE- ) where---import Text.Regex.Base-import Text.RE-import Text.RE.Internal.AddCaptureNames-import Text.RE.TDFA.RE-import qualified Text.Regex.TDFA as TDFA----- | find all matches in text-(*=~) :: String- -> RE- -> Matches String-(*=~) bs rex = addCaptureNamesToMatches (reCaptureNames rex) $ match (reRegex rex) bs---- | find first matches in text-(?=~) :: String- -> RE- -> Match String-(?=~) bs rex = addCaptureNamesToMatch (reCaptureNames rex) $ match (reRegex rex) bs---- | regex-base polymorphic match operator-(=~) :: ( RegexContext TDFA.Regex String a- , RegexMaker TDFA.Regex TDFA.CompOption TDFA.ExecOption String- )- => String- -> RE- -> a-(=~) bs rex = match (reRegex rex) bs---- | regex-base monadic, polymorphic match operator-(=~~) :: ( Monad m- , RegexContext TDFA.Regex String a- , RegexMaker TDFA.Regex TDFA.CompOption TDFA.ExecOption String- )- => String- -> RE- -> m a-(=~~) bs rex = matchM (reRegex rex) bs--instance IsRegex RE String where- matchOnce = flip (?=~)- matchMany = flip (*=~)- regexSource = reSource
− Text/RE/TDFA/Text.hs
@@ -1,61 +0,0 @@-{-# LANGUAGE MultiParamTypeClasses #-}-{-# LANGUAGE FlexibleContexts #-}-{-# LANGUAGE FlexibleInstances #-}-{-# OPTIONS_GHC -fno-warn-orphans #-}-{-# LANGUAGE CPP #-}-#if __GLASGOW_HASKELL__ >= 800-{-# OPTIONS_GHC -fno-warn-redundant-constraints #-}-#endif--module Text.RE.TDFA.Text- ( (*=~)- , (?=~)- , (=~)- , (=~~)- , module Text.RE- , module Text.RE.TDFA.RE- ) where--import qualified Data.Text as T-import Text.Regex.Base-import Text.RE-import Text.RE.Internal.AddCaptureNames-import Text.RE.TDFA.RE-import qualified Text.Regex.TDFA as TDFA----- | find all matches in text-(*=~) :: T.Text- -> RE- -> Matches T.Text-(*=~) bs rex = addCaptureNamesToMatches (reCaptureNames rex) $ match (reRegex rex) bs---- | find first matches in text-(?=~) :: T.Text- -> RE- -> Match T.Text-(?=~) bs rex = addCaptureNamesToMatch (reCaptureNames rex) $ match (reRegex rex) bs---- | regex-base polymorphic match operator-(=~) :: ( RegexContext TDFA.Regex T.Text a- , RegexMaker TDFA.Regex TDFA.CompOption TDFA.ExecOption String- )- => T.Text- -> RE- -> a-(=~) bs rex = match (reRegex rex) bs---- | regex-base monadic, polymorphic match operator-(=~~) :: ( Monad m- , RegexContext TDFA.Regex T.Text a- , RegexMaker TDFA.Regex TDFA.CompOption TDFA.ExecOption String- )- => T.Text- -> RE- -> m a-(=~~) bs rex = matchM (reRegex rex) bs--instance IsRegex RE T.Text where- matchOnce = flip (?=~)- matchMany = flip (*=~)- regexSource = reSource
− Text/RE/TDFA/Text/Lazy.hs
@@ -1,61 +0,0 @@-{-# LANGUAGE MultiParamTypeClasses #-}-{-# LANGUAGE FlexibleContexts #-}-{-# LANGUAGE FlexibleInstances #-}-{-# OPTIONS_GHC -fno-warn-orphans #-}-{-# LANGUAGE CPP #-}-#if __GLASGOW_HASKELL__ >= 800-{-# OPTIONS_GHC -fno-warn-redundant-constraints #-}-#endif--module Text.RE.TDFA.Text.Lazy- ( (*=~)- , (?=~)- , (=~)- , (=~~)- , module Text.RE- , module Text.RE.TDFA.RE- ) where--import qualified Data.Text.Lazy as TL-import Text.Regex.Base-import Text.RE-import Text.RE.Internal.AddCaptureNames-import Text.RE.TDFA.RE-import qualified Text.Regex.TDFA as TDFA----- | find all matches in text-(*=~) :: TL.Text- -> RE- -> Matches TL.Text-(*=~) bs rex = addCaptureNamesToMatches (reCaptureNames rex) $ match (reRegex rex) bs---- | find first matches in text-(?=~) :: TL.Text- -> RE- -> Match TL.Text-(?=~) bs rex = addCaptureNamesToMatch (reCaptureNames rex) $ match (reRegex rex) bs---- | regex-base polymorphic match operator-(=~) :: ( RegexContext TDFA.Regex TL.Text a- , RegexMaker TDFA.Regex TDFA.CompOption TDFA.ExecOption String- )- => TL.Text- -> RE- -> a-(=~) bs rex = match (reRegex rex) bs---- | regex-base monadic, polymorphic match operator-(=~~) :: ( Monad m- , RegexContext TDFA.Regex TL.Text a- , RegexMaker TDFA.Regex TDFA.CompOption TDFA.ExecOption String- )- => TL.Text- -> RE- -> m a-(=~~) bs rex = matchM (reRegex rex) bs--instance IsRegex RE TL.Text where- matchOnce = flip (?=~)- matchMany = flip (*=~)- regexSource = reSource
− Text/RE/TestBench.lhs
@@ -1,564 +0,0 @@-\begin{code}-{-# LANGUAGE NoImplicitPrelude #-}-{-# LANGUAGE RecordWildCards #-}-{-# LANGUAGE GeneralizedNewtypeDeriving #-}-{-# LANGUAGE OverloadedStrings #-}-{-# LANGUAGE CPP #-}-#if __GLASGOW_HASKELL__ >= 800-{-# OPTIONS_GHC -fno-warn-redundant-constraints #-}-#endif--module Text.RE.TestBench- ( MacroID(..)- , RegexType(..)- , MacroEnv- , WithCaptures(..)- , MacroDescriptor(..)- , TestResult(..)- , RegexSource(..)- , FunctionID(..)- , mkMacros- , testMacroEnv- , badMacros- , runTests- , runTests'- , formatMacroTable- , dumpMacroTable- , formatMacroSummary- , formatMacroSources- , formatMacroSource- , testMacroDescriptors- ) where--import Data.Array-import Data.Char-import qualified Data.HashMap.Lazy as HML-import qualified Data.List as L-import Data.Maybe-import Data.Ord-import Data.String-import Text.Printf-import Prelude.Compat-import Text.RE.Capture-import Text.RE.Options-import Text.RE.Replace-import qualified Text.Regex.PCRE as PCRE-import qualified Text.Regex.TDFA as TDFA-\end{code}--Types--------\begin{code}--- | what kind of back end will be compiling the RE-data RegexType- = TDFA -- the TDFA back end- | PCRE -- the PCRE back end- deriving (Bounded,Enum,Eq,Ord,Show)---- | do we need the captures in the RE or whould they be stripped out--- where possible-data WithCaptures- = InclCaptures- | ExclCaptures- deriving (Eq,Ord,Show)---- | each macro can reference others, the whole environment being--- required for each macro, so we use a Lazy HashMap-type MacroEnv = HML.HashMap MacroID MacroDescriptor---- | describes a macro, giving the text of the RE and a si=ummary--- description-data MacroDescriptor =- MacroDescriptor- { _md_source :: !RegexSource -- ^ the RE- , _md_samples :: ![String] -- ^ some sample matches- , _md_counter_samples :: ![String] -- ^ some sample non-matches- , _md_test_results :: ![TestResult] -- ^ validation test results- , _md_parser :: !(Maybe FunctionID) -- ^ WA, the parser function- , _md_description :: !String -- ^ summary comment- }- deriving (Show)---- | list of failures on a validation run-newtype TestResult =- TestResult { _TestResult :: String }- deriving (IsString,Show)---- | a RE that should work for POSIX and PCRE with open brackets ('(')--- represented as follows:--- \( mere symbol--- (?: used for grouping only, not for captures--- (}: used for captures only, not for grouping--- (]: used for captures and grouping--- ( do not modify-newtype RegexSource =- RegexSource { _RegexSource :: String }- deriving (IsString,Show)---- | name of the Haskell parser function for parsing the text matched--- by a macro-newtype FunctionID =- FunctionID { _FunctionID :: String }- deriving (IsString,Show)---- | we are only interested in the open parentheses used for--- grouping and/or capturing; if neither grouping or capturing then--- there is no initial '(' or '(?:', just the suffic text-data REToken =- REToken- { _ret_prefix :: String -- ^ following text optional ( or (?:- , _ret_fixed :: Bool -- ^ a '(' that is not safe to modify- , _ret_grouping :: Bool -- ^ is this a grouping group- , _ret_capturing :: Bool -- ^ is this a capturing group- }- deriving (Show)-\end{code}---mkMacros-----------\begin{code}-mkMacros :: (Monad m,Functor m)- => (String->m r)- -> RegexType- -> WithCaptures- -> MacroEnv- -> m (Macros r)-mkMacros prs rty wc env =- HML.fromList <$> mapM (uncurry mk) (HML.toList env)- where- mk mid md = (,) mid <$> prs (mdRegexSource rty wc env md)-\end{code}---testMacroEnv, badMacros--------------------------\begin{code}-testMacroEnv :: String -> RegexType -> MacroEnv -> IO Bool-testMacroEnv lab rty m_env = case badMacros m_env of- [] -> return True- fails -> do- putStrLn $ lab' ++ " has failing tests for these macros: "- putStr $ unlines $ [ " "++_MacroID mid | mid<-fails ]- putStrLn $ "The whole table:"- putStrLn $ "========================================================"- putStr $ formatMacroTable rty m_env- putStrLn $ "========================================================"- return False- where- lab' = lab ++ " [" ++ show rty ++"]"--badMacros :: MacroEnv -> [MacroID]-badMacros m_env =- [ mid- | (mid,MacroDescriptor{..}) <- HML.toList m_env- , not $ null _md_test_results- ]--runTests :: (Eq a,Show a)- => RegexType- -> (String->Maybe a)- -> [(String,a)]- -> MacroEnv- -> MacroID- -> MacroDescriptor- -> MacroDescriptor-runTests rty parser = runTests' rty parser'- where- parser' caps = fmap capturedText (matchCapture caps) >>= parser--runTests' :: (Eq a,Show a)- => RegexType- -> (Match String->Maybe a)- -> [(String,a)]- -> MacroEnv- -> MacroID- -> MacroDescriptor- -> MacroDescriptor-runTests' rty parser vector env mid md@MacroDescriptor{..} =- md { _md_test_results = test_results }- where- test_results = concat- [ concat $ map test vector- , concat $ map test_neg _md_counter_samples- ]-- test (src,x) = test' mid rty parser x $ match_ src env md-- test_neg src = test_neg' mid rty parser $ match_ src env md-- match_ = case rty of- TDFA -> match_tdfa- PCRE -> match_pcre-\end{code}---dumpMacroTable, formatMacroTable, formatMacroSummary, formatMacroSources, formatMacroSource----------------------------------------------------------------------------------------------\begin{code}-dumpMacroTable :: String -> RegexType -> MacroEnv -> IO ()-dumpMacroTable lab rty m_env = do- writeFile fp_t $ formatMacroTable rty m_env- writeFile fp_s $ formatMacroSources rty ExclCaptures m_env- where- fp_t = "docs/" ++ rty_s ++ "-" ++ lab ++ ".txt"- fp_s = "docs/" ++ rty_s ++ "-" ++ lab ++ "-src.txt"-- rty_s = map toLower $ show rty-\end{code}--\begin{code}-formatMacroTable :: RegexType -> MacroEnv -> String-formatMacroTable rty env = unlines $- format_table macro_table_hdr- [ macro_table_row rty mid md- | (mid,md) <- L.sortBy (comparing fst) $ HML.toList env- ]-\end{code}--\begin{code}-formatMacroSummary :: RegexType -> MacroEnv -> MacroID -> String-formatMacroSummary rty env mid = maybe oops prep $ HML.lookup mid env- where- prep :: MacroDescriptor -> String- prep md = unlines $ concat $ map (fmt md) [minBound..maxBound]-- fmt :: MacroDescriptor -> Col -> [String]- fmt md c =- [ printf "%-15s : %s" (present_col c) ini- ] ++ map (" "++) lns- where- (ini,lns) = case macro_attribute rty mid md c of- [] -> (,) "" []- [ln] -> (,) ln []- lns_ -> (,) "" lns_-- oops = error $ _MacroID mid ++ ": macro not defined in this environment"-\end{code}--\begin{code}-formatMacroSources :: RegexType- -> WithCaptures- -> MacroEnv- -> String-formatMacroSources rty wc env = unlines $- [ printf "%-20s : %s" (_MacroID mid) $ formatMacroSource rty wc env mid- | mid <- L.sort $ HML.keys env- ]-\end{code}--\begin{code}-formatMacroSource :: RegexType- -> WithCaptures- -> MacroEnv- -> MacroID- -> String-formatMacroSource rty wc env mid =- mdRegexSource rty wc env $ fromMaybe oops $ HML.lookup mid env- where- oops = error $ "formatMacroSource: not found: " ++ _MacroID mid-\end{code}---testMacroDescriptors, regexSource------------------------------------\begin{code}-testMacroDescriptors :: [MacroDescriptor] -> [TestResult]-testMacroDescriptors = concat . map _md_test_results--regexSource :: RegexType -> WithCaptures -> RegexSource -> String-regexSource rty wc = format_tokens rty wc . scan_re-\end{code}---Formatting helpers---------------------\begin{code}-type TableRow = Array Col [String]--data Col- = C_name- | C_caps- | C_regex- | C_examples- | C_anti_examples- | C_fails- | C_parser- | C_comment- deriving (Ix,Bounded,Enum,Ord,Eq,Show)--present_col :: Col -> String-present_col = map tr . drop 2 . show- where- tr '_' = '-'- tr c = c--macro_table_hdr :: TableRow-macro_table_hdr = listArray (minBound,maxBound)- [ [present_col c]- | c<-[minBound..maxBound]- ]--macro_table_row :: RegexType -> MacroID -> MacroDescriptor -> TableRow-macro_table_row rty mid md =- listArray (minBound,maxBound) $- map (macro_attribute rty mid md) [minBound..maxBound]--macro_attribute :: RegexType- -> MacroID- -> MacroDescriptor- -> Col- -> [String]-macro_attribute rty mid MacroDescriptor{..} c =- case c of- C_name -> [_MacroID mid]- C_caps -> [show $ min_captures rty $ scan_re _md_source]- C_regex -> [regexSource rty ExclCaptures _md_source]- C_examples -> _md_samples- C_anti_examples -> _md_counter_samples- C_fails -> map _TestResult _md_test_results- C_parser -> [maybe "-" _FunctionID _md_parser]- C_comment -> [_md_description]--format_table :: TableRow -> [TableRow] -> [String]-format_table hdr rows0 = concat- [ format_row cws hdr'- , format_row cws dsh- , concat $ map (format_row cws) rows- ]- where- dsh = listArray (minBound,maxBound)- [ [replicate n '-'] | n<-elems cws ]-- hdr' = hdr // [(,) C_regex $ [take n $ concat $ repeat "regex="] ]- where- n = min 29 $ cws!C_regex-- cws = widths $ hdr : rows-- rows = map wrap_row rows0--field_width :: Int-field_width = 40--wrap_row :: TableRow -> TableRow-wrap_row = fmap $ concat . map f- where- f, g :: String -> [String]-- f cts = (ini ++ ['\\' | not (null rst)]) : g rst- where- (ini,rst) = splitAt (1+field_width) cts-- g "" = []- g cts = ('\\' : ini ++ ['\\' | not (null rst)]) : g rst- where- (ini,rst) = splitAt field_width cts---widths :: [TableRow] -> Array Col Int-widths rows = listArray (minBound,maxBound)- [ maximum $ concat [ map length $ row!c | row<-rows ]- | c<-[minBound..maxBound]- ]--format_row :: Array Col Int -> TableRow -> [String]-format_row cw_arr row =- [ ("|"++) $ L.intercalate "|"- [ field cw_arr row c i | c<-[minBound..maxBound] ]- | i <- [0..depth-1]- ]- where- depth = maximum [ length $ row!c | c<-[minBound..maxBound] ]--field :: Array Col Int -> TableRow -> Col -> Int -> String-field cws row c i = ljust (cws!c) $ sel i $ row!c--sel :: Int -> [String] -> String-sel i ss = case drop i ss of- [] -> ""- s:_ -> s--ljust :: Int -> String -> String-ljust w s = s ++ replicate n ' '- where- n = max 0 $ w - length s--min_captures :: RegexType -> [REToken] -> Int-min_captures rty rets = length- [ ()- | REToken{..}<-rets- , _ret_fixed || (_ret_grouping && rty==TDFA)- ]-\end{code}---Formatting tokens--------------------\begin{code}-format_tokens :: RegexType -> WithCaptures -> [REToken] -> String-format_tokens rty wc = foldr f ""- where- f REToken{..} rst = _ret_prefix ++ bra ++ xket rst- where- bra = case _ret_fixed of- True -> "("- False ->- case (,) _ret_grouping (_ret_capturing && wc==InclCaptures) of- (False,False) -> ""- (True ,False) -> if rty==PCRE then "(?:" else "("- (False,True ) -> "("- (True ,True ) -> "("-- xket =- case not _ret_grouping && _ret_capturing && wc==ExclCaptures of- True -> delete_ket 0- False -> id--delete_ket :: Int -> String -> String-delete_ket _ "" = error "delete_ket: end of input"-delete_ket n (c:t) = case c of- '\\' -> case t of- "" -> error "delete_ket: end of input"- c':t' -> c : c' : delete_ket n t'- ')' -> case n of- 0 -> t- _ -> c : delete_ket (n-1) t- '(' -> c : delete_ket (n+1) t- _ -> c : delete_ket n t-\end{code}---scan_re----------\begin{code}-scan_re :: RegexSource -> [REToken]-scan_re (RegexSource src0) = loop src0- where- loop "" = []- loop src =- case rst of- '\\':t -> case t of- "" -> REToken (ini++['\\']) False False False : []- c':t' -> REToken (ini++['\\',c']) False False False : loop t'- '(' :t -> case t of- c:':':t'- | c=='?' -> REToken ini False True False : loop t'- | c=='}' -> REToken ini False False True : loop t'- | c==']' -> REToken ini False True True : loop t'- _ -> REToken ini True True True : loop t- _ -> [REToken src False False False]- where- (ini,rst) = break chk src-- chk '\\' = True- chk '(' = True- chk _ = False-\end{code}---scan_re----------\begin{code}-match_tdfa :: String -> MacroEnv -> MacroDescriptor -> Matches String-match_tdfa txt env md = txt TDFA.=~ mdRegexSource TDFA ExclCaptures env md--match_pcre :: String -> MacroEnv -> MacroDescriptor -> Matches String-match_pcre txt env md = txt PCRE.=~ mdRegexSource PCRE ExclCaptures env md-\end{code}---mdRegexSource----------------\begin{code}-mdRegexSource :: RegexType- -> WithCaptures- -> MacroEnv- -> MacroDescriptor- -> String-mdRegexSource rty wc env md =- expandMacros' lu $ regexSource rty wc $ _md_source md- where- lu = fmap (regexSource rty wc . _md_source) .- flip HML.lookup env-\end{code}---test', test_neg'-------------------\begin{code}-test' :: (Eq a,Show a)- => MacroID- -> RegexType- -> (Match String->Maybe a)- -> a- -> Matches String- -> [TestResult]-test' mid rty prs x Matches{..} = either (:[]) (const []) $ do- cs <- case allMatches of- [cs] -> return cs- _ -> oops "RE failed to parse"- mtx <- case matchCapture cs of- Nothing -> oops $ "RE parse failure: " ++ show cs- Just c -> return $ capturedText c- case mtx == matchesSource of- True -> return ()- False -> oops "RE failed to match the whole text"- x' <- case prs cs of- Nothing -> oops "matched text failed to parse"- Just x' -> return x'- case x'==x of- True -> return ()- False -> oops "parser failed to yield the expected result"- where- oops = Left . test_diagnostic mid False rty matchesSource--test_neg' :: MacroID- -> RegexType- -> (Match String->Maybe a)- -> Matches String- -> [TestResult]-test_neg' mid rty prs Matches{..} = either id (const []) $ do- case allMatches of- [] -> return ()- cz -> case ms of- [] -> return ()- _ -> Left [oops "RE parse succeeded"]- where- ms =- [ ()- | cs <- cz- , Just c <- [matchCapture cs]- , let t = capturedText c- , t == matchesSource- , isJust $ prs cs- ]-- where- oops = test_diagnostic mid True rty matchesSource--test_diagnostic :: MacroID- -> Bool- -> RegexType- -> String- -> String- -> TestResult-test_diagnostic mid is_neg rty tst msg =- TestResult $- printf "%-20s [%s %s] : %s (%s)" mid_s neg_s rty_s msg tst- where- mid_s = _MacroID mid- neg_s = if is_neg then "-ve" else "+ve" :: String- rty_s = show rty-\end{code}
− Text/RE/Tools/Grep.lhs
@@ -1,62 +0,0 @@-\begin{code}-{-# LANGUAGE NoImplicitPrelude #-}-{-# LANGUAGE RecordWildCards #-}-{-# LANGUAGE FlexibleContexts #-}-{-# LANGUAGE CPP #-}--module Text.RE.Tools.Grep- ( Line(..)- , grep- , grepLines- , GrepScript- , grepScript- , linesMatched- ) where--import qualified Data.ByteString.Lazy.Char8 as LBS-import Prelude.Compat-import Text.Printf-import Text.RE.Capture-import Text.RE.IsRegex-import Text.RE.LineNo---data Line =- Line- { _ln_no :: LineNo- , _ln_matches :: Matches LBS.ByteString- }- deriving (Show)--grep :: IsRegex re LBS.ByteString => re -> FilePath -> IO ()-grep rex fp = grepLines rex fp >>= putStr . report--grepLines :: IsRegex re LBS.ByteString => re -> FilePath -> IO [Line]-grepLines rex fp =- grepScript [(rex,mk)] . LBS.lines <$> LBS.readFile fp- where- mk i mtchs = Just $ Line i mtchs--type GrepScript re s t = [(re,LineNo -> Matches s -> Maybe t)]--grepScript :: IsRegex re s => GrepScript re s t -> [s] -> [t]-grepScript scr = loop firstLine- where- loop _ [] = []- loop i (ln:lns) = seq i $ choose i ln lns scr-- choose i _ lns [] = loop (succ i) lns- choose i ln lns ((rex,f):scr') = case f i $ matchMany rex ln of- Nothing -> choose i ln lns scr'- Just t -> t : loop (succ i) lns--report :: [Line] -> String-report = unlines . map fmt . linesMatched- where- fmt Line{..} =- printf "%05d %s" (getLineNo _ln_no) $- LBS.unpack $ matchesSource _ln_matches--linesMatched :: [Line] -> [Line]-linesMatched = filter $ anyMatches . _ln_matches-\end{code}
− Text/RE/Tools/Lex.lhs
@@ -1,39 +0,0 @@-\begin{code}-{-# LANGUAGE NoImplicitPrelude #-}--module Text.RE.Tools.Lex where--import Prelude.Compat-import Text.RE.Capture-import Text.RE.IsRegex-import Text.RE.Replace---alex :: IsRegex re s => [(re,Match s->Maybe t)] -> t -> s -> [t]-alex = alex' matchOnce--alex' :: Replace s- => (re->s->Match s)- -> [(re,Match s->Maybe t)]- -> t- -> s- -> [t]-alex' mo al t_err = loop- where- loop s = case length_ s == 0 of- True -> []- False -> choose al s-- choose [] _ = [t_err]- choose ((re,f):al') s = case mb_p of- Just (s',t) -> t : loop s'- _ -> choose al' s- where- mb_p = do- cap <- matchCapture mtch- case captureOffset cap == 0 of- True -> (,) (captureSuffix cap) <$> f mtch- False -> Nothing-- mtch = mo re s-\end{code}
− Text/RE/Tools/Sed.lhs
@@ -1,55 +0,0 @@-\begin{code}-{-# LANGUAGE NoImplicitPrelude #-}-{-# LANGUAGE RecordWildCards #-}-{-# LANGUAGE FlexibleContexts #-}-{-# LANGUAGE CPP #-}-#if __GLASGOW_HASKELL__ >= 800-{-# OPTIONS_GHC -fno-warn-redundant-constraints #-}-#endif--module Text.RE.Tools.Sed- ( SedScript- , sed- , sed'- ) where--import qualified Data.ByteString.Lazy.Char8 as LBS-import Prelude.Compat-import Text.RE.Edit-import Text.RE.LineNo-import Text.RE.IsRegex---type SedScript re = Edits IO re LBS.ByteString--sed :: IsRegex re LBS.ByteString- => SedScript re- -> FilePath- -> FilePath- -> IO ()-sed as i_fp o_fp = do- lns <- LBS.lines <$> read_file i_fp- lns' <- sequence- [ applyEdits lno as s- | (lno,s)<-zip [firstLine..] lns- ]- write_file o_fp $ LBS.concat lns'--sed' :: (IsRegex re LBS.ByteString,Monad m,Functor m)- => Edits m re LBS.ByteString- -> LBS.ByteString- -> m LBS.ByteString-sed' as lbs = do- LBS.concat <$> sequence- [ applyEdits lno as s- | (lno,s)<-zip [firstLine..] $ LBS.lines lbs- ]--read_file :: FilePath -> IO LBS.ByteString-read_file "-" = LBS.getContents-read_file fp = LBS.readFile fp--write_file :: FilePath -> LBS.ByteString ->IO ()-write_file "-" = LBS.putStr-write_file fp = LBS.writeFile fp-\end{code}
changelog view
@@ -1,5 +1,8 @@ -*-change-log-*- +0.2.0.1 Chris Dornan <chris.dornan@irisconnect.co.uk> 2017-02-20+ * remove library from regex-examples (#43)+ 0.2.0.0 Chris Dornan <chris.dornan@irisconnect.co.uk> 2017-02-19 * Split off the tutorial tests and examples into regex-examples, leaving just the library in regex
lib/cabal-masters/regex-examples.cabal view
@@ -34,8 +34,6 @@ %include "lib/cabal-masters/constraints-incl.cabal" -%include "lib/cabal-masters/library-incl.cabal"- %include "lib/cabal-masters/executables-incl.cabal" -- Generated from lib/cabal-masters/mega-regex with re-gen-cabals
lib/mega-regex.cabal view
@@ -1,5 +1,5 @@ Name: regex-Version: 0.2.0.0+Version: 0.2.0.1 Synopsis: Toolkit for regex-base Description: A Regular Expression Toolkit for regex-base with Compile-time checking of RE syntax, data types for@@ -62,7 +62,7 @@ Source-Repository this Type: git Location: https://github.com/iconnect/regex.git- Tag: 0.2.0.0+ Tag: 0.2.0.1 @@ -143,7 +143,7 @@ -fwarn-tabs Build-depends:- regex == 0.2.0.0+ regex == 0.2.0.1 , array >= 0.4 , base >= 4 && < 5 , base-compat >= 0.6.0@@ -171,7 +171,7 @@ -fwarn-tabs Build-depends:- regex == 0.2.0.0+ regex == 0.2.0.1 , array >= 0.4 , base >= 4 && < 5 , base-compat >= 0.6.0@@ -196,7 +196,7 @@ -fwarn-tabs Build-depends:- regex == 0.2.0.0+ regex == 0.2.0.1 , array >= 0.4 , base >= 4 && < 5 , base-compat >= 0.6.0@@ -220,7 +220,7 @@ -fwarn-tabs Build-depends:- regex == 0.2.0.0+ regex == 0.2.0.1 , array >= 0.4 , base >= 4 && < 5 , base-compat >= 0.6.0@@ -247,7 +247,7 @@ TestKit Build-depends:- regex == 0.2.0.0+ regex == 0.2.0.1 , base >= 4 && < 5 , base-compat >= 0.6.0 , bytestring >= 0.10.2.0@@ -271,7 +271,7 @@ TestKit Build-depends:- regex == 0.2.0.0+ regex == 0.2.0.1 , base >= 4 && < 5 , base-compat >= 0.6.0 , bytestring >= 0.10.2.0@@ -292,7 +292,7 @@ -fwarn-tabs Build-depends:- regex == 0.2.0.0+ regex == 0.2.0.1 , array >= 0.4 , base >= 4 && < 5 , base-compat >= 0.6.0@@ -320,7 +320,7 @@ -fwarn-tabs Build-depends:- regex == 0.2.0.0+ regex == 0.2.0.1 , array >= 0.4 , base >= 4 && < 5 , base-compat >= 0.6.0@@ -351,7 +351,7 @@ TestKit Build-depends:- regex == 0.2.0.0+ regex == 0.2.0.1 , base >= 4 && < 5 , base-compat >= 0.6.0 , bytestring >= 0.10.2.0@@ -377,7 +377,7 @@ TestKit Build-depends:- regex == 0.2.0.0+ regex == 0.2.0.1 , base >= 4 && < 5 , base-compat >= 0.6.0 , bytestring >= 0.10.2.0@@ -403,7 +403,7 @@ TestKit Build-depends:- regex == 0.2.0.0+ regex == 0.2.0.1 , array >= 0.4 , base >= 4 && < 5 , base-compat >= 0.6.0@@ -439,7 +439,7 @@ TestKit Build-depends:- regex == 0.2.0.0+ regex == 0.2.0.1 , array >= 0.4 , base >= 4 && < 5 , base-compat >= 0.6.0@@ -476,7 +476,7 @@ -fwarn-tabs Build-depends:- regex == 0.2.0.0+ regex == 0.2.0.1 , array >= 0.4 , base >= 4 && < 5 , base-compat >= 0.6.0@@ -518,7 +518,7 @@ -fwarn-tabs Build-depends:- regex == 0.2.0.0+ regex == 0.2.0.1 , array >= 0.4 , base >= 4 && < 5 , base-compat >= 0.6.0@@ -562,7 +562,7 @@ -fwarn-tabs Build-depends:- regex == 0.2.0.0+ regex == 0.2.0.1 , array >= 0.4 , base >= 4 && < 5 , base-compat >= 0.6.0
lib/version.txt view
@@ -1,1 +1,1 @@-0.2.0.0+0.2.0.1
regex-examples.cabal view
@@ -1,5 +1,5 @@ Name: regex-examples-Version: 0.2.0.0+Version: 0.2.0.1 Synopsis: Tutorial, tests and example programs for regex Description: Tutorial, tests and example programs for regex, a Regular Expression Toolkit for regex-base with@@ -63,70 +63,7 @@ Source-Repository this Type: git Location: https://github.com/iconnect/regex.git- Tag: 0.2.0.0----Library- Hs-Source-Dirs: .- Exposed-Modules:- Text.RE- Text.RE.Capture- Text.RE.CaptureID- Text.RE.Edit- Text.RE.Internal.NamedCaptures- Text.RE.Internal.PreludeMacros- Text.RE.Internal.QQ- Text.RE.IsRegex- Text.RE.LineNo- Text.RE.Options- Text.RE.Parsers- Text.RE.PCRE- Text.RE.PCRE.ByteString- Text.RE.PCRE.ByteString.Lazy- Text.RE.PCRE.RE- Text.RE.PCRE.Sequence- Text.RE.PCRE.String- Text.RE.Replace- Text.RE.TDFA- Text.RE.TDFA.ByteString- Text.RE.TDFA.ByteString.Lazy- Text.RE.TDFA.RE- Text.RE.TDFA.Sequence- Text.RE.TDFA.String- Text.RE.TDFA.Text- Text.RE.TDFA.Text.Lazy- Text.RE.TestBench- Text.RE.Tools.Grep- Text.RE.Tools.Lex- Text.RE.Tools.Sed-- Other-Modules:- Text.RE.Internal.AddCaptureNames-- Default-Language: Haskell2010- GHC-Options:- -Wall- -fwarn-tabs-- Build-depends:- array >= 0.4- , base >= 4 && < 5- , base-compat >= 0.6.0- , bytestring >= 0.10.2.0- , containers >= 0.4- , hashable >= 1.2.3.3- , heredoc >= 0.2.0.0- , regex-base >= 0.93.2- , regex-pcre-builtin >= 0.94.4.8.8.35- , regex-tdfa >= 1.2.0- , regex-tdfa-text >= 1.0.0.3- , template-haskell >= 2.7- , text >= 1.2.0.6- , time >= 1.4.2- , time-locale-compat >= 0.1.0.1- , transformers >= 0.2.2- , unordered-containers >= 0.2.5.1+ Tag: 0.2.0.1 @@ -144,7 +81,7 @@ -fwarn-tabs Build-depends:- regex == 0.2.0.0+ regex == 0.2.0.1 , array >= 0.4 , base >= 4 && < 5 , base-compat >= 0.6.0@@ -172,7 +109,7 @@ -fwarn-tabs Build-depends:- regex == 0.2.0.0+ regex == 0.2.0.1 , array >= 0.4 , base >= 4 && < 5 , base-compat >= 0.6.0@@ -197,7 +134,7 @@ -fwarn-tabs Build-depends:- regex == 0.2.0.0+ regex == 0.2.0.1 , array >= 0.4 , base >= 4 && < 5 , base-compat >= 0.6.0@@ -221,7 +158,7 @@ -fwarn-tabs Build-depends:- regex == 0.2.0.0+ regex == 0.2.0.1 , array >= 0.4 , base >= 4 && < 5 , base-compat >= 0.6.0@@ -248,7 +185,7 @@ TestKit Build-depends:- regex == 0.2.0.0+ regex == 0.2.0.1 , base >= 4 && < 5 , base-compat >= 0.6.0 , bytestring >= 0.10.2.0@@ -272,7 +209,7 @@ TestKit Build-depends:- regex == 0.2.0.0+ regex == 0.2.0.1 , base >= 4 && < 5 , base-compat >= 0.6.0 , bytestring >= 0.10.2.0@@ -293,7 +230,7 @@ -fwarn-tabs Build-depends:- regex == 0.2.0.0+ regex == 0.2.0.1 , array >= 0.4 , base >= 4 && < 5 , base-compat >= 0.6.0@@ -321,7 +258,7 @@ -fwarn-tabs Build-depends:- regex == 0.2.0.0+ regex == 0.2.0.1 , array >= 0.4 , base >= 4 && < 5 , base-compat >= 0.6.0@@ -352,7 +289,7 @@ TestKit Build-depends:- regex == 0.2.0.0+ regex == 0.2.0.1 , base >= 4 && < 5 , base-compat >= 0.6.0 , bytestring >= 0.10.2.0@@ -378,7 +315,7 @@ TestKit Build-depends:- regex == 0.2.0.0+ regex == 0.2.0.1 , base >= 4 && < 5 , base-compat >= 0.6.0 , bytestring >= 0.10.2.0@@ -404,7 +341,7 @@ TestKit Build-depends:- regex == 0.2.0.0+ regex == 0.2.0.1 , array >= 0.4 , base >= 4 && < 5 , base-compat >= 0.6.0@@ -440,7 +377,7 @@ TestKit Build-depends:- regex == 0.2.0.0+ regex == 0.2.0.1 , array >= 0.4 , base >= 4 && < 5 , base-compat >= 0.6.0@@ -477,7 +414,7 @@ -fwarn-tabs Build-depends:- regex == 0.2.0.0+ regex == 0.2.0.1 , array >= 0.4 , base >= 4 && < 5 , base-compat >= 0.6.0@@ -519,7 +456,7 @@ -fwarn-tabs Build-depends:- regex == 0.2.0.0+ regex == 0.2.0.1 , array >= 0.4 , base >= 4 && < 5 , base-compat >= 0.6.0@@ -563,7 +500,7 @@ -fwarn-tabs Build-depends:- regex == 0.2.0.0+ regex == 0.2.0.1 , array >= 0.4 , base >= 4 && < 5 , base-compat >= 0.6.0