package dune-private-libs
sectionYPositions = computeSectionYPositions($el), 10)"
x-init="setTimeout(() => sectionYPositions = computeSectionYPositions($el), 10)"
>
Private libraries of Dune
Install
dune-project
Dependency
Authors
Maintainers
Sources
dune-2.5.1.tbz
sha256=8f77d3a87f208e0d7cccaa1c48c4bb1bb87d62d07c3f25e9b8ba298e028ce52b
sha512=f209f12ced10c1abf8782bdb0143f4cec77795f7174d2cc75130afb1e01550b01f2f77b9e3ec4888efdad83d2f9878d179b39126f824f4e522f3ef4da34bf27e
doc/src/dune-private-libs.dune-lang/decoder.ml.html
Source file decoder.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 662open Stdune type ast = Ast.t = | Atom of Loc.t * Atom.t | Quoted_string of Loc.t * string | Template of Template.t | List of Loc.t * ast list type hint = { on : string ; candidates : string list } module Name = struct module T = struct type t = string let compare a b = let alen = String.length a and blen = String.length b in match Int.compare alen blen with | Eq -> String.compare a b | ne -> ne let to_dyn = Dyn.Encoder.string end include T module Map = Map.Make (T) end module Fields = struct module Unparsed = struct type t = { values : Ast.t list ; entry : Ast.t ; prev : t option (* Previous occurrence of this field *) } end type t = { unparsed : Unparsed.t Name.Map.t ; known : string list } let consume name state = { unparsed = Name.Map.remove state.unparsed name ; known = name :: state.known } let add_known name state = { state with known = name :: state.known } let unparsed_ast { unparsed; _ } = let rec loop acc = function | [] -> acc | x :: xs -> ( match x.Unparsed.prev with | None -> loop (x.entry :: acc) xs | Some p -> loop (x.entry :: acc) (p :: xs) ) in loop [] (Name.Map.values unparsed) |> List.sort ~compare:(fun a b -> Int.compare (Ast.loc a).start.pos_cnum (Ast.loc b).start.pos_cnum) end type fields = Fields.t type values = Ast.t list (* Arguments are: - the location of the whole list - the first atom when parsing a constructor or a field - the universal map holding the user context *) type 'kind context = | Values : Loc.t * string option * Univ_map.t -> values context | Fields : Loc.t * string option * Univ_map.t -> Fields.t context type ('a, 'kind) parser = 'kind context -> 'kind -> 'a * 'kind type 'a t = ('a, values) parser type 'a fields_parser = ('a, Fields.t) parser let return x _ctx state = (x, state) let ( >>= ) t f ctx state = let x, state = t ctx state in f x ctx state let ( >>| ) t f ctx state = let x, state = t ctx state in (f x, state) let ( >>> ) a b ctx state = let (), state = a ctx state in b ctx state let ( let* ) = ( >>= ) let ( let+ ) = ( >>| ) let ( and+ ) a b ctx state = let a, state = a ctx state in let b, state = b ctx state in ((a, b), state) let map t ~f = t >>| f let try_ t f ctx state = try t ctx state with exn -> f exn ctx state let get_user_context : type k. k context -> Univ_map.t = function | Values (_, _, uc) -> uc | Fields (_, _, uc) -> uc let get key ctx state = (Univ_map.find (get_user_context ctx) key, state) let get_all ctx state = (get_user_context ctx, state) let set : type a b k. a Univ_map.Key.t -> a -> (b, k) parser -> (b, k) parser = fun key v t ctx state -> match ctx with | Values (loc, cstr, uc) -> t (Values (loc, cstr, Univ_map.add uc key v)) state | Fields (loc, cstr, uc) -> t (Fields (loc, cstr, Univ_map.add uc key v)) state let set_many : type a k. Univ_map.t -> (a, k) parser -> (a, k) parser = fun map t ctx state -> match ctx with | Values (loc, cstr, uc) -> t (Values (loc, cstr, Univ_map.superpose uc map)) state | Fields (loc, cstr, uc) -> t (Fields (loc, cstr, Univ_map.superpose uc map)) state let loc : type k. k context -> k -> Loc.t * k = fun ctx state -> match ctx with | Values (loc, _, _) -> (loc, state) | Fields (loc, _, _) -> (loc, state) let eos : type k. k context -> k -> bool * k = fun ctx state -> match ctx with | Values _ -> (state = [], state) | Fields _ -> (Name.Map.is_empty state.unparsed, state) let repeat : 'a t -> 'a list t = let rec loop t acc ctx l = match l with | [] -> (List.rev acc, []) | _ -> let x, l = t ctx l in loop t (x :: acc) ctx l in fun t ctx state -> loop t [] ctx state let result : type a k. k context -> a * k -> a = fun ctx (v, state) -> match ctx with | Values (_, cstr, _) -> ( match state with | [] -> v | sexp :: _ -> ( match cstr with | None -> User_error.raise ~loc:(Ast.loc sexp) [ Pp.text "This value is unused" ] | Some s -> User_error.raise ~loc:(Ast.loc sexp) [ Pp.textf "Too many argument for %s" s ] ) ) | Fields _ -> ( match Name.Map.choose state.unparsed with | None -> v | Some (name, { entry; _ }) -> let name_loc = match entry with | List (_, s :: _) -> Ast.loc s | _ -> assert false in User_error.raise ~loc:name_loc ~hints:(User_message.did_you_mean name ~candidates:state.known) [ Pp.textf "Unknown field %s" name ] ) let parse t context sexp = let ctx = Values (Ast.loc sexp, None, context) in result ctx (t ctx [ sexp ]) let capture ctx state = let f t = result ctx (t ctx state) in (f, []) let end_of_list (Values (loc, cstr, _)) = match cstr with | None -> let loc = { loc with start = loc.stop } in User_error.raise ~loc [ Pp.text "Premature end of list" ] | Some s -> User_error.raise ~loc [ Pp.textf "Not enough arguments for %s" s ] [@@inline never] let next f ctx sexps = match sexps with | [] -> end_of_list ctx | sexp :: sexps -> (f sexp, sexps) [@@inline always] let next_with_user_context f ctx sexps = match sexps with | [] -> end_of_list ctx | sexp :: sexps -> (f (get_user_context ctx) sexp, sexps) [@@inline always] let peek _ctx sexps = match sexps with | [] -> (None, sexps) | sexp :: _ -> (Some sexp, sexps) [@@inline always] let peek_exn ctx sexps = match sexps with | [] -> end_of_list ctx | sexp :: _ -> (sexp, sexps) [@@inline always] let junk = next ignore let junk_everything : type k. (unit, k) parser = fun ctx state -> match ctx with | Values _ -> ((), []) | Fields _ -> ((), { state with unparsed = Name.Map.empty }) let keyword kwd = next (function | Atom (_, A s) when s = kwd -> () | sexp -> User_error.raise ~loc:(Ast.loc sexp) [ Pp.textf "'%s' expected" kwd ]) let atom_matching f ~desc = next (fun sexp -> match match sexp with | Atom (_, A s) -> f s | _ -> None with | Some x -> x | None -> User_error.raise ~loc:(Ast.loc sexp) [ Pp.textf "%s expected" desc ]) let until_keyword kwd ~before ~after = let rec loop acc = peek >>= function | None -> return (List.rev acc, None) | Some (Atom (_, A s)) when s = kwd -> junk >>> after >>= fun x -> return (List.rev acc, Some x) | _ -> before >>= fun x -> loop (x :: acc) in loop [] let plain_string f = next (function | Atom (loc, A s) | Quoted_string (loc, s) -> f ~loc s | Template { loc; _ } | List (loc, _) -> User_error.raise ~loc [ Pp.text "Atom or quoted string expected" ]) let filename = plain_string (fun ~loc s -> match s with | "." | ".." -> User_error.raise ~loc [ Pp.textf "'.' and '..' are not valid filenames" ] | fn -> fn) let enter t = next_with_user_context (fun uc sexp -> match sexp with | List (loc, l) -> let ctx = Values (loc, None, uc) in result ctx (t ctx l) | sexp -> User_error.raise ~loc:(Ast.loc sexp) [ Pp.text "List expected" ]) let ( <|> ) = (* Before you read this code, close your eyes and internalise the fact that this code is temporary. It is a temporary state as part of a larger work to turn [Decoder.t] into a pure applicative. Once this is done, this function will be implemented in a better way and with a much cleaner semantic. *) let approximate_how_much_input_a_failing_branch_consumed (exn : Exn_with_backtrace.t) = Printexc.raw_backtrace_length exn.backtrace in let compare_input_consumed exn1 exn2 = Int.compare (approximate_how_much_input_a_failing_branch_consumed exn1) (approximate_how_much_input_a_failing_branch_consumed exn2) in fun a b ctx state -> try a ctx state with exn_a -> ( let exn_a = Exn_with_backtrace.capture exn_a in try b ctx state with exn_b -> let exn_b = Exn_with_backtrace.capture exn_b in Exn_with_backtrace.reraise ( match compare_input_consumed exn_a exn_b with | Gt -> exn_a | Eq | Lt -> exn_b ) ) let fix f = let rec p = lazy (f r) and r ast = (Lazy.force p) ast in r let loc_between_states : type k. k context -> k -> k -> Loc.t = fun ctx state1 state2 -> match ctx with | Values _ -> ( match state1 with | sexp :: rest when rest == state2 -> (* common case *) Ast.loc sexp | [] -> let (Values (loc, _, _)) = ctx in { loc with start = loc.stop } | sexp :: rest -> let loc = Ast.loc sexp in let rec search last l = if l == state2 then { loc with stop = (Ast.loc last).stop } else match l with | [] -> let (Values (loc, _, _)) = ctx in { (Ast.loc sexp) with stop = loc.stop } | sexp :: rest -> search sexp rest in search sexp rest ) | Fields _ -> ( let parsed = Name.Map.merge state1.unparsed state2.unparsed ~f:(fun _key before after -> match (before, after) with | Some _, None -> before | _ -> None) in match Name.Map.values parsed |> List.map ~f:(fun f -> Ast.loc f.Fields.Unparsed.entry) |> List.sort ~compare:(fun a b -> Int.compare a.Loc.start.pos_cnum b.start.pos_cnum) with | [] -> let (Fields (loc, _, _)) = ctx in loc | first :: l -> let last = List.fold_left l ~init:first ~f:(fun _ x -> x) in { first with stop = last.stop } ) let located t ctx state1 = let x, state2 = t ctx state1 in ((loc_between_states ctx state1 state2, x), state2) let raw = next Fun.id let basic_loc desc f = next (function | Template { loc; _ } | List (loc, _) | Quoted_string (loc, _) -> User_error.raise ~loc [ Pp.textf "%s expected" desc ] | Atom (loc, s) -> ( match f ~loc (Atom.to_string s) with | None -> User_error.raise ~loc [ Pp.textf "%s expected" desc ] | Some x -> x )) let basic desc f = basic_loc desc (fun ~loc:_ -> f) let string = plain_string (fun ~loc:_ x -> x) let int = basic "Integer" Int.of_string let float = basic "Float" Float.of_string let pair a b = enter ( a >>= fun a -> b >>= fun b -> return (a, b) ) let triple a b c = enter ( a >>= fun a -> b >>= fun b -> c >>= fun c -> return (a, b, c) ) let unit_number name suffixes = let unit_number_of_string ~loc s = let possible_suffixes () = String.concat ~sep:", " (List.map ~f:fst suffixes) in let n, suffix = let f c = not (Char.code c >= Char.code '0' && Char.code c <= Char.code '9') in match String.findi s ~f with | None -> User_error.raise ~loc [ Pp.textf "missing suffix, use one of %s" (possible_suffixes ()) ] | Some i -> String.split_n s i in let factor = match List.assoc suffixes suffix with | Some f -> f | None -> User_error.raise ~loc [ Pp.textf "invalid suffix, use one of %s" (possible_suffixes ()) ] in Option.map ~f:(( * ) factor) (Int.of_string n) in basic_loc name unit_number_of_string let duration = unit_number "Duration" [ ("s", 1); ("m", 60); ("h", 60 * 60) ] let bytes_unit = unit_number "Byte amount" [ ("B", 1) ; ("kB", 1000) ; ("KB", 1000) ; ("MB", 1000 * 1000) ; ("GB", 1000 * 1000 * 1000) ] let maybe t = t >>| Option.some <|> return None let find_cstr cstrs loc name ctx values = match List.assoc cstrs name with | Some t -> result ctx (t ctx values) | None -> User_error.raise ~loc ~hints: (User_message.did_you_mean name ~candidates:(List.map cstrs ~f:fst)) [ Pp.textf "Unknown constructor %s" name ] let sum ?(force_parens = false) cstrs = next_with_user_context (fun uc sexp -> match sexp with | Atom (loc, A s) when not force_parens -> find_cstr cstrs loc s (Values (loc, Some s, uc)) [] | Atom (loc, _) | Template { loc; _ } | Quoted_string (loc, _) | List (loc, []) -> User_error.raise ~loc [ Pp.textf "S-expression of the form %s expected" ( if force_parens then "(<atom> ...)" else "(<atom> ...) or <atom>" ) ] | List (loc, name :: args) -> ( match name with | Quoted_string (loc, _) | List (loc, _) | Template { loc; _ } -> User_error.raise ~loc [ Pp.text "Atom expected" ] | Atom (s_loc, A s) -> find_cstr cstrs s_loc s (Values (loc, Some s, uc)) args )) let enum cstrs = next (function | Quoted_string (loc, _) | Template { loc; _ } | List (loc, _) -> User_error.raise ~loc [ Pp.text "Atom expected" ] | Atom (loc, A s) -> ( match List.assoc cstrs s with | Some value -> value | None -> User_error.raise ~loc [ Pp.textf "Unknown value %s" s ] ~hints: (User_message.did_you_mean s ~candidates:(List.map cstrs ~f:fst)) )) let bool = enum [ ("true", true); ("false", false) ] let map_validate t ~f ctx state1 = let x, state2 = t ctx state1 in match f x with | Result.Ok x -> (x, state2) | Error (msg : User_message.t) -> let msg = match msg.loc with | Some _ -> msg | None -> { msg with loc = Some (loc_between_states ctx state1 state2) } in raise (User_error.E msg) (** TODO: Improve consistency of error messages, e.g. use %S consistently for field names: see [field_missing] and [field_present_too_many_times]. *) let field_missing loc name = User_error.raise ~loc [ Pp.textf "field %s missing" name ] [@@inline never] let field_present_too_many_times _ name entries = match entries with | _ :: second :: _ -> User_error.raise ~loc:(Ast.loc second) [ Pp.textf "Field %S is present too many times" name ] | _ -> assert false let multiple_occurrences ?(on_dup = field_present_too_many_times) uc name last = let rec collect acc (x : Fields.Unparsed.t) = let acc = x.entry :: acc in match x.prev with | None -> acc | Some prev -> collect acc prev in on_dup uc name (collect [] last) [@@inline never] let find_single ?on_dup uc (state : Fields.t) name = let res = Name.Map.find state.unparsed name in ( match res with | Some ({ prev = Some _; _ } as last) -> multiple_occurrences uc name last ?on_dup | _ -> () ); res let field name ?default ?on_dup t (Fields (loc, _, uc)) state = match find_single uc state name ?on_dup with | Some { values; entry; _ } -> let ctx = Values (Ast.loc entry, Some name, uc) in let x = result ctx (t ctx values) in (x, Fields.consume name state) | None -> ( match default with | Some v -> (v, Fields.add_known name state) | None -> field_missing loc name ) let field_o name ?on_dup t (Fields (_, _, uc)) state = match find_single uc state name ?on_dup with | Some { values; entry; _ } -> let ctx = Values (Ast.loc entry, Some name, uc) in let x = result ctx (t ctx values) in (Some x, Fields.consume name state) | None -> (None, Fields.add_known name state) let field_b_gen field_gen ?check ?on_dup name = field_gen name ?on_dup (let* () = Option.value check ~default:(return ()) in eos >>= function | true -> return true | _ -> bool) let field_b = field_b_gen (field ~default:false) let field_o_b = field_b_gen field_o let multi_field name t (Fields (_, _, uc)) (state : Fields.t) = let rec loop acc (field : Fields.Unparsed.t option) = match field with | None -> acc | Some { values; prev; entry } -> let ctx = Values (Ast.loc entry, Some name, uc) in let x = result ctx (t ctx values) in loop (x :: acc) prev in let res = loop [] (Name.Map.find state.unparsed name) in (res, Fields.consume name state) let fields t (Values (loc, cstr, uc)) sexps = let unparsed = List.fold_left sexps ~init:Name.Map.empty ~f:(fun acc sexp -> match sexp with | List (_, name_sexp :: values) -> ( match name_sexp with | Atom (_, A name) -> Name.Map.set acc name { Fields.Unparsed.values ; entry = sexp ; prev = Name.Map.find acc name } | List (loc, _) | Quoted_string (loc, _) | Template { loc; _ } -> User_error.raise ~loc [ Pp.text "Atom expected" ] ) | _ -> User_error.raise ~loc:(Ast.loc sexp) [ Pp.text "S-expression of the form (<name> <values>...) expected" ]) in let ctx = Fields (loc, cstr, uc) in let x = result ctx (t ctx { Fields.unparsed; known = [] }) in (x, []) let leftover_fields_generic t more_fields (Fields (loc, cstr, uc)) state = let x = let ctx = Values (loc, cstr, uc) in result ctx (repeat t ctx (Fields.unparsed_ast state)) in (x, { Fields.known = state.known @ more_fields; unparsed = Name.Map.empty }) let leftover_fields ctx (state : Fields.t) = leftover_fields_generic raw (Name.Map.keys state.unparsed) ctx state let leftover_fields_as_sums cstrs = leftover_fields_generic (sum cstrs) (List.map cstrs ~f:fst) type kind = | Values of Loc.t * string option | Fields of Loc.t * string option let kind : type k. k context -> k -> kind * k = fun ctx state -> match ctx with | Values (loc, cstr, _) -> (Values (loc, cstr), state) | Fields (loc, cstr, _) -> (Fields (loc, cstr), state) let traverse l ~f ctx state = Tuple.T2.swap (List.fold_map ~init:state l ~f:(fun state x -> Tuple.T2.swap (f x ctx state))) let all = traverse ~f:(fun x -> x) let fields_missing_need_exactly_one loc names = User_error.raise ~loc [ Pp.textf "fields %s are all missing (exactly one is needed)" (String.concat ~sep:", " names) ] [@@inline never] let fields_mutual_exclusion_violation loc names = User_error.raise ~loc [ Pp.textf "fields %s are mutually exclusive" (String.concat ~sep:", " names) ] [@@inline never] let fields_mutually_exclusive ?on_dup ?default fields ((Fields (loc, _, _) : _ context) as ctx) state = let res, state = traverse ~f:(fun (name, parser) -> field_o name ?on_dup parser >>| fun res -> (name, res)) fields ctx state in match List.filter_map res ~f:(function | name, Some x -> Some (name, x) | _, None -> None) with | [] -> ( let names = List.map fields ~f:fst in match default with | None -> fields_missing_need_exactly_one loc names | Some default -> (default, state) ) | [ (_name, res) ] -> (res, state) | _ :: _ :: _ as results -> let names = List.map ~f:fst results in fields_mutual_exclusion_violation loc names
sectionYPositions = computeSectionYPositions($el), 10)"
x-init="setTimeout(() => sectionYPositions = computeSectionYPositions($el), 10)"
>