Skip to content

Commit

Permalink
Update vendored compilerlibs for OCaml 5.2
Browse files Browse the repository at this point in the history
  • Loading branch information
gpetiot committed Jan 16, 2024
1 parent 8418244 commit fe9f1f9
Show file tree
Hide file tree
Showing 4 changed files with 156 additions and 1 deletion.
2 changes: 1 addition & 1 deletion vendor/ocaml-common/location.ml
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ let print_updating_num_loc_lines ppf f arg =
pp_set_formatter_out_functions ppf out_functions

let setup_colors () =
Misc.Color.setup !Clflags.color
Misc.Style.setup !Clflags.color

(******************************************************************************)
(* Printing locations, e.g. 'File "foo.ml", line 3, characters 10-12' *)
Expand Down
2 changes: 2 additions & 0 deletions vendor/parser-shims/dune
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
(library
(name ocamlformat_parser_shims)
(public_name ocamlformat-lib.parser_shims)
(flags
(:standard -w -37 -w -38))
(libraries compiler-libs.common))
145 changes: 145 additions & 0 deletions vendor/parser-shims/ocamlformat_parser_shims.ml
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,159 @@ module Misc = struct
module Color = struct
include Color

external isatty : out_channel -> bool = "caml_sys_isatty"

(* reasonable heuristic on whether colors should be enabled *)
let should_enable_color () =
let term = try Sys.getenv "TERM" with Not_found -> "" in
term <> "dumb"
&& term <> ""
&& isatty stderr

let default_setting = Auto
let enabled = ref true
end

module Error_style = struct
include Error_style

let default_setting = Contextual
end

module Style = struct
(* use ANSI color codes, see https://en.wikipedia.org/wiki/ANSI_escape_code *)
type color =
| Black
| Red
| Green
| Yellow
| Blue
| Magenta
| Cyan
| White

type style =
| FG of color (* foreground *)
| BG of color (* background *)
| Bold
| Reset

let ansi_of_color = function
| Black -> "0"
| Red -> "1"
| Green -> "2"
| Yellow -> "3"
| Blue -> "4"
| Magenta -> "5"
| Cyan -> "6"
| White -> "7"

let code_of_style = function
| FG c -> "3" ^ ansi_of_color c
| BG c -> "4" ^ ansi_of_color c
| Bold -> "1"
| Reset -> "0"

let ansi_of_style_l l =
let s = match l with
| [] -> code_of_style Reset
| [s] -> code_of_style s
| _ -> String.concat ";" (List.map code_of_style l)
in
"\x1b[" ^ s ^ "m"

type Format.stag += Style of style list

type tag_style ={
ansi: style list;
text_open:string;
text_close:string
}

type styles = {
error: tag_style;
warning: tag_style;
loc: tag_style;
hint: tag_style;
inline_code: tag_style;
}

let no_markup stl = { ansi = stl; text_close = ""; text_open = "" }

let default_styles = {
warning = no_markup [Bold; FG Magenta];
error = no_markup [Bold; FG Red];
loc = no_markup [Bold];
hint = no_markup [Bold; FG Blue];
inline_code= { ansi=[Bold]; text_open = {|"|}; text_close = {|"|} }
}

let cur_styles = ref default_styles

(* map a tag to a style, if the tag is known.
@raise Not_found otherwise *)
let style_of_tag s = match s with
| Format.String_tag "error" -> (!cur_styles).error
| Format.String_tag "warning" ->(!cur_styles).warning
| Format.String_tag "loc" -> (!cur_styles).loc
| Format.String_tag "hint" -> (!cur_styles).hint
| Format.String_tag "inline_code" -> (!cur_styles).inline_code
| Style s -> no_markup s
| _ -> raise Not_found

let as_inline_code printer ppf x =
Format.pp_open_stag ppf (Format.String_tag "inline_code");
printer ppf x;
Format.pp_close_stag ppf ()

let inline_code ppf s = as_inline_code Format.pp_print_string ppf s

(* either prints the tag of [s] or delegates to [or_else] *)
let mark_open_tag ~or_else s =
try
let style = style_of_tag s in
if !Color.enabled then ansi_of_style_l style.ansi else style.text_open
with Not_found -> or_else s

let mark_close_tag ~or_else s =
try
let style = style_of_tag s in
if !Color.enabled then ansi_of_style_l [Reset] else style.text_close
with Not_found -> or_else s

(* add tag handling to formatter [ppf] *)
let set_tag_handling ppf =
let open Format in
let functions = pp_get_formatter_stag_functions ppf () in
let functions' = {functions with
mark_open_stag=(mark_open_tag ~or_else:functions.mark_open_stag);
mark_close_stag=(mark_close_tag ~or_else:functions.mark_close_stag);
} in
pp_set_mark_tags ppf true; (* enable tags *)
pp_set_formatter_stag_functions ppf functions';
()

let setup =
let first = ref true in (* initialize only once *)
let formatter_l =
[Format.std_formatter; Format.err_formatter; Format.str_formatter]
in
let enable_color = function
| Color.Auto -> Color.should_enable_color ()
| Color.Always -> true
| Color.Never -> false
in
fun o ->
if !first then (
first := false;
Format.set_mark_tags true;
List.iter set_tag_handling formatter_l;
Color.enabled := (match o with
| Some s -> enable_color s
| None -> enable_color Color.default_setting)
);
()
end
end

module Clflags = struct
Expand Down
8 changes: 8 additions & 0 deletions vendor/parser-shims/ocamlformat_parser_shims.mli
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ module Misc : sig
val default_setting : setting
(** @since ocaml-4.09 *)
end

module Style : sig
val inline_code: Format.formatter -> string -> unit
(** @since ocaml-5.2 *)

val setup : Color.setting option -> unit
(** @since ocaml-5.2 *)
end
end

module Clflags : sig
Expand Down

0 comments on commit fe9f1f9

Please sign in to comment.