module Language.HaLex.Parser where
infixl 3 <|> ; infixl 4 <*>
type Parser s r = [s] -> [(r,[s])]
symbol :: Eq a => a -> Parser a a
symbol _ [] = []
symbol s (x:xs) | x == s = [(s,xs)]
| otherwise = []
satisfy :: (s -> Bool) -> Parser s s
satisfy p [] = []
satisfy p (x:xs) | p x = [(x,xs)]
| otherwise = []
token :: Eq s => [s] -> Parser s [s]
token k xs | k == take n xs = [(k,drop n xs)]
| otherwise = []
where n = length k
succeed :: a -> Parser s a
succeed r xs = [(r,xs)]
(<|>) :: Parser s a -> Parser s a -> Parser s a
(p <|> q) xs = p xs ++ q xs
(p <*> q) xs = [ ( f z , zs )
| ( f , ys ) <- p xs
, ( z , zs ) <- q ys
]
(f <$> p) xs = [(f y, ys) | (y,ys) <- p xs ]