package reason

  1. Overview
  2. Docs
Reason: Syntax & Toolchain for OCaml

Install

dune-project
 Dependency

Authors

Maintainers

Sources

reason-3.17.2.tbz
sha256=7f4087016e8c393a13d57b7737677bc1dbf16978fd43c19354d4d79a794c8c47
sha512=3d3015d25bce329fbee9cef9b114831e7db1c10e19a26cbcaa27948c376bff3518b9d56fda9091c6df887df9ee8c33f45e1071e87933de79d65bcbf58c93cb17

doc/src/reason.refmt-lib/printer_maker.ml.html

Source file printer_maker.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
open Reason

type 'a parser_result =
  { ast : 'a
  ; comments : Reason_comment.t list
  ; parsed_as_ml : bool
  ; parsed_as_intf : bool
  }

type parse_itype =
  [ `ML
  | `Reason
  | `Binary
  | `BinaryReason
  | `Auto
  ]

type print_itype =
  [ `ML
  | `Reason
  | `Binary
  | `BinaryReason
  | `AST
  | `None
  ]

exception Invalid_config of string

module type PRINTER = sig
  type t

  val parse :
     use_stdin:bool
    -> parse_itype
    -> string
    -> (t * Reason_comment.t list) * bool

  val print :
     print_itype
    -> string
    -> bool
    -> out_channel
    -> Format.formatter
    -> t * Reason_comment.t list
    -> unit
end

let err s = raise (Invalid_config s)

let prepare_output_file = function
  | Some name -> open_out_bin name
  | None ->
    set_binary_mode_out stdout true;
    stdout

let close_output_file output_file output_chan =
  match output_file with Some _ -> close_out output_chan | None -> ()

let ocamlBinaryParser use_stdin filename =
  let module Ast_io = Ppxlib__.Utils.Ast_io in
  let input_source =
    match use_stdin with true -> Ast_io.Stdin | false -> File filename
  in
  match Ast_io.read input_source ~input_kind:Necessarily_binary with
  | Error _ -> assert false
  | Ok { ast = Impl ast; _ } ->
    { ast = Obj.magic ast
    ; comments = []
    ; parsed_as_ml = true
    ; parsed_as_intf = false
    }
  | Ok { ast = Intf ast; _ } ->
    { ast = Obj.magic ast
    ; comments = []
    ; parsed_as_ml = true
    ; parsed_as_intf = true
    }

let reasonBinaryParser use_stdin filename =
  let chan =
    match use_stdin with
    | true -> stdin
    | false ->
      let file_chan = open_in_bin filename in
      seek_in file_chan 0;
      file_chan
  in
  let _, _, ast, comments, parsed_as_ml, parsed_as_intf = input_value chan in
  { ast; comments; parsed_as_ml; parsed_as_intf }