Skip to content

Commit 44176e1

Browse files
committed
Prototype: Underscores and prefix anonymous function syntax
As discussed in JuliaLang/julia#38713
1 parent 92994ea commit 44176e1

File tree

2 files changed

+37
-7
lines changed

2 files changed

+37
-7
lines changed

src/expr.jl

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,27 @@ function reorder_parameters!(args, params_pos)
4646
insert!(args, params_pos, pop!(args))
4747
end
4848

49+
function lower_underscores!(anon_args, args, argrange=1:length(args))
50+
for i in argrange
51+
a = args[i]
52+
if a == :_
53+
g = gensym()
54+
push!(anon_args, g)
55+
args[i] = g
56+
elseif a isa Expr
57+
if Meta.isexpr(a, :call) && length(a.args) > 2 &&
58+
Meta.isexpr(a.args[2], :parameters)
59+
lower_underscores!(anon_args, a.args, 1:1)
60+
lower_underscores!(anon_args, a.args, 3:length(a.args))
61+
lower_underscores!(anon_args, a.args, 2:2)
62+
else
63+
# FIXME: Other out-of-source-order Exprs
64+
lower_underscores!(anon_args, a.args)
65+
end
66+
end
67+
end
68+
end
69+
4970
function _to_expr(node::SyntaxNode; iteration_spec=false, need_linenodes=true,
5071
eq_to_kw=false, map_kw_in_params=false)
5172
if !haschildren(node)
@@ -275,11 +296,17 @@ function _to_expr(node::SyntaxNode; iteration_spec=false, need_linenodes=true,
275296
# Block for conditional's source location
276297
args[1] = Expr(:block, loc, args[1])
277298
elseif headsym === :(->)
278-
if Meta.isexpr(args[2], :block)
279-
pushfirst!(args[2].args, loc)
299+
if is_prefix_op_call(node)
300+
anon_args = Symbol[]
301+
lower_underscores!(anon_args, args)
302+
pushfirst!(args, Expr(:tuple, anon_args...))
280303
else
281-
# Add block for source locations
282-
args[2] = Expr(:block, loc, args[2])
304+
if Meta.isexpr(args[2], :block)
305+
pushfirst!(args[2].args, loc)
306+
else
307+
# Add block for source locations
308+
args[2] = Expr(:block, loc, args[2])
309+
end
283310
end
284311
elseif headsym === :function
285312
if length(args) > 1

src/parser.jl

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ function is_syntactic_operator(k)
276276
end
277277

278278
function is_syntactic_unary_op(k)
279-
kind(k) in KSet"$ & ::"
279+
kind(k) in KSet"$ & :: ->"
280280
end
281281

282282
function is_type_operator(t)
@@ -1448,6 +1448,7 @@ end
14481448
# &a ==> (& a)
14491449
# ::a ==> (::-pre a)
14501450
# $a ==> ($ a)
1451+
# ->a ==> (-> a)
14511452
#
14521453
# flisp: parse-unary-prefix
14531454
function parse_unary_prefix(ps::ParseState)
@@ -1465,14 +1466,16 @@ function parse_unary_prefix(ps::ParseState)
14651466
if k in KSet"& ::"
14661467
# &a ==> (& a)
14671468
parse_where(ps, parse_call)
1469+
elseif k == K"->"
1470+
# -> binds loosely on the right
1471+
parse_eq_star(ps)
14681472
else
14691473
# $a ==> ($ a)
14701474
# $$a ==> ($ ($ a))
14711475
# $&a ==> ($ (& a))
14721476
parse_unary_prefix(ps)
14731477
end
1474-
# Only need PREFIX_OP_FLAG for ::
1475-
f = k == K"::" ? PREFIX_OP_FLAG : EMPTY_FLAGS
1478+
f = (k == K"::" || k == K"->") ? PREFIX_OP_FLAG : EMPTY_FLAGS
14761479
emit(ps, mark, k, f)
14771480
end
14781481
else

0 commit comments

Comments
 (0)