@@ -259,127 +259,6 @@ function gatherwheres(ex)
259
259
end
260
260
end
261
261
262
- """ splitdef(fdef)
263
-
264
- Match any function definition
265
-
266
- ```julia
267
- function name{params}(args; kwargs)::rtype where {whereparams}
268
- body
269
- end
270
- ```
271
-
272
- and return `Dict(:name=>..., :args=>..., etc.)`. The definition can be rebuilt by
273
- calling `MacroTools.combinedef(dict)`, or explicitly with
274
-
275
- ```
276
- rtype = get(dict, :rtype, :Any)
277
- all_params = [get(dict, :params, [])..., get(dict, :whereparams, [])...]
278
- :(function \$ (dict[:name]){\$ (all_params...)}(\$ (dict[:args]...);
279
- \$ (dict[:kwargs]...))::\$ rtype
280
- \$ (dict[:body])
281
- end)
282
- ```
283
- """
284
- function splitdef (fdef)
285
- error_msg = " Not a function definition: $(repr (fdef)) "
286
- @assert (@capture (longdef1 (fdef),
287
- function (fcall_ | fcall_) body_ end ),
288
- " Not a function definition: $fdef " )
289
- fcall_nowhere, whereparams = gatherwheres (fcall)
290
- func = args = kwargs = rtype = nothing
291
- if @capture (fcall_nowhere, ((func_ (args__; kwargs__)) |
292
- (func_ (args__; kwargs__):: rtype_ ) |
293
- (func_ (args__)) |
294
- (func_ (args__):: rtype_ )))
295
- elseif isexpr (fcall_nowhere, :tuple )
296
- if length (fcall_nowhere. args) > 1 && isexpr (fcall_nowhere. args[1 ], :parameters )
297
- args = fcall_nowhere. args[2 : end ]
298
- kwargs = fcall_nowhere. args[1 ]. args
299
- else
300
- args = fcall_nowhere. args
301
- end
302
- elseif isexpr (fcall_nowhere, :(:: ))
303
- args = Any[fcall_nowhere]
304
- else
305
- throw (ArgumentError (error_msg))
306
- end
307
- if func != = nothing
308
- @assert (@capture (func, (fname_{params__} | fname_)), error_msg)
309
- di = Dict (:name => fname, :args => args,
310
- :kwargs => (kwargs=== nothing ? [] : kwargs), :body => body)
311
- else
312
- params = nothing
313
- di = Dict (:args => args, :kwargs => (kwargs=== nothing ? [] : kwargs), :body => body)
314
- end
315
- if rtype != = nothing ; di[:rtype ] = rtype end
316
- if whereparams != = nothing ; di[:whereparams ] = whereparams end
317
- if params != = nothing ; di[:params ] = params end
318
- di
319
- end
320
-
321
- """
322
- combinedef(dict::Dict)
323
-
324
- `combinedef` is the inverse of `splitdef`. It takes a splitdef-like Dict
325
- and returns a function definition. """
326
- function combinedef (dict:: Dict )
327
- rtype = get (dict, :rtype , nothing )
328
- params = get (dict, :params , [])
329
- wparams = get (dict, :whereparams , [])
330
- body = block (dict[:body ])
331
- if haskey (dict, :name )
332
- name = dict[:name ]
333
- name_param = isempty (params) ? name : :($ name{$ (params... )})
334
- # We need the `if` to handle parametric inner/outer constructors like
335
- # SomeType{X}(x::X) where X = SomeType{X}(x, x+2)
336
- if isempty (wparams)
337
- if rtype== nothing
338
- @q (function $name_param ($ (dict[:args ]. .. );
339
- $ (dict[:kwargs ]. .. ))
340
- $ (body. args... )
341
- end )
342
- else
343
- @q (function $name_param ($ (dict[:args ]. .. );
344
- $ (dict[:kwargs ]. .. )):: $rtype
345
- $ (body. args... )
346
- end )
347
- end
348
- else
349
- if rtype== nothing
350
- @q (function $name_param ($ (dict[:args ]. .. );
351
- $ (dict[:kwargs ]. .. )) where {$ (wparams... )}
352
- $ (body. args... )
353
- end )
354
- else
355
- @q (function $name_param ($ (dict[:args ]. .. );
356
- $ (dict[:kwargs ]. .. )):: $rtype where {$ (wparams... )}
357
- $ (body. args... )
358
- end )
359
- end
360
- end
361
- else
362
- if isempty (dict[:kwargs ])
363
- arg = :($ (dict[:args ]. .. ),)
364
- else
365
- arg = Expr (:tuple , Expr (:parameters , dict[:kwargs ]. .. ), dict[:args ]. .. )
366
- end
367
- if isempty (wparams)
368
- if rtype== nothing
369
- @q ($ arg -> $ body)
370
- else
371
- @q (($ arg:: $rtype ) -> $ body)
372
- end
373
- else
374
- if rtype== nothing
375
- @q (($ arg where {$ (wparams... )}) -> $ body)
376
- else
377
- @q (($ arg:: $rtype where {$ (wparams... )}) -> $ body)
378
- end
379
- end
380
- end
381
- end
382
-
383
262
"""
384
263
combinearg(arg_name, arg_type, is_splat, default)
385
264
0 commit comments