Skip to content

Commit e57ecd3

Browse files
committed
Parse arr[end] differently from arr[var"end"]
1 parent 86bc433 commit e57ecd3

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

src/expr.jl

+4
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ function _leaf_to_Expr(source, txtbuf, txtbuf_offset, head, srcrange, node)
7676
return k == K"error" ?
7777
Expr(:error) :
7878
Expr(:error, "$(_token_error_descriptions[k]): `$(source[srcrange])`")
79+
elseif k == K"RefBegin"
80+
return Expr(:begin)
81+
elseif k == K"RefEnd"
82+
return Expr(:end)
7983
else
8084
val = isnothing(node) ?
8185
parse_julia_literal(txtbuf, head, srcrange .+ txtbuf_offset) :

src/kinds.jl

+4
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,10 @@ register_kinds!(JuliaSyntax, 0, [
198198
# macro name may not appear as characters in the source: The `@` may be
199199
# detached from the macro name as in `@A.x` (ugh!!), or have a _str or _cmd
200200
# suffix appended.
201+
"BEGIN_REF_IDENTIFIERS"
202+
"RefBegin"
203+
"RefEnd"
204+
"BEGIN_REF_IDENTIFIERS"
201205
"BEGIN_MACRO_NAMES"
202206
"MacroName"
203207
"StringMacroName"

src/parser.jl

+9-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ struct ParseState
1515
space_sensitive::Bool
1616
# Seeing `for` stops parsing macro arguments and makes a generator
1717
for_generator::Bool
18-
# Treat 'end' like a normal symbol instead of a reserved word
18+
# Treat begin/end like special symbols instead of reserved words
1919
end_symbol::Bool
2020
# Treat newline like ordinary whitespace instead of as a potential separator
2121
whitespace_newline::Bool
@@ -3627,6 +3627,14 @@ function parse_atom(ps::ParseState, check_identifiers=true, has_unary_prefix=fal
36273627
elseif check_identifiers && is_closing_token(ps, leading_kind)
36283628
# :(end) ==> (quote-: (error end))
36293629
bump(ps, error="invalid identifier")
3630+
elseif ps.end_symbol && leading_kind in KSet"begin end"
3631+
# https://github.com/JuliaLang/julia/issues/57269
3632+
# Parse a[begin] differently from a[var"begin"]
3633+
if leading_kind == K"begin"
3634+
bump(ps, remap_kind=K"RefBegin")
3635+
elseif leading_kind == K"end"
3636+
bump(ps, remap_kind=K"RefEnd")
3637+
end
36303638
else
36313639
# Remap keywords to identifiers.
36323640
# :end ==> (quote-: end)

0 commit comments

Comments
 (0)