1 -- |
    2 -- Module      : Text.RegExp.Matching.Leftmost
    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 matching based on weighted regular
    9 -- expressions. It should be imported qualified as the interface
   10 -- resembles that provided by other matching modules.
   11 -- 
   12 module Text.RegExp.Matching.Leftmost (
   13 
   14   matching, 
   15 
   16   Matching, matchingIndex,
   17 
   18   Leftmost, getLeftmost
   19 
   20   ) where
   21 
   22 import Text.RegExp
   23 import Text.RegExp.Matching.Leftmost.Type
   24 
   25 -- |
   26 -- A 'Matching' records the leftmost start index of a matching subword.
   27 -- 
   28 data Matching = Matching {
   29  
   30   -- | Start index of the matching subword in the queried word.
   31   matchingIndex :: !Int
   32  
   33   }
   34  deriving Eq
   35 
   36 instance Show Matching
   37  where
   38   showsPrec _ m = showString "<index:" . shows (matchingIndex m)
   39                 . showString ">"
   40 
   41 -- |
   42 -- Returns the leftmost of all non-empty matchings for a regular
   43 -- expression in a given word. If the empty word is the only matching
   44 -- its position is zero.
   45 -- 
   46 matching :: RegExp c -> [c] -> Maybe Matching
   47 matching r = getLeftmost . partialMatch r . zip [(0::Int)..]
   48 
   49 getLeftmost :: Leftmost -> Maybe Matching
   50 getLeftmost Zero          =  Nothing
   51 getLeftmost One           =  Just $ Matching 0 
   52 getLeftmost (Leftmost x)  =  Just $ Matching x