Skip to content
This repository
tag: v1698
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 737 lines (621 sloc) 23.472 kb
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
(*
Copyright © 2011 MLstate

This file is part of OPA.

OPA is free software: you can redistribute it and/or modify it under the
terms of the GNU Affero General Public License, version 3, as published by
the Free Software Foundation.

OPA is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
more details.

You should have received a copy of the GNU Affero General Public License
along with OPA. If not, see <http://www.gnu.org/licenses/>.
*)
(* CF mli *)

(* depends *)
module Format = BaseFormat
module List = BaseList
module String = BaseString

(* shorthands *)
module B = BslTypes
module D = BslDirectives
module BI = BslInterface
module BPI = BslPluginInterface
module L = BslLanguage

type ml_runtime = FBuffer.t
type ml_runtime_mli = FBuffer.t
type module_name = string
type skey = string
type pos = FilePos.pos
type opaname = string


(* debug *)

let debug fmt =
  OManager.printf ("@[<2>"^^fmt^^"@]@.")


(* Note about ty_spec_map, types scope:
from a key, returns the opaname, and the path in ocaml module of the definition of the type.
*)


let fbuffer () = FBuffer.create (8 * 1024)

type ty_spec_map = (opaname * module_name list) BslKeyMap.t

type env = {
  (* this is just for error messages *)
  last_module : FilePos.pos ;
  last_endmodule : FilePos.pos ;

  ml_runtime : ml_runtime ;
  ml_runtime_mli : ml_runtime_mli ;

  ml_line : int ;

  property_mli : bool ;

  rev_path : ( skey * module_name * pos ) list ;

  runtime_module_name : module_name ;

  ty_spec_map : ty_spec_map ;

  warn_x_field : unit ;
}

(*
Special env for separation of plugin compilation
*)
module SeparatedEnv :
sig
  type t

  val init : ty_spec_map:ty_spec_map -> t
  val fold : t -> BslPluginInterface.plugin -> t

  val ty_spec_map : t -> ty_spec_map

  module SideEffect :
  sig
    val get_ocaml_env : unit -> BslPluginInterface.ocaml_env
    val add_ty_spec_map : BslKey.t -> (opaname * module_name list) -> unit
  end
end =
struct
  type t = {
    ty_spec_map : ty_spec_map ;
  }
  let init ~ty_spec_map = {
    ty_spec_map ;
  }
  let ty_spec_map t = t.ty_spec_map
  external wrap : t -> BslPluginInterface.ocaml_env = "%identity"
  external unwrap : BslPluginInterface.ocaml_env -> t = "%identity"

  let fold t plugin =
    let tp = unwrap plugin.BPI.ocaml_env in
    let ty_spec_map =
      let fold key ((_opaname, path1) as value) ty_spec_map =
        BslKeyMap.replace key
          (function
           | None -> value
           | Some ((_, path2) as value2) ->
               OManager.warning ~wclass:WarningClass.bsl_register (
                 "@[<2>The type @{<bright>%a@} is defined in several imported plugins:@\n"^^
                   "path1: %a@\n"^^
                   "path2: %a@\n"^^
                   "This plugin will use the path2.@]"
               )
                 BslKey.pp key
                 (Format.pp_list "." Format.pp_print_string) path1
                 (Format.pp_list "." Format.pp_print_string) path2
               ;
               value2
          ) ty_spec_map
      in
      BslKeyMap.fold fold tp.ty_spec_map t.ty_spec_map
    in
    {
      ty_spec_map ;
    }

  module SideEffect =
  struct
    let ty_spec_map = BslKeyTable.create 16
    let add_ty_spec_map key value = BslKeyTable.replace ty_spec_map key value

    let get_ocaml_env () =
      let ty_spec_map = BslKeyTable.fold BslKeyMap.add ty_spec_map BslKeyMap.empty in
      let t = {
        ty_spec_map ;
      } in
      wrap t
  end
end

let nopos = FilePos.nopos "BslOcaml"

