Skip to content

Parse arr[end] differently from arr[var"end"] #537

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/expr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@
return k == K"error" ?
Expr(:error) :
Expr(:error, "$(_token_error_descriptions[k]): `$(source[srcrange])`")
elseif k == K"begin" # begin/end as firstindex/lastindex
return Expr(:begin)

Check warning on line 80 in src/expr.jl

View check run for this annotation

Codecov / codecov/patch

src/expr.jl#L80

Added line #L80 was not covered by tests
elseif k == K"end"
return Expr(:end)

Check warning on line 82 in src/expr.jl

View check run for this annotation

Codecov / codecov/patch

src/expr.jl#L82

Added line #L82 was not covered by tests
else
val = isnothing(node) ?
parse_julia_literal(txtbuf, head, srcrange .+ txtbuf_offset) :
Expand Down
6 changes: 5 additions & 1 deletion src/parser.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ struct ParseState
space_sensitive::Bool
# Seeing `for` stops parsing macro arguments and makes a generator
for_generator::Bool
# Treat 'end' like a normal symbol instead of a reserved word
# Treat begin/end like special symbols instead of reserved words
end_symbol::Bool
# Treat newline like ordinary whitespace instead of as a potential separator
whitespace_newline::Bool
Expand Down Expand Up @@ -3627,6 +3627,10 @@ function parse_atom(ps::ParseState, check_identifiers=true, has_unary_prefix=fal
elseif check_identifiers && is_closing_token(ps, leading_kind)
# :(end) ==> (quote-: (error end))
bump(ps, error="invalid identifier")
elseif ps.end_symbol && leading_kind in KSet"begin end" && ps.stream.version >= (1, 13)
# https://github.com/JuliaLang/julia/issues/57269
# Parse a[begin] differently from a[var"begin"]
bump(ps)
else
# Remap keywords to identifiers.
# :end ==> (quote-: end)
Expand Down
13 changes: 12 additions & 1 deletion test/parser.jl
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,9 @@ tests = [
"(a=1)[]" => "(ref (parens (= a 1)))"
"a[end]" => "(ref a end)"
"a[begin]" => "(ref a begin)"
"a[var\"end\"]" => "(ref a (var end))"
"a[var\"begin\"]" => "(ref a (var begin))"
"a[var\"test\"]" => "(ref a (var test))"
"a[:(end)]" => "(typed_hcat a (quote-: (parens (error-t))) (error-t))"
"T[x y]" => "(typed_hcat T x y)"
"T[x ; y]" => "(typed_vcat T x y)"
Expand Down Expand Up @@ -1153,11 +1156,19 @@ parsestmt_with_kind_tests = [
"a >>= b" => "(op= a::Identifier >>::Identifier b::Identifier)"
":+=" => "(quote-: +=::op=)"
":.+=" => "(quote-: (. +=::op=))"
((v=v"1.13",), "a[begin]") => "(ref a::Identifier begin::begin)"
((v=v"1.13",), "a[end]") => "(ref a::Identifier end::end)"
]

@testset "parser `Kind` remapping" begin
@testset "$(repr(input))" for (input, output) in parsestmt_with_kind_tests
input = ((show_kind=true,), input)
if !(input isa AbstractString)
opts, input_s = input
else
opts = NamedTuple()
input_s = input
end
input = ((show_kind=true, opts...), input_s)
test_parse(JuliaSyntax.parse_stmts, input, output)
end
end
Expand Down
Loading