Skip to content

Commit 2bbf924

Browse files
committed
require else after catch
1 parent 7c2a00f commit 2bbf924

File tree

2 files changed

+10
-16
lines changed

2 files changed

+10
-16
lines changed

src/julia-parser.scm

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1515,7 +1515,7 @@
15151515
(cond
15161516
((eq? nxt 'end)
15171517
(list* 'try try-block (or catchv '(false))
1518-
(or catchb (if (or finalb elseb) '(false) (error "try without catch, else or finally")))
1518+
(or catchb (if finalb '(false) (error "try without catch or finally")))
15191519
(cond (elseb (list (or finalb '(false)) elseb))
15201520
(finalb (list finalb))
15211521
(else '()))))
@@ -1561,6 +1561,8 @@
15611561
elseb)))
15621562
((and (eq? nxt 'else)
15631563
(not elseb))
1564+
(if (or (not catchb) finalb)
1565+
(error "else inside try block needs to be immediately after catch"))
15641566
(let ((eb (if (eq? (require-token s) '(catch finally))
15651567
'(block)
15661568
(parse-block s))))

test/syntax.jl

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2983,9 +2983,15 @@ end
29832983
@test fails(error)
29842984
@test !fails(() -> 1 + 2)
29852985

2986+
@test_throws ParseError Meta.parse("try foo() else bar() end")
2987+
@test_throws ParseError Meta.parse("try foo() else bar() catch; baz() end")
2988+
@test_throws ParseError Meta.parse("try foo() catch; baz() finally foobar() else bar() end")
2989+
@test_throws ParseError Meta.parse("try foo() finally foobar() else bar() catch; baz() end")
2990+
29862991
err = try
29872992
try
29882993
1 + 2
2994+
catch
29892995
else
29902996
error("foo")
29912997
end
@@ -2998,6 +3004,7 @@ end
29983004
err = try
29993005
try
30003006
1 + 2
3007+
catch
30013008
else
30023009
error("foo")
30033010
finally
@@ -3009,21 +3016,6 @@ end
30093016
@test err == ErrorException("foo")
30103017
@test x == 1
30113018

3012-
x = 0
3013-
err = try
3014-
try
3015-
1 + 2
3016-
else
3017-
3 + 4
3018-
finally
3019-
x += 1
3020-
end
3021-
catch e
3022-
e
3023-
end
3024-
@test err == 3 + 4
3025-
@test x == 1
3026-
30273019
x = 0
30283020
err = try
30293021
try

0 commit comments

Comments
 (0)