-
Notifications
You must be signed in to change notification settings - Fork 0
/
Parser.hs
45 lines (35 loc) · 1.1 KB
/
Parser.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
module Parser where
import Control.Monad
import Control.Applicative
newtype Parser a = Parser { parse :: String -> [(a, String)] }
instance Functor Parser where
fmap f (Parser p) = Parser $ \cs -> do
(a, as) <- p cs
return (f a, as)
instance Applicative Parser where
pure a = Parser $ \s -> [(a, s)]
(Parser f) <*> (Parser p) = Parser $ \cs -> do
(f', cs') <- f cs
(a, cs'') <- p cs'
return (f' a, cs'')
instance Monad Parser where
return = pure
(Parser p) >>= f = Parser $ \cs -> do
(a, cs') <- p cs
parse (f a) cs'
instance Alternative Parser where
empty = Parser $ const []
(Parser p) <|> (Parser q) = Parser $ \cs -> case p cs of
[] -> q cs
s -> s
instance MonadPlus Parser where
mzero = empty
mplus a b = Parser $ \cs -> parse a cs ++ parse b cs
item :: Parser Char
item = Parser $ \s -> case s of
"" -> []
(c:cs) -> [(c,cs)]
satisfy :: (Char -> Bool) -> Parser Char
satisfy p = item >>= \c -> if p c then return c else mzero
char :: Char -> Parser Char
char c = satisfy (==c)