From 4c28fa07758688677c4449f7d1badd2bffcbdf1b Mon Sep 17 00:00:00 2001 From: Stephen Sherratt Date: Fri, 20 Sep 2024 19:24:07 +1000 Subject: [PATCH] chore: update vendored pp to 2.0.0 (#10938) Signed-off-by: Stephen Sherratt --- dune-project | 2 +- otherlibs/dune-rpc/private/exported_types.ml | 18 +-- otherlibs/stdune/src/stdune.ml | 41 +++--- otherlibs/stdune/test/ansi_color_tests.ml | 5 +- stdune.opam | 2 +- vendor/pp/src/pp.ml | 70 ++-------- vendor/pp/src/pp.mli | 138 ++++++++++--------- vendor/update-pp.sh | 2 +- 8 files changed, 109 insertions(+), 169 deletions(-) diff --git a/dune-project b/dune-project index cca9f1c925a..2d69442a14a 100644 --- a/dune-project +++ b/dune-project @@ -189,7 +189,7 @@ understood by dune language.")) base-unix (dyn (= :version)) (ordering (= :version)) - (pp (>= 1.2.0)) + (pp (>= 2.0.0)) (csexp (>= 1.5.0))) (description "This library offers no backwards compatibility guarantees. Use at your own risk.")) diff --git a/otherlibs/dune-rpc/private/exported_types.ml b/otherlibs/dune-rpc/private/exported_types.ml index 2e7d04081ac..7d59b925f77 100644 --- a/otherlibs/dune-rpc/private/exported_types.ml +++ b/otherlibs/dune-rpc/private/exported_types.ml @@ -379,14 +379,7 @@ let sexp_pp_unit : unit Pp.t Conv.value = | Text s -> case s text | Tag ((), t) -> case t tag)) in - let to_ast x = - match Pp.to_ast x with - | Ok s -> s - | Error () -> - (* We don't use the format constructor in dune. *) - assert false - in - iso t Pp.of_ast to_ast + iso t Pp.of_ast Pp.to_ast ;; module Diagnostic = struct @@ -469,14 +462,7 @@ module Diagnostic = struct | Text s -> case s text | Tag (s, t) -> case (s, t) tag)) in - let to_ast x = - match Pp.to_ast x with - | Ok s -> s - | Error () -> - (* We don't use the format constructor in dune. *) - assert false - in - iso t Pp.of_ast to_ast + iso t Pp.of_ast Pp.to_ast ;; module Id = struct diff --git a/otherlibs/stdune/src/stdune.ml b/otherlibs/stdune/src/stdune.ml index a8bcc2c6ece..37f7e7cdf26 100644 --- a/otherlibs/stdune/src/stdune.ml +++ b/otherlibs/stdune/src/stdune.ml @@ -35,29 +35,26 @@ module Pp = struct ;; let to_dyn tag_to_dyn t = - match Pp.to_ast t with - | Error _ -> Dyn.variant "Contains Format" [ Dyn.opaque "" ] - | Ok t -> + let rec to_dyn t = let open Dyn in - let rec to_dyn t = - match (t : _ Pp.Ast.t) with - | Nop -> variant "Nop" [] - | Seq (x, y) -> variant "Seq" [ to_dyn x; to_dyn y ] - | Concat (x, y) -> variant "Concat" [ to_dyn x; list to_dyn y ] - | Box (i, t) -> variant "Box" [ int i; to_dyn t ] - | Vbox (i, t) -> variant "Vbox" [ int i; to_dyn t ] - | Hbox t -> variant "Hbox" [ to_dyn t ] - | Hvbox (i, t) -> variant "Hvbox" [ int i; to_dyn t ] - | Hovbox (i, t) -> variant "Hovbox" [ int i; to_dyn t ] - | Verbatim s -> variant "Verbatim" [ string s ] - | Char c -> variant "Char" [ char c ] - | Break (x, y) -> - variant "Break" [ triple string int string x; triple string int string y ] - | Newline -> variant "Newline" [] - | Text s -> variant "Text" [ string s ] - | Tag (s, t) -> variant "Tag" [ tag_to_dyn s; to_dyn t ] - in - to_dyn t + match (t : _ Pp.Ast.t) with + | Nop -> variant "Nop" [] + | Seq (x, y) -> variant "Seq" [ to_dyn x; to_dyn y ] + | Concat (x, y) -> variant "Concat" [ to_dyn x; list to_dyn y ] + | Box (i, t) -> variant "Box" [ int i; to_dyn t ] + | Vbox (i, t) -> variant "Vbox" [ int i; to_dyn t ] + | Hbox t -> variant "Hbox" [ to_dyn t ] + | Hvbox (i, t) -> variant "Hvbox" [ int i; to_dyn t ] + | Hovbox (i, t) -> variant "Hovbox" [ int i; to_dyn t ] + | Verbatim s -> variant "Verbatim" [ string s ] + | Char c -> variant "Char" [ char c ] + | Break (x, y) -> + variant "Break" [ triple string int string x; triple string int string y ] + | Newline -> variant "Newline" [] + | Text s -> variant "Text" [ string s ] + | Tag (s, t) -> variant "Tag" [ tag_to_dyn s; to_dyn t ] + in + to_dyn (Pp.to_ast t) ;; end diff --git a/otherlibs/stdune/test/ansi_color_tests.ml b/otherlibs/stdune/test/ansi_color_tests.ml index 540a4eb0325..f98c4a498d6 100644 --- a/otherlibs/stdune/test/ansi_color_tests.ml +++ b/otherlibs/stdune/test/ansi_color_tests.ml @@ -21,10 +21,7 @@ let dyn_of_pp tag pp = | Tag (ta, t) -> variant "Tag" [ tag ta; conv t ] | Text s -> variant "Text" [ string s ] in - conv - (match Pp.to_ast pp with - | Ok s -> s - | Error () -> assert false) + conv (Pp.to_ast pp) ;; let%expect_test "reproduce #2664" = diff --git a/stdune.opam b/stdune.opam index e6c2d840b57..e455fafbcb8 100644 --- a/stdune.opam +++ b/stdune.opam @@ -15,7 +15,7 @@ depends: [ "base-unix" "dyn" {= version} "ordering" {= version} - "pp" {>= "1.2.0"} + "pp" {>= "2.0.0"} "csexp" {>= "1.5.0"} "odoc" {with-doc} ] diff --git a/vendor/pp/src/pp.ml b/vendor/pp/src/pp.ml index 8a088c682cc..afaa7191e3f 100644 --- a/vendor/pp/src/pp.ml +++ b/vendor/pp/src/pp.ml @@ -24,59 +24,12 @@ module Ast = struct | Tag of 'a * 'a t end -type +'a t = - | Nop - | Seq of 'a t * 'a t - | Concat of 'a t * 'a t list - | Box of int * 'a t - | Vbox of int * 'a t - | Hbox of 'a t - | Hvbox of int * 'a t - | Hovbox of int * 'a t - | Verbatim of string - | Char of char - | Break of (string * int * string) * (string * int * string) - | Newline - | Text of string - | Tag of 'a * 'a t - | Format of (Format.formatter -> unit) +include Ast -let rec of_ast : 'a. 'a Ast.t -> 'a t = function - | Nop -> Nop - | Seq (x, y) -> Seq (of_ast x, of_ast y) - | Concat (x, y) -> Concat (of_ast x, List.map ~f:of_ast y) - | Box (x, y) -> Box (x, of_ast y) - | Vbox (x, y) -> Vbox (x, of_ast y) - | Hbox x -> Hbox (of_ast x) - | Hvbox (x, y) -> Hvbox (x, of_ast y) - | Hovbox (x, y) -> Hovbox (x, of_ast y) - | Verbatim s -> Verbatim s - | Char c -> Char c - | Break (x, y) -> Break (x, y) - | Newline -> Newline - | Text s -> Text s - | Tag (a, x) -> Tag (a, of_ast x) +let of_ast = Fun.id +let to_ast = Fun.id -let to_ast x = - let rec to_ast : 'a t -> 'a Ast.t = function - | Nop -> Nop - | Seq (x, y) -> Seq (to_ast x, to_ast y) - | Concat (x, y) -> Concat (to_ast x, List.map ~f:(fun x -> to_ast x) y) - | Box (x, y) -> Box (x, to_ast y) - | Vbox (x, y) -> Vbox (x, to_ast y) - | Hbox x -> Hbox (to_ast x) - | Hvbox (x, y) -> Hvbox (x, to_ast y) - | Hovbox (x, y) -> Hovbox (x, to_ast y) - | Verbatim s -> Verbatim s - | Char c -> Char c - | Break (x, y) -> Break (x, y) - | Newline -> Newline - | Tag (a, x) -> Tag (a, to_ast x) - | Text s -> Text s - | Format _ -> raise_notrace Exit - in - try Ok (to_ast x) with - | Exit -> Error () +type ('a, 'tag) format_string = ('a, unit, string, 'tag t) format4 let rec map_tags t ~f = match t with @@ -90,7 +43,6 @@ let rec map_tags t ~f = | Hovbox (indent, t) -> Hovbox (indent, map_tags t ~f) | (Verbatim _ | Char _ | Break _ | Newline | Text _) as t -> t | Tag (tag, t) -> Tag (f tag, map_tags t ~f) - | Format f -> Format f let rec filter_map_tags t ~f = match t with @@ -109,7 +61,6 @@ let rec filter_map_tags t ~f = match f tag with | None -> t | Some tag -> Tag (tag, t)) - | Format f -> Format f module Render = struct open Format @@ -152,7 +103,6 @@ module Render = struct | Newline -> pp_force_newline ppf () | Text s -> pp_print_text ppf s | Tag (tag, t) -> tag_handler ppf tag t - | Format f -> f ppf end let to_fmt_with_tags = Render.render @@ -186,6 +136,7 @@ let hbox t = Hbox t let hvbox ?(indent = 0) t = Hvbox (indent, t) let hovbox ?(indent = 0) t = Hovbox (indent, t) let verbatim x = Verbatim x +let verbatimf fmt = Printf.ksprintf verbatim fmt let char x = Char x let custom_break ~fits ~breaks = Break (fits, breaks) @@ -196,8 +147,10 @@ let space = break ~nspaces:1 ~shift:0 let cut = break ~nspaces:0 ~shift:0 let newline = Newline let text s = Text s -let textf fmt = Printf.ksprintf text fmt +let textf (fmt : ('a, 'tag) format_string) = Printf.ksprintf text fmt let tag tag t = Tag (tag, t) +let paragraph s = hovbox (text s) +let paragraphf (fmt : ('a, 'tag) format_string) = Printf.ksprintf paragraph fmt let enumerate l ~f = vbox @@ -221,8 +174,6 @@ module O = struct let ( ++ ) = seq end -let of_fmt f x = Format (fun ppf -> f ppf x) - let compare = let compare_both (type a b) (f : a -> a -> int) (g : b -> b -> int) (a, b) (c, d) = @@ -297,10 +248,5 @@ let compare = | Text _, _ -> -1 | _, Text _ -> 1 | Tag (a, b), Tag (c, d) -> compare_both compare_tag compare (a, b) (c, d) - | Format _, Format _ -> - raise - (Invalid_argument "[Pp.of_fmt] values not supported in [Pp.compare]") - | Format _, _ -> -1 - | _, Format _ -> 1 in compare diff --git a/vendor/pp/src/pp.mli b/vendor/pp/src/pp.mli index dd1f121688b..c75b6e7d87f 100644 --- a/vendor/pp/src/pp.mli +++ b/vendor/pp/src/pp.mli @@ -1,37 +1,45 @@ (** Pretty-printing. *) -(** A document that is not yet rendered. The argument is the type of tags in the - document. For instance tags might be used for styles. *) +(** ['tag t] represents a document that is not yet rendered. The argument ['tag] + is the type of tags in the document. For instance tags might be used for + styles. + + If you want to serialise and deserialise this datastructure, you can use the + [Ast.t] type together with the [of_ast] and [to_ast] functions. *) type +'tag t (** {1 Basic combinators} *) (** A pretty printer that prints nothing *) -val nop : _ t +val nop : 'tag t (** [seq x y] prints [x] and then [y] *) -val seq : 'a t -> 'a t -> 'a t +val seq : 'tag t -> 'tag t -> 'tag t (** [concat ?sep l] prints elements in [l] separated by [sep]. [sep] defaults to [nop]. *) -val concat : ?sep:'a t -> 'a t list -> 'a t +val concat : ?sep:'tag t -> 'tag t list -> 'tag t + +(** Convenience function for [List.map] followed by [concat]. *) +val concat_map : ?sep:'tag t -> 'a list -> f:('a -> 'tag t) -> 'tag t -(** Convenience function for [List.map] followed by [concat] *) -val concat_map : ?sep:'a t -> 'b list -> f:('b -> 'a t) -> 'a t +(** Convenience function for [List.mapi] followed by [concat]. *) +val concat_mapi : ?sep:'tag t -> 'a list -> f:(int -> 'a -> 'tag t) -> 'tag t -val concat_mapi : ?sep:'a t -> 'b list -> f:(int -> 'b -> 'a t) -> 'a t +(** An indivisible block of text. *) +val verbatim : string -> 'tag t -(** An indivisible block of text *) -val verbatim : string -> _ t +(** Same as [verbatim] but take a format string as argument. *) +val verbatimf : ('a, unit, string, 'tag t) format4 -> 'a -(** A single character *) -val char : char -> _ t +(** A single character. *) +val char : char -> 'tag t (** Print a bunch of text. The line may be broken at any spaces in the text. *) -val text : string -> _ t +val text : string -> 'tag t (** Same as [text] but take a format string as argument. *) -val textf : ('a, unit, string, _ t) format4 -> 'a +val textf : ('a, unit, string, 'tag t) format4 -> 'a (** {1 Break hints} *) @@ -41,7 +49,7 @@ val textf : ('a, unit, string, _ t) format4 -> 'a So for instance [verbatim "x" ++ space ++ verbatim "y"] might produce "x y" or "x\ny". *) -val space : _ t +val space : 'tag t (** [cut] instructs the pretty-printing algorithm that the line may be broken at this point. If the algorithm decides not to break the line, nothing is @@ -49,14 +57,14 @@ val space : _ t So for instance [verbatim "x" ++ space ++ verbatim "y"] might produce "xy" or "x\ny". *) -val cut : _ t +val cut : 'tag t (** [break] is a generalisation of [space] and [cut]. It also instructs the pretty-printing algorithm that the line may be broken at this point. If it ends up being broken, [shift] will be added to the indentation level, otherwise [nspaces] spaces will be printed. [shift] can be negative, in which case the indentation will be reduced. *) -val break : nspaces:int -> shift:int -> _ t +val break : nspaces:int -> shift:int -> 'tag t (** [custom_break ~fits:(a, b, c) ~breaks:(x, y, z)] is a generalisation of [break]. It also instructs the pretty-printing algorithm that the line may @@ -66,10 +74,13 @@ val break : nspaces:int -> shift:int -> _ t printed. The indentation [y] can be negative, in which case the indentation will be reduced. *) val custom_break : - fits:string * int * string -> breaks:string * int * string -> _ t + fits:string * int * string -> breaks:string * int * string -> 'tag t -(** Force a newline to be printed *) -val newline : _ t +(** Force a newline to be printed. Usage is discourage since it breaks printing + with boxes. If you need to add breaks to your text, put your items into + [box]es and [concat] with a separating [space] afterwhich wrapping it in a + [vbox]. *) +val newline : 'tag t (** {1 Boxes} *) @@ -105,20 +116,20 @@ val newline : _ t (** Try to put as much as possible on each line. Additionally, a break hint always break the line if the breaking would reduce the indentation level inside the box ([break] with negative [shift] value). *) -val box : ?indent:int -> 'a t -> 'a t +val box : ?indent:int -> 'tag t -> 'tag t (** Always break the line when encountering a break hint. *) -val vbox : ?indent:int -> 'a t -> 'a t +val vbox : ?indent:int -> 'tag t -> 'tag t (** Print everything on one line, no matter what *) -val hbox : 'a t -> 'a t +val hbox : 'tag t -> 'tag t (** If possible, print everything on one line. Otherwise, behave as a [vbox] *) -val hvbox : ?indent:int -> 'a t -> 'a t +val hvbox : ?indent:int -> 'tag t -> 'tag t (** Try to put as much as possible on each line. Basically the same as [box] but without the rule about breaks with negative [shift] value. *) -val hovbox : ?indent:int -> 'a t -> 'a t +val hovbox : ?indent:int -> 'tag t -> 'tag t (** {1 Tags} *) @@ -127,15 +138,25 @@ val hovbox : ?indent:int -> 'a t -> 'a t terminal with colors. *) (** [tag x t] Tag the material printed by [t] with [x] *) -val tag : 'a -> 'a t -> 'a t +val tag : 'tag -> 'tag t -> 'tag t (** Convert tags in a documents *) -val map_tags : 'a t -> f:('a -> 'b) -> 'b t +val map_tags : 'from_tag t -> f:('from_tag -> 'to_tag) -> 'to_tag t -val filter_map_tags : 'a t -> f:('a -> 'b option) -> 'b t +(** Convert tags in a documents, possibly removing some tags. *) +val filter_map_tags : + 'from_tag t -> f:('from_tag -> 'to_tag option) -> 'to_tag t (** {1 Convenience functions} *) +(** [paragraph s] is [hovbox (text s)]. This is useful to preserve the structure + of a paragraph of text without worrying about it being broken by a [vbox]. *) +val paragraph : string -> 'tag t + +(** [paragraphf s] is [textf s] followed by a [hovbox]. The [textf] version of + [paragraph]. *) +val paragraphf : ('a, unit, string, 'tag t) format4 -> 'a + (** [enumerate l ~f] produces an enumeration of the form: {v @@ -144,7 +165,7 @@ val filter_map_tags : 'a t -> f:('a -> 'b option) -> 'b t - item3 ... v} *) -val enumerate : 'a list -> f:('a -> 'b t) -> 'b t +val enumerate : 'a list -> f:('a -> 'tag t) -> 'tag t (** [chain l ~f] is used to print a succession of items that follow each other. It produces an output of this form: @@ -155,66 +176,59 @@ val enumerate : 'a list -> f:('a -> 'b t) -> 'b t -> item3 ... v} *) -val chain : 'a list -> f:('a -> 'b t) -> 'b t +val chain : 'a list -> f:('a -> 'tag t) -> 'tag t (** {1 Operators} *) module O : sig + (** Infix operators for [Pp.t] *) + (** Same as [seq] *) - val ( ++ ) : 'a t -> 'a t -> 'a t + val ( ++ ) : 'tag t -> 'tag t -> 'tag t end (** {1 Rendering} *) (** Render a document to a classic formatter *) -val to_fmt : Format.formatter -> _ t -> unit +val to_fmt : Format.formatter -> 'tag t -> unit val to_fmt_with_tags : Format.formatter - -> 'a t - -> tag_handler:(Format.formatter -> 'a -> 'a t -> unit) + -> 'tag t + -> tag_handler:(Format.formatter -> 'tag -> 'tag t -> unit) -> unit -(** {1 Injection} *) - -(** Inject a classic formatter in a document. - - Disclaimer: this function is to meant to help using [Pp] in existing code - that already use the [Format] module without having to port everything to - [Pp]. It is not meant as the normal way to create [Pp.t] values. *) -val of_fmt : (Format.formatter -> 'a -> unit) -> 'a -> _ t - (** {1 Ast} *) module Ast : sig - (** Stable representation useful for serialization *) - type 'a t = + (** Stable representation of [Pp.t] useful for serialization *) + + (** Stable abstract syntax tree for [Pp.t] that can be used for serialization + and deserialization. *) + type +'tag t = | Nop - | Seq of 'a t * 'a t - | Concat of 'a t * 'a t list - | Box of int * 'a t - | Vbox of int * 'a t - | Hbox of 'a t - | Hvbox of int * 'a t - | Hovbox of int * 'a t + | Seq of 'tag t * 'tag t + | Concat of 'tag t * 'tag t list + | Box of int * 'tag t + | Vbox of int * 'tag t + | Hbox of 'tag t + | Hvbox of int * 'tag t + | Hovbox of int * 'tag t | Verbatim of string | Char of char | Break of (string * int * string) * (string * int * string) | Newline | Text of string - | Tag of 'a * 'a t + | Tag of 'tag * 'tag t end -(** [of_ast t] [Ast.t] to [Pp.t] *) -val of_ast : 'a Ast.t -> 'a t +(** [of_ast t] converts an [Ast.t] to a [Pp.t]. *) +val of_ast : 'tag Ast.t -> 'tag t -(** [to_ast t] will try to convert [t] to [Ast.t]. When [t] contains values - constructed with [of_fmt], this function will fail and return [Error ()] *) -val to_ast : 'a t -> ('a Ast.t, unit) result +(** [to_ast t] converts a [Pp.t] to an [Ast.t]. *) +val to_ast : 'tag t -> 'tag Ast.t (** {1 Comparison} *) -(** [compare cmp x y] compares [x] and [y] using [cmp] to compare tags. - - @raise Invalid_argument if two [of_fmt] values are compared. *) -val compare : ('a -> 'a -> int) -> 'a t -> 'a t -> int +(** [compare cmp x y] compares [x] and [y] using [cmp] to compare tags. *) +val compare : ('tag -> 'tag -> int) -> 'tag t -> 'tag t -> int diff --git a/vendor/update-pp.sh b/vendor/update-pp.sh index 74b20c82216..d2f5102efcc 100755 --- a/vendor/update-pp.sh +++ b/vendor/update-pp.sh @@ -1,6 +1,6 @@ #!/bin/sh -version=0021887f75a6017486908cfd837b2ee126e063e7 +version=b6741dd41ef5fc5bda8b3640097ac29818a43577 set -e -o pipefail