1 -- | 2 -- Module : Text.RegExp.Matching.LeftLong 3 -- Copyright : Thomas Wilke, Frank Huch, and Sebastian Fischer 4 -- License : BSD3 5 -- Maintainer : Sebastian Fischer <mailto:sebf@informatik.uni-kiel.de> 6 -- Stability : experimental 7 -- 8 -- This module implements leftmost longest matching based on weighted 9 -- regular expressions. It should be imported qualified as the 10 -- interface resembles that provided by other matching modules. 11 -- 12 module Text.RegExp.Matching.LeftLong ( 13 14 matching, 15 16 Matching, matchingIndex, matchingLength, 17 18 LeftLong, getLeftLong 19 20 ) where 21 22 import Text.RegExp 23 import Text.RegExp.Matching.LeftLong.Type 24 25 -- | 26 -- Subwords of words that match a regular expression are represented 27 -- as values of type 'Matching'. 28 -- 29 data Matching = Matching { 30 31 -- | Start index of the matching subword in the queried word. 32 matchingIndex :: !Int, 33 34 -- | Length of the matching subword. 35 matchingLength :: !Int 36 37 } 38 deriving Eq 39 40 instance Show Matching 41 where 42 showsPrec _ m = showString "<index:" . shows (matchingIndex m) 43 . showString " length:" . shows (matchingLength m) 44 . showString ">" 45 46 -- | 47 -- Returns the leftmost longest of all non-empty matchings for a 48 -- regular expression in a given word. If the empty word is the only 49 -- matching its position is zero. 50 -- 51 matching :: RegExp c -> [c] -> Maybe Matching 52 matching r = getLeftLong . partialMatch r . zip [(0::Int)..] 53 54 getLeftLong :: LeftLong -> Maybe Matching 55 getLeftLong Zero = Nothing 56 getLeftLong One = Just $ Matching 0 0 57 getLeftLong (LeftLong x y) = Just $ Matching x (y-x+1) 58