util.ml 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. open Printf
  2. open Types
  3. (** Operators *)
  4. let (|>) a b = b a
  5. (** List utilities *)
  6. let rec filter_none = function
  7. | [] -> []
  8. | None :: tl -> filter_none tl
  9. | Some hd :: tl -> hd :: filter_none tl
  10. (** Reading input from file/stdin *)
  11. let input_all ic =
  12. let n = in_channel_length ic in
  13. let buf = String.create n in
  14. really_input ic buf 0 n;
  15. close_in ic;
  16. buf
  17. let input_buffered ic chunksize =
  18. let rec read_all buf bufsize pos =
  19. match input ic buf pos (bufsize - pos) with
  20. | 0 -> (close_in ic; buf)
  21. | nread when nread = bufsize - pos ->
  22. let bufsize = bufsize + chunksize in
  23. let pos = pos + nread in
  24. read_all (buf ^ String.create chunksize) bufsize pos
  25. | nread ->
  26. read_all buf bufsize (pos + nread)
  27. in
  28. read_all (String.create chunksize) chunksize 0
  29. (** Error printing *)
  30. let noloc = ("", 0, 0, 0, 0)
  31. let tabwidth = 4
  32. let count_tabs str upto =
  33. let rec count n = function
  34. | 0 -> n
  35. | i -> count (if String.get str (i - 1) = '\t' then n + 1 else n) (i - 1)
  36. in
  37. count 0 upto
  38. let rec repeat s n = if n < 1 then "" else s ^ (repeat s (n - 1))
  39. let retab str = Str.global_replace (Str.regexp "\t") (repeat " " tabwidth) str
  40. let indent n = repeat (repeat " " (tabwidth - 1)) n
  41. let prerr_loc (fname, ystart, yend, xstart, xend) =
  42. let file = open_in fname in
  43. (* skip lines until the first matched line *)
  44. for i = 1 to ystart - 1 do ignore (input_line file) done;
  45. (* for each line in `loc`, print the source line with an underline *)
  46. for l = ystart to yend do
  47. let line = input_line file in
  48. let linewidth = String.length line in
  49. let left = if l = ystart then xstart else 1 in
  50. let right = if l = yend then xend else linewidth in
  51. if linewidth > 0 then begin
  52. prerr_endline (retab line);
  53. prerr_string (indent (count_tabs line right));
  54. for i = 1 to left - 1 do prerr_char ' ' done;
  55. for i = left to right do prerr_char '^' done;
  56. prerr_endline "";
  57. end
  58. done
  59. let prerr_loc_msg verbose loc msg =
  60. if verbose then begin
  61. let (fname, ystart, yend, xstart, xend) = loc in
  62. if loc != noloc then begin
  63. let line_s = if yend != ystart
  64. then sprintf "lines %d-%d" ystart yend
  65. else sprintf "line %d" ystart
  66. in
  67. let char_s = if xend != xstart || yend != ystart
  68. then sprintf "characters %d-%d" xstart xend
  69. else sprintf "character %d" xstart
  70. in
  71. eprintf "File \"%s\", %s, %s:\n" fname line_s char_s;
  72. end;
  73. eprintf "%s\n" msg;
  74. if verbose && loc != noloc then
  75. try prerr_loc loc
  76. with Sys_error _ -> ()
  77. end