package odoc

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

Source file subst.ml

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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
(*
 * Copyright (c) 2014 Leo White <lpw25@cl.cam.ac.uk>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *)

open Odoc_model
open Paths
open Names

class type t = object
  method root : Root.t -> Root.t
  inherit Maps.paths
  method offset_identifier_signature :
    Identifier.Signature.t * int -> Identifier.Signature.t * int
  inherit Maps.types
end

let signature s sg =
  s#signature sg

let class_signature s csig =
  s#class_signature csig

let datatype s decl =
  s#type_decl_representation decl

let module_ s md =
  s#module_ md

let module_type s mty =
  s#module_type mty

let type_decl s decl =
  s#type_decl decl

let constructor s cstr =
  s#type_decl_constructor cstr

let field s field =
  s#type_decl_field field

let extension s ext =
  s#extension ext

let exception_ s exn =
  s#exception_ exn

let value s v =
  s#value v

let class_ s cl =
  s#class_ cl

let class_type s cty =
  s#class_type cty

let method_ s meth =
  s#method_ meth

let instance_variable s inst =
  s#instance_variable inst

let comment s com =
  s#documentation_comment com

let documentation s doc =
  s#documentation doc

let identifier_signature s id =
  s#identifier_signature id

let offset_identifier_signature s idoff =
  s#offset_identifier_signature idoff

(* TODO either expose more maps or expose argument map directly *)
let identifier_module s id =
  s#identifier_module id

let module_type_expr s expr =
  s#module_type_expr expr

let module_expansion s expr =
  s#module_expansion expr

