module Text.RegExp.Matching.Leftmost (
Leftmost(..), Matching(..),
matching, getLeftmost
) where
import Text.RegExp
data Matching = Matching {
matchingIndex :: !Int
}
deriving Eq
instance Show Matching
where
showsPrec _ m = showString "<index:" . shows (matchingIndex m)
. showString ">"
matching :: RegExp c -> [c] -> Maybe Matching
matching r = getLeftmost . partialMatch r
data Leftmost = Zero | One | Leftmost !Int
deriving (Eq,Show)
getLeftmost :: Leftmost -> Maybe Matching
getLeftmost Zero = Nothing
getLeftmost One = Just $ Matching 0
getLeftmost (Leftmost x) = Just $ Matching x
instance Semiring Leftmost where
zero = Zero; one = One
Zero .+. y = y
x .+. Zero = x
One .+. y = y
x .+. One = x
Leftmost a .+. Leftmost b = Leftmost (min a b)
Zero .*. _ = Zero
_ .*. Zero = Zero
One .*. y = y
x .*. One = x
Leftmost a .*. Leftmost b = Leftmost (min a b)
instance Weight c (Int,c) Leftmost where
symWeight p (n,c) = p c .*. Leftmost n