Skip to content

Commit

Permalink
Add Submodules with simpler APIs to Pp_ast
Browse files Browse the repository at this point in the history
Signed-off-by: Nathan Rebours <[email protected]>
  • Loading branch information
NathanReb committed Nov 26, 2024
1 parent 6b85aae commit f9f0f12
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 1 deletion.
2 changes: 1 addition & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ details.

- Add ppxlib's AST pretty-printing utilities in `Ppxlib.Pp_ast` and
a `ppxlib-pp-ast` executable in a new separate `ppxlib-tools` package
(#517, @NathanReb)
(#517, #525, #537, @NathanReb)

- Change `-dparsetree` from a sexp output to a pretty printed AST, closer
to what the compiler's `-dparsetree` is.
Expand Down
38 changes: 38 additions & 0 deletions src/pp_ast.ml
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,44 @@ class lift_simple_val =
| NoInjectivity -> Constr ("NoInjectivity", [])
end

module type Conf = sig
val config : Config.t
end

module type Configured = sig
val structure : Format.formatter -> structure -> unit
val structure_item : Format.formatter -> structure_item -> unit
val signature : Format.formatter -> signature -> unit
val signature_item : Format.formatter -> signature_item -> unit
val expression : Format.formatter -> expression -> unit
val pattern : Format.formatter -> pattern -> unit
val core_type : Format.formatter -> core_type -> unit
end

module Make (Conf : Conf) : Configured = struct
let lsv =
let lift_simple_val = new lift_simple_val in
lift_simple_val#set_config Conf.config;
lift_simple_val

let structure fmt str = pp_simple_val fmt (lsv#structure str)
let structure_item fmt str = pp_simple_val fmt (lsv#structure_item str)
let signature fmt str = pp_simple_val fmt (lsv#signature str)
let signature_item fmt str = pp_simple_val fmt (lsv#signature_item str)
let expression fmt str = pp_simple_val fmt (lsv#expression str)
let pattern fmt str = pp_simple_val fmt (lsv#pattern str)
let core_type fmt str = pp_simple_val fmt (lsv#core_type str)
end

let make config =
(module Make (struct
let config = config
end) : Configured)

module Default = Make (struct
let config = Config.default
end)

let lift_simple_val = new lift_simple_val

type 'node pp = ?config:Config.t -> Format.formatter -> 'node -> unit
Expand Down
20 changes: 20 additions & 0 deletions src/pp_ast.mli
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,26 @@ module Config : sig
be. *)
end

module type Conf = sig
val config : Config.t
end

module type Configured = sig
val structure : Format.formatter -> structure -> unit
val structure_item : Format.formatter -> structure_item -> unit
val signature : Format.formatter -> signature -> unit
val signature_item : Format.formatter -> signature_item -> unit
val expression : Format.formatter -> expression -> unit
val pattern : Format.formatter -> pattern -> unit
val core_type : Format.formatter -> core_type -> unit
end

module Make (Conf : Conf) : Configured

val make : Config.t -> (module Configured)

module Default : Configured

type 'node pp = ?config:Config.t -> Format.formatter -> 'node -> unit

val structure : structure pp
Expand Down

0 comments on commit f9f0f12

Please sign in to comment.