Debian Codegraph @

test-suite/unit-tests/parsing/lexer_recovery.ml

raw symbols SCIP
1 (* The idea of this test is to have the lexer crash at a new line,
2 with the ∀ symbol in line 2; then we test that we can recover correctly. *)
3 let doc =
4 "Definition map_union_weak `{β A, Insert K A (M A),
5 ∀ A, Empty (M A),
6 ∀ A, Lookup K A (M A),
7 ∀ A, FinMapToList K A (M A)} {A} (m1 m2 : M A) :=
8 map_imap (λ l v, Some (default v (m1 !! l))) m2."
9
10 let parse pa n =
11 let entry = Pvernac.Vernac_.main_entry in
12 let rec loop res n =
13 if n = 0 then res else
14 match Procq.Entry.parse entry pa with
15 | None -> res
16 | Some r -> loop (r :: res) (n-1)
17 in
18 loop [] n |> List.rev
19
20 let raw_pr_loc fmt (l : Loc.t) =
21 let { Loc.fname=_; line_nb; bol_pos; line_nb_last; bol_pos_last; bp; ep } = l in
22 Format.fprintf fmt "| line_nb: %d | bol_pos: %d | line_nb_last: %d | bol_pos_last: %d | bp: %d | ep: %d |"
23 line_nb bol_pos line_nb_last bol_pos_last bp ep
24
25 let print_locs fmt { CAst.loc; _ } =
26 Option.iter (Format.fprintf fmt "@[%a@]" raw_pr_loc) loc
27
28 let setup_pa () =
29 let text = doc in
30 Procq.Parsable.make (Gramlib.Stream.of_string text)
31
32 let parse_whole pa =
33 parse pa 10
34
35 (* Use junk *)
36 let log_file = __FILE__ ^ ".log"
37
38 let main () =
39 let pa = setup_pa () in
40 let res, loc =
41 try
42 let _ = parse_whole pa in
43 false, Procq.Parsable.loc pa
44 with
45 (* should be `E Undefined_token` but type is private *)
46 | CLexer.Error.E _ ->
47 (* We now consume a single token and check that the location is
48 correct for "A" *)
49 let () = Procq.Parsable.consume pa 1 in
50 let loc = Procq.Parsable.loc pa in
51 let res = (loc.line_nb = 2) && (loc.bol_pos = 52) && (loc.bp = 58) && (loc.ep = 59) in
52 res, loc
53 | _ -> false, Procq.Parsable.loc pa
54 in
55 let oc = Stdlib.open_out log_file in
56 let outf = Format.formatter_of_out_channel oc in
57 Format.fprintf outf "fail lexer test passed: %a@\n%!" raw_pr_loc loc;
58 Format.pp_print_flush outf ();
59 Stdlib.close_out oc;
60 if res then exit 0 else exit 1
61
62 let () = main ()