Skip to content

Commit

Permalink
Merge pull request #196 from mlechu/fix-end-parsing
Browse files Browse the repository at this point in the history
Allow for proposed new parsing of `array[end]` in Accessors
  • Loading branch information
jw3126 authored Feb 19, 2025
2 parents 6d0d965 + 226892e commit 133fd94
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions src/sugar.jl
Original file line number Diff line number Diff line change
Expand Up @@ -141,20 +141,19 @@ foldtree(op, init, ex::Expr) =

need_dynamic_optic(ex) =
foldtree(false, ex) do yes, x
yes || x === :end || (x === :begin) || x === :_
yes || x (:end, :begin, Expr(:end), Expr(:begin), :_)
end

replace_underscore(ex, to) = postwalk(x -> x === :_ ? to : x, ex)

function lower_index(collection::Symbol, index, dim)
index = MacroTools.replace(
index, :end,
dim === nothing ? :($(Base.lastindex)($collection)) : :($(Base.lastindex)($collection, $dim))
)
index = MacroTools.replace(
index, :begin,
dim === nothing ? :($(Base.firstindex)($collection)) : :($(Base.firstindex)($collection, $dim))
)
lasti = dim === nothing ? :($(Base.lastindex)($collection)) : :($(Base.lastindex)($collection, $dim))
firsti = dim === nothing ? :($(Base.firstindex)($collection)) : :($(Base.firstindex)($collection, $dim))
index = MacroTools.replace(index, :end, lasti)
index = MacroTools.replace(index, :begin, firsti)
# New syntax for begin/end in indexing expression, see https://github.com/JuliaLang/julia/pull/57368
index = MacroTools.replace(index, Expr(:end), lasti)
index = MacroTools.replace(index, Expr(:begin), firsti)
end

_secondarg(_, x) = x
Expand Down

0 comments on commit 133fd94

Please sign in to comment.