Skip to content

Commit 384f745

Browse files
authored
Remove TRY_CATCH_FINALLY_FLAG (#123)
A flag is a waste for this; we can just use a different kind for this horrible edge case.
1 parent 5f158b1 commit 384f745

File tree

5 files changed

+26
-15
lines changed

5 files changed

+26
-15
lines changed

src/expr.jl

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,19 +150,19 @@ function _to_expr(node::SyntaxNode; iteration_spec=false, need_linenodes=true,
150150
pushfirst!(args, args[end])
151151
pop!(args)
152152
end
153-
elseif headsym == :try
153+
elseif headsym in (:try, :try_finally_catch)
154154
# Try children in source order:
155155
# try_block catch_var catch_block else_block finally_block
156156
# Expr ordering:
157157
# try_block catch_var catch_block [finally_block] [else_block]
158158
catch_ = nothing
159-
if has_flags(node, TRY_CATCH_AFTER_FINALLY_FLAG)
159+
if headsym === :try_finally_catch
160160
catch_ = pop!(args)
161161
catch_var = pop!(args)
162162
end
163163
finally_ = pop!(args)
164164
else_ = pop!(args)
165-
if has_flags(node, TRY_CATCH_AFTER_FINALLY_FLAG)
165+
if headsym === :try_finally_catch
166166
pop!(args)
167167
pop!(args)
168168
push!(args, catch_var)
@@ -176,6 +176,7 @@ function _to_expr(node::SyntaxNode; iteration_spec=false, need_linenodes=true,
176176
if else_ !== false
177177
push!(args, else_)
178178
end
179+
headsym = :try
179180
elseif headsym == :filter
180181
pushfirst!(args, last(args))
181182
pop!(args)

src/kinds.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -897,6 +897,8 @@ const _kind_names =
897897
"flatten"
898898
"comprehension"
899899
"typed_comprehension"
900+
# Special kind for compatibility with the ever-ugly try-finally-catch ordering
901+
"try_finally_catch"
900902
"END_SYNTAX_KINDS"
901903
]
902904

src/parse_stream.jl

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ const DOTOP_FLAG = RawFlags(1<<2)
1515
const TRIPLE_STRING_FLAG = RawFlags(1<<3)
1616
# Set when a string or identifier needs "raw string" unescaping
1717
const RAW_STRING_FLAG = RawFlags(1<<4)
18-
# try-finally-catch
19-
const TRY_CATCH_AFTER_FINALLY_FLAG = RawFlags(1<<5)
2018
# Record whether operator has a suffix
2119
const SUFFIXED_FLAG = RawFlags(1<<6)
2220

@@ -75,7 +73,6 @@ function untokenize(head::SyntaxHead; unique=true, include_flag_suff=true)
7573
is_infix(head) && (str = str*"i")
7674
has_flags(head, TRIPLE_STRING_FLAG) && (str = str*"s")
7775
has_flags(head, RAW_STRING_FLAG) && (str = str*"r")
78-
has_flags(head, TRY_CATCH_AFTER_FINALLY_FLAG) && (str = str*"f")
7976
is_suffixed(head) && (str = str*"S")
8077
n = numeric_flags(head)
8178
n != 0 && (str = str*string(n))

src/parser.jl

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2085,6 +2085,7 @@ end
20852085
#
20862086
# flisp: embedded in parse_resword
20872087
function parse_try(ps)
2088+
out_kind = K"try"
20882089
mark = position(ps)
20892090
bump(ps, TRIVIA_FLAG)
20902091
parse_block(ps)
@@ -2132,15 +2133,15 @@ function parse_try(ps)
21322133
# in which these blocks execute.
21332134
bump_trivia(ps)
21342135
if !has_catch && peek(ps) == K"catch"
2135-
# try x finally y catch e z end ==> (try-f (block x) false false false (block y) e (block z))
2136-
flags |= TRY_CATCH_AFTER_FINALLY_FLAG
2136+
# try x finally y catch e z end ==> (try_finally_catch (block x) false false false (block y) e (block z))
2137+
out_kind = K"try_finally_catch"
21372138
m = position(ps)
21382139
parse_catch(ps)
21392140
emit_diagnostic(ps, m, position(ps),
21402141
warning="`catch` after `finally` will execute out of order")
21412142
end
21422143
bump_closing_token(ps, K"end")
2143-
emit(ps, mark, K"try", flags)
2144+
emit(ps, mark, out_kind, flags)
21442145
end
21452146

21462147
function parse_catch(ps::ParseState)

test/parser.jl

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
1-
function test_parse(production, code; v=v"1.6")
1+
function test_parse(production, code; v=v"1.6", expr=false)
22
stream = ParseStream(code, version=v)
33
production(ParseState(stream))
44
t = build_tree(GreenNode, stream, wrap_toplevel_as_kind=K"None")
55
source = SourceFile(code)
66
s = SyntaxNode(source, t)
7-
if kind(s) == K"None"
8-
join([sprint(show, MIME("text/x.sexpression"), c) for c in children(s)], ' ')
7+
if expr
8+
JuliaSyntax.remove_linenums!(Expr(s))
99
else
10-
sprint(show, MIME("text/x.sexpression"), s)
10+
if kind(s) == K"None"
11+
join([sprint(show, MIME("text/x.sexpression"), c) for c in children(s)], ' ')
12+
else
13+
sprint(show, MIME("text/x.sexpression"), s)
14+
end
1115
end
1216
end
1317

@@ -482,7 +486,8 @@ tests = [
482486
((v=v"1.8",), "try else end") => "(try (block) false false (error (block)) false)"
483487
((v=v"1.7",), "try catch ; else end") => "(try (block) false (block) (error (block)) false)"
484488
# finally before catch :-(
485-
"try x finally y catch e z end" => "(try-f (block x) false false false (block y) e (block z))"
489+
"try x finally y catch e z end" => "(try_finally_catch (block x) false false false (block y) e (block z))" =>
490+
Expr(:try, Expr(:block, :x), :e, Expr(:block, :z), Expr(:block, :y))
486491
],
487492
JuliaSyntax.parse_imports => [
488493
"import A as B: x" => "(import (: (error (as (. A) B)) (. x)))"
@@ -816,7 +821,12 @@ broken_tests = [
816821
else
817822
opts = NamedTuple()
818823
end
819-
@test test_parse(production, input; opts...) == output
824+
if output isa Pair
825+
@test test_parse(production, input; opts...) == output[1]
826+
@test test_parse(production, input; opts..., expr=true) == output[2]
827+
else
828+
@test test_parse(production, input; opts...) == output
829+
end
820830
end
821831
end
822832
@testset "Broken $production" for (production, test_specs) in broken_tests

0 commit comments

Comments
 (0)