package opam-file-format

  1. Overview
  2. Docs

Source file opamPrinter.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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
(**************************************************************************)
(*                                                                        *)
(*    Copyright 2012-2020 OCamlPro                                        *)
(*    Copyright 2012 INRIA                                                *)
(*                                                                        *)
(*  All rights reserved. This file is distributed under the terms of the  *)
(*  GNU Lesser General Public License version 2.1, with the special       *)
(*  exception on linking described in the file LICENSE.                   *)
(*                                                                        *)
(**************************************************************************)

open OpamParserTypes

(* The code duplication with OpamBaseParser is irritating, but can't be solved
   without introducing another module. *)
let nopatch v =
  let s =
  try
    let i = String.index v '.' in
    let i = String.index_from v (i+1) '.' in
    (String.sub v 0 i)
  with Not_found ->
    let rec f i =
      if i >= String.length v then v
      else match String.get v i with
        | '0'..'9' | '.' -> f (i+1)
        | _ -> String.sub v 0 i
    in
    f 0
  in
    try Scanf.sscanf s "%u.%u%!" (fun maj min -> (maj, min))
    with Scanf.Scan_failure _
       | Failure _
       | End_of_file ->
           try Scanf.sscanf s "%u%!" (fun maj -> (maj, 0))
           with Scanf.Scan_failure _
              | Failure _
              | End_of_file -> (0, 0)

let valid_opamfile_contents = function
| (Variable (_, "opam-version", String (_, ver)))::items
    when nopatch ver >= (2, 1) ->
      let opam_version_field = function
      | Variable (_, "opam-version", _) -> true
      | _ -> false
      in
        not (List.exists opam_version_field items)
| _::items ->
    let greater_2_0_opam_version_field = function
    | Variable (_, "opam-version", String (_, ver))
       when nopatch ver >= (2, 1) -> true
    | _ -> false
    in
      not (List.exists greater_2_0_opam_version_field items)
| [] -> true

