-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMonadicParserFJ.hs
325 lines (290 loc) · 6.6 KB
/
MonadicParserFJ.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
module MonadicParserFJ where
-- THANKS TO:
--
-- https://www.cs.rit.edu/~swm/cs561/monadic-parsing-jfp.pdf
-- https://wiki.haskell.org/Functor-Applicative-Monad_Proposal
-- Monadic Parser
import Control.Applicative (Applicative(..));;
import Control.Monad (liftM, ap);;
import DataTypesFJ;;
import Data.Char;;
-- Grammar for exp:
-- p ::= cd_1 ... cd_n
-- cd ::= class C extends C' { fds mds }
-- fds ::= fd_1 ... fd_n
-- fd ::= C f;
-- mds ::= md_1 ... md_n
-- md ::= C_0 m(C_1 x_1, ..., C_n x_n) { return e; }
-- e ::= x | e.f | e_0.m(e_1, ..., e_n) | new C(e_1, ..., e_n) | (C)e | ( e )
-- parser for strings
newtype Parser a = Parser(String -> [(a, String)]);;
-- function that apply a parser to a string
parse :: Parser a -> String -> [(a, String)]
parse (Parser p) s = p s;;
-- parser that ever fails
failure :: Parser a
failure = Parser(\_ -> []);;
-- parse a char
item :: Parser Char
item = Parser(\s ->
case s of
[] -> []
(x:xs) -> [(x, xs)]
)
;;
-- if the first parser fail, apply the second one
(+++) :: Parser a -> Parser a -> Parser a
p1 +++ p2 = Parser(\s ->
let p = parse p1 s in
case p of
[] -> parse p2 s
p' -> p'
)
;;
-- monadic behavior of parser
instance Functor Parser where
fmap = liftM
;;
instance Applicative Parser where
pure c = Parser(\s -> [(c,s)])
(<*>) = ap
;;
instance Monad Parser where
return = pure
p1 >>= p2 = Parser(\s ->
let p = parse p1 s in
concat (map (\(c, cs) -> parse (p2 c) cs) p)
)
;;
;;
-- if a predicate on char is satisfied
sat :: (Char -> Bool) -> Parser Char
sat p = do
c <- item;
if p c
then return c
else failure
;;
-- useful parsers
digit, upper, lower, letter :: Parser Char
digit = sat isDigit;;
upper = sat isUpper;;
lower = sat isLower;;
letter = sat isLetter;;
-- parser of a specific char
char :: Char -> Parser Char
char c = sat (c==);;
-- parser of a specific string
string :: String -> Parser String
string "" = return "";;
string (c:cs) = do
x <- char c;
xs <- string cs;
return (x:xs)
;;
-- try to apply one or more times a parser
many, many1 :: Parser a -> Parser [a]
many p = many1 p +++ return [];;
many1 p = do
x <- p;
xs <- many p;
return (x:xs)
;;
-- parser for space character
space :: Parser String
space = many (sat isSpace);;
-- parsers for symbols
symbol :: String -> Parser String
symbol s = do
space;
ss <- string s;
space;
return ss
;;
-- Parser for FJ
-- parser for a generic string, that represents a variable or a class name
varname :: Parser String
varname = do
fst <- letter +++ (char '_');
rest <- many (letter +++ (digit +++ (char '_')));
return (fst:rest)
;;
-- apply a parser and then a parser for a delimiter many times
separate_by, separate_by1 :: Parser a -> Parser b -> Parser [a]
separate_by p sep = (p `separate_by1` sep) +++ return [];;
separate_by1 p sep = do
a <- p;
as <- many (do {sep; p});
return (a:as)
;;
-- UTILS: base for method invocation and field access
parseE0 :: Parser Exp
parseE0 = parseNew +++ parseVariable;;
-- parse parenthesis to change the evaluation order
parseParenthesis :: Parser Exp
parseParenthesis = do
{
symbol "(";
e <- term;
symbol ")";
leftAssociate e
}
;;
---------------------------------------------------------------------------
-- parser for variable and its type
parseTypeAndName :: Parser (Type, String)
parseTypeAndName = do
{
t <- varname;
space;
e <- varname;
return (TypeDecl t, e)
}
;;
-- x
parseVariable :: Parser Exp
parseVariable = do
{
x <- varname;
return (Variable x)
}
;;
-- (C)e
parseCast :: Parser Exp
parseCast = do
{
symbol "(";
c <- varname;
symbol ")";
e <- term;
return (Cast c e)
}
;;
-- new C(e1,...,en)
parseNew :: Parser Exp
parseNew = do
{
symbol "new";
c <- varname;
symbol "(";
params <- term `separate_by` (symbol ",");
symbol ")";
return (New c params)
}
;;
-- e.f
parseFieldAccess :: Parser Exp
parseFieldAccess = do
{
e0 <- parseE0;
symbol ".";
f <- varname;
leftAssociate (FieldAccess e0 f)
}
;;
-- e0.m(e1,...,en)
parseMethodInv :: Parser Exp
parseMethodInv = do
{
e0 <- parseE0;
symbol ".";
m <- varname;
symbol "(";
params <- term `separate_by` (symbol ",");
symbol ")";
leftAssociate (MethodInv e0 m params)
}
;;
-- left association for . operator
leftAssociate :: Exp -> Parser Exp
leftAssociate accP = do
{
e <- (symbol ".") +++ return [];
case e of
[] -> return accP
"." -> do
{
e' <- varname;
openP <- symbol "(" +++ return [];
case openP of
[] -> leftAssociate (FieldAccess accP e')
_ -> do
{
params <- term `separate_by` (symbol ",");
symbol ")";
leftAssociate (MethodInv accP e' params)
}
}
}
;;
-- parser for a single term
term :: Parser Exp
term =
parseCast +++
parseParenthesis +++
parseMethodInv +++
parseFieldAccess +++
parseNew +++
parseVariable
;;
-- C0 m (C1 x1,...,Cn xn) {return e;}
parseMethodDecl :: Parser Method
parseMethodDecl = do
{
(c0, m) <- parseTypeAndName;
symbol "(";
params <- parseTypeAndName `separate_by` (symbol ",");
symbol ")";
symbol "{";
symbol "return";
e <- term;
symbol ";";
symbol "}";
return (MethodDecl c0 m params e)
}
;;
-- m1...mn
parseMethodsDecls :: Parser MDS
parseMethodsDecls = many parseMethodDecl;;
-- parse the body of a class
-- C1 f1;...;Cn fn;
parseClass :: Parser (FDS, MDS)
parseClass = do
{
m <- parseMethodsDecls;
case m of
[] -> do
{
f <- parseTypeAndName +++ return (TypeDecl "", "");
case f of
(TypeDecl "", "") -> return ([], [])
f' -> do
{
symbol ";";
(fs, ms) <- parseClass;
return (f':fs, ms)
}
}
m' -> return ([], m')
}
;;
-- class C extends C' { mds ; fds }
parseClassDecl :: Parser Class
parseClassDecl = do
{
symbol "class";
c <- varname;
symbol "extends";
cc <- varname;
symbol "{";
(fields, methods) <- parseClass;
symbol "}";
return (ClassDecl c cc fields methods)
}
;;
-- parse all the program
parseProg :: Parser ClassTable
parseProg = many1 parseClassDecl;;
-- parse all terms
terms :: Parser [Exp]
terms = many1 term;;