From 19aedfdcf66683a5ee3a015721e4db0c5d13cdbf Mon Sep 17 00:00:00 2001 From: Em Chu Date: Tue, 18 Feb 2025 12:22:19 -0800 Subject: [PATCH 1/3] Allow for proposed new parsing of `array[end]` in Accessors --- src/sugar.jl | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/sugar.jl b/src/sugar.jl index 041a38a0..7de70161 100644 --- a/src/sugar.jl +++ b/src/sugar.jl @@ -141,7 +141,7 @@ foldtree(op, init, ex::Expr) = need_dynamic_optic(ex) = foldtree(false, ex) do yes, x - yes || x === :end || (x === :begin) || x === :_ + yes || x === :end || x === :begin || x == Expr(:end) || x == Expr(:begin) || x === :_ end replace_underscore(ex, to) = postwalk(x -> x === :_ ? to : x, ex) @@ -155,6 +155,15 @@ function lower_index(collection::Symbol, index, dim) index, :begin, dim === nothing ? :($(Base.firstindex)($collection)) : :($(Base.firstindex)($collection, $dim)) ) + # New syntax for begin/end in indexing expression, see https://github.com/JuliaLang/julia/pull/57368 + index = MacroTools.replace( + index, Expr(:end), + dim === nothing ? :($(Base.lastindex)($collection)) : :($(Base.lastindex)($collection, $dim)) + ) + index = MacroTools.replace( + index, Expr(:begin), + dim === nothing ? :($(Base.firstindex)($collection)) : :($(Base.firstindex)($collection, $dim)) + ) end _secondarg(_, x) = x From 782a96536b031b6f374200e6f6489cf512168c29 Mon Sep 17 00:00:00 2001 From: Alexander Plavin Date: Tue, 18 Feb 2025 20:22:06 -0500 Subject: [PATCH 2/3] Update src/sugar.jl --- src/sugar.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sugar.jl b/src/sugar.jl index 7de70161..439912b4 100644 --- a/src/sugar.jl +++ b/src/sugar.jl @@ -141,7 +141,7 @@ foldtree(op, init, ex::Expr) = need_dynamic_optic(ex) = foldtree(false, ex) do yes, x - yes || x === :end || x === :begin || x == Expr(:end) || x == Expr(:begin) || x === :_ + yes || x ∈ (:end, :begin, Expr(:end), Expr(:begin), :_) end replace_underscore(ex, to) = postwalk(x -> x === :_ ? to : x, ex) From 226892e21cce9fb7280f6d5cae51395f743b2b66 Mon Sep 17 00:00:00 2001 From: Em Chu Date: Tue, 18 Feb 2025 18:12:24 -0800 Subject: [PATCH 3/3] Make lower_index nicer --- src/sugar.jl | 22 ++++++---------------- 1 file changed, 6 insertions(+), 16 deletions(-) diff --git a/src/sugar.jl b/src/sugar.jl index 439912b4..4919ea95 100644 --- a/src/sugar.jl +++ b/src/sugar.jl @@ -147,23 +147,13 @@ need_dynamic_optic(ex) = 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), - dim === nothing ? :($(Base.lastindex)($collection)) : :($(Base.lastindex)($collection, $dim)) - ) - index = MacroTools.replace( - index, Expr(:begin), - dim === nothing ? :($(Base.firstindex)($collection)) : :($(Base.firstindex)($collection, $dim)) - ) + index = MacroTools.replace(index, Expr(:end), lasti) + index = MacroTools.replace(index, Expr(:begin), firsti) end _secondarg(_, x) = x