Skip to content

Commit ec8874a

Browse files
committed
added some tests for Expr formatting
1 parent e3cb81e commit ec8874a

File tree

2 files changed

+210
-8
lines changed

2 files changed

+210
-8
lines changed

doc/src/test.md

Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,3 +300,207 @@ Parameter `tab` specifies the indentation level.
300300
initialization,
301301
intuition)
302302
=#
303+
304+
305+
## Formatting Julia Code
306+
307+
`pprint()` can format `Expr` objects. A fairly complete subset of Julia syntax
308+
is supported.
309+
310+
ex = quote
311+
module Test
312+
export f
313+
using Dates
314+
import Base: show
315+
abstract type A{T}
316+
end
317+
struct S{T} <: A{T}
318+
x::T
319+
end
320+
const v1 = [1,2,3]
321+
const v2 = Number[1,2,3]
322+
const t1 = (1,)
323+
const t2 = (1,2,3)
324+
const p = 1 => 2
325+
Base.show(Base.stdout)
326+
Base.@show Base.stdout
327+
println("x = $x")
328+
"Compute nothing"
329+
function f(::Number)
330+
return
331+
end
332+
g(y) = y > 0 ? y : -y
333+
h(args...; kw = 0) = (args, kw)
334+
global G
335+
if (x1 - (x2 - x3)) > ((x1 - x2) - x3)
336+
if p1 && p2 || p3 && p4
337+
nothing
338+
elseif (p1 || p2) && (p3 || p4)
339+
nothing
340+
else
341+
nothing
342+
end
343+
elseif (x1 ^ (x2 ^ x3)) <= ((x1 ^ x2) ^ x3) < x4 .+ x5
344+
if !p
345+
nothing
346+
end
347+
end
348+
while x > 0
349+
break
350+
end
351+
for t = 1:10
352+
continue
353+
end
354+
begin
355+
x = 1
356+
y = 2
357+
x + y
358+
end
359+
0 + (x = 1; y = 2; x + y)
360+
let x = 1
361+
y = 2
362+
x + y
363+
end
364+
quote
365+
$x + $y
366+
end
367+
try
368+
error()
369+
catch err
370+
nothing
371+
end
372+
try
373+
error()
374+
finally
375+
nothing
376+
end
377+
try
378+
error()
379+
catch err
380+
nothing
381+
finally
382+
nothing
383+
end
384+
foreach(1:10) do k
385+
println(k)
386+
end
387+
[k for k = 1:10 if isodd(k)]
388+
$(Expr(:fallback, 1, 2, 3))
389+
end
390+
end
391+
392+
pprint(ex)
393+
#=>
394+
quote
395+
module Test
396+
397+
export f
398+
399+
using Dates
400+
401+
import Base: show
402+
403+
abstract type A{T}
404+
end
405+
406+
struct S{T} <: A{T}
407+
x::T
408+
end
409+
410+
const v1 = [1, 2, 3]
411+
412+
const v2 = Number[1, 2, 3]
413+
414+
const t1 = (1,)
415+
416+
const t2 = (1, 2, 3)
417+
418+
const p = 1 => 2
419+
420+
Base.show(Base.stdout)
421+
422+
Base.@show Base.stdout
423+
424+
println("x = $(x)")
425+
426+
"Compute nothing"
427+
function f(::Number)
428+
return nothing
429+
end
430+
431+
g(y) = y > 0 ? y : -y
432+
433+
h(args...; kw = 0) = (args, kw)
434+
435+
global G
436+
437+
if x1 - (x2 - x3) > x1 - x2 - x3
438+
if p1 && p2 || p3 && p4
439+
nothing
440+
elseif (p1 || p2) && (p3 || p4)
441+
nothing
442+
else
443+
nothing
444+
end
445+
elseif x1 ^ x2 ^ x3 <= (x1 ^ x2) ^ x3 < x4 .+ x5
446+
if !(p)
447+
nothing
448+
end
449+
end
450+
451+
while x > 0
452+
break
453+
end
454+
455+
for t = 1:10
456+
continue
457+
end
458+
459+
begin
460+
x = 1
461+
y = 2
462+
x + y
463+
end
464+
465+
0 + (x = 1; y = 2; x + y)
466+
467+
let x = 1
468+
y = 2
469+
x + y
470+
end
471+
472+
quote
473+
$(x) + $(y)
474+
end
475+
476+
try
477+
error()
478+
catch err
479+
nothing
480+
end
481+
482+
try
483+
error()
484+
finally
485+
nothing
486+
end
487+
488+
try
489+
error()
490+
catch err
491+
nothing
492+
finally
493+
nothing
494+
end
495+
496+
foreach(1:10) do k
497+
println(k)
498+
end
499+
500+
[k for k = 1:10 if isodd(k)]
501+
502+
$(Expr(:fallback, 1, 2, 3))
503+
504+
end
505+
end
506+
=#

src/expr.jl

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,7 @@ function tile_expr(ex::Expr, pr = -1)
147147
elseif @isexpr ex Expr(:module, notbare::Bool, name::Symbol, body)
148148
return tile_expr_module(notbare ? :module : :baremodule, name, body)
149149
elseif @isexpr ex Expr(:export, names...)
150-
return list_layout(prefix = "export ", par = ("", ""),
151-
Layout[tile_expr(name, 0) for name in names])
150+
return tile_expr_export(names)
152151
elseif @isexpr ex Expr(head := :using || :import, Expr(:(:), from, args...))
153152
return tile_expr_import(head, from, args)
154153
elseif @isexpr ex Expr(head := :using || :import, arg)
@@ -232,7 +231,6 @@ function tile_expr(ex::Expr, pr = -1)
232231
elseif @isexpr ex Expr(:$, arg)
233232
return literal("\$(") * tile_expr(arg) * literal(")")
234233
end
235-
dump(ex)
236234
tile_expr_fallback(ex)
237235
end
238236

@@ -308,7 +306,7 @@ end
308306

309307
tile_expr_export(args) =
310308
list_layout(prefix = "export ", par = ("", ""),
311-
Layout[tile_expr(arg, 0) for arg in arg])
309+
Layout[tile_expr(arg, 0) for arg in args])
312310

313311
tile_expr_import_path(ex) =
314312
(@isexpr ex Expr(:., args...)) ?
@@ -416,10 +414,10 @@ function tile_expr_try(body, name, catch_body, finally_body)
416414
end
417415
if finally_body !== nothing
418416
lt = lt / literal("finally")
419-
end
420-
finally_body_lt = tile_exprs(finally_body)
421-
if finally_body_lt !== ZERO
422-
lt = lt / (indent(4) * finally_body_lt)
417+
finally_body_lt = tile_exprs(finally_body)
418+
if finally_body_lt !== ZERO
419+
lt = lt / (indent(4) * finally_body_lt)
420+
end
423421
end
424422
lt = lt / literal("end")
425423
lt

0 commit comments

Comments
 (0)