Skip to content

Commit 7d8c4b6

Browse files
Handle nested quotes and blocks.
Improve test_repr macro, testing whether removing line numbers spoils something.
1 parent 37a0ac7 commit 7d8c4b6

File tree

2 files changed

+39
-24
lines changed

2 files changed

+39
-24
lines changed

base/show.jl

+3-16
Original file line numberDiff line numberDiff line change
@@ -1520,22 +1520,9 @@ function show_unquoted(io::IO, ex::Expr, indent::Int, prec::Int, quote_level::In
15201520
elseif head === :quote && nargs == 1 && isa(args[1], Symbol)
15211521
show_unquoted_quote_expr(io, args[1]::Symbol, indent, 0, quote_level+1)
15221522
elseif head === :quote && nargs == 1 && Meta.isexpr(args[1], :block)
1523-
if length(args[1].args) == 1
1524-
# one argument: Expr(:quote, Expr(:block, a_single_arg))
1525-
if isa(args[1].args[1], Symbol)
1526-
show_unquoted_quote_expr(io, args[1].args[1]::Symbol, indent,
1527-
0, quote_level+1)
1528-
else
1529-
print(io, ":(")
1530-
show_unquoted(io, args[1].args[1], indent+2, 0, quote_level+1)
1531-
print(io, ")")
1532-
end
1533-
else
1534-
# multiple arguments: Expr(:quote, Expr(:block, many_args...))
1535-
show_block(io, "quote", Expr(:quote, args[1].args...), indent,
1536-
quote_level+1)
1537-
print(io, "end")
1538-
end
1523+
show_block(io, "quote", Expr(:quote, args[1].args...), indent,
1524+
quote_level+1)
1525+
print(io, "end")
15391526
elseif head === :quote && nargs == 1
15401527
print(io, ":(")
15411528
show_unquoted(io, args[1], indent+2, 0, quote_level+1)

test/show.jl

+36-8
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,35 @@ function test_repr(x::String)
6464
x2 = eval(Meta.parse(repr(x1)))
6565
x3 = eval(Meta.parse(repr(x2)))
6666
if ! (x1 == x2 == x3)
67-
error(string(
67+
# error(string(
68+
print(string(
6869
"repr test failed:",
6970
"\noriginal: ", x,
71+
"\n\npreparsed: ", x1, "\n", sprint(dump, x1),
7072
"\n\nparsed: ", x2, "\n", sprint(dump, x2),
7173
"\n\nreparsed: ", x3, "\n", sprint(dump, x3)
7274
))
7375
end
7476
@test x1 == x2 == x3
77+
78+
x4 = Base.remove_linenums!(Meta.parse(x))
79+
x5 = eval(Base.remove_linenums!(Meta.parse(repr(x4))))
80+
x6 = eval(Base.remove_linenums!(Meta.parse(repr(x5))))
81+
if ! (x4 == x5 == x6)
82+
error(string(
83+
"repr test without line numbers failed:",
84+
"\noriginal: ", x,
85+
"\n\npreparsed: ", x4, "\n", sprint(dump, x4),
86+
"\n\nparsed: ", x5, "\n", sprint(dump, x5),
87+
"\n\nreparsed: ", x6, "\n", sprint(dump, x6)
88+
))
89+
end
90+
@test x4 == x5 == x6
91+
92+
@test Base.remove_linenums!(x1) ==
93+
Base.remove_linenums!(x2) ==
94+
Base.remove_linenums!(x3) ==
95+
x4 == x5 == x6
7596
end
7697

7798
# primitive types
@@ -849,7 +870,9 @@ end""")) ==
849870
end"""))) ==
850871
"""
851872
:(macro m(a, b)
852-
:(\$a + \$b)
873+
quote
874+
\$a + \$b
875+
end
853876
end)"""
854877
@test repr(Meta.parse(
855878
"""macro m(a, b)
@@ -922,25 +945,30 @@ end"""
922945

923946
# nested quotes and blocks
924947
@test_repr "Expr(:quote, Expr(:block, :a, :b))"
925-
# @test_repr repr(Expr(:quote, Expr(:block, :a, :b)))
926948
@test_repr repr(Expr(:quote, Expr(:block, LineNumberNode(0, :none), :a, LineNumberNode(0, :none), :b)))
927949
@test repr(Expr(:quote, Expr(:block, :a, :b))) ==
928950
":(quote
929951
a
930952
b
931953
end)"
932954
@test_repr "Expr(:quote, Expr(:block, :a))"
933-
@test_repr repr(Expr(:quote, Expr(:block, :a)))
934-
@test repr(Expr(:quote, Expr(:block, :a))) == ":(:a)"
955+
@test_repr repr(Expr(:quote, Expr(:block, LineNumberNode(0, :none), :a)))
956+
@test repr(Expr(:quote, Expr(:block, :a))) ==
957+
":(quote
958+
a
959+
end)"
935960
@test_repr "Expr(:quote, Expr(:block, :(a + b)))"
936-
@test_repr repr(Expr(:quote, Expr(:block, :(a + b))))
937-
@test repr(Expr(:quote, Expr(:block, :(a + b)))) == ":(:(a + b))"
961+
@test_repr repr(Expr(:quote, Expr(:block, LineNumberNode(0, :none), :(a + b))))
962+
@test repr(Expr(:quote, Expr(:block, :(a + b)))) ==
963+
":(quote
964+
a + b
965+
end)"
938966

939967
# QuoteNode + quotes and unquotes
940968
@test_repr "QuoteNode(\$x)"
941969
@test_repr "QuoteNode(\$\$x)"
942970
@test_repr ":(QuoteNode(\$x))"
943-
@test_repr ":(:(QuoteNode(\$\$x))"
971+
@test_repr ":(:(QuoteNode(\$\$x)))"
944972
@test repr(QuoteNode(Expr(:$, :x))) == ":(\$(QuoteNode(:(\$(Expr(:\$, :x))))))"
945973
@test repr(QuoteNode(Expr(:quote, Expr(:$, :x)))) == ":(\$(QuoteNode(:(:(\$x)))))"
946974
@test repr(Expr(:quote, QuoteNode(Expr(:$, :x)))) == ":(:(\$(QuoteNode(:(\$(Expr(:\$, :x)))))))"

0 commit comments

Comments
 (0)