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
360
361
362
363
364
365
366
367
368
369
370
371
372
|
open Common;;
open Token;;
(* Fundamental parser types and actions *)
type get_mod_fn = (Ast.meta_pat
-> node_id
-> (node_id ref)
-> (opaque_id ref)
-> (filename * Ast.mod_items))
;;
type pstate =
{ mutable pstate_peek : token;
mutable pstate_ctxt : (string * pos) list;
mutable pstate_rstr : bool;
mutable pstate_depth: int;
pstate_lexbuf : Lexing.lexbuf;
pstate_file : filename;
pstate_sess : Session.sess;
pstate_temp_id : temp_id ref;
pstate_node_id : node_id ref;
pstate_opaque_id : opaque_id ref;
pstate_get_mod : get_mod_fn;
pstate_infer_lib_name : (Ast.ident -> filename);
pstate_required : (node_id, (required_lib * nabi_conv)) Hashtbl.t;
pstate_required_syms : (node_id, string) Hashtbl.t; }
;;
let log (ps:pstate) = Session.log "parse"
ps.pstate_sess.Session.sess_log_parse
ps.pstate_sess.Session.sess_log_out
;;
let iflog ps thunk =
if ps.pstate_sess.Session.sess_log_parse
then thunk ()
else ()
;;
let make_parser
(tref:temp_id ref)
(nref:node_id ref)
(oref:opaque_id ref)
(sess:Session.sess)
(get_mod:get_mod_fn)
(infer_lib_name:Ast.ident -> filename)
(required:(node_id, (required_lib * nabi_conv)) Hashtbl.t)
(required_syms:(node_id, string) Hashtbl.t)
(fname:string)
: pstate =
let lexbuf = Lexing.from_channel (open_in fname) in
let spos = { lexbuf.Lexing.lex_start_p with Lexing.pos_fname = fname } in
let cpos = { lexbuf.Lexing.lex_curr_p with Lexing.pos_fname = fname } in
lexbuf.Lexing.lex_start_p <- spos;
lexbuf.Lexing.lex_curr_p <- cpos;
let first = Lexer.token lexbuf in
let ps =
{ pstate_peek = first;
pstate_ctxt = [];
pstate_rstr = false;
pstate_depth = 0;
pstate_lexbuf = lexbuf;
pstate_file = fname;
pstate_sess = sess;
pstate_temp_id = tref;
pstate_node_id = nref;
pstate_opaque_id = oref;
pstate_get_mod = get_mod;
pstate_infer_lib_name = infer_lib_name;
pstate_required = required;
pstate_required_syms = required_syms; }
in
iflog ps (fun _ -> log ps "made parser for: %s\n%!" fname);
ps
;;
exception Parse_err of (pstate * string)
;;
let lexpos (ps:pstate) : pos =
let p = ps.pstate_lexbuf.Lexing.lex_start_p in
(p.Lexing.pos_fname,
p.Lexing.pos_lnum ,
(p.Lexing.pos_cnum) - (p.Lexing.pos_bol))
;;
let next_node_id (ps:pstate) : node_id =
let id = !(ps.pstate_node_id) in
ps.pstate_node_id := Node ((int_of_node id)+1);
id
;;
let next_opaque_id (ps:pstate) : opaque_id =
let id = !(ps.pstate_opaque_id) in
ps.pstate_opaque_id := Opaque ((int_of_opaque id)+1);
id
;;
let span
(ps:pstate)
(apos:pos)
(bpos:pos)
(x:'a)
: 'a identified =
let span = { lo = apos; hi = bpos } in
let id = next_node_id ps in
iflog ps (fun _ -> log ps "span for node #%d: %s"
(int_of_node id) (Session.string_of_span span));
htab_put ps.pstate_sess.Session.sess_spans id span;
{ node = x; id = id }
;;
let decl p i =
{ Ast.decl_params = p;
Ast.decl_item = i }
;;
let spans
(ps:pstate)
(things:('a identified) array)
(apos:pos)
(thing:'a)
: ('a identified) array =
Array.append things [| (span ps apos (lexpos ps) thing) |]
;;
(*
* The point of this is to make a new node_id entry for a node that is a
* "copy" of an lval returned from somewhere else. For example if you create
* a temp, the lval it returns can only be used in *one* place, for the
* node_id denotes the place that lval is first used; subsequent uses of
* 'the same' reference must clone_lval it into a new node_id. Otherwise
* there is trouble.
*)
let clone_span
(ps:pstate)
(oldnode:'a identified)
(newthing:'b)
: 'b identified =
let s = Hashtbl.find ps.pstate_sess.Session.sess_spans oldnode.id in
span ps s.lo s.hi newthing
;;
let rec clone_lval (ps:pstate) (lval:Ast.lval) : Ast.lval =
match lval with
Ast.LVAL_base nb ->
let nnb = clone_span ps nb nb.node in
Ast.LVAL_base nnb
| Ast.LVAL_ext (base, ext) ->
Ast.LVAL_ext ((clone_lval ps base), ext)
;;
let clone_atom (ps:pstate) (atom:Ast.atom) : Ast.atom =
match atom with
Ast.ATOM_literal _ -> atom
| Ast.ATOM_lval lv -> Ast.ATOM_lval (clone_lval ps lv)
;;
let ctxt (n:string) (f:pstate -> 'a) (ps:pstate) : 'a =
(ps.pstate_ctxt <- (n, lexpos ps) :: ps.pstate_ctxt;
let res = f ps in
ps.pstate_ctxt <- List.tl ps.pstate_ctxt;
res)
;;
let rstr (r:bool) (f:pstate -> 'a) (ps:pstate) : 'a =
let prev = ps.pstate_rstr in
(ps.pstate_rstr <- r;
let res = f ps in
ps.pstate_rstr <- prev;
res)
;;
let err (str:string) (ps:pstate) =
(Parse_err (ps, (str)))
;;
let (slot_nil:Ast.slot) =
{ Ast.slot_mode = Ast.MODE_interior;
Ast.slot_ty = Some Ast.TY_nil }
;;
let (slot_auto:Ast.slot) =
{ Ast.slot_mode = Ast.MODE_interior;
Ast.slot_ty = None }
;;
let build_tmp
(ps:pstate)
(slot:Ast.slot)
(apos:pos)
(bpos:pos)
: (temp_id * Ast.lval * Ast.stmt) =
let nonce = !(ps.pstate_temp_id) in
ps.pstate_temp_id := Temp ((int_of_temp nonce)+1);
iflog ps
(fun _ -> log ps "building temporary %d" (int_of_temp nonce));
let decl = Ast.DECL_slot (Ast.KEY_temp nonce, (span ps apos bpos slot)) in
let declstmt = span ps apos bpos (Ast.STMT_decl decl) in
let tmp = Ast.LVAL_base (span ps apos bpos (Ast.BASE_temp nonce)) in
(nonce, tmp, declstmt)
;;
(* Simple helpers *)
(* FIXME (issue #71): please rename these, they make eyes bleed. *)
let arr (ls:'a list) : 'a array = Array.of_list ls ;;
let arl (ls:'a list) : 'a array = Array.of_list (List.rev ls) ;;
let arj (ar:('a array array)) = Array.concat (Array.to_list ar) ;;
let arj1st (pairs:(('a array) * 'b) array) : (('a array) * 'b array) =
let (az, bz) = List.split (Array.to_list pairs) in
(Array.concat az, Array.of_list bz)
(* Bottom-most parser actions. *)
let peek (ps:pstate) : token =
iflog ps
begin
fun _ ->
log ps "peeking at: %s // %s"
(string_of_tok ps.pstate_peek)
(match ps.pstate_ctxt with
(s, _) :: _ -> s
| _ -> "<empty>")
end;
ps.pstate_peek
;;
let bump (ps:pstate) : unit =
begin
iflog ps (fun _ -> log ps "bumping past: %s"
(string_of_tok ps.pstate_peek));
ps.pstate_peek <- Lexer.token ps.pstate_lexbuf
end
;;
let bump_bracequote (ps:pstate) : unit =
begin
assert (ps.pstate_peek = LBRACE);
iflog ps (fun _ -> log ps "bumping past: %s"
(string_of_tok ps.pstate_peek));
let buf = Buffer.create 32 in
ps.pstate_peek <- Lexer.bracequote buf 1 ps.pstate_lexbuf
end
;;
let expect (ps:pstate) (t:token) : unit =
let p = peek ps in
if p == t
then bump ps
else
let msg = ("Expected '" ^ (string_of_tok t) ^
"', found '" ^ (string_of_tok p ) ^ "'") in
raise (Parse_err (ps, msg))
;;
let unexpected (ps:pstate) =
err ("Unexpected token '" ^ (string_of_tok (peek ps)) ^ "'") ps
;;
(* Parser combinators. *)
let one_or_more
(sep:token)
(prule:pstate -> 'a)
(ps:pstate)
: 'a array =
let accum = ref [prule ps] in
while peek ps == sep
do
bump ps;
accum := (prule ps) :: !accum
done;
arl !accum
;;
let bracketed_seq
(mandatory:int)
(bra:token)
(ket:token)
(sepOpt:token option)
(prule:pstate -> 'a)
(ps:pstate)
: 'a array =
expect ps bra;
let accum = ref [] in
let dosep _ =
(match sepOpt with
None -> ()
| Some tok ->
if (!accum = [])
then ()
else expect ps tok)
in
while mandatory > List.length (!accum) do
dosep ();
accum := (prule ps) :: (!accum)
done;
while (not (peek ps = ket))
do
dosep ();
accum := (prule ps) :: !accum
done;
expect ps ket;
arl !accum
;;
let bracketed_zero_or_more
(bra:token)
(ket:token)
(sepOpt:token option)
(prule:pstate -> 'a)
(ps:pstate)
: 'a array =
bracketed_seq 0 bra ket sepOpt (ctxt "bracketed_seq" prule) ps
;;
let paren_comma_list
(prule:pstate -> 'a)
(ps:pstate)
: 'a array =
bracketed_zero_or_more LPAREN RPAREN (Some COMMA) prule ps
;;
let bracketed_one_or_more
(bra:token)
(ket:token)
(sepOpt:token option)
(prule:pstate -> 'a)
(ps:pstate)
: 'a array =
bracketed_seq 1 bra ket sepOpt (ctxt "bracketed_seq" prule) ps
;;
let bracketed_two_or_more
(bra:token)
(ket:token)
(sepOpt:token option)
(prule:pstate -> 'a)
(ps:pstate)
: 'a array =
bracketed_seq 2 bra ket sepOpt (ctxt "bracketed_seq" prule) ps
;;
let bracketed (bra:token) (ket:token) (prule:pstate -> 'a) (ps:pstate) : 'a =
expect ps bra;
let res = ctxt "bracketed" prule ps in
expect ps ket;
res
;;
(*
* Local Variables:
* fill-column: 78;
* indent-tabs-mode: nil
* buffer-file-coding-system: utf-8-unix
* compile-command: "make -k -C ../.. 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
* End:
*)
|