let relop = function
  | `Eq  -> "="
  | `Neq -> "!="
  | `Geq -> ">="
  | `Gt  -> ">"
  | `Leq -> "<="
  | `Lt  -> "<"
  | `Sem -> "~"

let logop = function
  | `And -> "&"
  | `Or -> "|"

let pfxop = function
  | `Not -> "!"
  | `Defined -> "?"

let env_update_op = function
  | Eq -> "="
  | PlusEq -> "+="
  | EqPlus -> "=+"
  | EqPlusEq -> "=+="
  | ColonEq -> ":="
  | EqColon -> "=:"

let escape_string ?(triple=false) s =
  let len = String.length s in
  let buf = Buffer.create (len * 2) in
  for i = 0 to len -1 do
    let c = s.[i] in
    (match c with
     | '"'
       when not triple
         || (i < len - 2 && s.[i+1] = '"' && s.[i+2] = '"')
         || i = len - 1 ->
       Buffer.add_char buf '\\'
     | '\\' -> Buffer.add_char buf '\\'
     | _ -> ());
    Buffer.add_char buf c
  done;
  Buffer.contents buf

let rec format_value fmt = function
  | Relop (_,op,l,r) ->
    Format.fprintf fmt "@[<h>%a %s@ %a@]"
      format_value l (relop op) format_value r
  | Logop (_,op,l,r) ->
    Format.fprintf fmt "@[<hv>%a %s@ %a@]"
      format_value l (logop op) format_value r
  | Pfxop (_,op,r) ->
    Format.fprintf fmt "@[<h>%s%a@]" (pfxop op) format_value r
  | Prefix_relop (_,op,r) ->
    Format.fprintf fmt "@[<h>%s@ %a@]"
      (relop op) format_value r
  | Ident (_,s)     -> Format.fprintf fmt "%s" s
  | Int (_,i)       -> Format.fprintf fmt "%d" i
  | Bool (_,b)      -> Format.fprintf fmt "%b" b
  | String (_,s)    ->
    if String.contains s '\n'
    then Format.fprintf fmt "\"\"\"%s%s\"\"\""
        (if s.[0] = '\n' then "" else "\\\n")
        (escape_string ~triple:true s)
    else Format.fprintf fmt "\"%s\"" (escape_string s)
  | List (_, l) ->
    Format.fprintf fmt "@[<hv>[@;<0 2>@[<hv>%a@]@,]@]" format_values l
  | Group (_,g)     -> Format.fprintf fmt "@[<hv>(%a)@]" format_values g
  | Option(_,v,l)   -> Format.fprintf fmt "@[<hov 2>%a@ {@[<hv>%a@]}@]"
                         format_value v format_values l
  | Env_binding (_,id,op,v) ->
    Format.fprintf fmt "@[<h>%a %s@ %a@]"
      format_value id (env_update_op op) format_value v

and format_values fmt = function
  | [] -> ()
  | [v] -> format_value fmt v
  | v::r ->
    format_value fmt v;
    Format.pp_print_space fmt ();
    format_values fmt r

let value v =
  format_value Format.str_formatter v; Format.flush_str_formatter ()

let value_list vl =
  Format.fprintf Format.str_formatter "@[<hv>%a@]" format_values vl;
  Format.flush_str_formatter ()

let rec format_item fmt = function
  | Variable (_, _, List (_,[])) -> ()
  | Variable (_, _, List (_,[List(_,[])])) -> ()
  | Variable (_, i, List (_,l)) ->
    if List.exists
        (function List _ | Option (_,_,_::_) -> true | _ -> false)
        l
    then Format.fprintf fmt "@[<v>%s: [@;<0 2>@[<v>%a@]@,]@]"
        i format_values l
    else Format.fprintf fmt "@[<hv>%s: [@;<0 2>@[<hv>%a@]@,]@]"
        i format_values l
  | Variable (_, i, (String (_,s) as v)) when String.contains s '\n' ->
    Format.fprintf fmt "@[<hov 0>%s: %a@]" i format_value v
  | Variable (_, i, v) ->
    Format.fprintf fmt "@[<hov 2>%s:@ %a@]" i format_value v
  | Section (_,s) ->
    Format.fprintf fmt "@[<v 0>%s %s{@;<0 2>@[<v>%a@]@,}@]"
      s.section_kind
      (match s.section_name with
       | Some s -> Printf.sprintf "\"%s\" " (escape_string s)
       | None -> "")
      format_items s.section_items
and format_items fmt is =
  Format.pp_open_vbox fmt 0;
  (match is with
   | [] -> ()
   | i::r ->
     format_item fmt i;
     List.iter (fun i -> Format.pp_print_cut fmt (); format_item fmt i) r);
  Format.pp_close_box fmt ()

let format_opamfile fmt f =
  format_items fmt f.file_contents;
  Format.pp_print_newline fmt ()

let items l =
  if not (valid_opamfile_contents l) then
    invalid_arg "OpamPrinter.items";
  format_items Format.str_formatter l; Format.flush_str_formatter ()

let opamfile f =
  items f.file_contents

let rec value_equals v1 v2 = match v1, v2 with
  | Bool (_, b1), Bool (_, b2) -> b1 = b2
  | Int (_, i1), Int (_, i2) -> i1 = i2
  | String (_, s1), String (_, s2) -> s1 = s2
  | Relop (_, r1, va1, vb1), Relop (_, r2, va2, vb2) ->
    r1 = r2 && value_equals va1 va2 && value_equals vb1 vb2
  | Prefix_relop (_, r1, v1), Prefix_relop (_, r2, v2) ->
    r1 = r2 && value_equals v1 v2
  | Logop (_, l1, va1, vb1), Logop (_, l2, va2, vb2) ->
    l1 = l2 && value_equals va1 va2 && value_equals vb1 vb2
  | Pfxop (_, p1, v1), Pfxop (_, p2, v2) ->
    p1 = p2 && value_equals v1 v2
  | Ident (_, s1), Ident (_, s2) ->
    s1 = s2
  | List (_, vl1), List (_, vl2) ->
    (try List.for_all2 value_equals vl1 vl2 with Invalid_argument _ -> false)
  | Group (_, vl1), Group (_, vl2) ->
    (try List.for_all2 value_equals vl1 vl2 with Invalid_argument _ -> false)
  | Option (_, v1, vl1), Option (_, v2, vl2) ->
    value_equals v1 v2 &&
    (try List.for_all2 value_equals vl1 vl2 with Invalid_argument _ -> false)
  | Env_binding (_, v1, op1, vx1), Env_binding (_, v2, op2, vx2) ->
    op1 = op2 && value_equals v1 v2 && value_equals vx1 vx2
  | _ -> false

let rec opamfile_item_equals i1 i2 = match i1, i2 with
  | Variable (_, n1, v1), Variable (_, n2, v2) ->
    n1 = n2 && value_equals v1 v2
  | Section (_, s1), Section (_, s2) ->
    s1.section_kind = s2.section_kind &&
    s1.section_name = s2.section_name &&
    (try List.for_all2 opamfile_item_equals s1.section_items s2.section_items
     with Invalid_argument _ -> false)
  | _ -> false

module Normalise = struct
  let escape_string s =
    let len = String.length s in
    let buf = Buffer.create (len * 2) in
    Buffer.add_char buf '"';
    for i = 0 to len -1 do
      match s.[i] with
      | '\\' | '"' as c -> Buffer.add_char buf '\\'; Buffer.add_char buf c
      | '\n' -> Buffer.add_string buf "\\n"
      | c -> Buffer.add_char buf c
    done;
    Buffer.add_char buf '"';
    Buffer.contents buf

  let rec value = function
    | Relop (_,op,l,r) ->
      String.concat " " [value l; relop op; value r]
    | Logop (_,op,l,r) ->
      String.concat " " [value l; logop op; value r]
    | Pfxop (_,op,r) ->
      String.concat " " [pfxop op; value r]
    | Prefix_relop (_,op,r) ->
      String.concat " " [relop op; value r]
    | Ident (_,s) -> s
    | Int (_,i) -> string_of_int i
    | Bool (_,b) -> string_of_bool b
    | String (_,s) -> escape_string s
    | List (_, l) -> Printf.sprintf "[%s]" (String.concat " " (List.map value l))
    | Group (_,g) -> Printf.sprintf "(%s)" (String.concat " " (List.map value g))
    | Option(_,v,l) ->
      Printf.sprintf "%s {%s}" (value v) (String.concat " " (List.map value l))
    | Env_binding (_,id,op,v) ->
      String.concat " "
        [value id; env_update_op op; value v]

  let rec item = function
    | Variable (_, _, List (_,([]|[List(_,[])]))) -> ""
    | Variable (_, i, List (_,l)) ->
      Printf.sprintf "%s: [%s]" i (String.concat " " (List.map value l))
    | Variable (_, i, v) -> String.concat ": " [i; value v]
    | Section (_,s) ->
      Printf.sprintf "%s %s{\n%s\n}"
        s.section_kind
        (match s.section_name with
         | Some s -> escape_string s ^ " "
         | None -> "")
        (String.concat "\n" (List.map item s.section_items))

  let item_order a b = match a,b with
    | Variable (_, "opam-version", String (_, ver)), _
      when nopatch ver >= (2, 1) -> -1
    | _, Variable (_, "opam-version", String (_, ver))
      when nopatch ver >= (2, 1) -> 1
    | Section _, Variable _ -> 1
    | Variable _, Section _ -> -1
    | Variable (_,i,_), Variable (_,j,_) -> String.compare i j
    | Section (_,s), Section (_,t) ->
      let r = String.compare s.section_kind t.section_kind in
      if r <> 0 then r
      else compare s.section_name t.section_name

  let items its =
    if not (valid_opamfile_contents its) then
      invalid_arg "OpamPrinter.Normalise.items";
    let its = List.sort item_order its in
    String.concat "\n" (List.map item its) ^ "\n"

  let opamfile f = items f.file_contents
end

module Preserved = struct
  let items txt orig f =
    if not (valid_opamfile_contents f) then
      invalid_arg "OpamPrinter.Preserved.items";
    let pos_index =
      let lines_index =
        let rec aux acc s =
          let until =
            try Some (String.index_from s (List.hd acc) '\n')
            with Not_found -> None
          in
          match until with
          | Some until -> aux (until+1 :: acc) s
          | None -> Array.of_list (List.rev acc)
        in
        aux [0] txt
      in
      fun (_file, li, col) -> lines_index.(li - 1) + col
    in
    let get_substring start_pos rest =
      let start = pos_index start_pos in
      let stop = match rest with
        | (Section (pos,_) | Variable (pos,_,_)) :: _ -> pos_index pos
        | [] -> String.length txt
      in
      if stop < start then raise Exit
      else String.sub txt start (stop - start)
    in
    let list_take f l =
      let rec aux acc = function
        | [] -> None, List.rev acc
        | x::r ->
          if f x then Some x, List.rev_append acc r
          else aux (x::acc) r
      in
      aux [] l
    in
    let is_variable name = function
      | Variable (_, name1, _v1) -> name = name1
      | _ -> false
    in
    let is_section kind name = function
      | Section (_, {section_kind; section_name; _}) ->
        kind = section_kind && name = section_name
      | _ -> false
    in
    let rec aux acc f = function
      | Variable (pos, name, v) :: r ->
        (match list_take (is_variable name) f with
         | Some (Variable (_, _, v1)), f when value_equals v v1 ->
           aux (get_substring pos r :: acc) f r
         | Some item, f ->
           aux ((items [item] ^ "\n") :: acc) f r
         | None, f ->
           aux acc f r)
      | Section (pos, {section_kind; section_name; _}) as sec :: r ->
        (match list_take (is_section section_kind section_name) f with
         | Some s, f when opamfile_item_equals sec s ->
           aux (get_substring pos r :: acc) f r
         | Some item, f ->
           aux ((items [item] ^ "\n") :: acc) f r
         | None, f -> aux acc f r)
      | [] ->
        let remaining = match f with
          | [] -> []
          | f -> [items f ^ "\n"]
        in
        List.rev_append acc remaining
    in
    let header = [get_substring ("",1,0) orig] in
    String.concat "" (aux header f orig)

  let opamfile ?format_from f =
    let orig_file = match format_from with
      | Some name -> name
      | None -> f.file_name
    in
    let txt =
      let b = Buffer.create 4096 in
      let ic = open_in orig_file in
      try while true do Buffer.add_channel b ic 4096 done; assert false with
      | End_of_file -> close_in ic; Buffer.contents b
      | e -> close_in ic; raise e
    in
    let[@ocaml.warning "-3"] orig = OpamParser.string txt orig_file in
    items txt orig.file_contents f.file_contents

end

module FullPos = struct

  open OpamParserTypes.FullPos

  let valid_opamfile_contents = function
  | {pelem = Variable ({pelem = "opam-version"; _}, {pelem = String ver; _}); _}::items
      when nopatch ver >= (2, 1) ->
        let opam_version_field = function
        | {pelem = Variable ({pelem = "opam-version"; _}, _); _} -> true
        | _ -> false
        in
          not (List.exists opam_version_field items)
  | _::items ->
      let greater_2_0_opam_version_field = function
      | {pelem = Variable ({pelem = "opam-version"; _}, {pelem = String ver; _}); _}
         when nopatch ver >= (2, 1) -> true
      | _ -> false
      in
        not (List.exists greater_2_0_opam_version_field items)
  | [] -> true

  let relop_kind = relop
  let relop r = relop_kind r.pelem
  let logop_kind = logop
  let logop r = logop_kind r.pelem
  let pfxop_kind = pfxop
  let pfxop r = pfxop_kind r.pelem
  let env_update_op_kind = env_update_op
  let env_update_op r = env_update_op_kind r.pelem

  let rec format_value fmt v =
    match v.pelem with
    | Relop (op,l,r) ->
      Format.fprintf fmt "@[<h>%a %s@ %a@]"
        format_value l (relop op) format_value r
    | Logop (op,l,r) ->
      Format.fprintf fmt "@[<hv>%a %s@ %a@]"
        format_value l (logop op) format_value r
    | Pfxop (op,r) ->
      Format.fprintf fmt "@[<h>%s%a@]" (pfxop op) format_value r
    | Prefix_relop (op,r) ->
      Format.fprintf fmt "@[<h>%s@ %a@]"
        (relop op) format_value r
    | Ident s -> Format.fprintf fmt "%s" s
    | Int i -> Format.fprintf fmt "%d" i
    | Bool b -> Format.fprintf fmt "%b" b
    | String s ->
      if String.contains s '\n'
      then Format.fprintf fmt "\"\"\"%s%s\"\"\""
          (if s.[0] = '\n' then "" else "\\\n")
          (escape_string ~triple:true s)
      else Format.fprintf fmt "\"%s\"" (escape_string s)
    | List l ->
      Format.fprintf fmt "@[<hv>[@;<0 2>@[<hv>%a@]@,]@]" format_values l.pelem
    | Group g -> Format.fprintf fmt "@[<hv>(%a)@]" format_values g.pelem
    | Option (v,l) ->
      Format.fprintf fmt "@[<hov 2>%a@ {@[<hv>%a@]}@]"
        format_value v format_values l.pelem
    | Env_binding (id,op,v) ->
      Format.fprintf fmt "@[<h>%a %s@ %a@]"
        format_value id (env_update_op op) format_value v

  and format_values fmt = function
    | [] -> ()
    | [v] -> format_value fmt v
    | v::r ->
      format_value fmt v;
      Format.pp_print_space fmt ();
      format_values fmt r

  let value v =
    format_value Format.str_formatter v; Format.flush_str_formatter ()

  let value_list vl =
    Format.fprintf Format.str_formatter "@[<hv>%a@]" format_values vl.pelem;
    Format.flush_str_formatter ()

  let format_sstring fmt s =
    Format.fprintf fmt "%s" s.pelem

  let rec format_item fmt i =
    match i.pelem with
    | Variable (_, { pelem = List { pelem = []; _}; _}) -> ()
    | Variable (_,
                { pelem =
                    List { pelem =
                             [{ pelem =
                                  List { pelem = []; _}; _}]; _}; _}) -> ()
    | Variable (i, { pelem = List { pelem = l; _}; _}) ->
      if List.exists
          (fun v ->
             match v.pelem with
               List _ | Option (_,{ pelem = _::_; _}) -> true
             | _ -> false)
          l
      then Format.fprintf fmt "@[<v>%a: [@;<0 2>@[<v>%a@]@,]@]"
          format_sstring i format_values l
      else Format.fprintf fmt "@[<hv>%a: [@;<0 2>@[<hv>%a@]@,]@]"
          format_sstring i format_values l
    | Variable (i, ({ pelem = String s ; _} as v))
      when String.contains s '\n' ->
      Format.fprintf fmt "@[<hov 0>%a: %a@]" format_sstring i format_value v
    | Variable (i, v) ->
      Format.fprintf fmt "@[<hov 2>%a:@ %a@]" format_sstring i format_value v
    | Section s ->
      Format.fprintf fmt "@[<v 0>%a %s{@;<0 2>@[<v>%a@]@,}@]"
        format_sstring s.section_kind
        (match s.section_name with
         | Some s -> Printf.sprintf "\"%s\" " (escape_string s.pelem)
         | None -> "")
        format_items s.section_items.pelem
  and format_items fmt is =
    Format.pp_open_vbox fmt 0;
    (match is with
     | [] -> ()
     | i::r ->
       format_item fmt i;
       List.iter (fun i -> Format.pp_print_cut fmt (); format_item fmt i) r);
    Format.pp_close_box fmt ()

  let format_opamfile fmt f =
    format_items fmt f.file_contents;
    Format.pp_print_newline fmt ()

  let items l =
    if not (valid_opamfile_contents l) then
      invalid_arg "OpamPrinter.FullPos.items";
    format_items Format.str_formatter l; Format.flush_str_formatter ()

  let opamfile f =
    items f.file_contents

  let relop_equals r1 r2 = r1.pelem = r2.pelem
  let logop_equals r1 r2 = r1.pelem = r2.pelem
  let pfxop_equals r1 r2 = r1.pelem = r2.pelem
  let env_update_op_equals r1 r2 = r1.pelem = r2.pelem

  let rec value_equals v1 v2 =
    match v1.pelem, v2.pelem with
    | Bool (b1), Bool (b2) -> b1 = b2
    | Int (i1), Int (i2) -> i1 = i2
    | String (s1), String (s2) -> s1 = s2
    | Relop (r1, va1, vb1), Relop (r2, va2, vb2) ->
      relop_equals r1  r2 && value_equals va1 va2 && value_equals vb1 vb2
    | Prefix_relop (r1, v1), Prefix_relop (r2, v2) ->
      relop_equals r1 r2 && value_equals v1 v2
    | Logop (l1, va1, vb1), Logop (l2, va2, vb2) ->
      logop_equals l1 l2 && value_equals va1 va2 && value_equals vb1 vb2
    | Pfxop (p1, v1), Pfxop (p2, v2) ->
      pfxop_equals p1 p2 && value_equals v1 v2
    | Ident (s1), Ident (s2) ->
      s1 = s2
    | List (vl1), List (vl2) ->
      (try List.for_all2 value_equals vl1.pelem vl2.pelem
       with Invalid_argument _ -> false)
    | Group (vl1), Group (vl2) ->
      (try List.for_all2 value_equals vl1.pelem vl2.pelem
       with Invalid_argument _ -> false)
    | Option (v1, vl1), Option (v2, vl2) ->
      value_equals v1 v2 &&
      (try List.for_all2 value_equals vl1.pelem vl2.pelem
       with Invalid_argument _ -> false)
    | Env_binding (v1, op1, vx1), Env_binding (v2, op2, vx2) ->
      env_update_op_equals op1 op2 && value_equals v1 v2 && value_equals vx1 vx2
    | _ -> false

  let sstring_equals s1 s2 = s1.pelem = s2.pelem

  let rec opamfile_item_equals i1 i2 =
    match i1.pelem , i2.pelem with
    | Variable (n1, v1), Variable (n2, v2) ->
      sstring_equals n1 n2 && value_equals v1 v2
    | Section (s1), Section (s2) ->
      sstring_equals s1.section_kind s2.section_kind &&
      (match s1.section_name, s2.section_name with
       | None, None -> true
       | Some s1, Some s2 -> sstring_equals s1 s2
       | _,_ -> false)
      && (try List.for_all2 opamfile_item_equals
                s1.section_items.pelem s2.section_items.pelem
          with Invalid_argument _ -> false)
    | _ -> false

  module Normalise = struct
    let escape_string s =
      let len = String.length s in
      let buf = Buffer.create (len * 2) in
      Buffer.add_char buf '"';
      for i = 0 to len -1 do
        match s.[i] with
        | '\\' | '"' as c -> Buffer.add_char buf '\\'; Buffer.add_char buf c
        | '\n' -> Buffer.add_string buf "\\n"
        | c -> Buffer.add_char buf c
      done;
      Buffer.add_char buf '"';
      Buffer.contents buf

    let rec value v =
      match v.pelem with
      | Relop (op,l,r) ->
        String.concat " " [value l; relop op; value r]
      | Logop (op,l,r) ->
        String.concat " " [value l; logop op; value r]
      | Pfxop (op,r) ->
        String.concat " " [pfxop op; value r]
      | Prefix_relop (op,r) ->
        String.concat " " [relop op; value r]
      | Ident s -> s
      | Int i -> string_of_int i
      | Bool b -> string_of_bool b
      | String s -> escape_string s
      | List l ->
        Printf.sprintf "[%s]" (String.concat " " (List.map value l.pelem))
      | Group g ->
        Printf.sprintf "(%s)" (String.concat " " (List.map value g.pelem))
      | Option(v,l) ->
        Printf.sprintf "%s {%s}" (value v)
          (String.concat " " (List.map value l.pelem))
      | Env_binding (id,op,v) ->
        String.concat " "
          [value id; env_update_op op; value v]

    let rec item i =
      match i.pelem with
      | Variable (_, { pelem = List { pelem = []; _}; _})
      | Variable (_, { pelem =
                         List { pelem =
                                  [{ pelem = List { pelem = []; _}; _}]; _}; _})
        -> ""
      | Variable (i, { pelem = List { pelem = l; _}; _}) ->
        Printf.sprintf "%s: [%s]" i.pelem (String.concat " " (List.map value l))
      | Variable (i, v) -> String.concat ": " [i.pelem; value v]
      | Section s ->
        Printf.sprintf "%s %s{\n%s\n}"
          s.section_kind.pelem
          (match s.section_name with
           | Some s -> escape_string s.pelem ^ " "
           | None -> "")
          (String.concat "\n" (List.map item s.section_items.pelem))

    let item_order a b =
      match a.pelem ,b.pelem with
      | Variable ({pelem = "opam-version"; _}, {pelem = String ver; _}), _
        when nopatch ver >= (2, 1) -> -1
      | _, Variable ({pelem = "opam-version"; _}, {pelem = String ver; _})
        when nopatch ver >= (2, 1) -> 1
      | Section _, Variable _ -> 1
      | Variable _, Section _ -> -1
      | Variable (i,_), Variable (j,_) -> String.compare i.pelem j.pelem
      | Section s, Section t ->
        let r = String.compare s.section_kind.pelem t.section_kind.pelem in
        if r <> 0 then r
        else compare s.section_name t.section_name

    let items its =
      if not (valid_opamfile_contents its) then
        invalid_arg "OpamPrinter.Normalise.items";
      let its = List.sort item_order its in
      String.concat "\n" (List.map item its) ^ "\n"

    let opamfile f = items f.file_contents
  end

  module Preserved = struct
    let items txt orig f =
      if not (valid_opamfile_contents f) then
        invalid_arg "OpamPrinter.Preserved.items";
      let pos_index =
        let lines_index =
          let rec aux acc s =
            let until =
              try Some (String.index_from s (List.hd acc) '\n')
              with Not_found -> None
            in
            match until with
            | Some until -> aux (until+1 :: acc) s
            | None -> Array.of_list (List.rev acc)
          in
          aux [0] txt
        in
        fun p ->
          let sli, scol = p.start in
          let eli, ecol = p.stop in
          lines_index.(sli - 1) + scol, lines_index.(eli -1) + ecol
      in
      let get_substring pos =
        let start, stop = pos_index pos in
        if stop < start then raise Exit
        else String.sub txt start (stop - start)
      in
      let list_take f l =
        let rec aux acc = function
          | [] -> None, List.rev acc
          | x::r ->
            if f x then Some x, List.rev_append acc r
            else aux (x::acc) r
        in
        aux [] l
      in
      let is_variable name = function
        | { pelem = Variable (name1, _v1); _} -> sstring_equals name name1
        | _ -> false
      in
      let is_section kind name = function
        | { pelem = Section ({section_kind; section_name; _}); _} ->
          sstring_equals kind section_kind &&
          (match name, section_name with
           | None, None -> true
           | Some s1, Some s2 -> sstring_equals s1 s2
           | _,_ -> false)
        | _ -> false
      in
      let rec aux acc f = function
        | { pos; pelem = Variable ( name, v)} :: r ->
          (match list_take (is_variable name) f with
           | Some {pelem = Variable (_, v1); _}, f when value_equals v v1 ->
             aux (get_substring pos :: acc) f r
           | Some item, f ->
             aux (items [item] :: acc) f r
           | None, f ->
             aux acc f r)
        | {pos; pelem = Section {section_kind; section_name; _}} as sec :: r ->
          (match list_take (is_section section_kind section_name) f with
           | Some s, f when opamfile_item_equals sec s ->
             aux (get_substring pos :: acc) f r
           | Some item, f ->
             aux (items [item] :: acc) f r
           | None, f -> aux acc f r)
        | [] ->
          let remaining = match f with
            | [] -> [""]
            | f -> [items f; ""]
          in
          List.rev_append acc remaining
      in
      let body = String.concat "\n" (aux [] f orig) in
      let header =
        match orig with
        | [] -> ""
        | h::_ -> get_substring {filename=""; start=(1,0); stop=h.pos.start}
      in
      header ^ body

    let opamfile ?format_from f =
      let orig_file = match format_from with
        | Some name -> name
        | None -> f.file_name
      in
      let txt =
        let b = Buffer.create 4096 in
        let ic = open_in orig_file in
        try while true do Buffer.add_channel b ic 4096 done; assert false with
        | End_of_file -> close_in ic; Buffer.contents b
        | e -> close_in ic; raise e
      in
      let orig = OpamParser.FullPos.string txt orig_file in
      items txt orig.file_contents f.file_contents

  end
end
OCaml

Innovation. Community. Security.