{-# OPTIONS_GHC -fglasgow-exts #-} module Text.JRegex.Impl where import Array import Maybe -- | instances of this class may be used as regular expressions with this syntax. class RegexLike r a | r -> a where -- | Test whether the regex matches at all matchTest :: r -> [a] -> Bool -- | Count the number of times the regex matches matchCount :: r -> [a] -> Int -- | return all matches matchAll :: r -> [a] -> [(Array Int (Int,Int))] -- | match once matchOnce :: r -- ^ Regular Expression -> [a] -- ^ String to match -> Bool -- ^ Whether we are at the begining of the string (as opposed to continuing a previous match) -> Maybe (Array Int (Int,Int)) -- ^ array of matched expression matchShow :: r -> String -- ^ Used for error messages matchTest r xs = isJust (matchOnce r xs True) matchCount r xs = length (matchAll r xs) matchAll r xs = f 0 xs where f t xs = case matchOnce r xs (t == 0) of Nothing -> [] Just a | l == 0 -> [na] | otherwise -> na:f (t + x) (drop x xs) where (o,l) = a!0 x = o + l na = fmap (adj t) a adj t (x,y) = ((,) $! x + t) $! y matchShow _ = "Unknown"