From cc9c8edb0163aa9e57b5934caf1ff347d9962b2b Mon Sep 17 00:00:00 2001 From: Hong Ge <3279477+yebai@users.noreply.github.com> Date: Tue, 2 Apr 2024 12:25:53 +0100 Subject: [PATCH] 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 --------- Co-authored-by: Xianda Sun Co-authored-by: Xianda Sun <5433119+sunxd3@users.noreply.github.com> Co-authored-by: David Widmann --- Project.toml | 1 - src/varname.jl | 34 +++++++++------------------------- test/varname.jl | 1 - 3 files changed, 9 insertions(+), 27 deletions(-) diff --git a/Project.toml b/Project.toml index becfdbe..d5fe1a2 100644 --- a/Project.toml +++ b/Project.toml @@ -9,7 +9,6 @@ version = "0.8.0" AbstractMCMC = "80f14c24-f653-4e6a-9b94-39d6b0f70001" Accessors = "7d9f7c33-5ae7-4f3b-8dc6-eff91059b697" DensityInterface = "b429d917-457f-4dbc-8f4c-0cc954292b1d" -MacroTools = "1914dd2f-81c6-5fcd-8719-6d5c9610ff09" Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" [compat] diff --git a/src/varname.jl b/src/varname.jl index a3ca1dc..adcd568 100644 --- a/src/varname.jl +++ b/src/varname.jl @@ -1,6 +1,5 @@ using Accessors using Accessors: ComposedOptic, PropertyLens, IndexLens, DynamicIndexLens -using MacroTools const ALLOWED_OPTICS = Union{typeof(identity),PropertyLens,IndexLens,ComposedOptic} @@ -671,30 +670,13 @@ function _parse_obj_optic(ex) obj, optic end -# Accessors doesn't have the same support for interpolation, so copy and modify Setfield's parsing functions -is_interpolation(x) = x isa Expr && x.head == :$ - -function _parse_obj_optics_composite(lensexprs::Vector) - if isempty(lensexprs) - return esc(:_), () - else - obj, outermostlens = _parse_obj_optics(lensexprs[1]) - innerlenses = map(lensexprs[2:end]) do innerex - o, lens = _parse_obj_optics(innerex) - @assert o == esc(:_) - lens - end - return obj, (outermostlens, innerlenses...) - end -end - +# Accessors doesn't have the same support for interpolation +# so this function is copied and altered from `Setfield._parse_obj_lens` function _parse_obj_optics(ex) - if @capture(ex, ∘(opticsexprs__)) - return _parse_obj_optics_composite(opticsexprs) - elseif is_interpolation(ex) - @assert length(ex.args) == 1 + if Meta.isexpr(ex, :$, 1) return esc(:_), (esc(ex.args[1]),) - elseif @capture(ex, front_[indices__]) + elseif Meta.isexpr(ex, :ref) && !isempty(ex.args) + front, indices... = ex.args obj, frontoptics = _parse_obj_optics(front) if any(Accessors.need_dynamic_optic, indices) @gensym collection @@ -706,11 +688,13 @@ function _parse_obj_optics(ex) index = esc(Expr(:tuple, indices...)) optics = :($(Accessors.IndexLens)($index)) end - elseif @capture(ex, front_.property_) + elseif Meta.isexpr(ex, :., 2) + front = ex.args[1] + property = ex.args[2].value # ex.args[2] is a QuoteNode obj, frontoptics = _parse_obj_optics(front) if property isa Union{Symbol,String} optics = :($(Accessors.PropertyLens){$(QuoteNode(property))}()) - elseif is_interpolation(property) + elseif Meta.isexpr(property, :$, 1) optics = :($(Accessors.PropertyLens){$(esc(property.args[1]))}()) else throw(ArgumentError( diff --git a/test/varname.jl b/test/varname.jl index d5cb4f7..b117f0e 100644 --- a/test/varname.jl +++ b/test/varname.jl @@ -89,7 +89,6 @@ end @test collect(get(B, @varname(B[1, :], true))) == collect(get(B, @varname(B[1, -4:5]))) end - @testset "type stability" begin @inferred VarName{:a}() @inferred VarName{:a}(IndexLens(1))