@@ -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)
@@ -1724,13 +1724,12 @@ end
17241724
17251725# Expand the (sym,lib) argument to ccall/cglobal
17261726function expand_C_library_symbol (ctx, ex)
1727- expanded = expand_forms_2 (ctx, ex)
17281727 if kind (ex) == K " tuple"
1729- expanded = @ast ctx ex [K " static_eval" (meta= name_hint (" function name and library expression" ))
1730- expanded
1728+ return @ast ctx ex [K " static_eval" (meta= name_hint (" function name and library expression" ))
1729+ mapchildren (e -> expand_forms_2 (ctx,e), ctx, ex)
17311730 ]
17321731 end
1733- return expanded
1732+ return expand_forms_2 (ctx, ex)
17341733end
17351734
17361735function expand_ccall (ctx, ex)
@@ -1870,7 +1869,7 @@ function remove_kw_args!(ctx, args::SyntaxList)
18701869 for i in 1 : length (args)
18711870 arg = args[i]
18721871 k = kind (arg)
1873- if k == K "= "
1872+ if k == K "kw "
18741873 if isnothing (kws)
18751874 kws = SyntaxList (ctx)
18761875 end
@@ -2278,7 +2277,7 @@ end
22782277function expand_function_arg (ctx, body_stmts, arg, is_last_arg, is_kw, arg_id)
22792278 ex = arg
22802279
2281- if kind (ex) == K "= "
2280+ if kind (ex) == K "kw "
22822281 default = ex[2 ]
22832282 ex = ex[1 ]
22842283 else
@@ -2859,8 +2858,10 @@ function keyword_function_defs(ctx, srcref, callex_srcref, name_str, typevar_nam
28592858 kwcall_body_tail
28602859 ]
28612860 else
2862- 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)
28632863 end
2864+
28642865 main_kwcall_typevars = trim_used_typevars (ctx, kwcall_arg_types, typevar_names, typevar_stmts)
28652866 push! (kwcall_method_defs,
28662867 method_def_expr (ctx, srcref, callex_srcref, kwcall_mtable,
@@ -3234,6 +3235,29 @@ end
32343235
32353236# -------------------------------------------------------------------------------
32363237# 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+
32373261function expand_arrow_arglist (ctx, arglist, arrowname)
32383262 k = kind (arglist)
32393263 if k == K " where"
@@ -3242,23 +3266,9 @@ function expand_arrow_arglist(ctx, arglist, arrowname)
32423266 arglist[2 ]
32433267 ]
32443268 else
3245- # The arglist can sometimes be parsed as a block, or something else, and
3246- # fixing this is extremely awkward when nested inside `where`. See
3247- # https://github.com/JuliaLang/JuliaSyntax.jl/pull/522
3248- if k == K " block"
3249- @chk numchildren (arglist) == 2
3250- arglist = @ast ctx arglist [K " tuple"
3251- arglist[1 ]
3252- [K " parameters" arglist[2 ]]
3253- ]
3254- elseif k != K " tuple"
3255- arglist = @ast ctx arglist [K " tuple"
3256- arglist[1 ]
3257- ]
3258- end
32593269 @ast ctx arglist [K " call"
32603270 arrowname:: K"Placeholder"
3261- children (arglist)...
3271+ children (expand_arrow_args (ctx, arglist) )...
32623272 ]
32633273 end
32643274end
@@ -3281,7 +3291,7 @@ function expand_opaque_closure(ctx, ex)
32813291 func_expr = ex[5 ]
32823292 @chk kind (func_expr) == K " ->"
32833293 @chk numchildren (func_expr) == 2
3284- args = func_expr[1 ]
3294+ args = expand_arrow_args (ctx, func_expr[1 ])
32853295 @chk kind (args) == K " tuple"
32863296 check_no_parameters (ex, args)
32873297
@@ -3830,7 +3840,7 @@ function _rewrite_ctor_new_calls(ctx, ex, struct_name, global_struct_name, ctor_
38303840 )
38313841 end
38323842 # Rewrite a call to new()
3833- 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))
38343844 if ! isnothing (kw_arg_i)
38353845 throw (LoweringError (ex[kw_arg_i], " `new` does not accept keyword arguments" ))
38363846 end
@@ -4148,7 +4158,7 @@ function expand_struct_def(ctx, ex, docs)
41484158 struct_name
41494159 isnothing (docs) ? nothing_ (ctx, ex) : docs[1 ]
41504160 :: K"SourceLocation" (ex)
4151- [K "= "
4161+ [K "kw "
41524162 " field_docs" :: K"Identifier"
41534163 [K " call" " svec" :: K"core" field_docs... ]
41544164 ]
@@ -4195,7 +4205,7 @@ end
41954205function expand_curly (ctx, ex)
41964206 @assert kind (ex) == K " curly"
41974207 check_no_parameters (ex, " unexpected semicolon in type parameter list" )
4198- check_no_assignment (children (ex), " misplace assignment in type parameter list" )
4208+ check_no_assignment (children (ex), " misplaced assignment in type parameter list" )
41994209
42004210 typevar_stmts = SyntaxList (ctx)
42014211 type_args = SyntaxList (ctx)
@@ -4513,9 +4523,9 @@ function expand_forms_2(ctx::DesugaringContext, ex::SyntaxTree, docs=nothing)
45134523 if numchildren (ex) > 1
45144524 throw (LoweringError (ex[end ], " unexpected semicolon in tuple - use `,` to separate tuple elements" ))
45154525 end
4516- 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 ))
45174527 elseif any_assignment (children (ex))
4518- expand_forms_2 (ctx, expand_named_tuple (ctx, ex, children (ex)))
4528+ expand_forms_2 (ctx, expand_named_tuple (ctx, ex, children (ex), true ))
45194529 else
45204530 expand_forms_2 (ctx, @ast ctx ex [K " call"
45214531 " tuple" :: K"core"
@@ -4559,7 +4569,7 @@ function expand_forms_2(ctx::DesugaringContext, ex::SyntaxTree, docs=nothing)
45594569 ctx. mod :: K"Value"
45604570 [K " inert" ex]
45614571 [K " parameters"
4562- [K "= "
4572+ [K "kw "
45634573 " expr_compat_mode" :: K"Identifier"
45644574 ctx. expr_compat_mode:: K"Bool"
45654575 ]
0 commit comments