Source file parse_js.ml
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
let debug = Debug.find "js-parser"
let debug () = debug ()
open! Stdlib
module Lexer : sig
type t
type error
val of_file : string -> t
val of_channel : in_channel -> t
val of_string :
?report_error:(error -> unit)
-> ?pos:Lexing.position
-> ?filename:string
-> string
-> t
val print_error : error -> unit
val curr_pos : t -> Lexing.position
val token : t -> Js_token.t * Loc.t
val lex_as_regexp : t -> Js_token.t * Loc.t
val rollback : t -> unit
val dummy_pos : Lexing.position
end = struct
type error = Loc.t * Flow_lexer.Parse_error.t
type t =
{ l : Sedlexing.lexbuf
; report_error : error -> unit
; mutable env : Flow_lexer.Lex_env.t
}
let dummy_pos = { Lexing.pos_fname = ""; pos_lnum = 0; pos_cnum = 0; pos_bol = 0 }
let zero_pos = { Lexing.pos_fname = ""; pos_lnum = 1; pos_cnum = 0; pos_bol = 0 }
let print_error (loc, e) =
let f = Loc.filename loc in
let loc = Printf.sprintf "%s:%d:%d" f (Loc.line loc) (Loc.column loc) in
Printf.eprintf "Lexer error: %s: %s\n" loc (Flow_lexer.Parse_error.to_string e)
let create ?(report_error = print_error) l =
{ l; env = Flow_lexer.Lex_env.create l; report_error }
let of_file file : t =
let ic = open_in_bin file in
let lexbuf = Sedlexing.Utf8.from_channel ic in
Sedlexing.set_filename lexbuf file;
create lexbuf
let of_channel ci : t = create (Sedlexing.Utf8.from_channel ci)
let of_string ?report_error ?(pos = zero_pos) ?filename s =
let l = Sedlexing.Utf8.from_string s in
let pos =
match filename with
| None -> pos
| Some pos_fname -> { pos with pos_fname }
in
Sedlexing.set_position l pos;
Option.iter filename ~f:(Sedlexing.set_filename l);
create ?report_error l
let curr_pos lexbuf = Sedlexing.lexing_position_curr lexbuf.l
let report_errors t res =
match Flow_lexer.Lex_result.errors res with
| [] -> ()
| l -> List.iter l ~f:t.report_error
let token (t : t) =
let env, res = Flow_lexer.lex t.env in
t.env <- env;
let tok = Flow_lexer.Lex_result.token res in
let loc = Flow_lexer.Lex_result.loc res in
report_errors t res;
tok, loc
let rollback t = Sedlexing.rollback t.l
let lex_as_regexp (t : t) =
Sedlexing.rollback t.l;
let env, res = Flow_lexer.regexp t.env in
t.env <- env;
let tok = Flow_lexer.Lex_result.token res in
let loc = Flow_lexer.Lex_result.loc res in
report_errors t res;
tok, loc
end
exception Parsing_error of Parse_info.t
let = function
| Js_token.TComment _ | TAnnot _ | TCommentLineDirective _ -> true
| _ -> false
let token_to_ident t =
let name = Js_token.to_string t in
Js_token.T_IDENTIFIER (Stdlib.Utf8_string.of_string_exn name, name)
module State : sig
type token = Js_token.t * Loc.t
type yield_await_state =
{ yield : bool
; await : bool
}
type 'a checkpoint = 'a Js_parser.MenhirInterpreter.checkpoint * yield_await_state list
val normalize_token : 'a checkpoint -> Js_token.t -> Js_token.t
module Cursor : sig
type 'a t
val insert_token : 'a t -> Js_token.t -> Loc.t -> 'a t
val replace_token : 'a t -> Js_token.t -> Loc.t -> 'a t
val last_token : 'a t -> (Js_token.t * Loc.t * 'a t) option
val rewind_block : 'a t -> (Js_token.t * Loc.t * 'a t) option
end
type 'a t
val save_checkpoint : 'a t -> 'a t
val pending : 'a t -> (token * 'a t) option
val cursor : 'a t -> 'a Cursor.t
val checkpoint : 'a t -> 'a checkpoint
val offer : 'a t -> Js_token.t -> Loc.t -> 'a t
val finalize_error : 'a t -> 'a t
val try_recover : 'a Cursor.t -> 'a t
val create : 'a Js_parser.MenhirInterpreter.checkpoint -> yield_await_state -> 'a t
val all_tokens : 'a t -> token list
end = struct
type token = Js_token.t * Loc.t
type yield_await_state =
{ yield : bool
; await : bool
}
type 'a checkpoint = 'a Js_parser.MenhirInterpreter.checkpoint * yield_await_state list
type 'a w =
| Start of 'a checkpoint
| Checkpoint of 'a checkpoint * 'a w
| Token of Js_token.t * Loc.t * 'a w
module Cursor = struct
type 'a t = 'a w * token list
let last_token ((h, next) : _ t) : (_ * _ * _ t) option =
let rec find next = function
| Start _ -> None
| Checkpoint (_, t) -> find next t
| Token (tok, loc, t) ->
if is_comment tok
then find ((tok, loc) :: next) t
else Some (tok, loc, (t, (tok, loc) :: next))
in
find next h
let replace_token ((h, next) : _ t) tok loc : _ t =
match next with
| [] -> assert false
| _ :: next -> h, (tok, loc) :: next
let insert_token ((h, next) : _ t) tok loc : _ t = h, (tok, loc) :: next
let rewind_block : 'a t -> (Js_token.t * Loc.t * 'a t) option =
fun h ->
let rec rewind (stack : Js_token.t list) (h : _ t) =
match last_token h with
| None -> None
| Some (tok, loc, h) -> (
match tok, stack with
| (T_RPAREN | T_RCURLY | T_RBRACKET), _ ->
let stack = tok :: stack in
rewind stack h
| (T_LPAREN | T_LPAREN_ARROW), [ T_RPAREN ]
| T_LBRACKET, [ T_RBRACKET ]
| T_LCURLY, [ T_RCURLY ] -> Some (tok, loc, h)
| (T_LPAREN | T_LPAREN_ARROW), T_RPAREN :: stack
| T_LBRACKET, T_RBRACKET :: stack
| T_LCURLY, T_RCURLY :: stack -> rewind stack h
| T_LPAREN, _ -> assert false
| T_LBRACKET, _ -> assert false
| T_LCURLY, _ -> assert false
| _, [] -> None
| _, (_ :: _ as stack) -> rewind stack h)
in
rewind [] h
end
type 'a t =
{ checkpoint : 'a checkpoint
; history : 'a w
; next : token list
}
let pending t =
match t.next with
| [] -> None
| x :: next -> Some (x, { t with next })
let save_checkpoint { checkpoint; history; next } =
{ checkpoint; history = Checkpoint (checkpoint, history); next }
let cursor { history; next; _ } = history, next
let rec advance (t, ya) =
match (t : _ Js_parser.MenhirInterpreter.checkpoint) with
| Shifting _ -> advance (Js_parser.MenhirInterpreter.resume t, ya)
| AboutToReduce _ -> advance (Js_parser.MenhirInterpreter.resume t, ya)
| InputNeeded _ -> t, ya
| Accepted _ -> t, ya
| HandlingError _ -> t, ya
| Rejected -> t, ya
let advance' (checkpoint, yield_await) token =
let checkpoint, yield_await =
advance (Js_parser.MenhirInterpreter.offer checkpoint token, yield_await)
in
let yield_await =
match token, yield_await with
| ((T_YIELDOFF | T_YIELDON | T_AWAITOFF | T_AWAITON | T_YIELD_AWAIT_POP), _, _), []
-> assert false
| (T_YIELDOFF, _, _), ({ await; _ } :: _ as ya) -> { yield = false; await } :: ya
| (T_YIELDON, _, _), ({ await; _ } :: _ as ya) -> { yield = true; await } :: ya
| (T_AWAITOFF, _, _), ({ yield; _ } :: _ as ya) -> { yield; await = false } :: ya
| (T_AWAITON, _, _), ({ yield; _ } :: _ as ya) -> { yield; await = true } :: ya
| (T_YIELD_AWAIT_POP, _, _), _ :: xs -> xs
| _ -> yield_await
in
checkpoint, yield_await
let create checkpoint yield_await_state =
let checkpoint = checkpoint, [ yield_await_state ] in
{ checkpoint; history = Start checkpoint; next = [] }
let checkpoint { checkpoint; _ } = checkpoint
let normalize_token (_checkpoint, yield_await_state) (tok : Js_token.t) =
match yield_await_state with
| [] -> assert false
| yield_await :: _ -> (
match tok, yield_await with
| T_IDENTIFIER (Utf8 _, "yield"), { yield = false; _ } -> tok
| T_YIELD, { yield = true; _ } -> tok
| T_IDENTIFIER (Utf8 _, "await"), { await = false; _ } -> tok
| T_AWAIT, { await = true; _ } -> tok
| T_IDENTIFIER (Utf8 _, "yield"), { yield = true; _ } -> T_YIELD
| T_IDENTIFIER (Utf8 _, "await"), { await = true; _ } -> T_AWAIT
| T_YIELD, { yield = false; _ } | T_AWAIT, { await = false; _ } ->
token_to_ident tok
| _ -> tok)
let offer { checkpoint; history; next } tok loc : _ t =
match (checkpoint : _ checkpoint) with
| Accepted _, _ -> assert false
| Rejected, _ | HandlingError _, _ -> assert false
| Shifting _, _ | AboutToReduce _, _ -> assert false
| InputNeeded _, _ -> (
if is_comment tok
then { checkpoint; history = Token (tok, loc, history); next }
else
let tok = normalize_token checkpoint tok in
let new_checkpoint = advance' checkpoint (tok, Loc.p1 loc, Loc.p2 loc) in
match (new_checkpoint : 'a checkpoint) with
| Shifting _, _ | AboutToReduce _, _ -> assert false
| Rejected, _ | Accepted _, _ | InputNeeded _, _ ->
let history =
match tok with
| T_VIRTUAL_SEMICOLON ->
let rec insert = function
| Start _ as start -> Token (tok, loc, start)
| Checkpoint (_, x) -> insert x
| Token (inner_tok, loc', tail) as x ->
if is_comment inner_tok
then Token (inner_tok, loc', insert tail)
else Token (tok, loc, x)
in
insert history
| _ -> Token (tok, loc, history)
in
{ checkpoint = new_checkpoint; history; next }
| HandlingError _, _ ->
{ checkpoint = new_checkpoint
; history = Token (tok, loc, Checkpoint (checkpoint, history))
; next
})
let try_recover ((h, next) : _ Cursor.t) =
let rec compute (t : _ w) =
match t with
| Start env -> advance env
| Checkpoint (env, _) -> advance env
| Token (tok, loc, t) -> (
if is_comment tok
then compute t
else
match compute t with
| (InputNeeded _, _) as checkpoint ->
advance' checkpoint (tok, Loc.p1 loc, Loc.p2 loc)
| Shifting _, _ | AboutToReduce _, _ -> assert false
| Accepted _, _ | Rejected, _ | HandlingError _, _ -> assert false)
in
let checkpoint = compute h in
{ checkpoint; history = h; next }
let finalize_error { checkpoint; history; next } =
let rec loop ((t, ya) : _ checkpoint) =
match t with
| HandlingError _ | Shifting _ | AboutToReduce _ ->
loop (Js_parser.MenhirInterpreter.resume t, ya)
| Accepted _ | InputNeeded _ -> assert false
| Rejected -> t, ya
in
{ checkpoint = loop checkpoint; history; next }
let all_tokens { history; _ } =
let rec collect acc t =
match t with
| Start _ -> acc
| Checkpoint (_, tail) -> collect acc tail
| Token (tok, loc, tail) -> collect ((tok, loc) :: acc) tail
in
collect [] history
end
let parse_annot s =
match String.drop_prefix ~prefix:"//" s with
| None -> None
| Some s -> (
let buf = Lexing.from_string s in
try Some (Annot_parser.annot Annot_lexer.main buf) with
| Not_found -> None
| _ -> None)
let rec nl_separated prev loc' =
match State.Cursor.last_token prev with
| None -> true
| Some (T_VIRTUAL_SEMICOLON, _, prev) -> nl_separated prev loc'
| Some ((T_YIELD_AWAIT_POP | T_AWAITON | T_AWAITOFF | T_YIELDOFF | T_YIELDON), _, prev)
-> nl_separated prev loc'
| Some (_, loc, _) -> Loc.line loc' <> Loc.line_end loc
let rec last prev =
match State.Cursor.last_token prev with
| None -> raise Not_found
| Some (T_VIRTUAL_SEMICOLON, _, prev) -> last prev
| Some ((T_YIELD_AWAIT_POP | T_AWAITON | T_AWAITOFF | T_YIELDOFF | T_YIELDON), _, prev)
-> last prev
| Some (tok, _, _) -> tok
let acceptable state token =
let module I = Js_parser.MenhirInterpreter in
let checkpoint, _ = State.checkpoint state in
I.acceptable checkpoint token Lexer.dummy_pos
let semicolon = Js_token.T_VIRTUAL_SEMICOLON
let dummy_loc = Loc.create Lexer.dummy_pos Lexer.dummy_pos
let dummy_ident = Js_token.T_IDENTIFIER (Utf8_string.of_string_exn "<DUMMY>", "<DUMMY>")
let rec offer_one t (lexbuf : Lexer.t) =
let tok, loc = Lexer.token lexbuf in
match tok with
| TCommentLineDirective _ ->
let t = State.offer t tok loc in
offer_one t lexbuf
| TComment s as tok ->
let tok =
match parse_annot s with
| None -> tok
| Some a -> TAnnot (s, a)
in
let t = State.offer t tok loc in
offer_one t lexbuf
| _ ->
let tok = State.normalize_token (State.checkpoint t) tok in
let t =
match tok with
| T_LPAREN when acceptable t T_LPAREN_ARROW -> State.save_checkpoint t
| _ -> t
in
let h = State.cursor t in
let tok, loc =
match State.Cursor.last_token h, tok with
| ( Some
( (T_RETURN | T_CONTINUE | T_BREAK | T_THROW | T_YIELD | T_ASYNC | T_USING)
, _
, _ )
, ((T_SEMICOLON | T_VIRTUAL_SEMICOLON) as tok) ) -> tok, loc
| ( Some
( (T_RETURN | T_CONTINUE | T_BREAK | T_THROW | T_YIELD | T_ASYNC | T_USING)
, _
, _ )
, _ )
when nl_separated h loc && acceptable t T_VIRTUAL_SEMICOLON ->
Lexer.rollback lexbuf;
semicolon, dummy_loc
| _, ((T_DIV | T_DIV_ASSIGN) as tok) ->
if acceptable t tok
then tok, loc
else
let t, loc = Lexer.lex_as_regexp lexbuf in
t, loc
| _ -> tok, loc
in
State.offer t tok loc
let recover error_checkpoint previous_checkpoint =
match State.Cursor.last_token (State.cursor error_checkpoint) with
| None -> error_checkpoint
| Some (offending_token, offending_loc, rest) ->
let checkpoint =
match State.Cursor.last_token rest with
| None -> error_checkpoint
| Some (last_token, _, _) -> (
match offending_token with
| T_VIRTUAL_SEMICOLON -> error_checkpoint
| T_RCURLY when acceptable previous_checkpoint Js_token.T_VIRTUAL_SEMICOLON ->
State.Cursor.insert_token rest semicolon dummy_loc |> State.try_recover
| T_EOF when acceptable previous_checkpoint Js_token.T_VIRTUAL_SEMICOLON ->
State.Cursor.insert_token rest semicolon dummy_loc |> State.try_recover
| T_ARROW when not (nl_separated rest offending_loc) -> (
match last rest with
| T_RPAREN -> (
match State.Cursor.rewind_block rest with
| Some (T_LPAREN, loc, prev) ->
State.Cursor.replace_token prev T_LPAREN_ARROW loc
|> State.try_recover
| Some _ -> assert false
| None -> error_checkpoint)
| _ -> error_checkpoint)
| T_OF
when Poly.equal last_token T_USING
&& acceptable previous_checkpoint dummy_ident ->
State.Cursor.replace_token rest (token_to_ident T_OF) offending_loc
|> State.try_recover
| _ -> (
match last_token with
| T_VIRTUAL_SEMICOLON -> error_checkpoint
| _
when nl_separated rest offending_loc
&& acceptable previous_checkpoint Js_token.T_VIRTUAL_SEMICOLON ->
State.Cursor.insert_token rest semicolon dummy_loc
|> State.try_recover
| T_RPAREN
when acceptable
previous_checkpoint
Js_token.T_VIRTUAL_SEMICOLON_DO_WHILE ->
State.Cursor.insert_token rest semicolon dummy_loc
|> State.try_recover
| T_RCURLY
when acceptable
previous_checkpoint
Js_token.T_VIRTUAL_SEMICOLON_EXPORT_DEFAULT ->
State.Cursor.insert_token rest semicolon dummy_loc
|> State.try_recover
| _ -> error_checkpoint))
in
if phys_equal checkpoint error_checkpoint
then
match State.Cursor.last_token rest with
| None -> checkpoint
| Some (T_OF, last_token_loc, last_token_prev)
when match last last_token_prev with
| T_USING | T_ASYNC -> true
| _ -> false ->
State.Cursor.replace_token
last_token_prev
(token_to_ident T_OF)
last_token_loc
|> State.try_recover
| _ when acceptable previous_checkpoint T_YIELD_AWAIT_POP ->
State.Cursor.insert_token rest T_YIELD_AWAIT_POP dummy_loc
|> State.try_recover
| _ when acceptable previous_checkpoint T_YIELDOFF ->
State.Cursor.insert_token rest T_YIELDOFF dummy_loc |> State.try_recover
| _ when acceptable previous_checkpoint T_YIELDON ->
State.Cursor.insert_token rest T_YIELDON dummy_loc |> State.try_recover
| _ when acceptable previous_checkpoint T_AWAITON ->
State.Cursor.insert_token rest T_AWAITON dummy_loc |> State.try_recover
| _ when acceptable previous_checkpoint T_AWAITOFF ->
State.Cursor.insert_token rest T_AWAITOFF dummy_loc |> State.try_recover
| _ -> checkpoint
else checkpoint
let parse_aux the_parser yield_await_state (lexbuf : Lexer.t) =
let init = the_parser (Lexer.curr_pos lexbuf) in
let rec loop_error checkpoint =
match State.checkpoint checkpoint with
| InputNeeded _, _ | Shifting _, _ | AboutToReduce _, _ | Accepted _, _ ->
assert false
| Rejected, _ -> `Error checkpoint
| HandlingError _, _ -> loop_error checkpoint
in
let rec loop checkpoint previous_checkpoint =
match State.checkpoint checkpoint with
| Shifting _, _ | AboutToReduce _, _ -> assert false
| Accepted v, _ -> `Ok (v, checkpoint)
| Rejected, _ -> loop_error checkpoint
| InputNeeded _env, _ -> (
match State.pending checkpoint with
| None ->
let previous_checkpoint = checkpoint in
let new_checkpoint = offer_one checkpoint lexbuf in
loop new_checkpoint previous_checkpoint
| Some ((tok, loc), previous_checkpoint) ->
let new_checkpoint = State.offer previous_checkpoint tok loc in
loop new_checkpoint previous_checkpoint)
| HandlingError _, _ -> (
let error_checkpoint = checkpoint in
let new_checkpoint = recover error_checkpoint previous_checkpoint in
match State.checkpoint new_checkpoint with
| HandlingError _, _ -> (
let checkpoint = State.finalize_error new_checkpoint in
match State.checkpoint checkpoint with
| Rejected, _ -> `Error checkpoint
| _ -> assert false)
| _ -> loop new_checkpoint new_checkpoint)
in
let checkpoint = State.create init yield_await_state in
let res = loop checkpoint checkpoint in
match res with
| `Ok all -> all
| `Error t ->
let rec last cursor =
match State.Cursor.last_token cursor with
| None -> assert false
| Some
( ( T_VIRTUAL_SEMICOLON
| T_AWAITOFF
| T_AWAITON
| T_YIELDON
| T_YIELDOFF
| T_YIELD_AWAIT_POP )
, _
, cursor ) -> last cursor
| Some (_, loc, _) -> Loc.p1 loc
in
let p = last (State.cursor t) in
let rec lastn n acc cursor =
if n = 0
then acc
else
match State.Cursor.last_token cursor with
| None -> acc
| Some (tok, _, cursor) -> lastn (pred n) (tok :: acc) cursor
in
if debug ()
then
List.iter
(lastn 10 [] (State.cursor t))
~f:(fun tok -> Printf.eprintf "%s " (Js_token.to_string_extra tok));
if debug () then Printf.eprintf "\n";
raise (Parsing_error (Parse_info.t_of_pos p))
let fail_early =
object (m)
inherit Js_traverse.iter as super
method early_error p = raise (Parsing_error p.loc)
method statement s =
match s with
| Import (_, loc) -> raise (Parsing_error loc)
| Export (_, loc) -> raise (Parsing_error loc)
| _ -> super#statement s
method program p =
List.iter p ~f:(fun ((p : Javascript.statement), _loc) ->
match p with
| Import _ -> super#statement p
| Export (e, _) -> (
match e with
| CoverExportFrom e -> m#early_error e
| _ -> super#statement p)
| _ -> super#statement p)
end
let check_program p = List.iter p ~f:(function _, p -> fail_early#program [ p ])
let parse' script_or_module lex =
let p, toks =
match script_or_module with
| `Script ->
parse_aux Js_parser.Incremental.script { yield = false; await = false } lex
| `Module ->
parse_aux Js_parser.Incremental.module_ { yield = false; await = true } lex
in
check_program p;
let toks = State.all_tokens toks in
let take_annot_before =
let toks_r = ref toks in
let rec loop start_pos acc (toks : (Js_token.t * _) list) =
match toks with
| [] -> assert false
| (TAnnot a, loc) :: xs ->
loop start_pos ((a, Parse_info.t_of_pos (Loc.p1 loc)) :: acc) xs
| ((TComment _ | TCommentLineDirective _), _) :: xs -> loop start_pos acc xs
| (_, loc) :: xs ->
if Loc.cnum loc = start_pos.Lexing.pos_cnum
then (
toks_r := toks;
List.rev acc)
else loop start_pos [] xs
in
fun start_pos -> loop start_pos [] !toks_r
in
let p = List.map p ~f:(fun (start_pos, s) -> take_annot_before start_pos, s) in
let groups =
List.group p ~f:(fun a _pred ->
match a with
| [], _ -> true
| _ :: _, _ -> false)
in
let p =
List.map groups ~f:(function
| [] -> assert false
| (annot, _) :: _ as l -> annot, List.map l ~f:snd)
in
p, toks
let parse script_or_module lex =
let p, _ =
match script_or_module with
| `Script ->
parse_aux Js_parser.Incremental.script { yield = false; await = false } lex
| `Module ->
parse_aux Js_parser.Incremental.module_ { yield = false; await = true } lex
in
check_program p;
List.map p ~f:(fun (_, x) -> x)
let parse_expr lex =
let expr, _ =
parse_aux
Js_parser.Incremental.standalone_expression
{ yield = false; await = false }
lex
in
fail_early#expression expr;
expr