let empty =
  let empty = {
    last_module = nopos ;
    last_endmodule = nopos ;

    ml_runtime = fbuffer () ;
    ml_runtime_mli = fbuffer () ;

    ml_line = 1 ;

    property_mli = false ;
    rev_path = [] ;

    runtime_module_name = "" ;

    ty_spec_map = BslKeyMap.empty ;

    warn_x_field = () ;
  } in
  ( empty : env )

let (!!) pos fmt =
  OManager.printf "%a" FilePos.pp_citation pos ;
  OManager.error fmt

let warning pos fmt =
  let wclass = WarningClass.bsl_register in
  OManager.warning ~wclass ("%a"^^fmt) FilePos.pp_citation pos

let ocaml_module = String.capitalize
let kw_end = "end"
let kw_module = "module"
let kw_struct = "struct"
let kw_sig = "sig"

(*
Map a name of type, taking in consederation the current path,
and the ty_spec_map of the env.
@raise Not_found if the type reference does not correspond to
any known type.
*)
let find name ty_spec_map rev_path =
  let skey = String.rev_concat_map "_" BslKey.normalize_string (name :: rev_path) in
  let key = BslKey.of_string skey in
  let result = BslKeyMap.find_opt key ty_spec_map in
  let _=
    #<If:BSL_REGISTER $equals "find">
      match result with
      | Some (opaname, _) ->
          debug "find:%S opa:%S@\n" skey opaname
      | None ->
          debug "find:%S None@\n" skey
    #<End>
  in
  result

let env_map_ty_reference env pos skey =
  let ty_spec_map = env.ty_spec_map in
  let rev_pwd_keys = List.map (fun (s, _, _) -> s) env.rev_path in
  let pwd_ocaml = List.rev_map (fun (_, m, _) -> m) env.rev_path in
  (fun name ->
     (* 1) find the type reference *)
     let rec aux = function
       | [] -> (
           match find name ty_spec_map [] with
           | Some found -> found
           | None -> (
               (* inefficient, but it is just for an error message (computed once) *)
               let keys = BslKeyMap.keys_as_string ty_spec_map in
               OManager.printf "%a" FilePos.pp_citation pos ;
               OManager.printf "Cannot resolve the reference to the type @{<bright>%s@}@\n" name ;
               let _ = OManager.printf "%a" (HintUtils.pp_suggestion keys) name in
               OManager.error "##register: @{<bright>failing type resolution@} for key '@{<brigth>%s@}'@\n" skey
             )
         )
       | ( _::tl ) as rev_path -> (
           match find name ty_spec_map rev_path with
           | Some found -> found
           | None -> aux tl
         )
     in
     match aux rev_pwd_keys with
     | opaname, ocaml_path ->
         let module_path = OcamlUtils.Module.module_path ~full:ocaml_path ~pwd:pwd_ocaml in
         opaname, String.concat_map "." (fun s -> s) module_path
  )


