Skip to content

Commit

Permalink
Fixes for parameters blocks in Expr(:curly) (#174)
Browse files Browse the repository at this point in the history
Fix Expr conversion for expressions like `x{a, b; i=j}`
  • Loading branch information
c42f authored Dec 19, 2022
1 parent a80b038 commit f7ad15f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
15 changes: 9 additions & 6 deletions src/expr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ function _to_expr(node::SyntaxNode; iteration_spec=false, need_linenodes=true,
((headsym == :call || headsym == :dotcall) && is_prefix_call(node)) ||
headsym == :ref
eq_to_kw_all = headsym == :parameters && !map_kw_in_params
in_vbr = headsym == :vect || headsym == :braces || headsym == :ref
in_vcbr = headsym == :vect || headsym == :curly || headsym == :braces || headsym == :ref
if insert_linenums && isempty(node_args)
push!(args, source_location(LineNumberNode, node.source, node.position))
else
Expand All @@ -144,7 +144,7 @@ function _to_expr(node::SyntaxNode; iteration_spec=false, need_linenodes=true,
eq_to_kw = eq_to_kw_in_call && i > 1 || eq_to_kw_all
args[insert_linenums ? 2*i : i] =
_to_expr(n, eq_to_kw=eq_to_kw,
map_kw_in_params=in_vbr)
map_kw_in_params=in_vcbr)
end
end
end
Expand All @@ -157,7 +157,7 @@ function _to_expr(node::SyntaxNode; iteration_spec=false, need_linenodes=true,
end
reorder_parameters!(args, 2)
insert!(args, 2, loc)
elseif headsym in (:dotcall, :call, :ref)
elseif headsym in (:dotcall, :call)
# Julia's standard `Expr` ASTs have children stored in a canonical
# order which is often not always source order. We permute the children
# here as necessary to get the canonical order.
Expand All @@ -180,15 +180,18 @@ function _to_expr(node::SyntaxNode; iteration_spec=false, need_linenodes=true,
args[1] = Symbol(".", args[1])
end
end
elseif headsym in (:ref, :curly)
# Move parameters blocks to args[2]
reorder_parameters!(args, 2)
elseif headsym in (:tuple, :vect, :braces)
# Move parameters blocks to args[1]
reorder_parameters!(args, 1)
elseif headsym === :comparison
for i in 1:length(args)
if Meta.isexpr(args[i], :., 1)
args[i] = Symbol(".",args[i].args[1])
end
end
elseif headsym in (:tuple, :vect, :braces)
# Move parameters blocks to args[1]
reorder_parameters!(args, 1)
elseif headsym === :where
reorder_parameters!(args, 2)
elseif headsym in (:try, :try_finally_catch)
Expand Down
10 changes: 9 additions & 1 deletion test/expr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -208,14 +208,22 @@
# ref
@test parse(Expr, "x[i=j]") ==
Expr(:ref, :x, Expr(:kw, :i, :j))
@test parse(Expr, "(i=j)[x]") ==
Expr(:ref, Expr(:(=), :i, :j), :x)
@test parse(Expr, "x[a, b; i=j]") ==
Expr(:ref, :x, Expr(:parameters, Expr(:(=), :i, :j)), :a, :b)
# curly
@test parse(Expr, "(i=j){x}") ==
Expr(:curly, Expr(:(=), :i, :j), :x)
@test parse(Expr, "x{a, b; i=j}") ==
Expr(:curly, :x, Expr(:parameters, Expr(:(=), :i, :j)), :a, :b)

# vect/braces
# vect
@test parse(Expr, "[a=1,; b=2]") ==
Expr(:vect,
Expr(:parameters, Expr(:(=), :b, 2)),
Expr(:(=), :a, 1))
# braces
@test parse(Expr, "{a=1,; b=2}") ==
Expr(:braces,
Expr(:parameters, Expr(:(=), :b, 2)),
Expand Down

0 comments on commit f7ad15f

Please sign in to comment.