Skip to content

Commit cc9c8ed

Browse files
yebaisunxd3devmotion
authored
Remove MacroTools (#92)
* Update varname.jl * Update Project.toml * use `MacroTools` from `Accessors` * stop using `MacroTools.@capture` * rename test * remove support for composite lens * Apply suggestions from code review Co-authored-by: David Widmann <devmotion@users.noreply.github.com> --------- Co-authored-by: Xianda Sun <sunxdt@gmail.com> Co-authored-by: Xianda Sun <5433119+sunxd3@users.noreply.github.com> Co-authored-by: David Widmann <devmotion@users.noreply.github.com>
1 parent f3fbce0 commit cc9c8ed

File tree

3 files changed

+9
-27
lines changed

3 files changed

+9
-27
lines changed

Project.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ version = "0.8.0"
99
AbstractMCMC = "80f14c24-f653-4e6a-9b94-39d6b0f70001"
1010
Accessors = "7d9f7c33-5ae7-4f3b-8dc6-eff91059b697"
1111
DensityInterface = "b429d917-457f-4dbc-8f4c-0cc954292b1d"
12-
MacroTools = "1914dd2f-81c6-5fcd-8719-6d5c9610ff09"
1312
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
1413

1514
[compat]

src/varname.jl

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using Accessors
22
using Accessors: ComposedOptic, PropertyLens, IndexLens, DynamicIndexLens
3-
using MacroTools
43

54
const ALLOWED_OPTICS = Union{typeof(identity),PropertyLens,IndexLens,ComposedOptic}
65

@@ -671,30 +670,13 @@ function _parse_obj_optic(ex)
671670
obj, optic
672671
end
673672

674-
# Accessors doesn't have the same support for interpolation, so copy and modify Setfield's parsing functions
675-
is_interpolation(x) = x isa Expr && x.head == :$
676-
677-
function _parse_obj_optics_composite(lensexprs::Vector)
678-
if isempty(lensexprs)
679-
return esc(:_), ()
680-
else
681-
obj, outermostlens = _parse_obj_optics(lensexprs[1])
682-
innerlenses = map(lensexprs[2:end]) do innerex
683-
o, lens = _parse_obj_optics(innerex)
684-
@assert o == esc(:_)
685-
lens
686-
end
687-
return obj, (outermostlens, innerlenses...)
688-
end
689-
end
690-
673+
# Accessors doesn't have the same support for interpolation
674+
# so this function is copied and altered from `Setfield._parse_obj_lens`
691675
function _parse_obj_optics(ex)
692-
if @capture(ex, (opticsexprs__))
693-
return _parse_obj_optics_composite(opticsexprs)
694-
elseif is_interpolation(ex)
695-
@assert length(ex.args) == 1
676+
if Meta.isexpr(ex, :$, 1)
696677
return esc(:_), (esc(ex.args[1]),)
697-
elseif @capture(ex, front_[indices__])
678+
elseif Meta.isexpr(ex, :ref) && !isempty(ex.args)
679+
front, indices... = ex.args
698680
obj, frontoptics = _parse_obj_optics(front)
699681
if any(Accessors.need_dynamic_optic, indices)
700682
@gensym collection
@@ -706,11 +688,13 @@ function _parse_obj_optics(ex)
706688
index = esc(Expr(:tuple, indices...))
707689
optics = :($(Accessors.IndexLens)($index))
708690
end
709-
elseif @capture(ex, front_.property_)
691+
elseif Meta.isexpr(ex, :., 2)
692+
front = ex.args[1]
693+
property = ex.args[2].value # ex.args[2] is a QuoteNode
710694
obj, frontoptics = _parse_obj_optics(front)
711695
if property isa Union{Symbol,String}
712696
optics = :($(Accessors.PropertyLens){$(QuoteNode(property))}())
713-
elseif is_interpolation(property)
697+
elseif Meta.isexpr(property, :$, 1)
714698
optics = :($(Accessors.PropertyLens){$(esc(property.args[1]))}())
715699
else
716700
throw(ArgumentError(

test/varname.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@ end
8989
@test collect(get(B, @varname(B[1, :], true))) == collect(get(B, @varname(B[1, -4:5])))
9090

9191
end
92-
9392
@testset "type stability" begin
9493
@inferred VarName{:a}()
9594
@inferred VarName{:a}(IndexLens(1))

0 commit comments

Comments
 (0)