27
module StreamOrig = Stream;
30
for i = 1 to n do Stream.junk strm done;
32
value loc_bp = Tools.get_cur_loc;
33
value loc_ep = Tools.get_prev_loc;
34
value drop_prev_loc = Tools.drop_prev_loc;
36
value add_loc bp parse_fun strm =
37
let x = parse_fun strm in
38
let ep = loc_ep strm in
39
let loc = Loc.merge bp ep in
42
value stream_peek_nth strm n =
44
[ [x :: xs] -> if i = 1 then Some x else loop (i - 1) xs
47
loop n (Stream.npeek n strm);
49
(* We don't want Stream's functions to be used implictly. *)
27
50
module Stream = struct
29
value junk strm = Context.junk strm;
30
value count strm = Context.bp strm;
51
type t 'a = StreamOrig.t 'a;
52
exception Failure = StreamOrig.Failure;
53
exception Error = StreamOrig.Error;
54
value peek = StreamOrig.peek;
55
value junk = StreamOrig.junk;
58
(* This version of peek_nth is off-by-one from Stream.peek_nth *)
60
loop n (Stream.npeek (n + 1) strm) where rec loop n =
63
| [x] -> if n = 0 then Some x else None
64
| [_ :: l] -> loop (n - 1) l ]
33
value add_loc c bp parse_fun strm =
34
let x = parse_fun c strm in
35
let ep = Context.loc_ep c in
36
let loc = Loc.merge bp ep in
69
value try_parser ps strm =
70
let strm' = Stream.dup strm in
74
[ Stream.Error _ | Loc.Exc_located _ (Stream.Error _) ->
78
njunk strm (StreamOrig.count strm');
39
82
value level_number entry lab =
40
83
let rec lookup levn =
73
116
| _ -> raise Stream.Failure ]
76
value continue entry loc a s c son p1 =
119
value continue entry loc a s son p1 =
78
[: a = (entry_of_symb entry s).econtinue 0 loc a c;
121
[: a = (entry_of_symb entry s).econtinue 0 loc a;
79
122
act = p1 ?? Failed.tree_failed entry a s son :] ->
80
123
Action.mk (fun _ -> Action.getf act a)
83
126
(* PR#4603, PR#4330, PR#4551:
84
Here Context.loc_bp replaced Context.loc_ep to fix all these bugs.
127
Here loc_bp replaced get_loc_ep to fix all these bugs.
85
128
If you do change it again look at these bugs. *)
86
value skip_if_empty c bp _ =
87
if Context.loc_bp c = bp then Action.mk (fun _ -> raise Stream.Failure)
129
value skip_if_empty bp strm =
130
if loc_bp strm = bp then Action.mk (fun _ -> raise Stream.Failure)
89
132
raise Stream.Failure
92
value do_recover parser_of_tree entry nlevn alevn loc a s c son =
135
value do_recover parser_of_tree entry nlevn alevn loc a s son =
94
[ [: a = parser_of_tree entry nlevn alevn (top_tree entry son) c :] -> a
95
| [: a = skip_if_empty c loc :] -> a
137
[ [: a = parser_of_tree entry nlevn alevn (top_tree entry son) :] -> a
138
| [: a = skip_if_empty loc :] -> a
97
continue entry loc a s c son
98
(parser_of_tree entry nlevn alevn son c) :] ->
140
continue entry loc a s son
141
(parser_of_tree entry nlevn alevn son) :] ->
103
value recover parser_of_tree entry nlevn alevn loc a s c son strm =
146
value recover parser_of_tree entry nlevn alevn loc a s son strm =
104
147
if strict_parsing.val then raise (Stream.Error (Failed.tree_failed entry a s son))
107
if strict_parsing_warning.val then
150
if strict_parsing_warning.val then begin
109
151
let msg = Failed.tree_failed entry a s son;
110
152
Format.eprintf "Warning: trying to recover from syntax error";
111
153
if entry.ename <> "" then Format.eprintf " in [%s]" entry.ename else ();
112
154
Format.eprintf "\n%s%a@." msg Loc.print loc;
114
do_recover parser_of_tree entry nlevn alevn loc a s c son strm
156
do_recover parser_of_tree entry nlevn alevn loc a s son strm
117
159
value rec parser_of_tree entry nlevn alevn =
119
[ DeadEnd -> fun _ -> parser []
120
| LocAct act _ -> fun _ -> parser [: :] -> act
161
[ DeadEnd -> parser []
162
| LocAct act _ -> parser [: :] -> act
121
163
| Node {node = Sself; son = LocAct act _; brother = DeadEnd} ->
123
parser [: a = entry.estart alevn c :] -> Action.getf act a
164
parser [: a = entry.estart alevn :] -> Action.getf act a
124
165
| Node {node = Sself; son = LocAct act _; brother = bro} ->
125
166
let p2 = parser_of_tree entry nlevn alevn bro in
128
[ [: a = entry.estart alevn c :] -> Action.getf act a
129
| [: a = p2 c :] -> a ]
168
[ [: a = entry.estart alevn :] -> Action.getf act a
169
| [: a = p2 :] -> a ]
130
170
| Node {node = s; son = son; brother = DeadEnd} ->
156
198
let p1 = parser_of_tree entry nlevn alevn son in
157
199
let p1 = parser_cont p1 entry nlevn alevn s son in
158
200
let p2 = parser_of_tree entry nlevn alevn bro in
161
[ [: a = ps c; act = p1 c bp a :] -> Action.getf act a
162
| [: a = p2 c :] -> a ]
202
let bp = loc_bp strm in
203
match strm with parser
204
[ [: a = ps; act = p1 bp a :] -> Action.getf act a
205
| [: a = p2 :] -> a ]
163
206
| Some (tokl, last_tok, son) ->
164
207
let p1 = parser_of_tree entry nlevn alevn son in
165
208
let p1 = parser_cont p1 entry nlevn alevn last_tok son in
166
209
let p1 = parser_of_token_list p1 tokl in
167
210
let p2 = parser_of_tree entry nlevn alevn bro in
170
[ [: a = p1 c :] -> a
171
| [: a = p2 c :] -> a ] ] ]
172
and parser_cont p1 entry nlevn alevn s son c loc a =
213
| [: a = p2 :] -> a ] ] ]
214
and parser_cont p1 entry nlevn alevn s son loc a =
174
[ [: a = p1 c :] -> a
175
| [: a = recover parser_of_tree entry nlevn alevn loc a s c son :] -> a
217
| [: a = recover parser_of_tree entry nlevn alevn loc a s son :] -> a
176
218
| [: :] -> raise (Stream.Error (Failed.tree_failed entry a s son)) ]
177
219
and parser_of_token_list p1 tokl =
178
220
loop 1 tokl where rec loop n =
180
222
[ [Stoken (tematch, _) :: tokl] ->
184
match Context.peek_nth c n with
185
[ Some (tok, _) when tematch tok -> do { Context.njunk c n; Action.mk tok }
226
match stream_peek_nth strm n with
227
[ Some (tok, _) when tematch tok -> (njunk strm n; Action.mk tok)
186
228
| _ -> raise Stream.Failure ]
189
parser bp [: a = ps c; act = p1 c bp a :] -> Action.getf act a
231
let bp = loc_bp strm in
232
match strm with parser
233
[: a = ps; act = p1 bp a :] -> Action.getf act a
192
match Context.peek_nth c n with
236
match stream_peek_nth strm n with
193
237
[ Some (tok, _) when tematch tok -> tok
194
238
| _ -> raise Stream.Failure ]
196
240
let p1 = loop (n + 1) tokl in
198
parser [: tok = ps c; s :] ->
199
let act = p1 c s in Action.getf act tok ]
241
parser [: tok = ps; s :] ->
242
let act = p1 s in Action.getf act tok ]
200
243
| [Skeyword kwd :: tokl] ->
204
match Context.peek_nth c n with
247
match stream_peek_nth strm n with
205
248
[ Some (tok, _) when Token.match_keyword kwd tok ->
206
do { Context.njunk c n; Action.mk tok }
249
(njunk strm n; Action.mk tok)
207
250
| _ -> raise Stream.Failure ]
210
parser bp [: a = ps c; act = p1 c bp a :] -> Action.getf act a
253
let bp = loc_bp strm in
254
match strm with parser
255
[: a = ps; act = p1 bp a :] -> Action.getf act a
213
match Context.peek_nth c n with
258
match stream_peek_nth strm n with
214
259
[ Some (tok, _) when Token.match_keyword kwd tok -> tok
215
260
| _ -> raise Stream.Failure ]
217
262
let p1 = loop (n + 1) tokl in
219
parser [: tok = ps c; s :] ->
220
let act = p1 c s in Action.getf act tok ]
263
parser [: tok = ps; s :] ->
264
let act = p1 s in Action.getf act tok ]
221
265
| _ -> invalid_arg "parser_of_token_list" ]
222
266
and parser_of_symbol entry nlevn =
224
268
[ Smeta _ symbl act ->
225
269
let act = Obj.magic act entry symbl in
226
270
let pl = List.map (parser_of_symbol entry nlevn) symbl in
228
Obj.magic (List.fold_left (fun act p -> Obj.magic act (p c)) act pl)
271
Obj.magic (List.fold_left (fun act p -> Obj.magic act p) act pl)
230
273
let ps = parser_of_symbol entry nlevn s in
233
[ [: a = ps c; s :] -> loop c [a :: al] s
276
[ [: a = ps; s :] -> loop [a :: al] s
236
fun c -> parser [: a = loop c [] :] -> Action.mk (List.rev a)
279
parser [: a = loop [] :] -> Action.mk (List.rev a)
237
280
| Slist0sep symb sep ->
238
281
let ps = parser_of_symbol entry nlevn symb in
239
282
let pt = parser_of_symbol entry nlevn sep in
242
[ [: v = pt c; a = ps c ?? Failed.symb_failed entry v sep symb;
285
[ [: v = pt; a = ps ?? Failed.symb_failed entry v sep symb;
249
[ [: a = ps c; s :] -> Action.mk (List.rev (kont c [a] s))
250
| [: :] -> Action.mk [] ]
291
[ [: a = ps; s :] -> Action.mk (List.rev (kont [a] s))
292
| [: :] -> Action.mk [] ]
252
294
let ps = parser_of_symbol entry nlevn s in
255
[ [: a = ps c; s :] -> loop c [a :: al] s
297
[ [: a = ps; s :] -> loop [a :: al] s
259
parser [: a = ps c; s :] -> Action.mk (List.rev (loop c [a] s))
300
parser [: a = ps; s :] -> Action.mk (List.rev (loop [a] s))
260
301
| Slist1sep symb sep ->
261
302
let ps = parser_of_symbol entry nlevn symb in
262
303
let pt = parser_of_symbol entry nlevn sep in
268
[ [: a = ps c :] -> a
269
| [: a = parse_top_symb' entry symb c :] -> a
310
| [: a = parse_top_symb entry symb :] -> a
271
312
raise (Stream.Error (Failed.symb_failed entry v sep symb)) ];
277
parser [: a = ps c; s :] -> Action.mk (List.rev (kont c [a] s))
317
parser [: a = ps; s :] -> Action.mk (List.rev (kont [a] s))
279
319
let ps = parser_of_symbol entry nlevn s in
282
[ [: a = ps c :] -> Action.mk (Some a)
283
| [: :] -> Action.mk None ]
321
[ [: a = ps :] -> Action.mk (Some a)
322
| [: :] -> Action.mk None ]
324
let ps = parser_of_symbol entry nlevn s in
285
327
let pt = parser_of_tree entry 1 0 t in
287
parser bp [: (act, loc) = add_loc c bp pt :] ->
329
let bp = loc_bp strm in
330
match strm with parser
331
[: (act, loc) = add_loc bp pt :] ->
288
332
Action.getf act loc
289
| Snterm e -> fun c -> parser [: a = e.estart 0 c :] -> a
333
| Snterm e -> parser [: a = e.estart 0 :] -> a
291
fun c -> parser [: a = e.estart (level_number e l) c :] -> a
292
| Sself -> fun c -> parser [: a = entry.estart 0 c :] -> a
293
| Snext -> fun c -> parser [: a = entry.estart nlevn c :] -> a
335
parser [: a = e.estart (level_number e l) :] -> a
336
| Sself -> parser [: a = entry.estart 0 :] -> a
337
| Snext -> parser [: a = entry.estart nlevn :] -> a
294
338
| Skeyword kwd ->
297
[: `(tok, _) when Token.match_keyword kwd tok :] -> Action.mk tok
340
[: `(tok, _) when Token.match_keyword kwd tok :] ->
298
342
| Stoken (f, _) ->
299
fun _ -> parser [: `(tok, _) when f tok :] -> Action.mk tok ]
300
and parse_top_symb' entry symb c =
301
parser_of_symbol entry 0 (top_symb entry symb) c
302
and parse_top_symb entry symb =
304
Context.call_with_ctx strm
305
(fun c -> parse_top_symb' entry symb c (Context.stream c));
344
[: `(tok,_) when f tok :] -> Action.mk tok ]
345
and parse_top_symb entry symb strm =
346
parser_of_symbol entry 0 (top_symb entry symb) strm;
307
348
value rec start_parser_of_levels entry clevn =
309
[ [] -> fun _ _ -> parser []
350
[ [] -> fun _ -> parser []
310
351
| [lev :: levs] ->
311
352
let p1 = start_parser_of_levels entry (succ clevn) levs in
312
353
match lev.lprefix with
320
361
let p2 = parser_of_tree entry (succ clevn) alevn tree in
325
[: (act, loc) = add_loc c bp p2; strm :] ->
365
let bp = loc_bp strm in
366
match strm with parser
367
[: (act, loc) = add_loc bp p2; strm :] ->
326
368
let a = Action.getf act loc in
327
entry.econtinue levn loc a c strm
369
entry.econtinue levn loc a strm
330
if levn > clevn then p1 levn c strm
372
if levn > clevn then p1 levn strm
332
match strm with parser bp
333
[ [: (act, loc) = add_loc c bp p2 :] ->
374
let bp = loc_bp strm in
375
match strm with parser
376
[ [: (act, loc) = add_loc bp p2 :] ->
334
377
let a = Action.getf act loc in
335
entry.econtinue levn loc a c strm
336
| [: act = p1 levn c :] -> act ] ] ] ]
378
entry.econtinue levn loc a strm
379
| [: act = p1 levn :] -> act ] ] ] ]
339
382
value start_parser_of_entry entry =