Module Ppxlib.Pp_astSource

This module implements pretty printers for the OCaml AST's version used by ppxlib.

Those pretty printers show the AST as its OCaml representation and do not pretty print the corresponding source code. For printing ASTs as source code use the Ppxlib.Pprintast module instead.

For example, calling Pp_ast.expression Format.std_formatter [%expr x + 2] will print:

Pexp_apply
  ( Pexp_ident (Lident "+")
  , [ ( Nolabel, Pexp_ident (Lident "x"))
    ; ( Nolabel, Pexp_constant (Pconst_integer ( "2", None)))
    ]
  )

There is also a way to customise the output format with the optional printer argument. For example, calling the following:

Pp_ast.expression 
  Format.std_formatter 
  ?printer:(fun fmt repr -> Yojson.Basic.pretty_print fmt (repr_to_yojson repr)) 
  [%expr x + 2]

Will print:

[
  {
    "Pstr_eval": [
      {
        "Pexp_apply": [
          { "Pexp_ident": { "Lident": "+" } },
          [
            [ "Nolabel", { "Pexp_ident": { "Lident": "x" } } ],
            [
              "Nolabel",
              { "Pexp_constant": { "Pconst_integer": [ "2", "None" ] } }
            ]
          ]
        ]
      },
      "__attrs"
    ]
  }
]

To keep the output easily readable, records with _desc fields such as Ppxlib.Ast.expression or Ppxlib.Ast.pattern are not printed as such and only the value of the corresponding _desc field is printed instead. This prevents AST nodes metadata, such as locations or attributes, from polluting the output, keeping it relatively concise and clean. The same goes for Location.loc values which are printed as the value of their txt field.

Location.t and Ppxlib.Ast.attributes are not displayed by default even outside of the records mentioned above.

The Config module below allows to override part or all of this behaviour. When configured to display locations or attributes, the entire record will be displayed, not only its _desc field.

Sourcetype repr =
  1. | Unit
  2. | Int of int
  3. | String of string
  4. | Bool of bool
  5. | Char of char
  6. | Array of repr list
  7. | Float of float
  8. | Int32 of int32
  9. | Int64 of int64
  10. | Nativeint of nativeint
  11. | Record of (string * repr) list
  12. | Constr of string * repr list
  13. | Tuple of repr list
  14. | List of repr list
  15. | Special of string
Sourcetype 'a pp = Format.formatter -> 'a -> unit
Sourcemodule Config : sig ... end
Sourcetype 'a configurable = ?config:Config.t -> 'a pp
Sourcetype 'a configured = 'a pp
Sourcemodule type S = sig ... end
Sourcemodule type Conf = sig ... end
Sourcemodule type Configured = S with type 'a printer = 'a configured
Sourcemodule type Configurable = S with type 'a printer = 'a configurable
Sourceval make : Config.t -> (module Configured)
include Configurable
Sourcetype 'a printer = 'a configurable