Skip to content

Commit

Permalink
added some tests for Expr formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
xitology committed Mar 30, 2021
1 parent e3cb81e commit 3f158e4
Show file tree
Hide file tree
Showing 2 changed files with 180 additions and 3 deletions.
178 changes: 178 additions & 0 deletions doc/src/test.md
Original file line number Diff line number Diff line change
Expand Up @@ -300,3 +300,181 @@ Parameter `tab` specifies the indentation level.
initialization,
intuition)
=#


## Formatting Julia Code

`pprint()` can format `Expr` objects. A fairly complete subset of Julia syntax
is supported.

ex = quote
module Test
export f
using Dates
import Base: show
abstract type A{T}
end
struct S{T} <: A{T}
x::T
end
const v1 = [1,2,3]
const v2 = Number[1,2,3]
const t1 = (1,)
const t2 = (1,2,3)
const p = 1 => 2
Base.show(Base.stdout)
Base.@show Base.stdout
println("x = $x")
"Compute nothing"
function f(::Number)
return
end
g(y) = y > 0 ? y : -y
global G
if (x1 + x2) > x3
if p1 && p2 || p3 && p4
1
elseif (p1 || p2) && (p3 || p4)
2
else
3
end
elseif x1 <= x2 < x3
if !p
4
end
end
while x > 0
break
end
for t = 1:10
continue
end
let x = 1
y = 2
x + y
end
try
error()
catch err
1
end
try
error()
finally
2
end
try
error()
catch err
1
finally
2
end
foreach(1:10) do k
println(k)
end
[k for k = 1:10 if isodd(k)]
end
end

pprint(ex)
#=>
quote
module Test

export f

using Dates

import Base: show

abstract type A{T}
end

struct S{T} <: A{T}
x::T
end

const v1 = [1, 2, 3]

const v2 = Number[1, 2, 3]

const t1 = (1,)

const t2 = (1, 2, 3)

const p = 1 => 2

Base.show(Base.stdout)

Base.@show Base.stdout

println("x = $(x)")

"Compute nothing"
function f(::Number)
return nothing
end

g(y) = y > 0 ? y : -y

global G

if x1 + x2 > x3
if p1 && p2 || p3 && p4
1
elseif (p1 || p2) && (p3 || p4)
2
else
3
end
elseif x1 <= x2 < x3
if !(p)
4
end
end

while x > 0
break
end

for t = 1:10
continue
end

let x = 1
y = 2
x + y
end

try
error()
catch err
1
nothing
end

try
error()
finally
2
end

try
error()
catch err
1
finally
2
end

foreach(1:10) do k
println(k)
end

[k for k = 1:10 if isodd(k)]

end
end
=#
5 changes: 2 additions & 3 deletions src/expr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,7 @@ function tile_expr(ex::Expr, pr = -1)
elseif @isexpr ex Expr(:module, notbare::Bool, name::Symbol, body)
return tile_expr_module(notbare ? :module : :baremodule, name, body)
elseif @isexpr ex Expr(:export, names...)
return list_layout(prefix = "export ", par = ("", ""),
Layout[tile_expr(name, 0) for name in names])
return tile_expr_export(names)
elseif @isexpr ex Expr(head := :using || :import, Expr(:(:), from, args...))
return tile_expr_import(head, from, args)
elseif @isexpr ex Expr(head := :using || :import, arg)
Expand Down Expand Up @@ -308,7 +307,7 @@ end

tile_expr_export(args) =
list_layout(prefix = "export ", par = ("", ""),
Layout[tile_expr(arg, 0) for arg in arg])
Layout[tile_expr(arg, 0) for arg in args])

tile_expr_import_path(ex) =
(@isexpr ex Expr(:., args...)) ?
Expand Down

0 comments on commit 3f158e4

Please sign in to comment.