This repository has been archived by the owner on Jul 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParser.hs
279 lines (226 loc) · 6.91 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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
{-|
Parser for Expr.
-}
module Parser (parseExpr) where
import Expr (Expr(Var, Lam, App, Let, Case), Var, Tag, Binding, Pat(Pat),
con, app, zero, suc, nil, cons, nat)
import Data.Char (isDigit, isAlpha, isLower, isUpper)
import Control.Applicative (Alternative, empty, (<|>), some, many)
import Text.Printf (printf)
-- | Represents how many characters are consumed within the parsed string.
type Chars = Int
-- | The result of parsing. It can be an Error or Done.
-- | Done contains the value, line, column and the tail to be parsed.
data ParserResult a = Error String | Done a Chars String
-- | The Parser type. A parser takes a String, process the appropiate
-- | parser, and returns a ParserResult.
newtype Parser a = Parser { parse :: String -> ParserResult a }
instance Functor Parser where
fmap f (Parser parse) = Parser (\s ->
case parse s of
Error msg -> Error msg
Done a chars rest -> Done (f a) chars rest
)
instance Applicative Parser where
pure a = Parser (Done a 0)
(Parser pf) <*> (Parser p) = Parser (\s ->
case pf s of
Error msg -> Error msg
Done f chars rest -> case p rest of
Error msg' -> Error msg'
Done a chars' rest' -> Done (f a) (chars+chars') rest'
)
instance Monad Parser where
return = pure
(>>=) (Parser p) f = Parser (\s ->
case p s of
Error msg -> Error msg
Done a chars rest -> case parse (f a) rest of
Error msg' -> Error msg'
Done b chars' rest' -> Done b (chars+chars') rest'
)
instance Alternative Parser where
empty = Parser (const (Error "Empty parser"))
(<|>) (Parser p) (Parser q) = Parser (\s ->
case p s of
Error msg'p -> case q s of
Error msg'q -> Error (msg'p ++ " or " ++ msg'q)
done -> done
done -> done
)
-- | Parses a char.
item :: Parser Char
item = Parser (\s -> case s of
[] -> Error "Reached EOF"
(c:cs) -> Done c 1 cs)
satisfy :: String -> (Char -> Bool) -> Parser Char
satisfy msg pred = (>>=) item (\c ->
if pred c
then return c
else Parser (const (Error (printf "expecting %s got %c" msg c))))
oneOf :: String -> Parser Char
oneOf s = satisfy s (`elem` s)
char :: Char -> Parser Char
char c = satisfy [c] (c ==)
natural :: Parser Integer
natural = fmap read (some (satisfy "isDigit" isDigit))
string :: String -> Parser String
string [] = return []
string (c:cs) = do { char c; string cs; return (c:cs)}
token :: Parser a -> Parser a
token p = do { a <- p; spaces ; return a}
reserved :: String -> Parser String
reserved s = token (string s)
spaces :: Parser String
spaces = many $ oneOf " \n\r"
digit :: Parser Char
digit = satisfy "isdigit" isDigit
alpha :: Parser Char
alpha = satisfy "isalpha" isAlpha
loweralpha :: Parser Char
loweralpha = satisfy "loweralpha" (\c -> isAlpha c && isLower c)
upperalpha :: Parser Char
upperalpha = satisfy "upperalpha" (\c -> isAlpha c && isUpper c)
dollar :: Parser Char
dollar = satisfy "dollar sign" (== '$')
underscore :: Parser Char
underscore = satisfy "underscore" (== '_')
quote :: Parser Char
quote = satisfy "quote" (== '\'')
number :: Parser Int
number = do
s <- string "-" <|> return []
cs <- some digit
spaces
return (read (s ++ cs))
lowerword :: Parser String
lowerword = do
c <- loweralpha
cs <- many alpha
spaces
return (c:cs)
upperword :: Parser String
upperword = do
c <- upperalpha
cs <- many alpha
spaces
return (c:cs)
sat :: String -> Parser String -> (String -> Bool) -> Parser String
sat msg p pred = (>>=) p (\s -> if pred s
then return s
else Parser (const (Error (printf "expecting %s got %s" msg s))))
parens :: Parser a -> Parser a
parens m = do { reserved "("; n <- m; reserved ")"; return n }
braces :: Parser a -> Parser a
braces m = do { reserved "{"; n <- m; reserved "}"; return n }
brackets :: Parser a -> Parser a
brackets m = do { reserved "["; n <- m; reserved "]"; return n }
chainl1 :: Parser a -> Parser (a -> a -> a) -> Parser a
p `chainl1` op = do {a <- p; rest a}
where rest a = (do {f <- op; b <- p; rest (f a b)}) <|> return a
chainr1 :: Parser a -> Parser (a -> a -> a) -> Parser a
p `chainr1` op = do {a <- p; rest a}
where rest a = (do {f <- op; b <- p; b' <- rest b; return (f a b')})
<|> return a
fromRead :: Read a => Parser a
fromRead = Parser $ \s->
let readResult = reads s in
if null readResult || length readResult > 1
then Error "Read error"
else let [(res, cont)] = readResult in Done res 0 cont
exprp :: Parser (Expr Var Tag)
exprp = (termp `chainr1` conslistp) `chainl1` return App
conslistp :: Parser (Expr Var Tag -> Expr Var Tag -> Expr Var Tag)
conslistp = reserved ":" >> return (App . App cons)
termp :: Parser (Expr Var Tag)
termp = litintp
<|> letp
<|> casep
<|> varp
<|> conp
<|> braces lamp
<|> parens exprp
<|> brackets listp
litintp :: Parser (Expr v Tag)
litintp = do { n <- number; return (nat n) }
letp :: Parser (Expr Var Tag)
letp = do
reserved "let"
binds <- bindsp
--var <- varnamep
--reserved "="
--valexpr <- exprp
reserved "in"
inexpr <- exprp
return (Let binds inexpr)
bindsp :: Parser [Binding Var Tag]
bindsp = do
var <- varnamep
reserved "="
valexpr <- exprp
(do
reserved ";"
binds <- bindsp
return ((var, valexpr):binds) ) <|>
return [(var, valexpr)]
varp :: Parser (Expr Var Tag)
varp = do
var <- varnamep
--var <- vp
return (Var var)
--upperword
conp :: Parser (Expr Var Tag)
conp = do { tag <- upperword; return (con tag) }
lamp :: Parser (Expr Var Tag)
lamp = do
var <- varnamep
reserved "->"
valexpr <- exprp
return (Lam var valexpr)
casep :: Parser (Expr Var Tag)
casep = do
reserved "case"
scexpr <- exprp
reserved "of"
alts <- some altp
return (Case scexpr alts)
altp :: Parser (Pat Var Tag, Expr Var Tag)
altp = do
tag <- upperword
vars <- many varnamep
reserved "->"
res <- exprp
reserved ";"
return (Pat tag vars, res)
listp :: Parser (Expr Var Tag)
listp = (do
item <- exprp
(do
reserved ","
rest <- listp
return (app cons [item, rest])) <|>
return (app cons [item, nil])
) <|> return nil
varnamep :: Parser Var
varnamep = sat (show keywords) varid (not . (`elem` keywords))
where keywords = ["let", "in", "case", "of"]
varid :: Parser Var
varid = do
c <- loweralpha <|> dollar <|> underscore
cs <- many (alpha <|> digit <|> underscore <|> quote)
spaces
return (c:cs)
parseWith :: Show a => Parser a -> String -> a
parseWith p s =
case parse (do { spaces; p }) s of
Done a _ [] -> a
Done a chars rest -> error $
"Parser didn't consume entire stream: <<" ++ rest ++ ">> " ++
" in <<" ++ s ++ ">> at " ++ show chars ++
" with " ++ show a
Error msg -> error $ printf "Parser error: %s in ``%s''" msg s
{-|
Given a textual representation of Expr, returns the parsed Expr.
-}
parseExpr :: String -> Expr Var Tag
parseExpr = parseWith exprp