| 1 |
(**************************************************************************) |
| 2 |
(* *) |
| 3 |
(* Copyright 2012-2018 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 OpamTypesBase |
| 14 |
open OpamStd.Op |
| 15 |
|
| 16 |
type bad_format = pos option * string |
| 17 |
|
| 18 |
exception Bad_version of bad_format |
| 19 |
exception Bad_format of bad_format |
| 20 |
exception Bad_format_list of bad_format list |
| 21 |
|
| 22 |
let bad_format ?pos fmt = |
| 23 |
Printf.ksprintf |
| 24 |
(fun str -> |
| 25 |
raise (Bad_format (pos,str))) |
| 26 |
fmt |
| 27 |
|
| 28 |
let bad_version ?pos fmt = |
| 29 |
Printf.ksprintf |
| 30 |
(fun str -> |
| 31 |
raise (Bad_version (pos,str))) |
| 32 |
fmt |
| 33 |
|
| 34 |
let add_pos pos = function |
| 35 |
| Bad_format (pos_opt,msg) as e -> |
| 36 |
if pos_opt = None || pos_opt = Some pos_null |
| 37 |
then Bad_format (Some pos, msg) |
| 38 |
else e |
| 39 |
| Bad_version (pos_opt,msg) as e -> |
| 40 |
if pos_opt = None || pos_opt = Some pos_null |
| 41 |
then Bad_version (Some pos, msg) |
| 42 |
else e |
| 43 |
|
| 44 |
| e -> e |
| 45 |
|
| 46 |
let rec string_of_bad_format ?file e = |
| 47 |
match e, file with |
| 48 |
| Bad_version (None, msg), Some filename |
| 49 |
| Bad_format (None, msg), Some filename |
| 50 |
| Bad_version (Some {filename; start = -1, -1 ; stop = -1,-1 }, msg), _ |
| 51 |
| Bad_format (Some {filename; start = -1, -1 ; stop = -1,-1 }, msg), _ -> |
| 52 |
Printf.sprintf "In %s:\n%s" filename msg |
| 53 |
| Bad_version (Some pos, msg), _ |
| 54 |
| Bad_format (Some pos, msg), _ -> |
| 55 |
Printf.sprintf "At %s:\n%s" (string_of_pos pos) msg |
| 56 |
| Bad_version (None, msg), None |
| 57 |
| Bad_format (None, msg), None -> |
| 58 |
Printf.sprintf "Input error:\n%s" msg |
| 59 |
| Bad_format_list bfl, _ -> |
| 60 |
OpamStd.List.concat_map "\n" |
| 61 |
(fun bf -> string_of_bad_format ?file (Bad_format bf)) bfl |
| 62 |
| _ -> Printexc.to_string e |
| 63 |
|
| 64 |
let () = Printexc.register_printer @@ function |
| 65 |
| (Bad_version _ | Bad_format _ | Bad_format_list _ as e) -> |
| 66 |
Some (string_of_bad_format ?file:None e) |
| 67 |
| _ -> None |
| 68 |
|
| 69 |
type ('a,'b) t = { |
| 70 |
parse: pos:pos -> 'a -> 'b; |
| 71 |
print: 'b -> 'a; |
| 72 |
ppname: string; |
| 73 |
name_constr: string -> string; |
| 74 |
} |
| 75 |
|
| 76 |
let pp ?(name="") ?(name_constr=fun x -> x) parse print = |
| 77 |
{ |
| 78 |
parse; print; ppname = name; name_constr; |
| 79 |
} |
| 80 |
|
| 81 |
let of_pair name (simple_parse, print) = |
| 82 |
pp ~name (fun ~pos:_ -> simple_parse) print |
| 83 |
|
| 84 |
(** Utility functions *) |
| 85 |
|
| 86 |
exception Unexpected of pos option |
| 87 |
let unexpected ?pos () = raise (Unexpected pos) |
| 88 |
|
| 89 |
(** Basic pp usage *) |
| 90 |
|
| 91 |
let parse pp ~pos x = try pp.parse ~pos x with |
| 92 |
| Bad_version _ | Bad_format _ | Bad_format_list _ as e -> |
| 93 |
raise (add_pos pos e) |
| 94 |
| Unexpected (Some pos) -> bad_format ~pos "expected %s" pp.ppname |
| 95 |
| Unexpected None -> bad_format ~pos "expected %s" pp.ppname |
| 96 |
| Failure msg -> |
| 97 |
bad_format ~pos "%s%s" |
| 98 |
(if pp.ppname <> "" then Printf.sprintf "while expecting %s: " pp.ppname |
| 99 |
else "") |
| 100 |
msg |
| 101 |
| e -> |
| 102 |
OpamStd.Exn.fatal e; |
| 103 |
bad_format ~pos "%s%s" |
| 104 |
(if pp.ppname <> "" then Printf.sprintf "while expecting %s: " pp.ppname |
| 105 |
else "") |
| 106 |
(Printexc.to_string e) |
| 107 |
|
| 108 |
let print pp x = pp.print x |
| 109 |
|
| 110 |
|
| 111 |
(** Pp combination and transformation *) |
| 112 |
|
| 113 |
(** Piping *) |
| 114 |
let (-|) pp1 pp2 = { |
| 115 |
parse = (fun ~pos x -> |
| 116 |
let y = pp1.parse ~pos x in |
| 117 |
parse pp2 ~pos y |
| 118 |
); |
| 119 |
print = pp1.print @* pp2.print; |
| 120 |
ppname = |
| 121 |
(match pp2.ppname with "" -> pp1.ppname |
| 122 |
| name -> pp1.name_constr name); |
| 123 |
name_constr = pp1.name_constr @* pp2.name_constr; |
| 124 |
} |
| 125 |
|
| 126 |
let identity = { |
| 127 |
parse = (fun ~pos:_ x -> x); |
| 128 |
print = (fun x -> x); |
| 129 |
ppname = ""; |
| 130 |
name_constr = (fun x -> x); |
| 131 |
} |
| 132 |
|
| 133 |
let ignore = { |
| 134 |
parse = (fun ~pos:_ -> OpamStd.Option.none); |
| 135 |
print = (fun _ -> assert false); |
| 136 |
ppname = "ignored"; |
| 137 |
name_constr = (fun _ -> "<ignored>"); |
| 138 |
} |
| 139 |
|
| 140 |
let check ?name ?(raise=bad_format) ?errmsg f = |
| 141 |
pp |
| 142 |
?name |
| 143 |
(fun ~pos x -> |
| 144 |
if not (f x) then |
| 145 |
match errmsg with |
| 146 |
| Some m -> raise ~pos "%s" m |
| 147 |
| None -> unexpected () |
| 148 |
else x) |
| 149 |
(fun x -> |
| 150 |
assert ( |
| 151 |
f x || |
| 152 |
(OpamConsole.error "Check failed on value printing%s%s" |
| 153 |
(match name with Some n -> " at "^n | None -> "") |
| 154 |
(match errmsg with Some e -> " ("^e^")" | None -> ""); |
| 155 |
false)); |
| 156 |
x) |
| 157 |
|
| 158 |
let map_pair ?name ?posf1 ?posf2 (pp1: ('a,'b) t) (pp2: ('c,'d) t) = |
| 159 |
let name = match name with |
| 160 |
| None -> Printf.sprintf "(%s, %s)" pp1.ppname pp2.ppname |
| 161 |
| Some n -> n |
| 162 |
in |
| 163 |
pp ~name |
| 164 |
(fun ~pos (a,b) -> |
| 165 |
let posf1 = OpamStd.Option.default (fun _ -> pos) posf1 in |
| 166 |
parse pp1 ~pos:(posf1 a) a, |
| 167 |
let posf2 = OpamStd.Option.default (fun _ -> pos) posf2 in |
| 168 |
parse pp2 ~pos:(posf2 b) b) |
| 169 |
(fun (a,b) -> print pp1 a, print pp2 b) |
| 170 |
|
| 171 |
let map_fst pp1 = |
| 172 |
pp |
| 173 |
(fun ~pos (a,b) -> pp1.parse ~pos a, b) |
| 174 |
(fun (a, b) -> pp1.print a, b) |
| 175 |
|
| 176 |
let map_snd pp1 = |
| 177 |
pp |
| 178 |
(fun ~pos (a,b) -> a, pp1.parse ~pos b) |
| 179 |
(fun (a, b) -> a, pp1.print b) |
| 180 |
|
| 181 |
let map_list ?name ?posf pp1 = |
| 182 |
let name = match name with |
| 183 |
| None -> pp1.ppname ^ "*" |
| 184 |
| Some n -> n |
| 185 |
in |
| 186 |
pp ~name |
| 187 |
(fun ~pos l -> |
| 188 |
let posf = OpamStd.Option.default (fun _ -> pos) posf in |
| 189 |
List.rev (List.rev_map (fun x -> parse pp1 ~pos:(posf x) x) l)) |
| 190 |
(List.rev @* List.rev_map (print pp1)) |
| 191 |
|
| 192 |
let map_option ?name pp1 = |
| 193 |
let name = match name with |
| 194 |
| None -> pp1.ppname ^ "?" |
| 195 |
| Some n -> n |
| 196 |
in |
| 197 |
pp ~name |
| 198 |
(fun ~pos -> OpamStd.Option.map (parse pp1 ~pos)) |
| 199 |
(OpamStd.Option.map (print pp1)) |
| 200 |
|
| 201 |
let singleton = { |
| 202 |
parse = (fun ~pos:_ -> function [x] -> x | _ -> unexpected ()); |
| 203 |
print = (fun x -> [x]); |
| 204 |
ppname = ""; |
| 205 |
name_constr = (fun x -> x); |
| 206 |
} |
| 207 |
|
| 208 |
(** Pps from strings *) |
| 209 |
|
| 210 |
module type STR = sig |
| 211 |
type t |
| 212 |
val of_string: string -> t |
| 213 |
val to_string: t -> string |
| 214 |
end |
| 215 |
|
| 216 |
let of_module (type a) name m = |
| 217 |
let module X = (val m: STR with type t = a) in |
| 218 |
pp ~name |
| 219 |
(fun ~pos:_ -> X.of_string) |
| 220 |
X.to_string |
| 221 |
|
| 222 |
(** Build tuples from lists *) |
| 223 |
let (^+) pp1 pp2 = |
| 224 |
pp |
| 225 |
~name:(Printf.sprintf "%s %s" pp1.ppname pp2.ppname) |
| 226 |
(fun ~pos -> function |
| 227 |
| x::r -> parse pp1 ~pos x, parse pp2 ~pos r |
| 228 |
| [] -> unexpected ()) |
| 229 |
(fun (x,y) -> print pp1 x :: print pp2 y) |
| 230 |
|
| 231 |
let last = singleton |
| 232 |
|
| 233 |
let opt pp1 = |
| 234 |
pp |
| 235 |
~name:("?"^pp1.ppname) |
| 236 |
(fun ~pos -> function [] -> None | l -> Some (pp1.parse ~pos l)) |
| 237 |
(function Some x -> pp1.print x | None -> []) |
| 238 |
|
| 239 |
let default d = |
| 240 |
pp |
| 241 |
(fun ~pos:_ -> function None -> d | Some x -> x) |
| 242 |
(fun x -> Some x) |
| 243 |
|
| 244 |
let fallback pp1 pp2 = |
| 245 |
let parse ~pos x = |
| 246 |
try pp1.parse ~pos x with e -> |
| 247 |
OpamStd.Exn.fatal e; |
| 248 |
let bt = Printexc.get_raw_backtrace () in |
| 249 |
try pp2.parse ~pos x with _ -> |
| 250 |
Printexc.raise_with_backtrace e bt |
| 251 |
in |
| 252 |
{ pp1 with parse } |
| 253 |
|
| 254 |
|
| 255 |
module Op = struct |
| 256 |
let ( -| ) = ( -| ) |
| 257 |
let ( ^+ ) = ( ^+ ) |
| 258 |
end |
| 259 |
|
| 260 |
(** Pps for file contents (item lists), mostly list of [Variable(...)] |
| 261 |
fields *) |
| 262 |
|
| 263 |
type ('a, 'value) field_parser = ('a * 'value option, 'a) t |
| 264 |
|
| 265 |
(** add setter/getter and an accumulator to a pp; useful to use |
| 266 |
to get/set field records *) |
| 267 |
let ppacc_opt |
| 268 |
(* : ('a -> 'b -> 'a) -> ('a -> 'b option) -> ('value, 'b) t -> 'a field_parser *) |
| 269 |
= fun ?(cleanup = fun ~pos:_ _acc x -> x) set get pp1 -> |
| 270 |
let parse ~pos = function |
| 271 |
| acc, Some s -> |
| 272 |
set (cleanup ~pos acc (pp1.parse ~pos s)) acc |
| 273 |
| acc, None -> acc |
| 274 |
in |
| 275 |
let print s = s, OpamStd.Option.map pp1.print (get s) in |
| 276 |
{ |
| 277 |
parse; print; |
| 278 |
ppname = pp1.ppname; |
| 279 |
name_constr = (fun x -> x); |
| 280 |
} |
| 281 |
|
| 282 |
let ppacc ?cleanup set get pp = |
| 283 |
ppacc_opt set (fun x -> Some (get x)) ?cleanup pp |
| 284 |
|
| 285 |
let ppacc_ignore = { |
| 286 |
parse = (fun ~pos:_ (acc,_) -> acc); |
| 287 |
print = (fun s -> s, None); |
| 288 |
ppname = "<ignored>"; |
| 289 |
name_constr = (fun _ -> "<ignored>"); |
| 290 |
} |
| 291 |
|
| 292 |
let embed set get ppacc = { |
| 293 |
parse = (fun ~pos (acc, x) -> set (ppacc.parse ~pos (get acc, x)) acc); |
| 294 |
print = (fun s -> let s1, v = ppacc.print (get s) in set s1 s, v); |
| 295 |
ppname = ppacc.ppname; |
| 296 |
name_constr = ppacc.name_constr; |
| 297 |
} |