| 1 |
(**************************************************************************) |
| 2 |
(* *) |
| 3 |
(* Copyright 2012-2020 OCamlPro *) |
| 4 |
(* Copyright 2012 INRIA *) |
| 5 |
(* *) |
| 6 |
(* All rights reserved. This file is distributed under the terms of the *) |
| 7 |
(* GNU Lesser General Public License version 2.1, with the special *) |
| 8 |
(* exception on linking described in the file LICENSE. *) |
| 9 |
(* *) |
| 10 |
(**************************************************************************) |
| 11 |
|
| 12 |
open OpamParserTypes.FullPos |
| 13 |
open OpamTypes |
| 14 |
open OpamTypesBase |
| 15 |
open OpamStd.Op |
| 16 |
open OpamPp |
| 17 |
open OpamPp.Op |
| 18 |
|
| 19 |
let item_pos (i: opamfile_item) = i.pos |
| 20 |
|
| 21 |
let value_pos (v: value) = v.pos |
| 22 |
|
| 23 |
let values_pos = function |
| 24 |
| [] -> None |
| 25 |
| x::_ -> Some (value_pos x) |
| 26 |
|
| 27 |
let optelem = OpamStd.Option.map (fun x -> x.pelem) |
| 28 |
let nullposelem = OpamStd.Option.map nullify_pos |
| 29 |
|
| 30 |
(** low-level Pps for the Lines parser ([string list list]) *) |
| 31 |
|
| 32 |
type lines = string list list |
| 33 |
|
| 34 |
let lines_set ~empty ~add ~fold pp1 = |
| 35 |
pp |
| 36 |
~name:(Printf.sprintf "(%s) lines" pp1.ppname) |
| 37 |
(fun ~pos lines -> |
| 38 |
List.fold_left (fun (i,acc) -> function |
| 39 |
| [] -> i + 1, acc |
| 40 |
| line -> |
| 41 |
(* XXX POSCHECK *) |
| 42 |
let pos = { pos with start = i,0 ; stop = i,0 } in |
| 43 |
i + 1, |
| 44 |
add (parse pp1 ~pos line) acc) |
| 45 |
(1, empty) lines |
| 46 |
|> snd) |
| 47 |
(fun x -> |
| 48 |
List.rev (fold (fun v acc -> print pp1 v::acc) x [])) |
| 49 |
|
| 50 |
let lines_map ~empty ~add ~fold pp1 = |
| 51 |
pp |
| 52 |
~name:(Printf.sprintf "(%s) lines" pp1.ppname) |
| 53 |
(fun ~pos lines -> |
| 54 |
List.fold_left (fun (i,acc) -> function |
| 55 |
| [] -> i + 1, acc |
| 56 |
| line -> |
| 57 |
let pos = { pos with start = i,0 ; stop = i,0 } in |
| 58 |
(* XXX POSCHECK *) |
| 59 |
let k,v = parse pp1 ~pos line in |
| 60 |
i + 1, add k v acc) |
| 61 |
(1, empty) lines |
| 62 |
|> snd) |
| 63 |
(fun x -> |
| 64 |
List.rev (fold (fun k v acc -> print pp1 (k,v)::acc) x [])) |
| 65 |
|
| 66 |
(* |
| 67 |
let list2 pp1 pp2 = |
| 68 |
pp ~name:(Printf.sprintf "%s %s" pp1.ppname pp2.ppname) |
| 69 |
(function [a; b] -> parse pp1 a, parse pp2 b |
| 70 |
| _ -> unexpected ()) |
| 71 |
(fun (x,y) -> [print pp1 x; print pp2 y]) |
| 72 |
*) |
| 73 |
|
| 74 |
(** All Pps dealing with the [value] type *) |
| 75 |
module V = struct |
| 76 |
|
| 77 |
(** Low-level Pps *) |
| 78 |
|
| 79 |
let bool = |
| 80 |
pp ~name:"bool" |
| 81 |
(fun ~pos:_ v -> match v.pelem with Bool b -> b | _ -> unexpected ()) |
| 82 |
(fun b -> nullify_pos (Bool b)) |
| 83 |
|
| 84 |
let int = |
| 85 |
pp ~name:"int" |
| 86 |
(fun ~pos:_ v -> match v.pelem with Int i -> i | _ -> unexpected ()) |
| 87 |
(fun i -> nullify_pos (Int i)) |
| 88 |
|
| 89 |
let pos_int = int -| check ~name:"positive-int" (fun i -> i >= 0) |
| 90 |
|
| 91 |
let ident = |
| 92 |
pp ~name:"ident" |
| 93 |
(fun ~pos:_ v -> match v.pelem with Ident i -> i | _ -> unexpected ()) |
| 94 |
(fun str -> nullify_pos (Ident str)) |
| 95 |
|
| 96 |
let string = |
| 97 |
pp ~name:"string" |
| 98 |
(fun ~pos:_ v -> match v.pelem with String s -> s | _ -> unexpected ()) |
| 99 |
(fun str -> nullify_pos (String str)) |
| 100 |
|
| 101 |
let string_tr = string -| pp (fun ~pos:_ -> OpamStd.String.strip) (fun x -> x) |
| 102 |
|
| 103 |
let simple_arg = |
| 104 |
pp ~name:"ident-or-string" |
| 105 |
(fun ~pos:_ v -> |
| 106 |
match v.pelem with |
| 107 |
| Ident i -> CIdent i |
| 108 |
| String s -> CString s |
| 109 |
| _ -> unexpected ()) |
| 110 |
(function |
| 111 |
| CIdent i -> nullify_pos (Ident i) |
| 112 |
| CString s -> nullify_pos (String s)) |
| 113 |
|
| 114 |
let variable_contents = |
| 115 |
pp ~name:"string-or-stringlist-or-bool" |
| 116 |
(fun ~pos:_ v -> |
| 117 |
match v.pelem with |
| 118 |
| String s -> S s |
| 119 |
| Bool b -> B b |
| 120 |
| List l -> |
| 121 |
L (List.map (fun v -> |
| 122 |
match v.pelem with String s -> s | _ -> unexpected ()) |
| 123 |
l.pelem) |
| 124 |
| _ -> unexpected ()) |
| 125 |
(function |
| 126 |
| S s -> nullify_pos (String s) |
| 127 |
| B b -> nullify_pos (Bool b) |
| 128 |
| L l -> |
| 129 |
nullify_pos (List (nullify_pos (List.map (fun s -> |
| 130 |
nullify_pos (String s)) l)))) |
| 131 |
|
| 132 |
let list = |
| 133 |
pp ~name:"list" ~name_constr:(Printf.sprintf "[%s]") |
| 134 |
(fun ~pos:_ v -> |
| 135 |
match v.pelem with |
| 136 |
| List l -> l.pelem |
| 137 |
| _ -> [v]) |
| 138 |
(fun l -> nullify_pos (List (nullify_pos l))) |
| 139 |
|
| 140 |
let group = |
| 141 |
pp ~name:"group" ~name_constr:(Printf.sprintf "(%s)") |
| 142 |
(fun ~pos:_ v -> |
| 143 |
match v.pelem with |
| 144 |
| Group l -> l.pelem |
| 145 |
| _ -> [v]) |
| 146 |
(fun l -> nullify_pos (Group (nullify_pos l))) |
| 147 |
|
| 148 |
let option = |
| 149 |
pp ~name:"option" |
| 150 |
(fun ~pos:_ v -> |
| 151 |
match v.pelem with |
| 152 |
| Option (k,l) -> k, l.pelem |
| 153 |
| _ -> v, []) |
| 154 |
(function |
| 155 |
| (v, []) -> v |
| 156 |
| (v, l) -> nullify_pos (Option (v, nullify_pos l))) |
| 157 |
|
| 158 |
let option_strict = |
| 159 |
pp ~name:"option" |
| 160 |
(fun ~pos v -> |
| 161 |
match v.pelem with |
| 162 |
| Option (k,l) -> k, l.pelem |
| 163 |
| _ -> bad_format ~pos "Expected an option") |
| 164 |
(function (v, l) -> nullify_pos (Option (v, nullify_pos l))) |
| 165 |
|
| 166 |
let map_group pp1 = group -| map_list ~posf:value_pos pp1 |
| 167 |
|
| 168 |
let list_depth expected_depth = |
| 169 |
let rec depth v = |
| 170 |
match v.pelem with |
| 171 |
| List { pelem = []; _} -> 1 |
| 172 |
| List { pelem = v::_; _} -> 1 + depth v |
| 173 |
| Option (v,_) -> depth v |
| 174 |
| _ -> 0 |
| 175 |
in |
| 176 |
let rec wrap n v = |
| 177 |
if n <= 0 then v else wrap (n-1) (nullify_pos (List (nullify_pos [v]))) |
| 178 |
in |
| 179 |
let rec lift n v = |
| 180 |
if n <= 0 then v else |
| 181 |
match v.pelem with |
| 182 |
| List { pelem = [v]; _} -> lift (n-1) v |
| 183 |
| _ -> v |
| 184 |
in |
| 185 |
pp |
| 186 |
(fun ~pos:_ v -> wrap (expected_depth - depth v) v) |
| 187 |
(fun v -> lift expected_depth v) |
| 188 |
|
| 189 |
let option_depth expected_depth = |
| 190 |
let rec depth = function |
| 191 |
| { pelem = Option (v,_); _} -> 1 + depth v |
| 192 |
| _ -> 0 |
| 193 |
in |
| 194 |
let rec wrap n v = |
| 195 |
if n <= 0 then v else |
| 196 |
wrap (n-1) (nullify_pos (Option (v, nullify_pos []))) |
| 197 |
in |
| 198 |
let rec lift n v = |
| 199 |
if n <= 0 then v else |
| 200 |
match v.pelem with |
| 201 |
| Option (v, {pelem = []; _}) -> lift (n-1) v |
| 202 |
| _ -> v |
| 203 |
in |
| 204 |
pp |
| 205 |
(fun ~pos:_ v -> wrap (expected_depth - depth v) v) |
| 206 |
(fun v -> lift expected_depth v) |
| 207 |
|
| 208 |
let map_list ?(depth=0) pp1 = |
| 209 |
list_depth depth -| |
| 210 |
pp ~name:(Printf.sprintf "[%s]" pp1.ppname) |
| 211 |
(fun ~pos:_ v -> |
| 212 |
match v.pelem with |
| 213 |
| List l -> |
| 214 |
List.rev @@ |
| 215 |
List.rev_map (fun v -> parse pp1 ~pos:(value_pos v) v) l.pelem |
| 216 |
| _ -> unexpected ()) |
| 217 |
(fun l -> |
| 218 |
nullify_pos (List (nullify_pos @@ |
| 219 |
List.rev @@ List.rev_map (print pp1) l))) |
| 220 |
|
| 221 |
let map_option_contents pp1 pp2 = |
| 222 |
map_pair ~name:(Printf.sprintf "%s ?{%s}" pp1.ppname pp2.ppname) |
| 223 |
~posf1:value_pos |
| 224 |
~posf2:(fun v -> OpamStd.Option.default pos_null (values_pos v)) |
| 225 |
pp1 pp2 |
| 226 |
|
| 227 |
let map_option pp1 pp2 = option -| map_option_contents pp1 pp2 |
| 228 |
|
| 229 |
let map_options_2 pp1 pp2 pp3 = |
| 230 |
option_depth 2 -| |
| 231 |
option_strict -| map_option_contents |
| 232 |
(option_strict -| map_option_contents |
| 233 |
pp1 pp2) |
| 234 |
pp3 -| |
| 235 |
pp (fun ~pos:_ ((a,b),c) -> a,b,c) (fun (a,b,c) -> (a,b),c) |
| 236 |
|
| 237 |
let map_options_3 pp1 pp2 pp3 pp4 = |
| 238 |
option_depth 3 -| |
| 239 |
option_strict -| map_option_contents |
| 240 |
(option_strict -| map_option_contents |
| 241 |
(option_strict -| map_option_contents |
| 242 |
pp1 pp2) |
| 243 |
pp3) |
| 244 |
pp4 -| |
| 245 |
pp (fun ~pos:_ (((a,b),c),d) -> a,b,c,d) (fun (a,b,c,d) -> ((a,b),c),d) |
| 246 |
|
| 247 |
let map_pair pp1 pp2 = |
| 248 |
pp ~name:(Printf.sprintf "[%s %s]" pp1.ppname pp2.ppname) |
| 249 |
(fun ~pos:_ v -> match v.pelem with |
| 250 |
| List { pelem = [a; b]; _} -> |
| 251 |
parse pp1 ~pos:(value_pos a) a, parse pp2 ~pos:(value_pos b) b |
| 252 |
| _ -> unexpected ()) |
| 253 |
(fun (a, b) -> |
| 254 |
nullify_pos @@ List (nullify_pos @@ [pp1.print a; pp2.print b])) |
| 255 |
|
| 256 |
let map_triple pp1 pp2 pp3 = |
| 257 |
pp ~name:(Printf.sprintf "[%s %s %s]" pp1.ppname pp2.ppname pp3.ppname) |
| 258 |
(fun ~pos:_ v -> match v.pelem with |
| 259 |
| List { pelem = [a; b; c]; _} -> |
| 260 |
parse pp1 ~pos:(value_pos a) a, |
| 261 |
parse pp2 ~pos:(value_pos b) b, |
| 262 |
parse pp3 ~pos:(value_pos c) c |
| 263 |
| _ -> unexpected ()) |
| 264 |
(fun (a, b, c) -> |
| 265 |
nullify_pos @@ List (nullify_pos @@ |
| 266 |
[pp1.print a; pp2.print b; pp3.print c])) |
| 267 |
|
| 268 |
(** Pps for the [value] type to higher level types *) |
| 269 |
|
| 270 |
let url = string -| of_module "url" (module OpamUrl) |
| 271 |
|
| 272 |
let url_with_backend backend = |
| 273 |
string -| |
| 274 |
pp ~name:"url" |
| 275 |
(fun ~pos:_ s -> OpamUrl.parse ~backend ~handle_suffix:false s) |
| 276 |
(fun url -> OpamUrl.to_string url) |
| 277 |
|
| 278 |
(* a hack to allow "system" compiler as ident rather than string. For |
| 279 |
backwards-compat. Deprecated, for migration only *) |
| 280 |
let compiler_version = |
| 281 |
let system_compiler = "system" in |
| 282 |
let parse ~pos:_ = fun v -> |
| 283 |
match v.pelem with |
| 284 |
| Ident v when v = system_compiler -> v |
| 285 |
| String v -> v |
| 286 |
| _ -> unexpected () |
| 287 |
in |
| 288 |
let print v = |
| 289 |
if v = system_compiler then print ident v |
| 290 |
else print string v |
| 291 |
in |
| 292 |
pp ~name:"compiler-version" parse print |
| 293 |
|
| 294 |
let filter_ident = |
| 295 |
ident -| |
| 296 |
pp ~name:"filter-ident" |
| 297 |
(fun ~pos:_ -> filter_ident_of_string) |
| 298 |
string_of_filter_ident |
| 299 |
|
| 300 |
let filter = |
| 301 |
let rec parse_filter ~pos l = |
| 302 |
let rec aux = fun v -> |
| 303 |
match v.pelem with |
| 304 |
| Bool b -> FBool b |
| 305 |
| String s -> FString s |
| 306 |
| Int i -> FString (string_of_int i) |
| 307 |
| Ident _ -> FIdent (parse ~pos:v.pos filter_ident v) |
| 308 |
| Group g -> parse_filter ~pos:v.pos g.pelem |
| 309 |
| Relop (op,e,f) -> FOp (aux e, op.pelem, aux f) |
| 310 |
| Pfxop ({ pelem = `Not; _}, e) -> FNot (aux e) |
| 311 |
| Pfxop ({ pelem = `Defined; _}, e) -> FDefined (aux e) |
| 312 |
| Logop ({ pelem = `And; _}, e, f)-> FAnd (aux e, aux f) |
| 313 |
| Logop ({ pelem = `Or; _}, e, f)-> FOr (aux e, aux f) |
| 314 |
| _ -> unexpected () |
| 315 |
in |
| 316 |
match l with |
| 317 |
| [] -> FBool true |
| 318 |
| [{ pelem = Group { pelem = [] | _::_::_ ; _}; _}] |
| 319 |
| _::_::_ -> |
| 320 |
bad_format ~pos "expected a single filter expression" |
| 321 |
| [{ pelem = Group { pelem = [f]; _}; _}] | [f] -> aux f |
| 322 |
in |
| 323 |
let print_filter f = |
| 324 |
let rec aux ?(context=`Or) f = |
| 325 |
let group_if ?(cond=false) f = |
| 326 |
if cond || OpamFormatConfig.(!r.all_parens) |
| 327 |
then nullify_pos @@ Group (nullify_pos [f]) |
| 328 |
else f |
| 329 |
in |
| 330 |
match f with |
| 331 |
| FString s -> print string s |
| 332 |
| FIdent fid -> print filter_ident fid |
| 333 |
| FBool b -> print bool b |
| 334 |
| FOp (e,s,f) -> |
| 335 |
group_if ~cond:(context <> `Or && context <> `And) @@ nullify_pos @@ |
| 336 |
Relop (nullify_pos s, aux ~context:`Relop e, aux ~context:`Relop f) |
| 337 |
| FOr (e,f) -> |
| 338 |
group_if ~cond:(context <> `Or) @@ nullify_pos @@ |
| 339 |
Logop (nullify_pos `Or, aux ~context:`Or e, aux ~context:`Or f) |
| 340 |
| FAnd (e,f) -> |
| 341 |
group_if ~cond:(context <> `Or && context <> `And) @@ nullify_pos @@ |
| 342 |
Logop (nullify_pos `And, aux ~context:`And e, aux ~context:`And f) |
| 343 |
| FNot f -> |
| 344 |
group_if ~cond:(context = `Relop) @@ nullify_pos @@ |
| 345 |
Pfxop (nullify_pos `Not, aux ~context:`Not f) |
| 346 |
| FDefined f -> |
| 347 |
group_if ~cond:(context = `Relop) @@ nullify_pos @@ |
| 348 |
Pfxop (nullify_pos `Defined, aux ~context:`Defined f) |
| 349 |
| FUndef _ -> assert false |
| 350 |
in |
| 351 |
match f with |
| 352 |
| FBool true -> [] |
| 353 |
| f -> [aux f] |
| 354 |
in |
| 355 |
pp ~name:"filter-expression" parse_filter print_filter |
| 356 |
|
| 357 |
let arg = map_option simple_arg (opt filter) |
| 358 |
|
| 359 |
let command = map_option (map_list arg) (opt filter) |
| 360 |
|
| 361 |
let constraints version = |
| 362 |
let rec parse_constraints ~pos:_ l = |
| 363 |
let rec aux v = match v.pelem with |
| 364 |
| Prefix_relop (op, v) -> Atom (op.pelem, parse version ~pos:v.pos v) |
| 365 |
| Logop ({ pelem = `And; _}, l, r) -> And (aux l, aux r) |
| 366 |
| Logop ({ pelem = `Or; _}, l, r) -> Or (aux l, aux r) |
| 367 |
| Pfxop ({ pelem = `Not; _}, v) -> |
| 368 |
OpamFormula.neg (fun (op, s) -> |
| 369 |
(OpamFormula.neg_relop op, s)) (aux v) |
| 370 |
| Group g -> Block (parse_constraints ~pos:v.pos g.pelem) |
| 371 |
| _ -> unexpected ~pos:(value_pos v) () |
| 372 |
in |
| 373 |
OpamFormula.ands (List.map aux l) |
| 374 |
in |
| 375 |
let rec print_constraints cs = |
| 376 |
let rec aux ?(in_and=false) cs = |
| 377 |
let group_if ?(cond=false) f = |
| 378 |
if cond || OpamFormatConfig.(!r.all_parens) |
| 379 |
then nullify_pos @@ Group (nullify_pos [f]) |
| 380 |
else f |
| 381 |
in |
| 382 |
match cs with |
| 383 |
| Empty -> assert false |
| 384 |
| Atom (r, v) -> |
| 385 |
group_if @@ nullify_pos @@ |
| 386 |
Prefix_relop (nullify_pos r, print version v) |
| 387 |
| And (x, y) -> |
| 388 |
group_if @@ nullify_pos @@ |
| 389 |
Logop (nullify_pos `And, aux ~in_and:true x, aux ~in_and:true y) |
| 390 |
| Or (x, y) -> |
| 391 |
group_if ~cond:in_and @@ nullify_pos @@ |
| 392 |
Logop (nullify_pos `Or, aux x, aux y) |
| 393 |
| Block g -> |
| 394 |
nullify_pos @@ Group (nullify_pos @@ print_constraints g) |
| 395 |
in |
| 396 |
match cs with |
| 397 |
| Empty -> [] |
| 398 |
| cs -> [aux cs] |
| 399 |
in |
| 400 |
pp ~name:(version.ppname ^ "-constraints") |
| 401 |
parse_constraints print_constraints |
| 402 |
|
| 403 |
let filtered_constraints version = |
| 404 |
let rec parse_cs ~pos:_ items = |
| 405 |
let rec aux_parse v = match v.pelem with |
| 406 |
| Prefix_relop (op, v) -> |
| 407 |
Atom (Constraint (op.pelem, parse version ~pos:v.pos v)) |
| 408 |
| Logop ({ pelem = `And; _}, a, b) -> |
| 409 |
OpamFormula.ands [aux_parse a; aux_parse b] |
| 410 |
| Logop ({ pelem = `Or; _}, a, b) -> |
| 411 |
OpamFormula.ors [aux_parse a; aux_parse b] |
| 412 |
| Group g -> OpamFormula.Block (parse_cs ~pos:v.pos g.pelem) |
| 413 |
| Pfxop ({ pelem = `Not; _}, v') -> |
| 414 |
parse_cs ~pos:v.pos [v'] |> |
| 415 |
OpamFormula.neg (function |
| 416 |
| Constraint (op, v) -> Constraint (OpamFormula.neg_relop op, v) |
| 417 |
| Filter f -> Filter (FNot f)) |
| 418 |
| _ -> |
| 419 |
Atom (Filter (filter.parse ~pos:(value_pos v) [v])) |
| 420 |
in |
| 421 |
OpamFormula.ands (List.map aux_parse items) |
| 422 |
in |
| 423 |
let rec print_cs cs = (* form -> val list *) |
| 424 |
let rec aux ?(in_and=false) cs = |
| 425 |
let group_if ?(cond=false) f = |
| 426 |
if cond || OpamFormatConfig.(!r.all_parens) |
| 427 |
then nullify_pos @@ Group (nullify_pos [f]) |
| 428 |
else f |
| 429 |
in |
| 430 |
match cs with |
| 431 |
| Empty -> assert false |
| 432 |
| And (x, y) -> |
| 433 |
group_if @@ nullify_pos @@ |
| 434 |
Logop (nullify_pos `And, aux ~in_and:true x, aux ~in_and:true y) |
| 435 |
| Or (x, y) -> |
| 436 |
group_if ~cond:in_and @@ nullify_pos @@ |
| 437 |
Logop (nullify_pos `Or, aux x, aux y) |
| 438 |
| Block g -> nullify_pos @@ Group (nullify_pos @@ print_cs g) |
| 439 |
| Atom (Constraint (op,v)) -> |
| 440 |
group_if @@ nullify_pos @@ |
| 441 |
Prefix_relop (nullify_pos op, print version v) |
| 442 |
| Atom (Filter flt) -> |
| 443 |
(match filter.print flt with |
| 444 |
| f1::fr -> |
| 445 |
group_if (List.fold_left (fun a b -> |
| 446 |
nullify_pos @@ Logop (nullify_pos `And, a, b)) |
| 447 |
f1 fr) |
| 448 |
| [] -> nullify_pos @@ Group (nullify_pos [])) |
| 449 |
in |
| 450 |
match cs with |
| 451 |
| Empty -> [] |
| 452 |
| cs -> [aux cs] |
| 453 |
in |
| 454 |
pp ~name:"filtered-constraints" parse_cs print_cs |
| 455 |
|
| 456 |
let version = |
| 457 |
string -| of_module "version" (module OpamPackage.Version) |
| 458 |
|
| 459 |
let ext_version = |
| 460 |
pp ~name:"version-expr" |
| 461 |
(fun ~pos:_ v -> match v.pelem with |
| 462 |
| String s -> |
| 463 |
let _ = |
| 464 |
try |
| 465 |
OpamPackage.Version.of_string |
| 466 |
(OpamFilter.expand_string (fun _ -> Some (S "-")) s) |
| 467 |
with Failure msg -> |
| 468 |
bad_format ~pos:v.pos "Invalid version string %S: %s" s msg |
| 469 |
in |
| 470 |
FString s |
| 471 |
| Ident s -> FIdent (filter_ident_of_string s) |
| 472 |
| _ -> unexpected ()) |
| 473 |
(function |
| 474 |
| FString s -> nullify_pos (String s) |
| 475 |
| FIdent id -> nullify_pos @@ Ident (string_of_filter_ident id) |
| 476 |
| _ -> assert false) |
| 477 |
|
| 478 |
let pkgname = |
| 479 |
string -| of_module "pkg-name" (module OpamPackage.Name) |
| 480 |
|
| 481 |
let package_atom constraints = |
| 482 |
map_option pkgname constraints |
| 483 |
|
| 484 |
(* These two functions are duplicated from [OpamFormula] but we need to have a |
| 485 |
it here because of a change on [Block] handling: to have a coherent |
| 486 |
printing, we must not always discard them *) |
| 487 |
let rec ands_to_list = function |
| 488 |
| Empty -> [] |
| 489 |
| And (e,f) -> |
| 490 |
List.rev_append (rev_ands_to_list e) (ands_to_list f) |
| 491 |
| x -> [x] |
| 492 |
and rev_ands_to_list = function |
| 493 |
| Empty -> [] |
| 494 |
| And (e,f) -> |
| 495 |
List.rev_append (ands_to_list f) (rev_ands_to_list e) |
| 496 |
| x -> [x] |
| 497 |
|
| 498 |
let rec ors_to_list = function |
| 499 |
| Empty -> [] |
| 500 |
| Or (e,f) |
| 501 |
| Block (Or (e,f)) -> |
| 502 |
List.rev_append (rev_ors_to_list e) (ors_to_list f) |
| 503 |
| x -> [x] |
| 504 |
and rev_ors_to_list = function |
| 505 |
| Empty -> [] |
| 506 |
| Or (e,f) |
| 507 |
| Block (Or (e,f)) -> |
| 508 |
List.rev_append (ors_to_list f) (rev_ors_to_list e) |
| 509 |
| x -> [x] |
| 510 |
|
| 511 |
let package_formula_items kind constraints = |
| 512 |
let split, join = match kind with |
| 513 |
| `Conj -> ands_to_list, OpamFormula.ands |
| 514 |
| `Disj -> ors_to_list, OpamFormula.ors |
| 515 |
in |
| 516 |
let rec parse_formula ~pos:_ l = |
| 517 |
let rec aux v = match v.pelem with |
| 518 |
| String _ | Option (_,_) -> |
| 519 |
Atom (parse (package_atom constraints) ~pos:v.pos v) |
| 520 |
| Group g -> Block (parse_formula ~pos:v.pos g.pelem) |
| 521 |
| Logop ({ pelem = `Or; _}, e1, e2) -> |
| 522 |
let left = aux e1 in Or (left, aux e2) |
| 523 |
| Logop ({ pelem = `And; _}, e1, e2) -> |
| 524 |
let left = aux e1 in And (left, aux e2) |
| 525 |
| _ -> unexpected ~pos:(value_pos v) () |
| 526 |
in |
| 527 |
join (List.map aux l) |
| 528 |
in |
| 529 |
let rec print_formula ?(inner=false) f = |
| 530 |
let rec aux ?(in_and=false) f = |
| 531 |
let group_if ?(cond=false) f = |
| 532 |
if cond || OpamFormatConfig.(!r.all_parens) |
| 533 |
then nullify_pos @@ Group (nullify_pos [f]) |
| 534 |
else f |
| 535 |
in |
| 536 |
match f with |
| 537 |
| Empty -> assert false |
| 538 |
| Block f -> |
| 539 |
nullify_pos @@ Group (nullify_pos @@ print_formula ~inner:true f) |
| 540 |
| And (e,f) -> |
| 541 |
group_if @@ nullify_pos @@ |
| 542 |
Logop (nullify_pos `And, aux ~in_and:true e, aux ~in_and:true f) |
| 543 |
| Or (e,f) -> |
| 544 |
group_if ~cond:in_and @@ nullify_pos @@ |
| 545 |
Logop (nullify_pos `Or, aux e, aux f) |
| 546 |
| Atom at -> group_if (print (package_atom constraints) at) |
| 547 |
in |
| 548 |
let fl = if inner then [f] else split f in |
| 549 |
List.map (aux ~in_and:false) fl |
| 550 |
in |
| 551 |
pp ~name:"pkg-formula" parse_formula print_formula |
| 552 |
|
| 553 |
let package_formula kind constraints = |
| 554 |
list -| package_formula_items kind constraints |
| 555 |
|
| 556 |
let env_binding = |
| 557 |
let parse ~pos:_ v = match v.pelem with |
| 558 |
| Relop ({ pelem = `Eq;_}, { pelem = Ident i;_}, { pelem = String s;_}) -> |
| 559 |
i, OpamParserTypes.Eq, s, None |
| 560 |
| Env_binding ({ pelem = Ident i; _}, op, { pelem = String s; _}) -> |
| 561 |
i, op.pelem, s, None |
| 562 |
| _ -> unexpected () |
| 563 |
in |
| 564 |
let print (id, op, str, _) = |
| 565 |
nullify_pos @@ |
| 566 |
Env_binding (print ident id, nullify_pos op, print string str) |
| 567 |
in |
| 568 |
list -| singleton -| pp ~name:"env-binding" parse print |
| 569 |
|
| 570 |
(* Only used by the deprecated "os" field *) |
| 571 |
let os_constraint = |
| 572 |
let rec parse_osc ~pos:_ l = |
| 573 |
let rec aux v = match v.pelem with |
| 574 |
| Group g -> Block (parse_osc ~pos:g.pos g.pelem) |
| 575 |
| String os -> Atom (true, os) |
| 576 |
| Logop ({ pelem = `And; _}, l, r) -> And (aux l, aux r) |
| 577 |
| Logop ({ pelem = `Or; _}, l, r) -> Or (aux l, aux r) |
| 578 |
| Pfxop ({ pelem = `Not; _}, v) -> |
| 579 |
OpamFormula.neg (fun (b, s) -> (not b, s)) (aux v) |
| 580 |
| _ -> unexpected ~pos:(value_pos v) () |
| 581 |
in |
| 582 |
OpamFormula.ors (List.map aux l) |
| 583 |
in |
| 584 |
let print_osc f = |
| 585 |
let rec aux = function |
| 586 |
| Empty -> assert false |
| 587 |
| Atom (true , os) -> print string os |
| 588 |
| Atom (false, os) -> |
| 589 |
nullify_pos @@ Pfxop (nullify_pos `Not, print string os) |
| 590 |
| Block g -> nullify_pos @@ Group (nullify_pos [aux g]) |
| 591 |
| And (e,f) -> nullify_pos @@ Logop (nullify_pos `And, aux e, aux f) |
| 592 |
| Or (e,f) -> nullify_pos @@ Logop (nullify_pos `Or, aux e, aux f) |
| 593 |
in |
| 594 |
match f with |
| 595 |
| Empty -> [] |
| 596 |
| f -> [aux f] |
| 597 |
in |
| 598 |
list -| pp ~name:"os-constraint" parse_osc print_osc |
| 599 |
|
| 600 |
end |
| 601 |
|
| 602 |
(** Parsers for item lists (standard opam file contents: list of field |
| 603 |
bindings). *) |
| 604 |
module I = struct |
| 605 |
|
| 606 |
let file = |
| 607 |
pp ~name:"opam-file" |
| 608 |
(fun ~pos:_ file -> |
| 609 |
OpamFilename.of_string file.file_name, |
| 610 |
file.file_contents) |
| 611 |
(fun (file_name, file_contents) -> |
| 612 |
{ file_name = OpamFilename.to_string file_name; |
| 613 |
file_contents }) |
| 614 |
|
| 615 |
let map_file pp1 = file -| map_snd pp1 |
| 616 |
|
| 617 |
let item = |
| 618 |
pp ~name:"field-binding" |
| 619 |
(fun ~pos:_ v -> match v.pelem with |
| 620 |
| Section (sec) -> |
| 621 |
bad_format ~pos:v.pos "Unexpected section %s" sec.section_kind.pelem |
| 622 |
| Variable (k,v) -> k.pelem, v) |
| 623 |
(fun (k,v) -> nullify_pos @@ Variable (nullify_pos k, v)) |
| 624 |
|
| 625 |
let items = map_list ~posf:item_pos item |
| 626 |
|
| 627 |
let anonymous_section pp1 = |
| 628 |
pp ~name:pp1.ppname |
| 629 |
(fun ~pos -> function |
| 630 |
| [ None, contents ] -> pp1.parse ~pos contents |
| 631 |
| [ Some _, _ ] -> |
| 632 |
bad_format ~pos "Unexpected section title" |
| 633 |
| [] -> |
| 634 |
bad_format ~pos "Missing section" |
| 635 |
| _::_::_ -> |
| 636 |
bad_format ~pos "Duplicate section") |
| 637 |
(fun l -> [None, pp1.print l]) |
| 638 |
|
| 639 |
let section kind = |
| 640 |
pp ~name:"file-section" |
| 641 |
(fun ~pos:_ v -> match v.pelem with |
| 642 |
| Section ({section_kind; _} as s) when section_kind.pelem = kind -> |
| 643 |
optelem s.section_name, s.section_items.pelem |
| 644 |
| Section sec -> |
| 645 |
bad_format ~pos:v.pos "Unexpected section %s" sec.section_kind.pelem |
| 646 |
| Variable (k,_) -> |
| 647 |
bad_format ~pos:v.pos "Unexpected field %s" k.pelem) |
| 648 |
(fun (name, items) -> |
| 649 |
nullify_pos @@ Section { |
| 650 |
section_kind = nullify_pos kind; |
| 651 |
section_name = nullposelem name; |
| 652 |
section_items = nullify_pos items }) |
| 653 |
|
| 654 |
type ('a, 'value) fields_def = (string * ('a, 'value) field_parser) list |
| 655 |
|
| 656 |
let fields ?name ~empty ?(sections=[]) ?(mandatory_fields=[]) ppas = |
| 657 |
let parse ~pos items = |
| 658 |
(* For consistency, always read fields in ppa order, ignoring file |
| 659 |
order. Some parsers may depend on it. *) |
| 660 |
let module SEM = OpamStd.Map.Make(struct |
| 661 |
type t = string * string option |
| 662 |
let compare = compare |
| 663 |
let to_string (s,o) = s ^ OpamStd.Option.to_string ((^) "^") o |
| 664 |
let to_json (s,o) = |
| 665 |
`O (("kind", `String s) :: |
| 666 |
match o with None -> [] | Some s -> ["name", `String s]) |
| 667 |
(* these serializers/deserializers are not accessible |
| 668 |
from the OpamFormat.mli interface, so there are not currently |
| 669 |
tested -- it's not clear if usage of the SEM functor touches |
| 670 |
them in any way... *) |
| 671 |
let of_json = function |
| 672 |
| `O dict -> |
| 673 |
begin try |
| 674 |
match OpamStd.List.assoc String.equal "kind" dict with |
| 675 |
| `String s -> |
| 676 |
begin |
| 677 |
let o = |
| 678 |
if not (OpamStd.List.mem_assoc String.equal |
| 679 |
"name" dict) then None else |
| 680 |
match OpamStd.List.assoc String.equal "name" dict with |
| 681 |
| `String s -> Some s |
| 682 |
| _ -> raise Not_found |
| 683 |
in Some (s, o) |
| 684 |
end |
| 685 |
| _ -> raise Not_found |
| 686 |
with Not_found -> None |
| 687 |
end |
| 688 |
| _ -> None |
| 689 |
end) |
| 690 |
in |
| 691 |
(* XXX trasnform field map into set *) |
| 692 |
let errs, section_map, field_map = |
| 693 |
List.fold_left |
| 694 |
(fun (errs, section_map, field_map) it -> match it.pelem with |
| 695 |
| Section sec -> |
| 696 |
let k = sec.section_kind.pelem in |
| 697 |
let v = sec.section_items.pelem in |
| 698 |
let n = optelem sec.section_name in |
| 699 |
if OpamStd.List.mem_assoc String.equal k sections then |
| 700 |
try |
| 701 |
errs, |
| 702 |
SEM.safe_add (k, n) (pos,v) section_map, field_map |
| 703 |
with Failure _ -> |
| 704 |
(k,(Some pos,"Duplicate section "^k)) :: errs, |
| 705 |
section_map, field_map |
| 706 |
else |
| 707 |
(k,(Some pos,"Invalid section "^k)) :: errs, |
| 708 |
section_map, field_map |
| 709 |
| Variable (k, v) -> |
| 710 |
let k = k.pelem in |
| 711 |
let v = v.pelem in |
| 712 |
let pos = it.pos in |
| 713 |
if OpamStd.List.mem_assoc String.equal k ppas then |
| 714 |
try |
| 715 |
errs, |
| 716 |
section_map, OpamStd.String.Map.safe_add k (pos,v) field_map |
| 717 |
with Failure _ -> |
| 718 |
(k,(Some pos,"Duplicate field "^k)) :: errs, |
| 719 |
section_map, field_map |
| 720 |
else |
| 721 |
(k,(Some pos,"Invalid field "^k))::errs, |
| 722 |
section_map, field_map) |
| 723 |
([], SEM.empty, OpamStd.String.Map.empty) items |
| 724 |
in |
| 725 |
let errs, r = |
| 726 |
List.fold_left |
| 727 |
(fun (errs,acc) (field,ppa) -> |
| 728 |
try |
| 729 |
let pos, v = OpamStd.String.Map.find field field_map in |
| 730 |
try errs, parse ppa ~pos (acc, Some { pelem = v; pos }) with |
| 731 |
| Bad_format (pos, msg) -> |
| 732 |
(field, (pos, msg)) :: errs, acc |
| 733 |
with Not_found -> |
| 734 |
(if List.mem field mandatory_fields |
| 735 |
then (field, (Some pos, "Missing field "^field)) :: errs |
| 736 |
else errs), |
| 737 |
acc) |
| 738 |
(errs, empty) ppas |
| 739 |
in |
| 740 |
let errs, r = |
| 741 |
List.fold_left |
| 742 |
(fun (errs,acc) (section_kind, ppa) -> |
| 743 |
let secs = |
| 744 |
SEM.fold |
| 745 |
(fun (kind, name) (_, items) acc -> |
| 746 |
if kind = section_kind then (name, items) :: acc else acc) |
| 747 |
section_map [] |
| 748 |
|> List.rev |
| 749 |
in |
| 750 |
if secs = [] then errs, acc else |
| 751 |
try errs, parse ppa ~pos (acc, Some secs) with |
| 752 |
| Bad_format (pos, msg) -> |
| 753 |
(section_kind,(pos, msg)) :: errs, acc) |
| 754 |
(errs, r) sections |
| 755 |
in |
| 756 |
r, errs |
| 757 |
in |
| 758 |
let print (acc, _) = |
| 759 |
OpamStd.List.filter_map |
| 760 |
(fun (field,ppa) -> |
| 761 |
match snd (ppa.print acc) with |
| 762 |
| None | Some ({ pelem = List { pelem = []; _}; _} |
| 763 |
| { pelem = Group { pelem = []; _}; _}) -> None |
| 764 |
| Some value -> |
| 765 |
Some (nullify_pos @@ Variable (nullify_pos field, value))) |
| 766 |
ppas |
| 767 |
@ |
| 768 |
(List.flatten @@ List.map |
| 769 |
(fun (kind, ppa) -> |
| 770 |
OpamStd.Option.default [] (snd (ppa.print acc)) |> |
| 771 |
List.map (fun (name, items) -> |
| 772 |
nullify_pos @@ Section { |
| 773 |
section_kind = nullify_pos kind; |
| 774 |
section_name = nullposelem name; |
| 775 |
section_items = nullify_pos items })) |
| 776 |
sections) |
| 777 |
in |
| 778 |
pp ?name parse print |
| 779 |
|
| 780 |
let show_errors ?name |
| 781 |
?(strict=OpamFormatConfig.(!r.strict)) |
| 782 |
?(condition=fun _ -> true) |
| 783 |
() = |
| 784 |
let parse ~pos (t, errs) = |
| 785 |
let file = pos.filename in |
| 786 |
if errs = [] then t |
| 787 |
else if strict then raise (Bad_format_list (List.rev_map snd errs)) |
| 788 |
else |
| 789 |
(if condition t then |
| 790 |
OpamConsole.warning |
| 791 |
"Errors in %s, some fields have been ignored:\n%s" |
| 792 |
file |
| 793 |
(OpamStd.Format.itemize |
| 794 |
(fun e -> OpamPp.string_of_bad_format (Bad_format e)) |
| 795 |
(List.rev_map snd errs)) |
| 796 |
else |
| 797 |
OpamConsole.log "FORMAT" "File errors in %s, ignored fields: %s" |
| 798 |
file |
| 799 |
(OpamStd.List.concat_map "; " |
| 800 |
(fun e -> OpamPp.string_of_bad_format (Bad_format e)) |
| 801 |
(List.rev_map snd errs)); |
| 802 |
t) |
| 803 |
in |
| 804 |
let print t = t, [] in |
| 805 |
pp ?name parse print |
| 806 |
|
| 807 |
let on_errors ?name f = |
| 808 |
let parse ~pos:_ (t, errs) = |
| 809 |
List.fold_left f t errs |
| 810 |
in |
| 811 |
let print t = (t, []) in |
| 812 |
pp ?name parse print |
| 813 |
|
| 814 |
let partition filter = |
| 815 |
pp |
| 816 |
(fun ~pos:_ -> List.partition filter) |
| 817 |
(fun (a,b) -> a @ b) |
| 818 |
|
| 819 |
let partition_fields ?(section=false) filter = |
| 820 |
partition @@ fun v -> match v.pelem with |
| 821 |
| Variable (k,_) -> filter k.pelem |
| 822 |
| Section _ -> section |
| 823 |
|
| 824 |
let field name parse = |
| 825 |
pp |
| 826 |
(fun ~pos items -> |
| 827 |
match |
| 828 |
OpamStd.List.filter_map (fun v -> match v.pelem with |
| 829 |
| Variable (k,v) when k.pelem = name -> Some v |
| 830 |
| _ -> None) |
| 831 |
items |
| 832 |
with |
| 833 |
| [] -> None, items |
| 834 |
| _::_::_ -> bad_format ~pos "Duplicate '%s:' field" name |
| 835 |
| [v] -> Some (parse ~pos v), items) |
| 836 |
snd |
| 837 |
|
| 838 |
|
| 839 |
let extract_field name = |
| 840 |
partition_fields ((=) name) -| |
| 841 |
(map_fst @@ opt @@ |
| 842 |
singleton -| item -| |
| 843 |
pp ~name:(Printf.sprintf "'%s:' field" name) |
| 844 |
(fun ~pos:_ (_,v) -> v) |
| 845 |
(fun v -> name,v)) |
| 846 |
|
| 847 |
let check_opam_version |
| 848 |
?(optional=false) |
| 849 |
~format_version |
| 850 |
?(f=fun v -> OpamVersion.(compare format_version (nopatch v) >= 0)) |
| 851 |
() |
| 852 |
= |
| 853 |
let name = "opam-version" in |
| 854 |
let opam_v = V.string -| of_module name (module OpamVersion) in |
| 855 |
let f v = |
| 856 |
OpamFormatConfig.(!r.skip_version_checks) || match v with |
| 857 |
| Some v -> f v |
| 858 |
| None -> optional |
| 859 |
in |
| 860 |
let errmsg = |
| 861 |
Printf.sprintf |
| 862 |
"unsupported or missing file format version; should be %s or older" |
| 863 |
(OpamVersion.to_string format_version) |
| 864 |
in |
| 865 |
field name (parse opam_v) -| |
| 866 |
map_fst (check ~name ~raise:OpamPp.bad_version ~errmsg f) -| |
| 867 |
pp ~name |
| 868 |
(fun ~pos:_ (_,x) -> x) |
| 869 |
(fun x -> |
| 870 |
(* re-extract the field using parse when printing, to check that it is |
| 871 |
not undefined *) |
| 872 |
match parse ~pos:pos_null (field name (parse opam_v)) x with |
| 873 |
| None, _ -> failwith "opam version must be printed" |
| 874 |
| v, l -> v, l) |
| 875 |
|
| 876 |
let opam_version ?(undefined=false) ~format_version () = |
| 877 |
let name = "opam-version" in |
| 878 |
pp |
| 879 |
(fun ~pos:_ items -> |
| 880 |
if not undefined then items else |
| 881 |
List.filter (function |
| 882 |
| { pelem = Variable ({ pelem = fname; _}, |
| 883 |
{ pelem = String _version; _}); _} |
| 884 |
when fname = name -> |
| 885 |
(* check opam version already called, we don't need to check |
| 886 |
that it is the same version *) |
| 887 |
false |
| 888 |
| _ -> true) items) |
| 889 |
(fun items -> |
| 890 |
let opam_v = V.string -| of_module name (module OpamVersion) in |
| 891 |
match parse ~pos:pos_null (field name (parse opam_v)) items with |
| 892 |
| None, items -> |
| 893 |
let opam_v = |
| 894 |
nullify_pos @@ |
| 895 |
Variable (nullify_pos name, |
| 896 |
nullify_pos @@ |
| 897 |
String (OpamVersion.to_string format_version)) |
| 898 |
in |
| 899 |
opam_v :: items |
| 900 |
| Some _, items -> items) |
| 901 |
|
| 902 |
type signature = string * string * string |
| 903 |
|
| 904 |
let signature = |
| 905 |
V.list -| (V.string ^+ V.string ^+ last -| V.string) -| |
| 906 |
pp (fun ~pos:_ (a,(b,c)) -> a,b,c) (fun (a,b,c) -> a,(b,c)) |
| 907 |
|
| 908 |
exception Invalid_signature of pos * (string*string*string) list option |
| 909 |
|
| 910 |
let signed ~check = |
| 911 |
let module OpamPrinter = OpamPrinter.FullPos in |
| 912 |
let pp_sig = V.map_list ~depth:2 signature in |
| 913 |
extract_field "signature" -| |
| 914 |
pp ~name:"signed-file" |
| 915 |
(fun ~pos -> function |
| 916 |
| Some sgs, items -> |
| 917 |
let sgs = parse ~pos pp_sig sgs in |
| 918 |
let str = OpamPrinter.Normalise.items items in |
| 919 |
if not (check sgs str) then |
| 920 |
raise (Invalid_signature (pos, Some sgs)) |
| 921 |
else (sgs, items) |
| 922 |
| None, _ -> |
| 923 |
raise (Invalid_signature (pos, None))) |
| 924 |
(fun (sgs, items) -> |
| 925 |
assert (check sgs (OpamPrinter.Normalise.items items)); |
| 926 |
Some (print pp_sig sgs), |
| 927 |
items) |
| 928 |
end |