Skip to content

Commit

Permalink
Remove MacroTools (#92)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>

---------

Co-authored-by: Xianda Sun <[email protected]>
Co-authored-by: Xianda Sun <[email protected]>
Co-authored-by: David Widmann <[email protected]>
  • Loading branch information
4 people authored Apr 2, 2024
1 parent f3fbce0 commit cc9c8ed
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 27 deletions.
1 change: 0 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
34 changes: 9 additions & 25 deletions src/varname.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using Accessors
using Accessors: ComposedOptic, PropertyLens, IndexLens, DynamicIndexLens
using MacroTools

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

Expand Down Expand Up @@ -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
Expand All @@ -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(
Expand Down
1 change: 0 additions & 1 deletion test/varname.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down

2 comments on commit cc9c8ed

@sunxd3
Copy link
Member

@sunxd3 sunxd3 commented on cc9c8ed Apr 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request updated: JuliaRegistries/General/103811

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.8.0 -m "<description of version>" cc9c8edb0163aa9e57b5934caf1ff347d9962b2b
git push origin v0.8.0

Please sign in to comment.