Skip to content

Commit 133fd94

Browse files
authored
Merge pull request #196 from mlechu/fix-end-parsing
Allow for proposed new parsing of `array[end]` in Accessors
2 parents 6d0d965 + 226892e commit 133fd94

File tree

1 file changed

+8
-9
lines changed

1 file changed

+8
-9
lines changed

src/sugar.jl

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -141,20 +141,19 @@ foldtree(op, init, ex::Expr) =
141141

142142
need_dynamic_optic(ex) =
143143
foldtree(false, ex) do yes, x
144-
yes || x === :end || (x === :begin) || x === :_
144+
yes || x (:end, :begin, Expr(:end), Expr(:begin), :_)
145145
end
146146

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

149149
function lower_index(collection::Symbol, index, dim)
150-
index = MacroTools.replace(
151-
index, :end,
152-
dim === nothing ? :($(Base.lastindex)($collection)) : :($(Base.lastindex)($collection, $dim))
153-
)
154-
index = MacroTools.replace(
155-
index, :begin,
156-
dim === nothing ? :($(Base.firstindex)($collection)) : :($(Base.firstindex)($collection, $dim))
157-
)
150+
lasti = dim === nothing ? :($(Base.lastindex)($collection)) : :($(Base.lastindex)($collection, $dim))
151+
firsti = dim === nothing ? :($(Base.firstindex)($collection)) : :($(Base.firstindex)($collection, $dim))
152+
index = MacroTools.replace(index, :end, lasti)
153+
index = MacroTools.replace(index, :begin, firsti)
154+
# New syntax for begin/end in indexing expression, see https://github.com/JuliaLang/julia/pull/57368
155+
index = MacroTools.replace(index, Expr(:end), lasti)
156+
index = MacroTools.replace(index, Expr(:begin), firsti)
158157
end
159158

160159
_secondarg(_, x) = x

0 commit comments

Comments
 (0)