-
Notifications
You must be signed in to change notification settings - Fork 1
/
prettyshow.jl
269 lines (224 loc) · 8.06 KB
/
prettyshow.jl
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# Pretty printing
# ---------------
# Implement pshow(io::PrettyIO, t::T) for types T to be prettyprinted.
# pshow() should print no initial or final newlines.
# It should print using pshow and pprint.
# pprint(io, a, {b, c})
# prints a, then prints b and c indented.
# (The indenting is only noticable if there is a line break within {b, c})
# pprint(io, a, io->fun(io), b)
# invokes fun(io) on the current io. I e
# pprint(io, {a, io->fun(io), b})
# invokes fun() with an indented io.
#load("utils/utils.jl")
is_expr(ex, head::Symbol) = (isa(ex, Expr) && (ex.head == head))
function is_expr(ex, head::Symbol, nargs::Int)
is_expr(ex, head) && length(ex.args) == nargs
end
pprintln(args...) = pprint(args..., '\n')
pprint(args...) = pprint(default_pretty(), args...)
pshow(args...) = pshow(default_pretty(), args...)
# fallback for io::IO
pprint(io::IO, args...) = print(io, args...)
# -- PrettyIO -----------------------------------------------------------------
abstract PrettyIO
pprint_nowrap(io::PrettyIO, s::String) = (for c in s; pprint_nowrap(io, c);end)
function pprint(io::PrettyIO, s::String)
n = strlen(s)
if !str_fits_on_line(io, n)
pprint(io, '\n')
end
for c in s; pprint(io, c); end
end
pprint(io::PrettyIO, arg::Any) = pshow(io, arg)
pprint(io::PrettyIO, args...) = (for arg in args; pprint(io, arg); end)
pshow(io::PrettyIO, arg::Any) = pprint(io, sshow(arg))
pprint(io::PrettyIO, v::Vector) = pprint(indent(io), v...)
pprint(io::PrettyIO, pprinter::Function) = pprinter(io)
comment(io::PrettyIO) = PrettyChild(io, ()->"# ")
indent(io::PrettyIO) = indent(io::PrettyIO, 4)
indent(io::PrettyIO, indent::Int) = (pre=" "^indent; PrettyChild(io, ()->pre))
# -- PrettyRoot ---------------------------------------------------------------
type PrettyRoot <: PrettyIO
parent::IO
width::Int
currpos::Int
autowrap::Bool
function PrettyRoot(parent::IO, width::Int)
if width < 1; error("width must be >= 1, got ", width); end
new(parent, width, 0, false)
end
end
function str_fits_on_line(io::PrettyRoot, n::Integer)
(!io.autowrap) || (io.currpos+n <= io.width)
end
function pprint_nowrap(io::PrettyRoot, c::Char)
if c=='\t'
nsp::Int = (-io.currpos)&7
#if nsp==0; nsp=8; end
print(io.parent, " "^nsp)
io.currpos += nsp
else
print(io.parent, c)
io.currpos += 1
end
if c == '\n'
io.currpos = 0
io.autowrap = false
return true
end
return false
end
function pprint(io::PrettyRoot, c::Char)
if 2*io.currpos < io.width; io.autowrap=true; end
if pprint_nowrap(io, c); return true; end
if io.autowrap && (io.currpos >= io.width)
return pprint_nowrap(io, '\n')
end
return false
end
default_pretty() = PrettyRoot(OUTPUT_STREAM, 80)
# -- PrettyChild --------------------------------------------------------------
type PrettyChild <: PrettyIO
parent::PrettyIO
newline_hook::Function
function PrettyChild(parent::PrettyIO, newline_hook::Function)
new(parent, newline_hook)
end
end
str_fits_on_line(io::PrettyChild, n::Integer) = str_fits_on_line(io.parent, n)
pprint_nowrap(io::PrettyChild, c::Char) = pprint_nowrap(io.parent, c)
function pprint(io::PrettyChild, c::Char)
newline = pprint(io.parent, c)::Bool
if newline
pprint_nowrap(io.parent, io.newline_hook())
end
return newline
end
# == Expr prettyprinting ======================================================
const doublecolon = @eval (:(x::Int)).head
## list printing
function pshow_comma_list(io::PrettyIO, args::Vector,
open::String, close::String)
pshow_delim_list(io, args, open, ", ", close)
end
function pshow_delim_list(io::PrettyIO, args::Vector, open::String,
delim::String, close::String)
pprint(io, {open,
io->pshow_list_delim(io, args, delim)},
close)
end
function pshow_list_delim(io::PrettyIO, args::Vector, delim::String)
for (arg, k) in enumerate(args)
pshow(io, arg)
if k < length(args)
pprint(io, delim)
end
end
end
## show the body of a :block
pshow_mainbody(io::PrettyIO, ex) = pshow(io, ex)
function pshow_mainbody(io::PrettyIO, ex)
if is_expr(ex, :block)
args = ex.args
for (arg, k) in enumerate(args)
if !is_expr(arg, :line)
pprint(io, "\n")
end
pshow(io, arg)
end
else
if !is_expr(ex, :line); pprint(io, "\n"); end
pshow(io, ex)
end
end
## show arguments of a block, and then body
pshow_body(io::PrettyIO, body) = pshow_body(io, {}, body)
function pshow_body(io::PrettyIO, arg, body)
pprint(io, {arg, io->pshow_mainbody(io, body) })
end
function pshow_body(io::PrettyIO, args::Vector, body)
pprint(io, {
io->pshow_comma_list(io, args, "", ""),
io->pshow_mainbody(io, body)
})
end
## show ex as if it were quoted
function pshow_quoted_expr(io::PrettyIO, sym::Symbol)
if !is(sym,:(:)) && !is(sym,:(==))
pprint(io, ":$sym")
else
pprint(io, ":($sym)")
end
end
function pshow_quoted_expr(io::PrettyIO, ex::Expr)
if ex.head == :block
pprint(io, "quote ", io->pshow_body(io, ex), "\nend")
else
pprint(io, "quote(", {ex}, ")")
end
end
pshow_quoted_expr(io::PrettyIO, ex) =pprint(io, ":($ex)")
## show an expr
function pshow(io::PrettyIO, ex::Expr)
const infix = {:(=)=>"=", :(.)=>".", doublecolon=>"::", :(:)=>":",
:(->)=>"->", :(=>)=>"=>",
:(&&)=>" && ", :(||)=>" || "}
const parentypes = {:call=>("(",")"), :ref=>("[","]"), :curly=>("{","}")}
head = ex.head
args = ex.args
nargs = length(args)
if has(infix, head) && nargs==2 # infix operations
# pprint(io, "(",{args[1], infix[head], args[2]},")")
pprint(io, {args[1], infix[head], args[2]})
elseif has(parentypes, head) && nargs >= 1 # :call/:ref/:curly
pprint(io, args[1])
pshow_comma_list(io, args[2:end], parentypes[head]...)
elseif (head == :comparison) && (nargs>=3 && isodd(nargs)) # :comparison
pprint("(",{args},")")
elseif ((contains([:return, :abstract, :const] , head) && nargs==1) ||
contains([:local, :global], head))
pshow_comma_list(io, args, string(head)*" ", "")
elseif head == :typealias && nargs==2
pshow_delim_list(io, args, string(head)*" ", " ", "")
elseif (head == :quote) && (nargs==1) # :quote
pshow_quoted_expr(io, args[1])
elseif (head == :line) && (1 <= nargs <= 2) # :line
let io=comment(io)
if nargs == 1
linecomment = "line "*string(args[1])*": "
else
@assert nargs==2
# linecomment = "line "*string(args[1])*", "*string(args[2])*": "
linecomment = string(args[2])*", line "*string(args[1])*": "
end
if str_fits_on_line(io, strlen(linecomment)+13)
pprint(io, "\t# ", linecomment)
else
pprint(io, "\n", linecomment)
end
end
elseif head == :if && nargs == 3 # if/else
pprint(io,
"if ", io->pshow_body(io, args[1], args[2]),
"\nelse ", io->pshow_body(io, args[3]),
"\nend")
elseif head == :try && nargs == 3 # try[/catch]
pprint(io, "try ", io->pshow_body(io, args[1]))
if !(is(args[2], false) && is_expr(args[3], :block, 0))
pprint(io, "\ncatch ", io->pshow_body(io, args[2], args[3]))
end
pprint(io, "\nend")
elseif head == :let # :let
pprint(io, "let ",
io->pshow_body(io, args[2:end], args[1]), "\nend")
elseif head == :block
pprint(io, "begin ", io->pshow_body(io, ex), "\nend")
elseif contains([:for, :while, :function, :if, :type], head) && nargs == 2
pprint(io, string(head), " ",
io->pshow_body(io, args[1], args[2]), "\nend")
else
pprint(io, head)
pshow_comma_list(indent(io), args, "(", ")")
end
end