File: deriving.mli

package info (click to toggle)
ppxlib 0.15.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 1,284 kB
  • sloc: ml: 17,184; sh: 149; makefile: 36; python: 36
file content (130 lines) | stat: -rw-r--r-- 4,195 bytes parent folder | download
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
(** Deriving code from type declarations *)

open Import

(** Specification of generator arguments *)
module Args : sig
  type ('a, 'b) t
  type 'a param

  val empty  : ('m, 'm) t

  val arg
    :  string
    -> (expression, 'a -> 'a option, 'a option) Ast_pattern.t
    -> 'a option param

  (** Flag matches punned labelled argument, i.e. of the form
     [~foo]. It returns [true] iff the argument is present. *)
  val flag : string -> bool param

  val ( +> ) : ('m1, 'a -> 'm2) t -> 'a param -> ('m1, 'm2) t

  (** For convenience, so that one can write the following without having to
      open both [Ast_pattern] and [Deriving.Args]:

      {[
        Deriving.Args.(empty
                       +> arg_option "foo" (estring __)
                       +> arg_option "bar" (pack2 (eint __ ** eint __))
                       +> flag "dotdotdot"
                      )
      ]}
  *)
  include module type of struct include Ast_pattern end
  with type ('a, 'b, 'c) t := ('a, 'b, 'c) Ast_pattern.t
end

(** {6 Generator registration} *)

(** Type of registered derivers *)
type t

module Generator : sig
  type deriver = t
  type ('output_ast, 'input_ast) t

  val make
    :  ?attributes:Attribute.packed list
    -> ?deps:deriver list
    -> ('f, 'output_ast) Args.t
    -> (loc:Location.t -> path:string -> 'input_ast -> 'f)
    -> ('output_ast, 'input_ast) t

  val make_noarg
    :  ?attributes:Attribute.packed list
    -> ?deps:deriver list
    -> (loc:Location.t -> path:string -> 'input_ast -> 'output_ast)
    -> ('output_ast, 'input_ast) t

  module V2 : sig
    val make
      :  ?attributes:Attribute.packed list
      -> ?deps:deriver list
      -> ('f, 'output_ast) Args.t
      -> (ctxt:Expansion_context.Deriver.t -> 'input_ast -> 'f)
      -> ('output_ast, 'input_ast) t

    val make_noarg
      :  ?attributes:Attribute.packed list
      -> ?deps:deriver list
      -> (ctxt:Expansion_context.Deriver.t -> 'input_ast -> 'output_ast)
      -> ('output_ast, 'input_ast) t
  end

  val apply
    :  ('output_ast, 'input_ast) t
    -> name:string
    -> ctxt:Expansion_context.Deriver.t
    -> 'input_ast
    -> (string * expression) list
    -> 'output_ast
end with type deriver := t

(** Register a new deriving generator.

    The various arguments are for the various items on which derivers
    can be attached in structure and signatures.

    We distinguish [exception] from [type_extension] as [exception E]
    is not exactly the same as [type exn += E]. Indeed if the type
    [exn] is redefined, then [type exn += E] will add [E] to the new
    [exn] type while [exception E] will add [E] to the predefined [exn]
    type.

    [extension] register an expander for extension with the name of
    the deriver. This is here mostly to support the ppx_deriving
    backend. *)
val add
  :  ?str_type_decl:(structure, rec_flag * type_declaration list) Generator.t
  -> ?str_type_ext :(structure, type_extension                  ) Generator.t
  -> ?str_exception:(structure, type_exception                  ) Generator.t
  -> ?str_module_type_decl:(structure, module_type_declaration  ) Generator.t
  -> ?sig_type_decl:(signature, rec_flag * type_declaration list) Generator.t
  -> ?sig_type_ext :(signature, type_extension                  ) Generator.t
  -> ?sig_exception:(signature, type_exception                  ) Generator.t
  -> ?sig_module_type_decl:(signature, module_type_declaration  ) Generator.t
  -> ?extension:(loc:Location.t -> path:string -> core_type -> expression)
  -> string
  -> t

(** [add_alias name set] add an alias. When the user write the alias,
    all the generator of [set] will be used instead.  It is possible to
    override the set for any of the context by passing the specific set
    in the approriate optional argument of [add_alias]. *)
val add_alias
  :  string
  -> ?str_type_decl:t list
  -> ?str_type_ext :t list
  -> ?str_exception:t list
  -> ?str_module_type_decl:t list
  -> ?sig_type_decl:t list
  -> ?sig_type_ext :t list
  -> ?sig_exception:t list
  -> ?sig_module_type_decl:t list
  -> t list
  -> t

(** Ignore a deriver. So that one can write: [Deriving.add ... |>
    Deriving.ignore] *)
val ignore : t -> unit