-
Notifications
You must be signed in to change notification settings - Fork 16
/
parser.hs
187 lines (156 loc) · 3.18 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
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import Control.Applicative
import Data.Char
newtype Parser a =
P (String -> [(a, String)])
parse :: Parser a -> String -> [(a, String)]
parse (P p) inp = p inp
item :: Parser Char
item =
P (\inp ->
case inp of
[] -> []
(x:xs) -> [(x, xs)])
instance Functor Parser
-- fmap :: (a -> b) -> Parser a -> Parser b
where
fmap g p =
P
(\inp ->
case parse p inp of
[] -> []
[(v, out)] -> [(g v, out)])
instance Applicative Parser
-- pure :: a -> Parser a
where
pure v = P (\inp -> [(v, inp)])
-- (<*>) :: Parser (a -> b) -> Parser a -> Parser b
pg <*> px =
P
(\inp ->
case parse pg inp of
[] -> []
[(g, out)] -> parse (fmap g px) out)
three :: Parser (Char, Char)
three = pure g <*> item <*> item <*> item
where
g x y z = (x, z)
instance Monad Parser
-- (>>=) :: Parser a -> (a -> Parser b) -> Parser b
where
p >>= f =
P
(\inp ->
case parse p inp of
[] -> []
[(v, out)] -> parse (f v) out)
three' :: Parser (Char, Char)
three' = do
x <- item
item
z <- item
return (x, z)
instance Alternative Parser
-- empty :: Parser a
where
empty = P (\inp -> [])
-- (<|>) :: Parser a -> Parser a -> Parser a
p <|> q =
P
(\inp ->
case parse p inp of
[] -> parse q inp
[(v, out)] -> [(v, out)])
sat :: (Char -> Bool) -> Parser Char
sat p = do
x <- item
if p x
then return x
else empty
digit :: Parser Char
digit = sat isDigit
lower :: Parser Char
lower = sat isLower
upper :: Parser Char
upper = sat isUpper
letter :: Parser Char
letter = sat isAlpha
alphanum :: Parser Char
alphanum = sat isAlphaNum
char :: Char -> Parser Char
char x = sat (== x)
string :: String -> Parser String
string [] = return []
string (x:xs) = do
char x
string xs
return (x : xs)
ident :: Parser String
ident = do
x <- lower
xs <- many alphanum
return (x : xs)
nat :: Parser Int
nat = do
xs <- some digit
return (read xs)
space :: Parser ()
space = do
many (sat isSpace)
return ()
int :: Parser Int
int =
do char '-'
n <- nat
return (-n)
<|> nat
token :: Parser a -> Parser a
token p = do
space
v <- p
space
return v
identifier :: Parser String
identifier = token ident
natural :: Parser Int
natural = token nat
integer :: Parser Int
integer = token int
symbol :: String -> Parser String
symbol xs = token (string xs)
nats :: Parser [Int]
nats = do
symbol "["
n <- natural
ns <-
many
(do symbol ","
natural)
symbol "]"
return (n : ns)
expr :: Parser Int
expr = do
t <- term
do symbol "+"
e <- expr
return (t + e)
<|> return t
term :: Parser Int
term = do
f <- factor
do symbol "*"
t <- term
return (f * t)
<|> return f
factor :: Parser Int
factor = do
symbol "("
e <- expr
symbol ")"
return e
<|> natural
eval :: String -> Int
eval xs =
case parse expr xs of
[(n, [])] -> n
[(_, out)] -> error ("Unused input " ++ out)
[] -> error "Invalid input"