/tryocaml/js_of_ocaml-patched/compiler/util.ml

http://github.com/cago/tryocaml · OCaml · 78 lines · 42 code · 14 blank · 22 comment · 4 complexity · 0bad8c99647922560452690a3f1dcde0 MD5 · raw file

  1. (* Js_of_ocaml compiler
  2. * http://www.ocsigen.org/js_of_ocaml/
  3. * Copyright (C) 2010 Jérôme Vouillon
  4. * Laboratoire PPS - CNRS Université Paris Diderot
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation, with linking exception;
  9. * either version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19. *)
  20. module Int = struct type t = int let compare (x : int) y = compare x y end
  21. module IntSet = Set.Make (Int)
  22. module IntMap = Map.Make (Int)
  23. module StringSet = Set.Make (String)
  24. let opt_map f x = match x with None -> None | Some v -> Some (f v)
  25. let opt_iter f x = match x with None -> () | Some v -> f v
  26. let opt_bind x f = match x with None -> None | Some v -> f v
  27. let opt_filter p x =
  28. match x with None -> None | Some v -> if p v then Some v else None
  29. (****)
  30. let rec find_in_paths paths name =
  31. match paths with
  32. [] ->
  33. raise Not_found
  34. | path :: rem ->
  35. let file = Filename.concat path name in
  36. if Sys.file_exists file then file else find_in_paths rem name
  37. let read_file f =
  38. let ch = open_in_bin f in
  39. let b = Buffer.create 4096 in
  40. let s = String.create 4096 in
  41. while
  42. let n = input ch s 0 4096 in
  43. Buffer.add_substring b s 0 n;
  44. n <> 0
  45. do () done;
  46. close_in ch;
  47. Buffer.contents b
  48. (****)
  49. let debugs = ref []
  50. let debug s =
  51. let state = ref false in
  52. debugs := (s, state) :: !debugs;
  53. fun () -> !state
  54. let set_debug s =
  55. try List.assoc s !debugs := true with Not_found -> ()
  56. (****)
  57. let disabled_lst = ref []
  58. let disabled s =
  59. let state = ref false in
  60. disabled_lst := (s, state) :: !disabled_lst;
  61. fun () -> !state
  62. let set_disabled s =
  63. try List.assoc s !disabled_lst := true with Not_found ->
  64. Format.eprintf "%s: no disable option named '%s'@." Sys.argv.(0) s; exit 1