···991010let is_mangled_broken orig src =
1111 String.length orig <> String.length src
1212- (* ||
1313- Seq.exists2 (fun c c' ->
1414- (c' = ' ' || c = c')) (String.to_seq orig) (String.to_seq src) *)
1212+ ||
1313+ Seq.exists2 (fun c c' ->
1414+ c <> c' && c' <> ' ') (String.to_seq orig) (String.to_seq src)
15151616let mangle_toplevel is_toplevel orig_source deps =
1717 let src =
···2424 else begin
2525 try
2626 let s = String.sub orig_source 2 (String.length orig_source - 2) in
2727- let list = Ocamltop.parse_toplevel s in
2828- let buff = Buffer.create 100 in
2929- List.iter (fun (phr, junk, output) ->
3030- Printf.bprintf buff " %s%s\n" phr (String.make (String.length junk) ' ');
3131- let s = List.map (fun x ->
3232- Printf.sprintf "%s" (String.make (String.length x) ' ')) output
3333- in
3434- Buffer.add_string buff (String.concat "\n" s);
3535- ()) list;
3636- Buffer.contents buff
2727+ let list =
2828+ try Ocamltop.parse_toplevel s with _ -> Ocamltop.fallback_parse_toplevel s in
2929+ let lines =List.map (fun (phr, junk, output) ->
3030+ let l1 = Printf.sprintf " %s%s" phr (String.make (String.length junk) ' ') in
3131+ match output with
3232+ | [] -> l1
3333+ | _ ->
3434+ let s = List.map (fun x ->
3535+ String.make (String.length x) ' ') output
3636+ in
3737+ (String.concat "\n" (l1 :: s));
3838+ ) list in
3939+ String.concat "\n" lines
3740 with e ->
3841 Logs.err (fun m -> m "Error in mangle_toplevel: %s" (Printexc.to_string e));
3942 let ppf = Format.err_formatter in
···5154 Printf.printf "Warning: mangled source is broken\n%!";
5255 Printf.printf "orig length: %d\n%!" (String.length orig_source);
5356 Printf.printf "src length: %d\n%!" (String.length src);
5454- failwith "broken"
5557 );
5658 line1, src
5759
+14-16
lib/ocamltop.ml
···99 p := !p + len'';
1010 len''
11111212-let parse_toplevel s =
1313- let legacy_warn =
1414- let b = ref false in
1515- fun () ->
1616- if !b
1717- then ()
1818- else
1919- (Logs.warn (fun m -> m "Warning: Legacy toplevel output detected");
2020- b := true)
1212+let fallback_parse_toplevel s =
1313+ Printf.printf "fallback parser\n%!";
1414+ let lexbuf = Lexing.from_string s in
1515+ let rec loop pos =
1616+ let _phr = Toplexer.fallback_expression lexbuf in
1717+ Printf.printf "Got phrase\n%!";
1818+ let new_pos = Lexing.lexeme_end lexbuf in
1919+ let phr = String.sub s pos (new_pos - pos) in
2020+ let (junk, (cont, output)) = Toplexer.entry lexbuf in
2121+ let new_pos = Lexing.lexeme_end lexbuf in
2222+ if cont then (phr, junk, output) :: loop new_pos else [ (phr, junk, output) ]
2123 in
2424+ loop 0
22252626+let parse_toplevel s =
2327 let lexbuf = Lexing.from_string s in
2428 let rec loop pos =
2529 let _phr = !Toploop.parse_toplevel_phrase lexbuf in
2630 let new_pos = Lexing.lexeme_end lexbuf in
2731 let phr = String.sub s pos (new_pos - pos) in
2828- let (junk, (cont, is_legacy, output)) = Toplexer.entry lexbuf in
2929- let output =
3030- if is_legacy then begin
3131- legacy_warn ();
3232- output
3333- end else output
3434- in
3232+ let (junk, (cont, output)) = Toplexer.entry lexbuf in
3533 let new_pos = Lexing.lexeme_end lexbuf in
3634 if cont then (phr, junk, output) :: loop new_pos else [ (phr, junk, output) ]
3735 in
···1212 {mime_vals:[];parts:[];script:S(# Printf.printf "Hello, world\n";;
1313 Hello, world
1414 - : unit = ())}
1515- unix_worker: [WARNING] Warning: Legacy toplevel output detected
1615 {mime_vals:[];parts:[];script:S(# let x = 1 + 2;;
1716 val x : int = 3
1817 # let x = 2+3;;
+42-33
test/libtest/parse_test.ml
···18181919let%expect_test _ =
2020 check "# foo;; junk\n bar\n# baz;;\n moo\n# unterminated;; foo\n";
2121- [%expect.unreachable]
2222-[@@expect.uncaught_exn {|
2323- (* CR expect_test_collector: This test expectation appears to contain a backtrace.
2424- This is strongly discouraged as backtraces are fragile.
2525- Please change this test to not include a backtrace. *)
2121+ [%expect{|
2222+ input:
2323+ # foo;; junk
2424+ bar
2525+ # baz;;
2626+ moo
2727+ # unterminated;; foo
26282727- (Failure broken)
2828- Called from unknown location
2929- Called from unknown location
3030- Called from unknown location
3131- Called from unknown location
2929+ output:
3030+ foo;;
32313333- Trailing output
3434- ---------------
3535- Warning: mangled source is broken
3636- orig length: 54
3737- src length: 52 |}]
3232+ baz;;
3333+3434+ unterminated;;
3535+3636+ output mapped:
3737+ ..foo;;.....
3838+ .....
3939+ ..baz;;
4040+ .....
4141+ ..unterminated;;.... |}]
38423943let%expect_test _ =
4044 check "# 1+2;;\n- 3 : int\n \n";
···56605761let%expect_test _ =
5862 check "# 1+2;;";
5959- [%expect.unreachable]
6060-[@@expect.uncaught_exn {|
6161- (* CR expect_test_collector: This test expectation appears to contain a backtrace.
6262- This is strongly discouraged as backtraces are fragile.
6363- Please change this test to not include a backtrace. *)
6464-6565- (Failure broken)
6666- Called from unknown location
6767- Called from unknown location
6868- Called from unknown location
6969- Called from unknown location
7070-7171- Trailing output
7272- ---------------
7373- Warning: mangled source is broken
7474- orig length: 7
7575- src length: 8 |}]
6363+ [%expect{|
6464+ input:
6565+ # 1+2;;
6666+ output:
6767+ 1+2;;
6868+ output mapped:
6969+ ..1+2;; |}]
76707771let%expect_test _ =
7872 check "# 1+2;;\nx\n";
···87818882 output mapped:
8983 ..1+2;;
9090- . |}]8484+ . |}]
8585+8686+let%expect_test _ =
8787+ check "# let ;;\n foo";
8888+ [%expect "
8989+ fallback parser
9090+ Got phrase
9191+ input:
9292+ # let ;;
9393+ foo
9494+ output:
9595+ let ;;
9696+9797+ output mapped:
9898+ ..let.;;
9999+ ....."]