Skip to content

Commit e5c7603

Browse files
authored
Stricter parsing of exception names in catch $excname (#106)
1 parent 3906436 commit e5c7603

File tree

3 files changed

+8
-2
lines changed

3 files changed

+8
-2
lines changed

src/hooks.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ function _core_parser_hook(code, filename, lineno, offset, options)
157157
return Core.svec(nothing, last_byte(stream))
158158
end
159159
end
160-
JuliaSyntax.parse(stream; rule=rule)
160+
parse(stream; rule=rule)
161161
if rule === :statement
162162
bump_trivia(stream)
163163
end

src/parser.jl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2154,7 +2154,12 @@ function parse_catch(ps::ParseState)
21542154
else
21552155
# try x catch e y end ==> (try (block x) e (block y) false false)
21562156
# try x catch $e y end ==> (try (block x) ($ e) (block y) false false)
2157-
parse_identifier_or_interpolate(ps)
2157+
mark = position(ps)
2158+
parse_eq_star(ps)
2159+
if !(peek_behind(ps).kind in KSet"Identifier $")
2160+
# try x catch e+3 y end ==> (try (block x) (error (call-i e + 3)) (block y) false false)
2161+
emit(ps, mark, K"error", error="a variable name is expected after `catch`")
2162+
end
21582163
end
21592164
parse_block(ps)
21602165
end

test/parser.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,7 @@ tests = [
475475
"try x catch \n y end" => "(try (block x) false (block y) false false)"
476476
"try x catch e y end" => "(try (block x) e (block y) false false)"
477477
"try x catch \$e y end" => "(try (block x) (\$ e) (block y) false false)"
478+
"try x catch e+3 y end" => "(try (block x) (error (call-i e + 3)) (block y) false false)"
478479
"try x finally y end" => "(try (block x) false false false (block y))"
479480
# v1.8 only
480481
((v=v"1.8",), "try catch ; else end") => "(try (block) false (block) (block) false)"

0 commit comments

Comments
 (0)