Mentions légales du service

Skip to content

Add markers to Mlw_printer

Benedikt Becker requested to merge marker-for-mlw-printer into master

When Ptree elements are generated (instead of being parsed from a whyml file), locations of typing errors are useless, because they do not correspond to any concrete syntax.

This MR introduces two functions Mlw_printer.next_pos and Mlw_printer.with_marker.

We can give every Ptree element a unique location by using the function Mlw_printer.next_pos. When a located error is encountered, the function with_marker can then be used to instruct the mlw-printer to insert a message as a comment just before an expression, term, or pattern with the given location.

For example, this can be used to indicate and show a typing error in the printed mlw-file:

try
  let mm = Typing.type_mlw_file env path filename mlw_file in
  (* ... well typed mlw_file ... *)
with Loc.Located (loc, e) -> (* A located exception [e] *)
   let msg = Format.asprintf "%a" Exn_printer.exn_printer e in
   Format.fprintf fmt "%a@."
    (Mlw_printer.with_marker ~msg loc Mlw_printer.pp_mlw_file)
    mlw_file

Merge request reports