module Language.HaLex.RegExpParser ( parseRegExp
) where
import Data.Char
import Language.HaLex.Parser
import Language.HaLex.RegExp
parseRegExp :: [Char]
-> Maybe (RegExp Char)
parseRegExp re = res
where parsed_re = expr re
res | parsed_re == [] = Nothing
| otherwise = Just (fst (head parsed_re))
expr :: Parser Char (RegExp Char)
expr = f <$> termThen <*> symbol '|' <*> expr
<|> id <$> termThen
<|> succeed Epsilon
where f l _ r = Or l r
termThen :: Parser Char (RegExp Char)
termThen = f <$> term <*> termThen
<|> id <$> term
where f l r = Then l r
term :: Parser Char (RegExp Char)
term = f <$> factor <*> symbol '?'
<|> g <$> factor <*> symbol '*'
<|> h <$> factor <*> symbol '+'
<|> id <$> factor
where
f e _ = Or e Epsilon
g e _ = Star e
h e _ = Then e (Star e)
factor :: Parser Char (RegExp Char)
factor = f <$> letterOrDigit
<|> g <$> symbol '\'' <*> satisfy (\ x -> True) <*> symbol '\''
<|> h <$> symbol '(' <*> expr <*> symbol ')'
where
f a = Literal a
g _ e _ = Literal e
h _ e _ = e
letterOrDigit :: Parser Char Char
letterOrDigit = satisfy (\x -> isDigit x || isAlpha x)
spaces :: Parser Char ()
spaces = (\ _ _ -> ()) <$> symbol ' ' <*> spaces
<|> succeed ()