-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCoreParser.hs
360 lines (278 loc) · 9.9 KB
/
CoreParser.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
{-# LANGUAGE InstanceSigs #-}
{-# LANGUAGE ExistentialQuantification #-}
module CoreParser (
module Control.Applicative,
module Data.Char,
Parser,
Feedback(Result, ExpectedEof,UnexpectedEof,UnexpectedChar,UnexpectedString),
constantParser,
expectedEofParser, unexpectedEofParser,
unexpectedCharParser, unexpectedStringParser,
getResult,
(|||),
parse,
character,
list,
list1,
foldParser,
alpha, digit, letter,symbol,
alpha',digit',letter',
satisfy,
satisfy_S,
is,
space,spaces,spaces1,
oneOf,noneOf,
tok,charTok,stringTok,
string,string1,
pKeyWords,
liftA4,
sepByc1,sepByparser1,
betweenWithc,betweenWithparser,
endByParser
)where
import Control.Applicative
import Data.Char
type Tokens = String
data Feedback a =
Result Tokens a -- Result (剩余部分) 结果
| ExpectedEof Tokens
| UnexpectedEof
| UnexpectedChar Char
| UnexpectedString String
deriving Eq
data Parser a = Parser (Tokens -> Feedback a)
instance Show a => Show (Feedback a) where
show (Result t a)
= stringConcat ["Result >", t, "< ", show a]
show (ExpectedEof t)
= stringConcat ["Expected end of stream, but got >", show t, "<"]
show (UnexpectedEof) = "Unexpected end of stream"
show (UnexpectedChar c)
= stringConcat ["Unexpected character: ", show c]
show (UnexpectedString s)
= stringConcat ["Unexpected string: ", show s]
--只有处理正确的结果时,f 才会被成功应用
instance Functor Feedback where
{-
_ <$> err@(ExpectedEof _) = err
_ <$> err@(UnexpectedEof) = err
_ <$> err@(UnexpectedChar _) = err
_ <$> err@(UnexpectedString _) = err
f <$> (Result t a) = Result t (f a)
-}
fmap _ (ExpectedEof i) = ExpectedEof i
fmap _ UnexpectedEof = UnexpectedEof
fmap _ (UnexpectedChar c) = UnexpectedChar c
fmap _ (UnexpectedString s) = UnexpectedString s
fmap f (Result t a) = Result t (f a)
--判断是正确的结果还是产生了错误
isErrorResult :: Feedback a -> Bool
isErrorResult (Result _ _) = False
isErrorResult _ = True
onResult :: Feedback a -> (Tokens -> a -> Feedback b) -> Feedback b
onResult (ExpectedEof i) _ = ExpectedEof i
onResult UnexpectedEof _ = UnexpectedEof
onResult (UnexpectedChar c) _ = UnexpectedChar c
onResult (UnexpectedString s) _ = UnexpectedString s
onResult (Result t a) f = f t a
parse :: Parser a -> Tokens -> Feedback a
parse (Parser p) = p
expectedEofParser :: String -> Parser a
expectedEofParser s = constantParser $ ExpectedEof s
unexpectedEofParser :: Parser a
unexpectedEofParser = constantParser UnexpectedEof
unexpectedCharParser :: Char -> Parser a
unexpectedCharParser c = Parser (\_ -> UnexpectedChar c)
unexpectedStringParser :: String -> Parser a
unexpectedStringParser s = constantParser $ UnexpectedString s
getResult :: Feedback a -> a
getResult (Result _ x) = x
getResult _ = error "we need \"Result rs x\""
constantParser :: Feedback a -> Parser a
constantParser = Parser . const
instance Functor Parser where
-- fmap :: (a -> b) -> Parser a -> Parser b
-- fmap f per = Parser (\tok -> f <$> (parse per tok))
fmap :: (a -> b) -> Parser a -> Parser b
fmap f per = do r <- per
return $ f r
instance Applicative Parser where
pure :: a -> Parser a
pure = return
(<*>) :: Parser (a -> b) -> Parser a -> Parser b
(<*>) perF per = do f <- perF
r <- per
return $ f r
instance Monad Parser where
return :: a -> Parser a
return x = Parser (\tok -> Result tok x)
(>>=) :: Parser a -> (a -> Parser b) -> Parser b
(>>=) per f_per
= Parser (\tok ->
case parse per tok of
Result t a -> parse (f_per a) t
err -> onResult err (\x y -> parse (f_per y) x))
instance Alternative Parser where
empty :: Parser a
empty = Parser (\tok -> ExpectedEof tok)
(<|>) :: Parser a -> Parser a -> Parser a
(<|>) per1 per2 = Parser (\tok -> case parse per1 tok of
(Result t a) -> Result t a
_ -> parse per2 tok)
(|||) :: Parser a -> Parser a -> Parser a
(|||) per1 per2
= Parser (\tok ->
case parse per1 tok of
(Result t a) -> Result t a
_ -> parse per2 tok)
infix 3 |||
character :: Parser Char
character = Parser check
where
check (h:rma) = Result rma h
check _ = UnexpectedEof
--不会返回错误,一旦不匹配,返回[]
list :: Parser a -> Parser [a]
list per = do r <- per
(r:) <$> (list per)
<|> return []
--当一开始就出现不匹配时,返回错误
list1 :: Parser a -> Parser [a]
list1 per = do r <- per
(r:) <$> (list per)
satisfy :: (Char -> Bool) -> Parser Char
satisfy f = do c <- character
if f c
then return $ c
else unexpectedCharParser c
satisfy_S :: Int -> (String -> Bool) -> Parser String
satisfy_S n f = do s <- thisManyI n character
if f s
then return s
else return []
oneOf :: String -> Parser Char
oneOf cs = tok $ satisfy (\c -> c `elem` cs)
noneOf :: String -> Parser Char
noneOf cs = tok $ satisfy (\c -> not (c `elem` cs))
is :: Char -> Parser Char
is c = satisfy (== c)
space :: Parser Char
space = satisfy (== ' ')
spaces :: Parser String
spaces = list space
spaces1 :: Parser String
spaces1 = list1 space
thisManyI :: Int -> Parser a -> Parser [a]
thisManyI n per = do r <- per
make (n-1) (pure (r:))
where
make c mrt
| c == 0 = mrt >>= \rt -> return $ rt []
| otherwise = do rt <- mrt
r <- per
make (c-1) $ pure (\x -> rt (r:x))
tok :: Parser a -> Parser a
tok per = do spaces
r <- per
spaces
return r
charTok :: Char -> Parser Char
charTok c = tok (is c)
string1 :: String -> Parser String
string1 (h:rma) = do r <- is h
(r:) <$> string1 rma
string1 _ = return []
string :: String -> Parser String
string (h:rma) = do r <- is h
makeIt (pure (r:)) rma
<|> do c <- character
return [c]
where
makeIt mrs (h':rma') = do r <- is h'
makeIt (chng_rs r mrs) rma'
<|> do return (mrs >>= \rs -> rs [])
makeIt mrs _ = return (mrs >>= \rs -> rs [])
chng_rs c = liftA (\f -> (\x -> f (c:x)))
stringTok :: String -> Parser String
stringTok (h:rma) = do r <- charTok h
(r:) <$> stringTok rma
stringTok _ = return []
--Parser b是一个间隔符,因此,该函数的目的是针对含有间隔的对象,将间隔之间的对象放到一块儿
sepByparser1 :: Parser a -> Parser b -> Parser [a]
sepByparser1 per sep = do r <- liftA2 (\x _ -> x) per sep
(r:) <$> sepByparser1 per sep
<|> do r <- per
return [r]
sepByc1 :: Parser a -> Char -> Parser [a]
sepByc1 per sep = let pSep = charTok sep
in sepByparser1 per pSep
betweenWithparser :: Parser a -> Parser b -> Parser b -> Parser a
betweenWithparser per pleft pright = do pleft
r <- per
pright
return r
betweenWithc :: Parser a -> Char -> Char -> Parser a
betweenWithc per left right = let pl = charTok left
pr = charTok right
in do pl
r <- per
pr
return r
endByParser :: Parser a -> Parser [a] -> Parser [a]
endByParser per ped = do c <- per
makeIt (pure (c:))
where
makeIt mrs = do ed <- ped
rs <- mrs
return $ rs ed
<|> do c <- per
rs <- mrs
makeIt (pure (\x -> rs (c:x)))
<|> do rs <- mrs
return $ rs []
foldParser :: Parser a -> Parser [a]
foldParser per = do r <- per
rs <- (foldParser per)
return (r:rs)
<|> return []
--auxilliary function
alpha = tok $ satisfy isAlpha
digit = tok $ satisfy isDigit
letter = tok $ satisfy isLetter
symbol = tok $ satisfy (\c -> c `elem` "!$%*+-/:=?>@^_~#")
alpha' = satisfy isAlpha
digit' = satisfy isDigit
letter' = satisfy isLetter
keyWords1 :: [String]
keyWords1 = ["case","else","in","if","letrec","let","of","Pack"]
pKeyWords :: Parser String
pKeyWords = foldr make (string1 "") keyWords1
where
make x rs = (string x) <|> rs
stringConcat :: [String] -> String
stringConcat = foldr1 (++)
--Parser in spj-lest-book
pLit = string1
pVar = pKeyWords >> oneVar
where
oneVar = list (satisfy isLetter)
pHelloOrGoodbye :: Parser String
pHelloOrGoodbye = (pLit "hello") <|> (pLit "goodbye")
pGreeting :: Parser (String, String)
pGreeting = liftA3 (\x y _-> (x,y)) pHelloOrGoodbye pVar (pLit "!")
pGreetings :: Parser [(String, String)]
pGreetings = pZeroOrMore pGreeting
pThen :: (a -> b -> c) -> Parser a -> Parser b -> Parser c
pThen = liftA2
pThen3 :: (a -> b -> c -> d) -> Parser a -> Parser b -> Parser c -> Parser d
pThen3 = liftA3
pThen4 :: (a -> b -> c -> d -> e) -> Parser a -> Parser b -> Parser c -> Parser d -> Parser e
pThen4 f p1 p2 p3 p4 = f <$> p1 <*> p2 <*> p3 <*> p4
liftA4 :: (a -> b -> c -> d -> e) -> Parser a -> Parser b -> Parser c -> Parser d -> Parser e
liftA4 f p1 p2 p3 p4 = f <$> p1 <*> p2 <*> p3 <*> p4
pZeroOrMore per = (pOneOrMore per) <|> (return [])
pOneOrMore = list
pApply :: Parser a -> (a -> b) -> Parser b
pApply = flip (<$>)
pOneOrMoreWithSep :: Parser a -> Parser b -> Parser [a]
pOneOrMoreWithSep = sepByparser1