let env_map_ty_reference_for_select ~select env pos skey ty =
  let map_ref = env_map_ty_reference env pos skey in
  BslTypes.Walk.map
    (function
     | B.External (p, name, vs) ->
         let name' = select (map_ref name) in
         let _ =
           #<If:BSL_REGISTER $equals "map_ref">
             debug "map_ref(%S): %S ==> %S" skey name name'
             #<End>
         in
         B.External (p, name', vs)

     | t -> t) ty

let env_map_ty_reference_for_ocaml env =
  env_map_ty_reference_for_select ~select:snd env

let env_map_ty_reference_for_opa env pos skey ty =
  let maped_ty =
    env_map_ty_reference_for_select ~select:fst env pos skey ty
  in
  let _ =
    #<If:BSL_REGISTER $equals ("walk_"^skey)>
      BslKeyMap.iter (
        fun key (opa, ocaml) ->
          Printf.fprintf stderr "key:%S ==> opa:%S, ocaml:%S\n%!"
            (BslKey.to_string key) opa
            (String.concat_map "." (fun s -> s) ocaml)
      ) env.ty_spec_map ;
      debug "OPA:walking through %a returns %a@\n" BslTypes.pp ty BslTypes.pp maped_ty
    #<End>
  in
  maped_ty


(*
Compute the module path in ocaml, from the current_state of the env and the key
*)
let env_ocaml_path env skey =
  let skey_rev_path = List.map (fun (_, s, _) -> s) env.rev_path in
  List.rev (skey :: skey_rev_path)

(*
Compute the module path in skeys, from the current state of the env and the key
*)
let env_rp_ks env skey =
  let skey_rev_path = List.map (fun (s, _, _) -> s) env.rev_path in
  List.rev (skey :: skey_rev_path)


let env_rp_ty env bslty =
  (* TODO: map bslty according to the path *)
  ignore env;
  bslty

let env_rt_ks = env_rp_ks

let env_rt_ty_skey env tags skey =
  match tags.BslTags.opaname with
  | false ->
      let skey_rev_path = List.map (fun (s, _, _) -> s) env.rev_path in
      String.rev_concat_map "_" BslKey.normalize_string ( skey :: skey_rev_path )
  | true -> skey

let env_rp_implementation env implementation injected =
  if injected then implementation
  else
    let impl_rev_path = List.map (fun (_, m, _) -> m) env.rev_path in
    let impl_rev_path = implementation :: impl_rev_path in
    let runtime = env.runtime_module_name ^"." in
    let implementation = String.rev_concat_map ~left:runtime "." (fun s -> s) impl_rev_path in
    implementation


let env_add_module pos skey implementation env =
  let implementation = Option.default skey implementation in

  let name = ocaml_module skey in
  let implementation = ocaml_module implementation in

  let add_module = name, implementation, pos in
  let rev_path = add_module :: env.rev_path in
  let last_module = pos in

  let ml_runtime = env.ml_runtime in
  let ml_runtime =
    FBuffer.printf ml_runtime "%s %s = %s@\n" kw_module implementation kw_struct in

  let ml_line = succ env.ml_line in

  let ml_runtime_mli = env.ml_runtime_mli in
  let ml_runtime_mli =
    FBuffer.printf ml_runtime_mli "%s %s : %s@\n" kw_module implementation kw_sig in

  let env = {
    env with
      last_module ;
      ml_runtime ;
      ml_runtime_mli ;
      ml_line ;
      rev_path ;
  } in

  env


let env_add_endmodule pos env =
  let rev_path =
    match env.rev_path with
    | [] ->
        let pos = List.fold_left FilePos.merge_pos pos [ env.last_module ; env.last_endmodule ] in
        !! pos "This @{<bright>##endmodule@} does not match any @{<bright>##module@}@\n"

    | _::tl ->
        tl
  in
  let last_endmodule = pos in
  let ml_runtime = env.ml_runtime in
  let ml_runtime = FBuffer.addln ml_runtime kw_end in
  let ml_line = succ env.ml_line in

  let ml_runtime_mli = env.ml_runtime_mli in
  let ml_runtime_mli = FBuffer.addln ml_runtime_mli kw_end in

  let env = {
    env with
      last_endmodule ;
      ml_runtime ;
      ml_runtime_mli ;
      ml_line ;
      rev_path ;
  } in

  env


let fold_source_elt ~dynloader_interface ~filename env source_elt =
  let env =
    match source_elt with
    | D.Source (pos, source) ->
        let _, ml_line = FilePos.get_one_loc pos in
        let ml_runtime = env.ml_runtime in
        let ml_runtime =
          if ml_line = env.ml_line then ml_runtime else
            FBuffer.printf ml_runtime "#%d %S@\n" ml_line filename
        in
        let ml_runtime = FBuffer.addln ml_runtime source in
        let env = {
          env with
            ml_runtime = ml_runtime ;
            ml_line = succ ml_line ;
        } in

        env

    | D.Directive (pos, tags, directive) -> (
        match directive with
        | D.ExternalTypeDef (skey, params, implementation) ->
            (* Register part *)
            let rt_ks = env_rt_ks env skey in
            let rt_ty_skey = env_rt_ty_skey env tags skey in

            let key = BslKey.normalize rt_ty_skey in
            (* for ocaml generation, all types are normalized *)
            let skey = BslKey.normalize_string skey in
            let tyitem = rt_ty_skey, env_ocaml_path env skey in
            let _ =
              #<If:BSL_REGISTER $equals "type">
                match tyitem with
                | opa, ocaml ->
                    debug "##extern-type %s ==> opa:%S, ocaml:%S@\n" (BslKey.to_string key) opa
                      (String.concat_map ";" (fun s -> s) ocaml)
              #<End>
            in

            let ty_spec_map =
              let replace = function
                | None ->
                    tyitem
                | Some (_, conflict_path) ->
                    (* Check of overwrite of key in ty_spec_map *)
                    warning pos (
                      "an extern-type with the same opa name is already defined:@\n"^^
                        "key: %a@\n"^^
                        "path: %a@\n"^^
                        "This is a bad practice, and will be rejected in a further version of Opa"
                    )
                      BslKey.pp key
                      (Format.pp_list "." Format.pp_print_string) conflict_path
                    ;
                    tyitem
              in
              let () = SeparatedEnv.SideEffect.add_ty_spec_map key tyitem in
              BslKeyMap.replace key replace env.ty_spec_map
            in

            let params = List.map (fun v -> B.TypeVar (pos, v)) params in
            let bslty = B.External (pos, skey, params) in

            let rt_ty = B.External (pos, rt_ty_skey, params) in
            let rt = { BPI.
              rt_ks = rt_ks ;
              rt_ty = rt_ty ;
            } in
            BPI.apply_register_type dynloader_interface.BPI.register_type rt ;

            (* Generation part *)

            let implementation =
              match implementation with
              | None -> ""
              | Some code -> " = " ^ code
            in

            let ml_runtime = env.ml_runtime in
            let ml_runtime = FBuffer.printf ml_runtime
              "%a%s@\n" BslTypesGeneration.Ocaml.pp_definition bslty implementation in
            let ml_line = succ env.ml_line in

            let ml_runtime_mli = env.ml_runtime_mli in
            let ml_runtime_mli =
              if env.property_mli
              then
                FBuffer.printf ml_runtime_mli
                  "%a%s@\n" BslTypesGeneration.Ocaml.pp_definition bslty implementation
              else
                FBuffer.printf ml_runtime_mli
                  "%a@\n" BslTypesGeneration.Ocaml.pp_definition bslty
            in

            let env = {
              env with
                ml_runtime ;
                ml_runtime_mli ;
                ml_line ;
                ty_spec_map ;
            } in

            env

        | D.OpaTypeDef (skey, params) ->
            (* Register part *)
            let () =
              if not tags.BslTags.opaname
              then
                !! pos "an opa-type cannot be @{<brigth>normalized@}"
            in

            (* for opa types, all rt_ty_skey are opaname *)
            let rt_ty_skey = skey in

            let skey = "opa_" ^ skey in
            let rt_ks = env_rt_ks env skey in

            let key = BslKey.normalize rt_ty_skey in
            (* for ocaml generation, all types are normalized *)
            let skey = BslKey.normalize_string skey in
            let tyitem = rt_ty_skey, env_ocaml_path env skey in
            let _ =
              #<If:BSL_REGISTER $equals "type">
                match tyitem with
                | opa, ocaml ->
                    debug "##opa-type %s ==> opa:%S, ocaml:%S@\n" (BslKey.to_string key) opa
                      (String.concat_map ";" (fun s -> s) ocaml)
              #<End>
            in

            let ty_spec_map =
              let replace = function
                | None ->
                    tyitem
                | Some (_opaname, conflict_path) ->
                    let path = String.concat "." conflict_path in
                    (* Check of overwrite of key in ty_spec_map *)
                    warning pos (
                      "an opa-type with the same opa name is already defined:@\n"^^
                        "key: %a@\n"^^
                        "path: %s@\n"^^
                        "This is allowed, but this is a bad practice because this hides the@ "^^
                        "previous definition, and this will lead to code duplication.@\n"^^
                        "@[<2>@{<bright>Hint@}:@\n"^^
                        "Use rather functions working on type %s"^^
                        "@]"
                    )
                      BslKey.pp key
                      path
                      path
                    ;
                    tyitem
              in
              let () = SeparatedEnv.SideEffect.add_ty_spec_map key tyitem in
              BslKeyMap.replace key replace env.ty_spec_map
            in


            let bslty_params = List.map (fun v -> B.TypeVar (pos, v)) params in
            let rt_ty = B.External (pos, rt_ty_skey, bslty_params) in
            let rt = { BPI.
              rt_ks = rt_ks ;
              rt_ty = rt_ty ;
            } in
            BPI.apply_register_type dynloader_interface.BPI.register_type rt ;


           (* Generation part *)
            let scope = BslTypes.TypeVarPrint.new_scope () in
            let typevar fmt = BslTypes.TypeVarPrint.pp scope fmt in
            let st_ty = Base.Format.sprintf "%a" (OcamlPrint.pp_parameters typevar skey) params in

            let ml_runtime = env.ml_runtime in
            let ml_runtime = FBuffer.printf ml_runtime
              "type %s@\n" st_ty
            in
            let ml_runtime = FBuffer.printf ml_runtime
              "external wrap_%s : ServerLib.ty_record -> %s@ = \"%%identity\"\n" skey st_ty
            in
            let ml_runtime = FBuffer.printf ml_runtime
              "external unwrap_%s : %s -> ServerLib.ty_record = \"%%identity\"@\n" skey st_ty
            in
            (* keep coherence for line count in the ml *)
            let ml_line = env.ml_line + 2 in

            let ml_runtime_mli = env.ml_runtime_mli in
            let ml_runtime_mli = FBuffer.printf ml_runtime_mli
              "type %s@\n" st_ty
            in

            let env = {
              env with
                ml_runtime ;
                ml_runtime_mli ;
                ml_line ;
                ty_spec_map ;
            } in

            env

        | D.Module (skey, implementation) ->
            env_add_module pos skey implementation env

        | D.EndModule ->
            env_add_endmodule pos env

        | D.Register (skey, source_opt, injected, bslty) ->
            let implementation as skey_mli = Option.default skey source_opt in
            (* Register part *)
            let rp_ks = env_rp_ks env skey in
            let rp_ty = env_map_ty_reference_for_opa env pos skey bslty in
            let parsed_t = BslTags.parsed_t tags in
            let implementation = env_rp_implementation env implementation injected in
            let rp_ips = [ L.ml, filename, parsed_t, implementation ] in
            let rp_obj = None in
            let rp = { BPI.
              rp_ks = rp_ks ;
              rp_ty = rp_ty ;
              rp_ips = rp_ips ;
              rp_obj = rp_obj ;
            } in
            BPI.apply_register_primitive dynloader_interface.BPI.register_primitive rp ;

            (* Generation part *)
            let bslty = env_map_ty_reference_for_ocaml env pos skey bslty in
            let skey_ocaml =
              if OcamlUtils.Ident.is_operator skey then Printf.sprintf "( %s )" skey
              else skey in
            let ml_runtime = env.ml_runtime in
            let ml_runtime =
              if injected then (
                FBuffer.printf ml_runtime "let %s = %s@\n" skey_ocaml implementation
              )
              else FBuffer.printf ml_runtime "(* ##register %s \\ %s : %a *)@\n"
                skey implementation BslTypes.pp bslty
            in
            let ml_line = succ env.ml_line in

            let skey_ocaml_mli = if injected then skey_ocaml else skey_mli in
            let ml_runtime_mli = env.ml_runtime_mli in
            let ml_runtime_mli =
              FBuffer.printf ml_runtime_mli
                "val %s : %a@\n" skey_ocaml_mli BslTypesGeneration.Ocaml.pp bslty
            in

            let env = {
              env with
                ml_runtime ;
                ml_runtime_mli ;
                ml_line ;
            } in

            env

        (* We can actually do something in Ocaml too (currently Args is used only is Javascript)
let f x y z =
(
<body>
)
*)
        | D.Args (name, args, bslty) ->
            ignore name ;
            ignore args ;
            ignore bslty ;
            !! pos "The directive ##args is not available in OCaml source files"

        | D.Property p ->
            let property_mli =
              if List.mem_assoc "mli" p then true
              else if List.mem_assoc "endmli" p then false
                else env.property_mli
            in
            let env = {
              env with
                property_mli ;
            } in

            env
      )
  in
  env


let env_add_file_line ~filename env =
  let ml_runtime = env.ml_runtime in
  let ml_runtime = FBuffer.printf ml_runtime "#1 %S@\n" filename in

  let ml_runtime_mli = env.ml_runtime_mli in
  let ml_runtime_mli = FBuffer.printf ml_runtime_mli "#1 \"generated interface for %s\"@\n" filename in

  let env = {
    env with
      ml_runtime = ml_runtime ;
      ml_runtime_mli = ml_runtime_mli ;
      ml_line = 1 ;
  } in
  env


let fold_decorated_file ~dynloader_interface env decorated_file =
  let filename = decorated_file.D.filename in
  let implementation = OcamlUtils.Module.of_filename filename in
  (* we add a module for each file *)
  let env = env_add_module nopos implementation None env in
  let env = env_add_file_line ~filename env in
  let source = decorated_file.D.decorated_source in
  let env =
    List.fold_left (fold_source_elt ~dynloader_interface ~filename) env source
  in
  let _ =
    match env.rev_path with
    | [] | [_]-> ()
    | list ->
        let list = List.rev (List.tl (List.rev list)) in
        List.iter (
          fun (_, _, pos) ->
            OManager.printf "%a" FilePos.pp_citation pos ;
        ) list;
        OManager.error
          "File %S: unclosed module(s)@\n@[<2>@{<bright>Hint@}:@\nAdd the corresponding @{<bright>##endmodule@} directive(s)@]@\n"
          filename
  in
  let env = env_add_endmodule nopos env in
  env

(*
update the ty_spec_map with the previous plugin (cf in options)
*)
let preprocess ~options ~plugins ~dynloader_interface decorated_files =
  let env = empty in
  (*
Add open of depends plugins
*)
  let ml_runtime, ml_runtime_mli =
    let ml = FBuffer.addln env.ml_runtime "(* plugins depends *)" in
    let mli = FBuffer.addln env.ml_runtime_mli "(* plugins depends *)" in
    List.fold_left
      (fun (ml, mli) plugin ->
         let () =
           #<If:BSL_REGISTER $contains "open">
             debug "adding open %s" plugin.BPI.ml_runtime
           #<End>
         in
         let ml = FBuffer.printf ml "open %s\n" plugin.BPI.ml_runtime in
         let mli = FBuffer.printf mli "open %s\n" plugin.BPI.ml_runtime in
         ml, mli
      ) (ml, mli) plugins
  in
  let sep_env = SeparatedEnv.init ~ty_spec_map:env.ty_spec_map in
  let sep_env = List.fold_left SeparatedEnv.fold sep_env plugins in
  let ty_spec_map = SeparatedEnv.ty_spec_map sep_env in
  let runtime_module_name = OcamlUtils.Module.of_filename options.BI.ml_runtime_filename in
  let env = {
    env with
      ty_spec_map ;
      ml_runtime ;
      ml_runtime_mli ;
      runtime_module_name ;
  } in
  let env =
    List.fold_left (fold_decorated_file ~dynloader_interface) env decorated_files in
  let ocaml_env = SeparatedEnv.SideEffect.get_ocaml_env () in
  ocaml_env, env.ml_runtime, env.ml_runtime_mli
Something went wrong with that request. Please try again.