Source file model.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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
open Core

module Interpolation_kind = struct
  type t =
    | Normal (* %{} *)
    | Option (* ?{} *)
    | List (* *{} *)
    | String (* #{} *)
  [@@deriving sexp_of]

  let to_string = function
    | Normal -> "percent-based (%{})"
    | Option -> "option (?{})"
    | List -> "list (*{})"
    | String -> "string (#{})"
  ;;
end

module Ocaml_expr = struct
  type t = Ppxlib.Ast.expression

  let to_string t =
    assert (String.is_empty (Format.flush_str_formatter ()));
    Ppxlib.Pprintast.expression Format.str_formatter t;
    Format.flush_str_formatter ()
  ;;

  let sexp_of_t t = Sexp.Atom (to_string t)
end

module String_relative_location = struct
  type t =
    { start : int
    ; end_ : int
    }
  [@@deriving sexp_of]
end

module Location = struct
  include Ppxlib.Location

  let to_string { loc_start; loc_end; _ } =
    if String.( <> ) loc_start.pos_fname loc_end.pos_fname
    then
      [%string
        "%{loc_start.pos_fname}[%{loc_start.pos_lnum#Int}:%{loc_start.pos_cnum - \
         loc_start.pos_bol#Int}]-%{loc_end.pos_fname}[%{loc_end.pos_lnum#Int}:%{loc_end.pos_cnum \
         - loc_end.pos_bol#Int}]"]
    else if Int.( <> ) loc_start.pos_lnum loc_end.pos_lnum
    then
      [%string
        "%{loc_start.pos_fname}[%{loc_start.pos_lnum#Int}:%{loc_start.pos_cnum - \
         loc_start.pos_bol#Int}-%{loc_end.pos_lnum#Int}:%{loc_end.pos_cnum - \
         loc_end.pos_bol#Int}]"]
    else if Int.( <> ) loc_start.pos_cnum loc_end.pos_cnum
    then
      [%string
        "%{loc_start.pos_fname}[%{loc_start.pos_lnum#Int}:%{loc_start.pos_cnum - \
         loc_start.pos_bol#Int}-%{loc_end.pos_cnum - loc_end.pos_bol#Int}]"]
    else
      [%string
        "%{loc_start.pos_fname}[%{loc_start.pos_lnum#Int}:%{loc_start.pos_cnum - \
         loc_start.pos_bol#Int}"]
  ;;

  let sexp_of_t t = Sexp.Atom (to_string t)
  let length t = t.loc_end.pos_cnum - t.loc_start.pos_cnum
end

module Loc = struct
  type 'a t = 'a Ppxlib.Loc.t =
    { txt : 'a
    ; loc : Location.t
    }
  [@@deriving sexp_of]
end

module Escape_kind = struct
  type t =
    | Escaped
    | Not_escaped
  [@@deriving sexp_of]
end

module Expr = struct
  type t =
    { expr : Ocaml_expr.t
    ; code : string Loc.t
    ; to_t : string Loc.t option
    ; loc : Location.t
    ; string_relative_location : String_relative_location.t
    ; escape_kind : Escape_kind.t
    }
  [@@deriving sexp_of]

  let loc t = t.loc

  let to_source t =
    let code = t.code.txt in
    let to_t =
      match t.to_t with
      | None -> ""
      | Some to_t -> [%string "#%{to_t.txt}"]
    in
    let min_len = String.length code + String.length to_t in
    let target_len =
      Location.length t.loc
      -
      match t.escape_kind with
      | Escaped -> String.length "%{}"
      | Not_escaped -> 0
    in
    (* Try really hard to preserve the expression length *)
    let code = String.pad_right ~char:' ' code ~len:(target_len - String.length to_t) in
    let len = String.length code + String.length to_t in
    assert (len >= min_len);
    assert (len >= target_len);
    assert (len = if target_len < min_len then min_len else target_len);
    "%{" ^ code ^ to_t ^ "}"
  ;;
end

module Quote = struct
  module Elt = struct
    type t =
      | Literal of string Loc.t
      | Expr of Expr.t
    [@@deriving sexp_of]

    let loc = function
      | Literal t -> t.loc
      | Expr t -> Expr.loc t
    ;;

    let to_source = function
      | Literal t -> t.txt
      | Expr t -> Expr.to_source t
    ;;
  end

  type t = Elt.t list Loc.t [@@deriving sexp_of]

  let loc (t : t) = t.loc

  let split_on_space ({ txt; loc = _ } : t) =
    let is_ws = function
      | Elt.Literal { txt; _ } ->
        (not (String.is_empty txt)) && String.for_all txt ~f:Char.is_whitespace
      | _ -> false
    in
    txt
    |> List.group ~break:(fun a b -> is_ws a || is_ws b)
    |> List.filter ~f:(Fn.non (List.for_all ~f:is_ws))
    |> List.map ~f:(fun txt ->
      let first = List.hd_exn txt |> Elt.loc in
      let last = List.last_exn txt |> Elt.loc in
      { Loc.txt; loc = { first with loc_end = last.loc_end } })
  ;;

  let to_source (t : t) = List.map t.txt ~f:Elt.to_source |> String.concat ~sep:""
end

module Longident = struct
  type t = Ppxlib.Longident.t =
    | Lident of string
    | Ldot of t * string
    | Lapply of t * t
  [@@deriving sexp_of]
end

module Closing_tag = struct
  type t =
    { loc : String_relative_location.t
    ; is_fragment_like : bool (* if is_fragment_like then "</>" else "</Foo.f>" *)
    }
  [@@deriving sexp_of]
end

module Literal = struct
  type t =
    | Literal of string Loc.t
    | Component of
        { name : Longident.t Loc.t
        ; string_relative_location : String_relative_location.t
        ; code : string Loc.t
        }
  [@@deriving sexp_of]
end

module Tag = struct
  type t =
    | Literal of Literal.t
    | Expr of Expr.t
    | Fragment of Location.t
  [@@deriving sexp_of]

  let loc = function
    | Literal (Literal { loc; _ } | Component { name = { loc; _ }; _ }) -> loc
    | Expr t -> Expr.loc t
    | Fragment loc -> loc
  ;;
end

module rec Node : sig
  type t =
    | Text of string Ppxlib.Loc.t
    | Expr of
        { expr : Expr.t
        ; interpolation_kind : Interpolation_kind.t
        }
    | Element of Element.t
  [@@deriving sexp_of]

  val loc : t -> Ppxlib.Location.t
end = struct
  type t =
    | Text of string Loc.t
    | Expr of
        { expr : Expr.t
        ; interpolation_kind : Interpolation_kind.t
        }
    | Element of Element.t
  [@@deriving sexp_of]

  let loc = function
    | Text t -> t.loc
    | Expr t -> Expr.loc t.expr
    | Element t -> Element.loc t
  ;;
end

and Element : sig
  type t =
    { tag : Tag.t
    ; attrs : Attr.t list
    ; inner : Node.t list option
    ; loc : Location.t
    ; open_loc : Location.t
    ; open_string_relative_location : String_relative_location.t
    ; closing_tag : Closing_tag.t option
    }
  [@@deriving sexp_of]

  val loc : t -> Ppxlib.Location.t
end = struct
  type t =
    { tag : Tag.t
    ; attrs : Attr.t list
    ; inner : Node.t list option
    ; loc : Location.t
    ; open_loc : Location.t
    ; open_string_relative_location : String_relative_location.t
    ; closing_tag : Closing_tag.t option
    }
  [@@deriving sexp_of]

  let loc t = t.loc
end

and Attr : sig
  module Value : sig
    type t =
      | Literal of Quote.t
      | Expr of Expr.t
    [@@deriving sexp_of]

    val loc : t -> Ppxlib.Location.t
  end

  module Sigil : sig
    type t =
      | Tilde
      | Question_mark
    [@@deriving sexp_of]
  end

  module Argument : sig
    type t =
      | Element of Element.t
      | Expr of Expr.t
    [@@deriving sexp_of]
  end

  type t =
    | Attr of
        { name : string Ppxlib.Loc.t
        ; value : Value.t option
        ; loc : Ppxlib.Location.t
        }
    | Expr of
        { expr : Expr.t
        ; interpolation_kind : Interpolation_kind.t
        }
    | Argument of
        { name : string Ppxlib.Loc.t
        ; argument : Argument.t option
        ; loc : Location.t
        ; sigil : Sigil.t
        }
  [@@deriving sexp_of]

  val loc : t -> Ppxlib.Location.t
end = struct
  module Value = struct
    type t =
      | Literal of Quote.t
      | Expr of Expr.t
    [@@deriving sexp_of]

    let loc = function
      | Literal t -> Quote.loc t
      | Expr t -> Expr.loc t
    ;;
  end

  module Sigil = struct
    type t =
      | Tilde
      | Question_mark
    [@@deriving sexp_of]
  end

  module Argument = struct
    type t =
      | Element of Element.t
      | Expr of Expr.t
    [@@deriving sexp_of]
  end

  type t =
    | Attr of
        { name : string Loc.t
        ; value : Value.t option
        ; loc : Location.t
        }
    | Expr of
        { expr : Expr.t
        ; interpolation_kind : Interpolation_kind.t
        }
    | Argument of
        { name : string Loc.t
        ; argument : Argument.t option
        ; loc : Location.t
        ; sigil : Sigil.t
        }
  [@@deriving sexp_of]

  let loc = function
    | Attr { name = _; value = _; loc } -> loc
    | Expr expr -> Expr.loc expr.expr
    | Argument { name = _; argument = _; sigil = _; loc } -> loc
  ;;
end

include struct
  type ocaml_expr = Ocaml_expr.t
  type location = Ppxlib.location

  type 'a with_loc = 'a Ppxlib.Loc.t =
    { txt : 'a
    ; loc : location
    }

  and interpolation_kind = Interpolation_kind.t =
    | Normal
    | Option
    | List
    | String

  and escape_kind = Escape_kind.t =
    | Escaped
    | Not_escaped

  and string_relative_location = String_relative_location.t =
    { start : int
    ; end_ : int
    }

  and expr = Expr.t =
    { expr : ocaml_expr
    ; code : string with_loc
    ; to_t : string with_loc option
    ; loc : location
    ; string_relative_location : string_relative_location
    ; escape_kind : escape_kind
    }

  and element = Element.t =
    { tag : tag
    ; attrs : attr list
    ; inner : node list option
    ; loc : location
    ; open_loc : location
    ; open_string_relative_location : string_relative_location
    ; closing_tag : closing_tag option
    }

  and argument = Attr.Argument.t =
    | Element of element
    | Expr of expr

  and quote_elt = Quote.Elt.t =
    | Literal of string with_loc
    | Expr of expr

  and quote = quote_elt list with_loc

  and attr_value = Attr.Value.t =
    | Literal of quote
    | Expr of expr

  and sigil = Attr.Sigil.t =
    | Tilde
    | Question_mark

  and attr = Attr.t =
    | Attr of
        { name : string with_loc
        ; value : attr_value option
        ; loc : location
        }
    | Expr of
        { expr : expr
        ; interpolation_kind : interpolation_kind
        }
    | Argument of
        { name : string with_loc
        ; argument : argument option
        ; loc : location
        ; sigil : sigil
        }

  and longident = Longident.t =
    | Lident of string
    | Ldot of longident * string
    | Lapply of longident * longident

  and closing_tag = Closing_tag.t =
    { loc : string_relative_location
    ; is_fragment_like : bool
    }

  and literal = Literal.t =
    | Literal of string with_loc
    | Component of
        { name : longident with_loc
        ; string_relative_location : string_relative_location
        ; code : string with_loc
        }

  and tag = Tag.t =
    | Literal of literal
    | Expr of expr
    | Fragment of location

  and node = Node.t =
    | Text of string with_loc
    | Expr of
        { expr : expr
        ; interpolation_kind : interpolation_kind
        }
    | Element of element
  [@@deriving traverse_map, traverse_iter]
end

module Traverse = struct
  class map' =
    object
      inherit map
      method list : 'a. ('a -> 'a) -> 'a list -> 'a list = fun f -> List.map ~f
      method option : 'a. ('a -> 'a) -> 'a option -> 'a option = fun f -> Option.map ~f
      method string : string -> string = Fn.id
      method int : int -> int = Fn.id
      method bool : bool -> bool = Fn.id
      method ocaml_expr : ocaml_expr -> ocaml_expr = Fn.id
      method location : location -> location = Fn.id
    end

  class iter' =
    object
      inherit iter
      method list : 'a. ('a -> unit) -> 'a list -> unit = fun f -> List.iter ~f
      method option : 'a. ('a -> unit) -> 'a option -> unit = fun f -> Option.iter ~f
      method string : string -> unit = fun _ -> ()
      method int : int -> unit = fun _ -> ()
      method bool : bool -> unit = fun _ -> ()
      method ocaml_expr : ocaml_expr -> unit = fun _ -> ()
      method location : location -> unit = fun _ -> ()
    end

  class map = map'
  class iter = iter'
end