package ocaml-migrate-parsetree

  1. Overview
  2. Docs
Legend:
Library
Module
Module type
Parameter
Class
Class type

A generic Parsetree mapper

type mapper = {
  1. attribute : mapper -> Parsetree.attribute -> Parsetree.attribute;
  2. attributes : mapper -> Parsetree.attribute list -> Parsetree.attribute list;
  3. binding_op : mapper -> Parsetree.binding_op -> Parsetree.binding_op;
  4. case : mapper -> Parsetree.case -> Parsetree.case;
  5. cases : mapper -> Parsetree.cases -> Parsetree.cases;
  6. class_declaration : mapper -> Parsetree.class_declaration -> Parsetree.class_declaration;
  7. class_description : mapper -> Parsetree.class_description -> Parsetree.class_description;
  8. class_expr : mapper -> Parsetree.class_expr -> Parsetree.class_expr;
  9. class_field : mapper -> Parsetree.class_field -> Parsetree.class_field;
  10. class_signature : mapper -> Parsetree.class_signature -> Parsetree.class_signature;
  11. class_structure : mapper -> Parsetree.class_structure -> Parsetree.class_structure;
  12. class_type : mapper -> Parsetree.class_type -> Parsetree.class_type;
  13. class_type_declaration : mapper -> Parsetree.class_type_declaration -> Parsetree.class_type_declaration;
  14. class_type_field : mapper -> Parsetree.class_type_field -> Parsetree.class_type_field;
  15. constructor_declaration : mapper -> Parsetree.constructor_declaration -> Parsetree.constructor_declaration;
  16. expr : mapper -> Parsetree.expression -> Parsetree.expression;
  17. extension : mapper -> Parsetree.extension -> Parsetree.extension;
  18. extension_constructor : mapper -> Parsetree.extension_constructor -> Parsetree.extension_constructor;
  19. include_declaration : mapper -> Parsetree.include_declaration -> Parsetree.include_declaration;
  20. include_description : mapper -> Parsetree.include_description -> Parsetree.include_description;
  21. label_declaration : mapper -> Parsetree.label_declaration -> Parsetree.label_declaration;
  22. location : mapper -> Location.t -> Location.t;
  23. module_binding : mapper -> Parsetree.module_binding -> Parsetree.module_binding;
  24. module_declaration : mapper -> Parsetree.module_declaration -> Parsetree.module_declaration;
  25. module_substitution : mapper -> Parsetree.module_substitution -> Parsetree.module_substitution;
  26. module_expr : mapper -> Parsetree.module_expr -> Parsetree.module_expr;
  27. module_type : mapper -> Parsetree.module_type -> Parsetree.module_type;
  28. module_type_declaration : mapper -> Parsetree.module_type_declaration -> Parsetree.module_type_declaration;
  29. open_declaration : mapper -> Parsetree.open_declaration -> Parsetree.open_declaration;
  30. open_description : mapper -> Parsetree.open_description -> Parsetree.open_description;
  31. pat : mapper -> Parsetree.pattern -> Parsetree.pattern;
  32. payload : mapper -> Parsetree.payload -> Parsetree.payload;
  33. signature : mapper -> Parsetree.signature -> Parsetree.signature;
  34. signature_item : mapper -> Parsetree.signature_item -> Parsetree.signature_item;
  35. structure : mapper -> Parsetree.structure -> Parsetree.structure;
  36. structure_item : mapper -> Parsetree.structure_item -> Parsetree.structure_item;
  37. typ : mapper -> Parsetree.core_type -> Parsetree.core_type;
  38. type_declaration : mapper -> Parsetree.type_declaration -> Parsetree.type_declaration;
  39. type_extension : mapper -> Parsetree.type_extension -> Parsetree.type_extension;
  40. type_exception : mapper -> Parsetree.type_exception -> Parsetree.type_exception;
  41. type_kind : mapper -> Parsetree.type_kind -> Parsetree.type_kind;
  42. value_binding : mapper -> Parsetree.value_binding -> Parsetree.value_binding;
  43. value_description : mapper -> Parsetree.value_description -> Parsetree.value_description;
  44. with_constraint : mapper -> Parsetree.with_constraint -> Parsetree.with_constraint;
}

