Skip to content
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

fix parsing of long chars #286

Merged
merged 1 commit into from
Jun 18, 2021
Merged
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
2 changes: 1 addition & 1 deletion src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -657,7 +657,7 @@ function valid_escaped_seq(s::AbstractString)
if popfirst!(a) == '\\'
c = popfirst!(a)
if c === 'x' || c === 'u' || c === 'U'
maxiter = c === 'x' ? 2 : c === 'u' ? 4 : 5
maxiter = c === 'x' ? 2 : c === 'u' ? 4 : 8
0 < length(a) <= maxiter || return false
n = 0
while !isempty(a)
Expand Down
2 changes: 2 additions & 0 deletions test/parser.jl
Original file line number Diff line number Diff line change
Expand Up @@ -994,6 +994,7 @@ end""" |> test_expr
@test test_expr(raw"'\u2222'")
@test test_expr(raw"'\U2222'")
@test test_expr(raw"'\U22222'")
@test test_expr(raw"'\U00000001'")

@test CSTParser.parse(raw"'\200'").head == :errortoken
@test CSTParser.parse(raw"'\300'").head == :errortoken
Expand All @@ -1009,6 +1010,7 @@ end""" |> test_expr
@test CSTParser.parse(raw"'\u222ää'").head == :errortoken
@test CSTParser.parse(raw"'\x222ää'").head == :errortoken
@test CSTParser.parse(raw"'\U222ää'").head == :errortoken
@test CSTParser.parse(raw"'\U10000001'").head == :errortoken

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is actually a parser error in Julia's own parser:

julia> '\U10000001'
ERROR: syntax: invalid escape sequence

The largest valid code point is U+10FFFF.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that's basically what this test tests:

julia> CSTParser.parse(raw"'\U10000001'")
  1:12  errortoken( CSTParser.InvalidChar)
  1:12   CHAR: '\'

julia> CSTParser.parse(raw"'\U00000001'")
  1:12  CHAR: '\U00000001'

Could've chosen a less arbitrary codepoint though, I suppose.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't it make sense to test the two values on either side? I.e. test that '\U10FFFF' is valid and that '\U110000' is invalid?

for c in rand(Char, 1000)
@test test_expr(string("'", c, "'"))
end
Expand Down