module RegExD where

import RegExS

infixr 5 `Alt`
infixr 7 `Seq`

data RE = Eps
        | Chr Char
        | Str String
        | Arb
        | Seq RE RE
        | Alt RE RE
        | Star RE
        | Plus RE
        | Range [Char]
        deriving (Eq, Show)

intp :: RE-> RegEx
intp Eps     = eps
intp (Chr c) = char c
intp (Str s) = str s
intp Arb     = arb
intp (Seq re1 re2) = RegExS.seq (intp re1) (intp re2)
intp (Alt re1 re2) = alt (intp re1) (intp re2)
intp (Star re)     = star (intp re)
intp (Plus re)     = plus (intp re)
intp (Range cs)    = chrp (flip elem cs)

-- VERY inefficient!
searchAll :: RE-> String->
                     [String]
searchAll re str =
  case intp re str of 
    Just r  -> let (match, rest) = splitAt (length str- length r) str 
               in  if match == "" then if rest == "" then []
                                       else searchAll re (tail rest)
                   else match : searchAll re rest
    Nothing -> case str of ""  -> []
                           (_:rest) -> searchAll re rest 


