Advertisement
Guest User

Untitled

a guest
Feb 21st, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.64 KB | None | 0 0
  1. let report_err exn =
  2. match exn with
  3. | Sys_error msg ->
  4. Format.printf "@[I/O error:@ %s@]@." msg
  5. | x ->
  6. match Location.error_of_exn x with
  7. | Some err ->
  8. Format.printf "@[%a@]@."
  9. Location.report_error err
  10. | None -> raise x
  11.  
  12. let remove_locs =
  13. let open Ast_mapper in
  14. { default_mapper with
  15. location = (fun _mapper _loc -> Location.none);
  16. attributes =
  17. (fun mapper attrs ->
  18. let attrs = default_mapper.attributes mapper attrs in
  19. List.filter (fun (s, _) -> s.Location.txt <> "#punning#") attrs (* this is to accomodate a LexiFi custom extension *)
  20. )
  21. }
  22.  
  23. let from_file parse_fun filename =
  24. Location.input_name := filename;
  25. let ic = open_in filename in
  26. let lexbuf = Lexing.from_channel ic in
  27. Location.init lexbuf filename;
  28. let ast = parse_fun lexbuf in
  29. close_in ic;
  30. ast
  31.  
  32. let from_string parse_fun str =
  33. Location.input_name := "<str>";
  34. let lexbuf = Lexing.from_string str in
  35. Location.init lexbuf "<str>";
  36. parse_fun lexbuf
  37.  
  38. let to_string print_fun ast =
  39. Format.fprintf Format.str_formatter "%a@." print_fun ast;
  40. Format.flush_str_formatter ()
  41.  
  42. let to_tmp_file print_fun ast =
  43. let fn, oc = Filename.open_temp_file "ocamlparse" ".txt" in
  44. output_string oc (to_string print_fun ast);
  45. close_out oc;
  46. fn
  47.  
  48. let test parse_fun pprint print map filename =
  49. match from_file parse_fun filename with
  50. | exception exn ->
  51. Printf.printf "%s: FAIL, CANNOT PARSE\n" filename;
  52. report_err exn;
  53. print_endline "====================================================="
  54. | ast ->
  55. let str = to_string pprint ast in
  56. match from_string parse_fun str with
  57. | exception exn ->
  58. Printf.printf "%s: FAIL, CANNOT REPARSE\n" filename;
  59. report_err exn;
  60. print_endline str;
  61. print_endline "====================================================="
  62. | ast2 ->
  63. let ast = map remove_locs remove_locs ast in
  64. let ast2 = map remove_locs remove_locs ast2 in
  65. if ast <> ast2 then begin
  66. Printf.printf "%s: FAIL, REPARSED AST IS DIFFERENT\n%!" filename;
  67. let f1 = to_tmp_file print ast in
  68. let f2 = to_tmp_file print ast2 in
  69. let cmd = Printf.sprintf "diff -u %s %s" (Filename.quote f1) (Filename.quote f2) in
  70. let _ret = Sys.command cmd in
  71. Sys.remove f1;
  72. Sys.remove f2;
  73. print_endline "====================================================="
  74. end
  75.  
  76. let test parse_fun pprint print map filename =
  77. try test parse_fun pprint print map filename
  78. with exn -> report_err exn
  79.  
  80. let rec process path =
  81. if Sys.is_directory path then
  82. let files = Sys.readdir path in
  83. Array.iter (fun s -> process (Filename.concat path s)) files
  84. else if Filename.check_suffix path ".ml" then
  85. test
  86. (*
  87. (fun lexbuf -> List.filter (fun x -> x <> Parsetree.Ptop_def [])
  88. (Parse.use_file lexbuf))
  89. (fun ppf -> List.iter (Format.fprintf ppf "%a@." Pprintast.toplevel_phrase))
  90. (fun ppf -> List.iter (Printast.top_phrase ppf))
  91. (fun mapper _ ->
  92. List.map
  93. (function
  94. | Parsetree.Ptop_dir _ as d -> d
  95. | Parsetree.Ptop_def str -> Ptop_def (mapper.structure mapper str)
  96. )
  97. )
  98. *)
  99. Parse.implementation
  100. Pprintast.structure
  101. Printast.implementation
  102. (fun mapper -> mapper.structure)
  103. path
  104. else if Filename.check_suffix path ".mli" then
  105. test
  106. Parse.interface
  107. Pprintast.signature
  108. Printast.interface
  109. (fun mapper -> mapper.signature)
  110. path
  111.  
  112. let () =
  113. for i = 1 to Array.length Sys.argv - 1 do
  114. process Sys.argv.(i)
  115. done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement