Skip to content

Commit 5ee7dc2

Browse files
Change how nested interpolations are displayed (omitting unrequired parenthesis).
Add test for nested quotes and interpolations. Correct small bug preventing correct display of "t[a b;]". Update metaprogramming docs to better reflect changes in how quoting and unquoting get displayed.
1 parent b2794d3 commit 5ee7dc2

File tree

3 files changed

+44
-9
lines changed

3 files changed

+44
-9
lines changed

base/show.jl

+5-4
Original file line numberDiff line numberDiff line change
@@ -1171,6 +1171,7 @@ function show_unquoted(io::IO, ex::Expr, indent::Int, prec::Int)
11711171
head = :hcat
11721172
end
11731173
args = args[2:end]
1174+
nargs = nargs - 1
11741175
end
11751176
op, cl = expr_parens[head]
11761177
if head === :vcat || head === :bracescat
@@ -1382,9 +1383,8 @@ function show_unquoted(io::IO, ex::Expr, indent::Int, prec::Int)
13821383
elseif isa(args[1], Symbol) &&
13831384
startswith(string(args[1]::Symbol), "@") &&
13841385
endswith(string(args[1]::Symbol), "_str")
1385-
m = match(r"^Symbol\(\"@(.+)_str\"\)$",
1386-
repr(args[1]))
1387-
print(io, m.captures[1], "\"", args[3], "\"")
1386+
s = repr(args[1])
1387+
print(io, s[10:prevind(s,end,6)], "\"", args[3], "\"")
13881388
if nargs == 4
13891389
print(io, args[4])
13901390
end
@@ -1473,7 +1473,8 @@ function show_unquoted(io::IO, ex::Expr, indent::Int, prec::Int)
14731473
elseif (head === :& || head === :$) && nargs == 1
14741474
print(io, head)
14751475
a1 = args[1]
1476-
parens = (isa(a1,Expr) && a1.head !== :tuple) || (isa(a1,Symbol) && isoperator(a1))
1476+
parens = (isa(a1,Expr) && (a1.head !== :tuple && a1.head !== :$)) ||
1477+
(isa(a1,Symbol) && isoperator(a1))
14771478
parens && print(io, "(")
14781479
show_unquoted(io, a1)
14791480
parens && print(io, ")")

doc/src/manual/metaprogramming.md

+6-3
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,9 @@ julia> Symbol(:var,'_',"sym")
126126
:var_sym
127127
```
128128

129+
Note that to use `:` syntax, the symbol's name must be a valid identifier.
130+
Otherwise the `Symbol(str)` constructor must be used.
131+
129132
In the context of an expression, symbols are used to indicate access to variables; when an expression
130133
is evaluated, a symbol is replaced with the value bound to that symbol in the appropriate [scope](@ref scope-of-variables).
131134

@@ -265,7 +268,7 @@ quote
265268
end
266269
```
267270

268-
Notice that the result contains `Expr(:$, :x)`, which means that `x` has not been
271+
Notice that the result contains `$x`, which means that `x` has not been
269272
evaluated yet.
270273
In other words, the `$` expression "belongs to" the inner quote expression, and
271274
so its argument is only evaluated when the inner quote expression is:
@@ -293,7 +296,7 @@ quote
293296
end
294297
```
295298

296-
Notice that `:(1 + 2)` now appears in the result instead of the symbol `:x`.
299+
Notice that `(1 + 2)` now appears in the result instead of the symbol `x`.
297300
Evaluating this expression yields an interpolated `3`:
298301

299302
```jldoctest interp1
@@ -550,7 +553,7 @@ julia> macro twostep(arg)
550553
@twostep (macro with 1 method)
551554
552555
julia> ex = macroexpand(Main, :(@twostep :(1, 2, 3)) );
553-
I execute at parse time. The argument is: $(Expr(:quote, :((1, 2, 3))))
556+
I execute at parse time. The argument is: :((1, 2, 3))
554557
```
555558

556559
The first call to [`println`](@ref) is executed when [`macroexpand`](@ref) is called. The

test/show.jl

+33-2
Original file line numberDiff line numberDiff line change
@@ -767,7 +767,7 @@ test_mt(show_f5, "show_f5(A::AbstractArray{T,N}, indices::Vararg{$Int,N})")
767767
@test_repr "T[a,b]"
768768
@test_repr "T[a;b;c]"
769769
@test_repr "T[a b]"
770-
# @test_repr "T[a b;]" #= don't now what to expect from this =#
770+
@test_repr "T[a b;]"
771771
@test_repr "T[a b c]"
772772
@test_repr "T[a b; c d]"
773773

@@ -797,7 +797,14 @@ end"""
797797
\$a + \$b
798798
end
799799
end""")) ==
800-
":(macro m(a, b)\n #= none:2 =#\n quote\n #= none:3 =#\n \$a + \$b\n end\n end)"
800+
"""
801+
:(macro m(a, b)
802+
#= none:2 =#
803+
quote
804+
#= none:3 =#
805+
\$a + \$b
806+
end
807+
end)"""
801808
@test repr(Meta.parse(
802809
"""macro m(a, b)
803810
:(\$a + \$b)
@@ -806,6 +813,30 @@ end""")) ==
806813
@test repr(Expr(:macro, Expr(:call, :m, :x), Expr(:quote, Expr(:call, :+, Expr(:($), :x), 1)))) ==
807814
":(macro m(x)\n :(\$x + 1)\n end)"
808815

816+
# nested quotes and interpolations
817+
@test repr(Meta.parse(
818+
"""quote
819+
quote
820+
\$\$x
821+
end
822+
end""")) ==
823+
"""
824+
:(quote
825+
#= none:2 =#
826+
quote
827+
#= none:3 =#
828+
\$\$x
829+
end
830+
end)"""
831+
@test_repr """
832+
quote
833+
#= none:2 =#
834+
quote
835+
#= none:3 =#
836+
\$\$x
837+
end
838+
end"""
839+
809840
# string interpolation, if this is what the comment in test_rep function
810841
# definition talk about
811842
@test repr(Expr(:string, "foo", :x, "bar")) == ":(\"foo\$(x)bar\")"

0 commit comments

Comments
 (0)