packages feed

skylighting-core-0.15.1 / src / Skylighting / Regex.hs

{-# LANGUAGE CPP                 #-}{-# LANGUAGE DeriveGeneric       #-}{-# LANGUAGE OverloadedStrings   #-}{-# LANGUAGE PatternSynonyms #-}{-# LANGUAGE ScopedTypeVariables #-}module Skylighting.Regex (                Regex(..)              , RE              , pattern RE, reCaseSensitive, reMinimal, reString              , compileRE              , compileRegex              , matchRegex              , matchRegexWithGroups              , testRegex              , isWordChar              ) whereimport Data.Aesonimport Data.Binary (Binary(..))import qualified Data.ByteString.Base64 as Base64import qualified Data.ByteString.Char8 as BSimport Data.Dataimport qualified Data.IntMap.Strict as Mimport qualified Data.Text as Textimport qualified Data.Text.Encoding as TE#if !MIN_VERSION_base(4,13,0)import Control.Monad.Fail (MonadFail)#endifimport Regex.KDEimport Text.Read hiding (get)-- | A representation of a regular expression.data RE = RE'{    _reString        :: BS.ByteString  , _reCaseSensitive :: Bool  , _reMinimal       :: Bool  , _reCompiled      :: Either String (Regex, M.IntMap Regex)}-- We define a smart constructor which also holds the compiled regex-- and its capturing groups, to avoid recomputing them each time we-- tokenize.{-# COMPLETE RE #-}pattern RE :: BS.ByteString -> Bool -> Bool -> REpattern RE {reString, reCaseSensitive, reMinimal} <-  RE' reString reCaseSensitive reMinimal _ where  RE str caseSensitive minimal =    RE' str caseSensitive minimal        (fmap (\r -> (r, extractCapturingGroups r))              (compileRegex caseSensitive minimal str))-- Unfortunately this means we need to derive all the instances ourselves.instance Show RE where  showsPrec d (RE str caseSensitive minimal) = showParen (d > 10)    $ showString "RE {reString = "    . showsPrec 11 str    . showString ", reCaseSensitive = "    . showsPrec 11 caseSensitive    . showString ", reMinimal = "    . showsPrec 11 minimal    . showString "}"instance Read RE where  readPrec = parens . prec 10 $ do    Ident "RE" <- lexP    Punc "{" <- lexP    Ident "reString" <- lexP    Punc "=" <- lexP    str <- readPrec    Punc "," <- lexP    Ident "reCaseSensitive" <- lexP    Punc "=" <- lexP    caseSensitive <- readPrec    Punc "," <- lexP    Ident "reMinimal" <- lexP    Punc "=" <- lexP    minimal <- readPrec    Punc "}" <- lexP    pure (RE str caseSensitive minimal)toComparisonKey :: RE -> (BS.ByteString, Bool, Bool)toComparisonKey (RE x y z) = (x, y, z)instance Eq RE where  x == y = toComparisonKey x == toComparisonKey yinstance Ord RE where  x `compare` y = toComparisonKey x `compare` toComparisonKey yconRE :: ConstrconRE = mkConstr tyRE "RE" [] PrefixtyRE :: DataTypetyRE   = mkDataType "Skylighting.Regex.RE" [conRE]instance Data RE where  gfoldl k z (RE s c m) = z RE `k` s `k` c `k` m  gunfold k z _ = k (k (k (z RE)))  toConstr _ = conRE  dataTypeOf _ = tyREinstance Binary RE where  put (RE x y z) = put x >> put y >> put z  get = RE <$> get <*> get <*> getinstance ToJSON RE where  toJSON re = object [ "reString"        .= encodeToText (reString re)                     , "reCaseSensitive" .= reCaseSensitive re                     , "reMinimal"       .= reMinimal re ]instance FromJSON RE where  parseJSON = withObject "RE" $ \v ->    RE <$> ((v .: "reString") >>= decodeFromText)       <*> v .: "reCaseSensitive"       <*> v .:? "reMinimal" .!= False-- functions to marshall bytestrings to textencodeToText :: BS.ByteString -> Text.TextencodeToText = TE.decodeUtf8 . Base64.encodedecodeFromText :: (Monad m, MonadFail m) => Text.Text -> m BS.ByteStringdecodeFromText = either fail return . Base64.decode . TE.encodeUtf8-- | The compiled regex and its capturing groups (cached in the-- 'RE' by the smart constructor).compileRE :: RE -> Either String (Regex, M.IntMap Regex)compileRE = _reCompiled