package spin

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file spin_refmt.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
exception Refmt_error of string

type ast =
  | Impl of Reason_toolchain_conf.Parsetree.structure
  | Intf of Reason_toolchain_conf.Parsetree.signature_item list

type parse_output =
  { ast : ast
  ; comments : Reason_comment.t list
  ; parsed_as_ml : bool
  }

let parse filename =
  if Filename.check_suffix filename ".re" then
    let lexbuf = Reason_toolchain.setup_lexbuf false filename in
    let impl = Reason_toolchain.RE.implementation_with_comments in
    let ast, comments = impl lexbuf in
    { ast = Impl ast; comments; parsed_as_ml = false }
  else if Filename.check_suffix filename ".rei" then
    let lexbuf = Reason_toolchain.setup_lexbuf false filename in
    let intf = Reason_toolchain.RE.interface_with_comments in
    let ast, comments = intf lexbuf in
    { ast = Intf ast; comments; parsed_as_ml = false }
  else if Filename.check_suffix filename ".ml" then
    let lexbuf = Reason_toolchain.setup_lexbuf false filename in
    let impl = Reason_toolchain.ML.implementation_with_comments in
    let ast, comments = impl lexbuf in
    { ast = Impl ast; comments; parsed_as_ml = false }
  else if Filename.check_suffix filename ".mli" then
    let lexbuf = Reason_toolchain.setup_lexbuf false filename in
    let intf = Reason_toolchain.ML.interface_with_comments in
    let ast, comments = intf lexbuf in
    { ast = Intf ast; comments; parsed_as_ml = false }
  else
    raise (Refmt_error "The file extension is not valid.")

let print filename parse_output output_formatter =
  if Filename.check_suffix filename ".re" then
    match parse_output.ast with
    | Impl impl ->
      Reason_toolchain.RE.print_implementation_with_comments
        output_formatter
        (impl, parse_output.comments)
    | Intf _ ->
      raise (Refmt_error "Cannot print an implementation from an interface")
  else if Filename.check_suffix filename ".rei" then
    match parse_output.ast with
    | Intf intf ->
      Reason_toolchain.RE.print_interface_with_comments
        output_formatter
        (intf, parse_output.comments)
    | Impl _ ->
      raise (Refmt_error "Cannot print an interface from an implementation")
  else if Filename.check_suffix filename ".ml" then
    match parse_output.ast with
    | Impl impl ->
      Reason_toolchain.ML.print_implementation_with_comments
        output_formatter
        (impl, parse_output.comments)
    | Intf _ ->
      raise (Refmt_error "Cannot print an implementation from an interface")
  else if Filename.check_suffix filename ".mli" then
    match parse_output.ast with
    | Intf intf ->
      Reason_toolchain.ML.print_interface_with_comments
        output_formatter
        (intf, parse_output.comments)
    | Impl _ ->
      raise (Refmt_error "Cannot print an interface from an implementation")
  else
    raise (Refmt_error "The file extension is not valid.")

let prepare_output_file name = open_out_bin name

let close_output_file output_chan = close_out output_chan

let output_of_input_file filename =
  if Filename.check_suffix filename ".re" then
    Filename.chop_suffix filename ".re" ^ ".ml"
  else if Filename.check_suffix filename ".rei" then
    Filename.chop_suffix filename ".rei" ^ ".mli"
  else if Filename.check_suffix filename ".ml" then
    Filename.chop_suffix filename ".ml" ^ ".re"
  else if Filename.check_suffix filename ".mli" then
    Filename.chop_suffix filename ".mli" ^ ".rei"
  else
    raise (Refmt_error "The file extension is not valid.")

let convert input_file =
  Reason_config.configure ~r:false;
  Location.input_name := input_file;
  let _ =
    Reason_pprint_ast.configure
      ~width:80
      ~assumeExplicitArity:true
      ~constructorLists:[]
  in
  let output_file = output_of_input_file input_file in
  let output_chan = prepare_output_file output_file in
  let eol = Eol_detect.get_eol_for_file input_file in
  let output_formatter = Eol_convert.get_formatter output_chan eol in
  let parse_output = parse input_file in
  print output_file parse_output output_formatter;
  (* Also closes all open boxes. *)
  Format.pp_print_flush output_formatter ();
  flush output_chan;
  close_output_file output_chan
OCaml

Innovation. Community. Security.