Skip to content

Commit

Permalink
chore: update vendored pp to 2.0.0 (ocaml#10938)
Browse files Browse the repository at this point in the history
Signed-off-by: Stephen Sherratt <stephen@sherra.tt>
  • Loading branch information
gridbugs authored and anmonteiro committed Nov 17, 2024
1 parent c59a73a commit 0539a11
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 169 deletions.
2 changes: 1 addition & 1 deletion dune-project
Original file line number Diff line number Diff line change
Expand Up @@ -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."))

Expand Down
18 changes: 2 additions & 16 deletions otherlibs/dune-rpc/private/exported_types.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
41 changes: 19 additions & 22 deletions otherlibs/stdune/src/stdune.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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 "<error>" ]
| 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

Expand Down
5 changes: 1 addition & 4 deletions otherlibs/stdune/test/ansi_color_tests.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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" =
Expand Down
2 changes: 1 addition & 1 deletion stdune.opam
Original file line number Diff line number Diff line change
Expand Up @@ -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}
]
Expand Down
70 changes: 8 additions & 62 deletions vendor/pp/src/pp.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand All @@ -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
Expand All @@ -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) =
Expand Down Expand Up @@ -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
Loading

0 comments on commit 0539a11

Please sign in to comment.