class rename_signature ~equal:_ (x : Identifier.Signature.t)
        (y : Identifier.Signature.t) offset : t = object

  inherit Maps.paths as super

  method root x = x

  method! identifier_signature id =
    if Identifier.Signature.equal id x then y
    else super#identifier_signature id

  method! identifier (id : Identifier.t)
         : Identifier.t =
    match id with
    | `Argument(parent, pos, name) ->
        if Identifier.Signature.equal parent x then
          `Argument(y, pos + offset, name)
        else super#identifier id
    | id -> super#identifier id

  method offset_identifier_signature (id, offset') =
    if Identifier.Signature.equal id x then (y, offset + offset')
    else (super#identifier_signature id, offset')

  inherit Maps.types

end

let rename_signature ~equal x y offset =
  new rename_signature ~equal x y offset

class rename_class_signature ~equal:_
           (x : Identifier.ClassSignature.t)
           (y : Identifier.ClassSignature.t) : t = object (self)

  inherit Maps.paths as super

  method root x = x

  method! identifier_class_signature id =
    if Identifier.ClassSignature.equal id x then y
    else super#identifier_class_signature id

  inherit Maps.types

  method offset_identifier_signature (id, offset) =
    (self#identifier_signature id, offset)

end

let rename_class_signature ~equal x y =
  new rename_class_signature ~equal x y

class rename_datatype ~equal:_ (x : Identifier.DataType.t)
        (y : Identifier.DataType.t) : t = object (self)

  inherit Maps.paths as super

  method root x = x

  method! identifier_datatype id =
    if Identifier.DataType.equal id x then y
    else super#identifier_datatype id

  inherit Maps.types

  method offset_identifier_signature (id, offset) =
    (self#identifier_signature id, offset)

end

let rename_datatype ~equal x y =
  new rename_datatype ~equal x y

(*let module_id_path (type k) (Witness : k is_path_kind)
                   (id : ('a, k) Identifier.t) name =
  let open Path.Resolved in
    (Module(Identifier id, name))*)

class prefix ~equal:_ ~canonical id : t = object

  inherit Maps.paths as super

  method root x = x

  method! identifier x = x

  method! path_resolved : Path.Resolved.t -> Path.Resolved.t =
    fun p ->
      let matches id' =
        Identifier.Signature.equal (id :> Identifier.Signature.t) id'
      in
      let replacement =
        match canonical with
        | None -> `Identifier id
        | Some(path, _) -> `Canonical(`Identifier id, path)
      in
        match p with
        | `Identifier (`Module(parent, name)) ->
            if matches parent then `Module(replacement, name)
            else super#path_resolved p
        | `Identifier (`ModuleType(parent, name)) ->
            if matches parent then (`ModuleType(replacement, name))
            else super#path_resolved p
        | `Identifier (`Type(parent, name)) ->
            if matches parent then (`Type(replacement, name))
            else super#path_resolved p
        | `Identifier (`Class(parent, name)) ->
            if matches parent then (`Class(replacement, name))
            else super#path_resolved p
        | `Identifier (`ClassType(parent, name)) ->
            if matches parent then (`ClassType(replacement, name))
            else super#path_resolved p
        | _ -> super#path_resolved p

  method! reference_resolved : Reference.Resolved.t ->
                              Reference.Resolved.t =
    fun r ->
      let sid = (id :> Identifier.Signature.t) in
      let matches id' =
        Identifier.Signature.equal sid id'
      in
      let open Reference.Resolved in
      let replacement =
        match canonical with
        | None -> `Identifier id
        | Some(_, reference) -> `Canonical(`Identifier id, reference)
      in
      let sreplacement = (replacement :> Signature.t) in
      let lreplacement = (replacement :> LabelParent.t) in
      match r with
      | `Identifier (`Module(parent, name)) ->
          if matches parent then `Module(sreplacement, name)
          else super#reference_resolved r
      | `Identifier (`ModuleType(parent, name)) ->
          if matches parent then `ModuleType(sreplacement, name)
          else super#reference_resolved r
      | `Identifier (`Type(parent, name)) ->
          if matches parent then `Type(sreplacement, name)
          else super#reference_resolved r
      | `Identifier (`Extension(parent, name)) ->
          if matches parent then `Extension(sreplacement, name)
          else super#reference_resolved r
      | `Identifier (`Exception(parent, name)) ->
          if matches parent then `Exception(sreplacement, name)
          else super#reference_resolved r
      | `Identifier (`Value(parent, name)) ->
          if matches parent then `Value(sreplacement, name)
          else super#reference_resolved r
      | `Identifier (`Class(parent, name)) ->
          if matches parent then `Class(sreplacement, name)
          else super#reference_resolved r
      | `Identifier (`ClassType(parent, name)) ->
          if matches parent then `ClassType(sreplacement, name)
          else super#reference_resolved r
      | `Identifier (`Label(parent, name)) -> begin
          match parent with
          | `Root _ | `Argument _
          | `Module _ | `ModuleType _ as parent ->
                if matches parent then `Label(lreplacement, name)
                else super#reference_resolved r
          | _ -> super#reference_resolved r
        end
      | _ -> super#reference_resolved r

  inherit Maps.types

  method offset_identifier_signature x = x

end

let prefix ~equal ~canonical id =
  new prefix ~equal ~canonical id

class strengthen path : t = object

  inherit Maps.types

  method root x = x

  method! documentation_comment x = x

  method! module_ md =
    if Path.Resolved.Module.is_hidden path then md
    else begin
      let open Lang.Module in
      match md.type_ with
      | Alias p when not (Path.Module.is_hidden p) -> md
      | _ ->
          let name = Identifier.name md.id in
          let path = `Resolved(`Module(path, ModuleName.of_string name)) in
          let type_ = Alias path in
          let expansion = None in
          { md with type_; expansion }
    end

  method! module_type x = x

  method! type_decl x = x

  method! extension x = x

  method! exception_ x = x

  method! value x = x

  method! external_ x = x

  method! class_ x = x

  method! class_type x = x

  method! include_ x = x

  inherit Maps.paths

  method offset_identifier_signature x = x

  method! module_type_expr x = x

end

let strengthen path =
  new strengthen path

let make_lookup ~equal:_ ~hash:_
                (items : (Identifier.Module.t * Identifier.Module.t) list) =
  let module Hash = struct
    type t = Identifier.Module.t
    let equal = Identifier.Module.equal
    let hash = Identifier.Module.hash
  end in
  let module Tbl = Hashtbl.Make(Hash) in
  let tbl = Tbl.create 13 in
  List.iter (fun (id1, id2) -> Tbl.add tbl id1 id2) items;
    fun id ->
        match Tbl.find tbl id with
        | id -> Some id
        | exception Not_found -> None

class pack ~equal ~hash
           (items : (Identifier.Module.t
                     * Identifier.Module.t) list) : t = object (self)

  val lookup = make_lookup ~equal ~hash items

  method root x = x

  inherit Maps.paths as super

  method! identifier : Identifier.t -> Identifier.t =
    fun id ->
        match id with
        | `Root _ as id -> begin
            match lookup id with
            | Some (`Root _ | `Module _ | `Argument _ as id) -> id
            | None -> super#identifier id
          end
        | `Module _ as id -> begin
            match lookup id with
            | Some (`Root _ | `Module _ | `Argument _ as id) -> id
            | None -> super#identifier id
          end
        | `Argument _ as id -> begin
            match lookup id with
            | Some (`Root _ | `Module _ | `Argument _ as id) -> id
            | None -> super#identifier id
          end
        | _ -> super#identifier id

  inherit Maps.types

  method offset_identifier_signature (id, offset) =
    (self#identifier_signature id, offset)

end

let pack ~equal ~hash items =
  new pack ~equal ~hash items
OCaml

Innovation. Community. Security.