Skip to content

Commit 1b2d9af

Browse files
committed
Switch to using ExprTools
1 parent 9a5963f commit 1b2d9af

File tree

4 files changed

+5
-123
lines changed

4 files changed

+5
-123
lines changed

Project.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@ uuid = "1914dd2f-81c6-5fcd-8719-6d5c9610ff09"
33
version = "0.5.5"
44

55
[deps]
6+
ExprTools = "e2ba6199-217a-4e67-a87a-7c52f15ade04"
67
Markdown = "d6f4376e-aef5-505a-96c1-9c027394607a"
78
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
89

910
[compat]
11+
ExprTools = "0.1"
1012
julia = "1"
1113

1214
[extras]

src/MacroTools.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
module MacroTools
22

33
using Markdown, Random
4+
using ExprTools: splitdef, combinedef
5+
46
export @match, @capture
57

68
include("match/match.jl")

src/utils.jl

Lines changed: 0 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -259,127 +259,6 @@ function gatherwheres(ex)
259259
end
260260
end
261261

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-
383262
"""
384263
combinearg(arg_name, arg_type, is_splat, default)
385264

test/split.jl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using MacroTools: splitstructdef, combinestructdef
22

3-
macro nothing_macro()
4-
end
3+
macro nothing_macro() end
54
@test @expand(@nothing_macro) === nothing
65

76
macro splitcombine(fundef) # should be a no-op

0 commit comments

Comments
 (0)