A mapper record implements one "method" per syntactic category, using an open recursion style: each method takes as its first argument the mapper to be applied to children in the syntax tree.

val default_mapper : mapper

A default mapper, which implements a "deep identity" mapping.

Apply mappers to compilation units

val tool_name : unit -> string

Can be used within a ppx preprocessor to know which tool is calling it "ocamlc", "ocamlopt", "ocamldoc", "ocamldep", "ocaml", ... Some global variables that reflect command-line options are automatically synchronized between the calling tool and the ppx preprocessor: Clflags.include_dirs, Load_path, Clflags.open_modules, Clflags.for_package, Clflags.debug.

val apply : source:string -> target:string -> mapper -> unit

Apply a mapper (parametrized by the unit name) to a dumped parsetree found in the source file and put the result in the target file. The structure or signature field of the mapper is applied to the implementation or interface.

val run_main : (string list -> mapper) -> unit

Entry point to call to implement a standalone -ppx rewriter from a mapper, parametrized by the command line arguments. The current unit name can be obtained from Location.input_name. This function implements proper error reporting for uncaught exceptions.

Registration API

val register_function : (string -> (string list -> mapper) -> unit) Pervasives.ref
val register : string -> (string list -> mapper) -> unit

Apply the register_function. The default behavior is to run the mapper immediately, taking arguments from the process command line. This is to support a scenario where a mapper is linked as a stand-alone executable.

It is possible to overwrite the register_function to define "-ppx drivers", which combine several mappers in a single process. Typically, a driver starts by defining register_function to a custom implementation, then lets ppx rewriters (linked statically or dynamically) register themselves, and then run all or some of them. It is also possible to have -ppx drivers apply rewriters to only specific parts of an AST.

The first argument to register is a symbolic name to be used by the ppx driver.

Convenience functions to write mappers

val map_opt : ('a -> 'b) -> 'a option -> 'b option
val extension_of_error : Location.error -> Parsetree.extension

Encode an error into an 'ocaml.error' extension node which can be inserted in a generated Parsetree. The compiler will be responsible for reporting the error.

val attribute_of_warning : Location.t -> string -> Parsetree.attribute

Encode a warning message into an 'ocaml.ppwarning' attribute which can be inserted in a generated Parsetree. The compiler will be responsible for reporting the warning.

type nonrec location_error = Location.error
val error_of_exn : exn -> location_error option
val register_error_of_exn : (exn -> location_error option) -> unit
val report_exception : Format.formatter -> exn -> unit
val get_error_message : location_error -> string
val set_error_message : location_error -> string -> location_error
val make_error_of_message : loc:Location.t -> string -> sub:(Location.t * string) list -> location_error
val print_error : Format.formatter -> location_error -> unit
val raise_error : location_error -> 'a

Helper functions to call external mappers

val add_ppx_context_str : tool_name:string -> Parsetree.structure -> Parsetree.structure

Extract information from the current environment and encode it into an attribute which is prepended to the list of structure items in order to pass the information to an external processor.

val add_ppx_context_sig : tool_name:string -> Parsetree.signature -> Parsetree.signature

Same as add_ppx_context_str, but for signatures.

val drop_ppx_context_str : restore:bool -> Parsetree.structure -> Parsetree.structure

Drop the ocaml.ppx.context attribute from a structure. If restore is true, also restore the associated data in the current process.

val drop_ppx_context_sig : restore:bool -> Parsetree.signature -> Parsetree.signature

Same as drop_ppx_context_str, but for signatures.

Cookies

Cookies are used to pass information from a ppx processor to a further invocation of itself, when called from the OCaml toplevel (or other tools that support cookies).