Skip to content

Commit f7ad15f

Browse files
authored
Fixes for parameters blocks in Expr(:curly) (#174)
Fix Expr conversion for expressions like `x{a, b; i=j}`
1 parent a80b038 commit f7ad15f

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

src/expr.jl

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ function _to_expr(node::SyntaxNode; iteration_spec=false, need_linenodes=true,
132132
((headsym == :call || headsym == :dotcall) && is_prefix_call(node)) ||
133133
headsym == :ref
134134
eq_to_kw_all = headsym == :parameters && !map_kw_in_params
135-
in_vbr = headsym == :vect || headsym == :braces || headsym == :ref
135+
in_vcbr = headsym == :vect || headsym == :curly || headsym == :braces || headsym == :ref
136136
if insert_linenums && isempty(node_args)
137137
push!(args, source_location(LineNumberNode, node.source, node.position))
138138
else
@@ -144,7 +144,7 @@ function _to_expr(node::SyntaxNode; iteration_spec=false, need_linenodes=true,
144144
eq_to_kw = eq_to_kw_in_call && i > 1 || eq_to_kw_all
145145
args[insert_linenums ? 2*i : i] =
146146
_to_expr(n, eq_to_kw=eq_to_kw,
147-
map_kw_in_params=in_vbr)
147+
map_kw_in_params=in_vcbr)
148148
end
149149
end
150150
end
@@ -157,7 +157,7 @@ function _to_expr(node::SyntaxNode; iteration_spec=false, need_linenodes=true,
157157
end
158158
reorder_parameters!(args, 2)
159159
insert!(args, 2, loc)
160-
elseif headsym in (:dotcall, :call, :ref)
160+
elseif headsym in (:dotcall, :call)
161161
# Julia's standard `Expr` ASTs have children stored in a canonical
162162
# order which is often not always source order. We permute the children
163163
# here as necessary to get the canonical order.
@@ -180,15 +180,18 @@ function _to_expr(node::SyntaxNode; iteration_spec=false, need_linenodes=true,
180180
args[1] = Symbol(".", args[1])
181181
end
182182
end
183+
elseif headsym in (:ref, :curly)
184+
# Move parameters blocks to args[2]
185+
reorder_parameters!(args, 2)
186+
elseif headsym in (:tuple, :vect, :braces)
187+
# Move parameters blocks to args[1]
188+
reorder_parameters!(args, 1)
183189
elseif headsym === :comparison
184190
for i in 1:length(args)
185191
if Meta.isexpr(args[i], :., 1)
186192
args[i] = Symbol(".",args[i].args[1])
187193
end
188194
end
189-
elseif headsym in (:tuple, :vect, :braces)
190-
# Move parameters blocks to args[1]
191-
reorder_parameters!(args, 1)
192195
elseif headsym === :where
193196
reorder_parameters!(args, 2)
194197
elseif headsym in (:try, :try_finally_catch)

test/expr.jl

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,14 +208,22 @@
208208
# ref
209209
@test parse(Expr, "x[i=j]") ==
210210
Expr(:ref, :x, Expr(:kw, :i, :j))
211+
@test parse(Expr, "(i=j)[x]") ==
212+
Expr(:ref, Expr(:(=), :i, :j), :x)
211213
@test parse(Expr, "x[a, b; i=j]") ==
212214
Expr(:ref, :x, Expr(:parameters, Expr(:(=), :i, :j)), :a, :b)
215+
# curly
216+
@test parse(Expr, "(i=j){x}") ==
217+
Expr(:curly, Expr(:(=), :i, :j), :x)
218+
@test parse(Expr, "x{a, b; i=j}") ==
219+
Expr(:curly, :x, Expr(:parameters, Expr(:(=), :i, :j)), :a, :b)
213220

214-
# vect/braces
221+
# vect
215222
@test parse(Expr, "[a=1,; b=2]") ==
216223
Expr(:vect,
217224
Expr(:parameters, Expr(:(=), :b, 2)),
218225
Expr(:(=), :a, 1))
226+
# braces
219227
@test parse(Expr, "{a=1,; b=2}") ==
220228
Expr(:braces,
221229
Expr(:parameters, Expr(:(=), :b, 2)),

0 commit comments

Comments
 (0)