summaryrefslogtreecommitdiff
path: root/parsing/ast_mapper.mli
blob: 6d0efd2d8164d67984a8e547b709cb1651c0400c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
(***********************************************************************)
(*                                                                     *)
(*                                OCaml                                *)
(*                                                                     *)
(*                        Alain Frisch, LexiFi                         *)
(*                                                                     *)
(*  Copyright 2012 Institut National de Recherche en Informatique et   *)
(*  en Automatique.  All rights reserved.  This file is distributed    *)
(*  under the terms of the Q Public License version 1.0.               *)
(*                                                                     *)
(***********************************************************************)

(** Helpers to write Parsetree rewriters *)

open Parsetree

(** {2 A generic mapper class} *)

class mapper:
  object
    method class_declaration: class_declaration -> class_declaration
    method class_description: class_description -> class_description
    method class_expr: class_expr -> class_expr
    method class_field: class_field -> class_field
    method class_signature: class_signature -> class_signature
    method class_structure: class_structure -> class_structure
    method class_type: class_type -> class_type
    method class_type_declaration: class_type_declaration -> class_type_declaration
    method class_type_field: class_type_field -> class_type_field
    method expr: expression -> expression
    method implementation: string -> structure -> string * structure
    method interface: string -> signature -> string * signature
    method location: Location.t -> Location.t
    method module_binding: module_binding -> module_binding
    method module_declaration: module_declaration -> module_declaration
    method module_expr: module_expr -> module_expr
    method module_type: module_type -> module_type
    method module_type_binding: module_type_binding -> module_type_binding
    method module_type_declaration: module_type_declaration -> module_type_declaration
    method pat: pattern -> pattern
    method signature: signature -> signature
    method signature_item: signature_item -> signature_item
    method structure: structure -> structure
    method structure_item: structure_item -> structure_item
    method typ: core_type -> core_type
    method type_declaration: type_declaration -> type_declaration
    method type_kind: type_kind -> type_kind
    method value_description: value_description -> value_description
    method with_constraint: with_constraint -> with_constraint
    method attribute: attribute -> attribute
    method attributes: attribute list -> attribute list
    method extension: extension -> extension
    method constructor_declaration: constructor_declaration -> constructor_declaration
    method label_declaration: label_declaration -> label_declaration
  end

class type main_entry_points =
  object
    method implementation: string -> structure -> string * structure
    method interface: string -> signature -> string * signature
  end

val apply: source:string -> target:string -> #main_entry_points -> unit
    (** Apply a mapper to a dumped parsetree found in the [source] file
        and put the result in the [target] file. *)

val main: #main_entry_points -> unit
    (** Entry point to call to implement a standalone -ppx rewriter
        from a mapper object. *)

val run_main: (string list -> #main_entry_points) -> unit
    (** Same as [main], but with extra arguments from the command line. *)

(** {2 Registration API} *)

val register_function: (string -> (string list -> mapper) -> unit) 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.  *)