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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
|
module DetParse where
type Parser a = String -> [(a, String)]
parse :: Parser a -> String -> Maybe a
parse p s = case filter (null . snd) $ p s of
((x, _):_) -> Just x
_ -> Nothing
failure :: Parser a
failure = \_ -> []
yield :: a -> Parser a
yield x = \s -> [(x, s)]
empty :: Parser ()
empty = yield ()
anyChar :: Parser Char
anyChar = \s -> case s of
[] -> []
(c:cs) -> [(c,cs)]
check :: (a -> Bool) -> Parser a -> Parser a
check ok p = filter (ok . fst) . p
char :: Char -> Parser ()
char c = check (c==) anyChar *> yield ()
word :: String -> Parser ()
word [] = empty
word (c:cs) = char c *> word cs
infixl 3 <|>, <!>
(<|>) :: Parser a -> Parser a -> Parser a
p <|> q = \s -> p s ++ q s
(<!>) :: Parser a -> Parser a -> Parser a
p <!> q = \s -> case p s of
[] -> q s
xs -> xs
infixl 4 <$>
(<$>) :: (a -> b) -> Parser a -> Parser b
f <$> p = map (\(x, s) -> (f x, s)) . p
infixl 4 <*>, <*, *>
(<*>) :: Parser (a -> b) -> Parser a -> Parser b
p <*> q = \s -> [ (f x, s2) | (f, s1) <- p s,
(x, s2) <- q s1 ]
(<*) :: Parser a -> Parser b -> Parser a
p <* q = (\x _ -> x) <$> p <*> q
(*>) :: Parser a -> Parser b -> Parser b
p *> q = (\_ y -> y) <$> p <*> q
infixl 1 *>=
(*>=) :: Parser a -> (a -> Parser b) -> Parser b
p *>= f = \s -> [ (y, s2) | (x, s1) <- p s,
(y, s2) <- (f x) s1 ]
many :: Parser a -> Parser [a]
many p = some p <|> yield []
some :: Parser a -> Parser [a]
some p = (:) <$> p <*> many p
|