@@ -86,7 +86,7 @@ function check_no_parameters(ex::SyntaxTree, msg)
8686end
8787
8888function check_no_assignment (exs, msg= " misplaced assignment statement in `[ ... ]`" )
89- i = findfirst (kind (e) == K " =" for e in exs)
89+ i = findfirst (kind (e) == K " =" || kind (e) == K " kw " for e in exs)
9090 if ! isnothing (i)
9191 throw (LoweringError (exs[i], msg))
9292 end
@@ -532,17 +532,17 @@ end
532532
533533# -------------------------------------------------------------------------------
534534# Expansion of array indexing
535- function _arg_to_temp (ctx, stmts, ex, eq_is_kw = false )
535+ function _arg_to_temp (ctx, stmts, ex)
536536 k = kind (ex)
537537 if is_effect_free (ex)
538538 ex
539539 elseif k == K " ..."
540540 @ast ctx ex [k _arg_to_temp (ctx, stmts, ex[1 ])]
541- elseif k == K "= " && eq_is_kw
542- @ast ctx ex [K "= " ex[1 ] _arg_to_temp (ctx, stmts, ex[2 ], false )]
541+ elseif k == K "kw "
542+ @ast ctx ex [K "kw " ex[1 ] _arg_to_temp (ctx, stmts, ex[2 ])]
543543 elseif k == K " parameters"
544544 mapchildren (ctx, ex) do e
545- _arg_to_temp (ctx, stmts, e, true )
545+ _arg_to_temp (ctx, stmts, e)
546546 end
547547 else
548548 emit_assign_tmp (stmts, ctx, ex)
@@ -565,9 +565,8 @@ function remove_argument_side_effects(ctx, stmts, ex)
565565 emit_assign_tmp (stmts, ctx, ex)
566566 else
567567 args = SyntaxList (ctx)
568- eq_is_kw = ((k == K " call" || k == K " dotcall" ) && is_prefix_call (ex)) || k == K " ref"
569568 for (i,e) in enumerate (children (ex))
570- push! (args, _arg_to_temp (ctx, stmts, e, eq_is_kw && i > 1 ))
569+ push! (args, _arg_to_temp (ctx, stmts, e))
571570 end
572571 # TODO : Copy attributes?
573572 @ast ctx ex [k args... ]
@@ -1613,7 +1612,7 @@ function _merge_named_tuple(ctx, srcref, old, new)
16131612 end
16141613end
16151614
1616- function expand_named_tuple (ctx, ex, kws;
1615+ function expand_named_tuple (ctx, ex, kws, eq_is_kw ;
16171616 field_name= " named tuple field" ,
16181617 element_name= " named tuple element" )
16191618 name_strs = Set {String} ()
@@ -1628,7 +1627,8 @@ function expand_named_tuple(ctx, ex, kws;
16281627 # x ==> x = x
16291628 name = to_symbol (ctx, kw)
16301629 value = kw
1631- elseif k == K " ="
1630+ elseif k == K " kw" || (eq_is_kw && k == K " =" )
1631+ # syntax TODO : This should parse to K"kw"
16321632 # x = a
16331633 if kind (kw[1 ]) != K " Identifier" && kind (kw[1 ]) != K " Placeholder"
16341634 throw (LoweringError (kw[1 ], " invalid $field_name name" ))
@@ -1696,7 +1696,7 @@ end
16961696function expand_kw_call (ctx, srcref, farg, args, kws)
16971697 @ast ctx srcref [K " block"
16981698 func := farg
1699- kw_container := expand_named_tuple (ctx, srcref, kws;
1699+ kw_container := expand_named_tuple (ctx, srcref, kws, false ;
17001700 field_name= " keyword argument" ,
17011701 element_name= " keyword argument" )
17021702 if all (kind (kw) == K " ..." for kw in kws)
@@ -1869,7 +1869,7 @@ function remove_kw_args!(ctx, args::SyntaxList)
18691869 for i in 1 : length (args)
18701870 arg = args[i]
18711871 k = kind (arg)
1872- if k == K "= "
1872+ if k == K "kw "
18731873 if isnothing (kws)
18741874 kws = SyntaxList (ctx)
18751875 end
@@ -2277,7 +2277,7 @@ end
22772277function expand_function_arg (ctx, body_stmts, arg, is_last_arg, is_kw, arg_id)
22782278 ex = arg
22792279
2280- if kind (ex) == K "= "
2280+ if kind (ex) == K "kw "
22812281 default = ex[2 ]
22822282 ex = ex[1 ]
22832283 else
@@ -2858,8 +2858,10 @@ function keyword_function_defs(ctx, srcref, callex_srcref, name_str, typevar_nam
28582858 kwcall_body_tail
28592859 ]
28602860 else
2861- scope_nest (ctx, kw_names, kw_values, kwcall_body_tail)
2861+ scope_nest (ctx, has_kw_slurp ? kw_names[1 : end - 1 ] : kw_names,
2862+ kw_values, kwcall_body_tail)
28622863 end
2864+
28632865 main_kwcall_typevars = trim_used_typevars (ctx, kwcall_arg_types, typevar_names, typevar_stmts)
28642866 push! (kwcall_method_defs,
28652867 method_def_expr (ctx, srcref, callex_srcref, kwcall_mtable,
@@ -3233,6 +3235,29 @@ end
32333235
32343236# -------------------------------------------------------------------------------
32353237# Anon function syntax
3238+ function expand_arrow_args (ctx, arglist)
3239+ k = kind (arglist)
3240+ # The arglist can sometimes be parsed as a block, or something else, and
3241+ # fixing this is extremely awkward when nested inside `where`. See
3242+ # https://github.com/JuliaLang/JuliaSyntax.jl/pull/522
3243+ if k == K " block"
3244+ @chk numchildren (arglist) == 2
3245+ kw = arglist[2 ]
3246+ if kind (kw) === K " ="
3247+ kw = @ast ctx kw [K " kw" children (kw)... ]
3248+ end
3249+ arglist = @ast ctx arglist [K " tuple"
3250+ arglist[1 ]
3251+ [K " parameters" kw]
3252+ ]
3253+ elseif k != K " tuple"
3254+ arglist = @ast ctx arglist [K " tuple" arglist]
3255+ end
3256+ return mapchildren (ctx, arglist) do a
3257+ kind (a) === K " =" ? @ast (ctx, a, [K " kw" children (a)... ]) : a
3258+ end
3259+ end
3260+
32363261function expand_arrow_arglist (ctx, arglist, arrowname)
32373262 k = kind (arglist)
32383263 if k == K " where"
@@ -3241,23 +3266,9 @@ function expand_arrow_arglist(ctx, arglist, arrowname)
32413266 arglist[2 ]
32423267 ]
32433268 else
3244- # The arglist can sometimes be parsed as a block, or something else, and
3245- # fixing this is extremely awkward when nested inside `where`. See
3246- # https://github.com/JuliaLang/JuliaSyntax.jl/pull/522
3247- if k == K " block"
3248- @chk numchildren (arglist) == 2
3249- arglist = @ast ctx arglist [K " tuple"
3250- arglist[1 ]
3251- [K " parameters" arglist[2 ]]
3252- ]
3253- elseif k != K " tuple"
3254- arglist = @ast ctx arglist [K " tuple"
3255- arglist[1 ]
3256- ]
3257- end
32583269 @ast ctx arglist [K " call"
32593270 arrowname:: K"Placeholder"
3260- children (arglist)...
3271+ children (expand_arrow_args (ctx, arglist) )...
32613272 ]
32623273 end
32633274end
@@ -3280,7 +3291,7 @@ function expand_opaque_closure(ctx, ex)
32803291 func_expr = ex[5 ]
32813292 @chk kind (func_expr) == K " ->"
32823293 @chk numchildren (func_expr) == 2
3283- args = func_expr[1 ]
3294+ args = expand_arrow_args (ctx, func_expr[1 ])
32843295 @chk kind (args) == K " tuple"
32853296 check_no_parameters (ex, args)
32863297
@@ -3829,7 +3840,7 @@ function _rewrite_ctor_new_calls(ctx, ex, struct_name, global_struct_name, ctor_
38293840 )
38303841 end
38313842 # Rewrite a call to new()
3832- kw_arg_i = findfirst (e-> (k = kind (e); k == K "= " || k == K " parameters" ), children (ex))
3843+ kw_arg_i = findfirst (e-> (k = kind (e); k == K "kw " || k == K " parameters" ), children (ex))
38333844 if ! isnothing (kw_arg_i)
38343845 throw (LoweringError (ex[kw_arg_i], " `new` does not accept keyword arguments" ))
38353846 end
@@ -4147,7 +4158,7 @@ function expand_struct_def(ctx, ex, docs)
41474158 struct_name
41484159 isnothing (docs) ? nothing_ (ctx, ex) : docs[1 ]
41494160 :: K"SourceLocation" (ex)
4150- [K "= "
4161+ [K "kw "
41514162 " field_docs" :: K"Identifier"
41524163 [K " call" " svec" :: K"core" field_docs... ]
41534164 ]
@@ -4194,7 +4205,7 @@ end
41944205function expand_curly (ctx, ex)
41954206 @assert kind (ex) == K " curly"
41964207 check_no_parameters (ex, " unexpected semicolon in type parameter list" )
4197- check_no_assignment (children (ex), " misplace assignment in type parameter list" )
4208+ check_no_assignment (children (ex), " misplaced assignment in type parameter list" )
41984209
41994210 typevar_stmts = SyntaxList (ctx)
42004211 type_args = SyntaxList (ctx)
@@ -4512,9 +4523,9 @@ function expand_forms_2(ctx::DesugaringContext, ex::SyntaxTree, docs=nothing)
45124523 if numchildren (ex) > 1
45134524 throw (LoweringError (ex[end ], " unexpected semicolon in tuple - use `,` to separate tuple elements" ))
45144525 end
4515- expand_forms_2 (ctx, expand_named_tuple (ctx, ex, children (ex[1 ])))
4526+ expand_forms_2 (ctx, expand_named_tuple (ctx, ex, children (ex[1 ]), true ))
45164527 elseif any_assignment (children (ex))
4517- expand_forms_2 (ctx, expand_named_tuple (ctx, ex, children (ex)))
4528+ expand_forms_2 (ctx, expand_named_tuple (ctx, ex, children (ex), true ))
45184529 else
45194530 expand_forms_2 (ctx, @ast ctx ex [K " call"
45204531 " tuple" :: K"core"
@@ -4558,7 +4569,7 @@ function expand_forms_2(ctx::DesugaringContext, ex::SyntaxTree, docs=nothing)
45584569 ctx. mod :: K"Value"
45594570 [K " inert" ex]
45604571 [K " parameters"
4561- [K "= "
4572+ [K "kw "
45624573 " expr_compat_mode" :: K"Identifier"
45634574 ctx. expr_compat_mode:: K"Bool"
45644575 ]
0 commit comments