forked from rescript-lang/rescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathext_pp.ml
171 lines (145 loc) · 4.22 KB
/
ext_pp.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
(* Copyright (C) 2015-2016 Bloomberg Finance L.P.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* In addition to the permissions granted to you by the LGPL, you may combine
* or link a "work that uses the Library" with a publicly distributed version
* of this file to produce a combined library or application, then distribute
* that combined work under the terms of your choosing, with no requirement
* to comply with the obligations normally placed on you by section 4 of the
* LGPL version 3 (or the corresponding section of a later version of the LGPL
* should you choose to use a later version).
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
module L = struct
let space = " "
let indent_str = " "
end
let indent_length = String.length L.indent_str
type t = {
output_string : string -> unit;
output_char : char -> unit;
flush : unit -> unit;
mutable indent_level : int;
mutable last_new_line : bool;
(* only when we print newline, we print the indent *)
}
let from_channel chan =
{
output_string = (fun s -> output_string chan s);
output_char = (fun c -> output_char chan c);
flush = (fun _ -> flush chan);
indent_level = 0;
last_new_line = false;
}
let from_buffer buf =
{
output_string = (fun s -> Buffer.add_string buf s);
output_char = (fun c -> Buffer.add_char buf c);
flush = (fun _ -> ());
indent_level = 0;
last_new_line = false;
}
(* If we have [newline] in [s],
all indentations will be broken
in the future, we can detect this in [s]
*)
let string t s =
t.output_string s;
t.last_new_line <- false
let newline t =
if not t.last_new_line then (
t.output_char '\n';
for _ = 0 to t.indent_level - 1 do
t.output_string L.indent_str
done;
t.last_new_line <- true)
let at_least_two_lines t =
if not t.last_new_line then t.output_char '\n';
t.output_char '\n';
for _ = 0 to t.indent_level - 1 do
t.output_string L.indent_str
done;
t.last_new_line <- true
let force_newline t =
t.output_char '\n';
for _ = 0 to t.indent_level - 1 do
t.output_string L.indent_str
done;
t.last_new_line <- true
let space t = string t L.space
let nspace t n = string t (String.make n ' ')
let group t i action =
if i = 0 then action ()
else
let old = t.indent_level in
t.indent_level <- t.indent_level + i;
Ext_pervasives.finally ~clean:(fun _ -> t.indent_level <- old) () action
let vgroup = group
let paren t action =
string t "(";
let v = action () in
string t ")";
v
let brace fmt u =
string fmt "{";
(* break1 fmt ; *)
let v = u () in
string fmt "}";
v
let bracket fmt u =
string fmt "[";
let v = u () in
string fmt "]";
v
let brace_vgroup st n action =
string st "{";
let v =
vgroup st n (fun _ ->
newline st;
let v = action () in
v)
in
force_newline st;
string st "}";
v
let bracket_vgroup st n action =
string st "[";
let v =
vgroup st n (fun _ ->
newline st;
let v = action () in
v)
in
force_newline st;
string st "]";
v
let bracket_group st n action = group st n (fun _ -> bracket st action)
let paren_vgroup st n action =
string st "(";
let v =
group st n (fun _ ->
newline st;
let v = action () in
v)
in
newline st;
string st ")";
v
let paren_group st n action = group st n (fun _ -> paren st action)
let cond_paren_group st b n action =
if b then paren_group st n action else action ()
let brace_group st n action = group st n (fun _ -> brace st action)
(* let indent t n =
t.indent_level <- t.indent_level + n *)
let flush t () = t.flush ()