Skip to content

Commit 8a47045

Browse files
authored
Also disallow bare generators in interpolation syntax (#307)
This should be the only other case which needs to be disallowed - the reference parser uses parse_eq_star here and that's also what's used within parse_brackets with `,` `;` and `for` being the only allowed continuation tokens. Also refine error recovery a little and add additional tests.
1 parent af64b6d commit 8a47045

File tree

3 files changed

+16
-5
lines changed

3 files changed

+16
-5
lines changed

src/parser.jl

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3180,11 +3180,16 @@ function parse_string(ps::ParseState, raw::Bool)
31803180
# "a $(x + y) b" ==> (string "a " (parens (call-i x + y)) " b")
31813181
# "hi$("ho")" ==> (string "hi" (parens (string "ho")))
31823182
m = position(ps)
3183-
parse_atom(ps)
3184-
if peek_behind(ps, skip_parens=false).kind != K"parens"
3185-
# "$(x,y)" ==> (string (error (tuple-p x y)))
3183+
bump(ps, TRIVIA_FLAG)
3184+
opts = parse_brackets(ps, K")") do had_commas, had_splat, num_semis, num_subexprs
3185+
return (needs_parameters=false,
3186+
simple_interp=!had_commas && num_semis == 0 && num_subexprs == 1)
3187+
end
3188+
if !opts.simple_interp || peek_behind(ps, skip_parens=false).kind == K"generator"
3189+
# "$(x,y)" ==> (string (parens (error x y)))
31863190
emit(ps, m, K"error", error="invalid interpolation syntax")
31873191
end
3192+
emit(ps, m, K"parens")
31883193
elseif k == K"var"
31893194
# var identifiers disabled in strings
31903195
# "$var" ==> (string var)

test/diagnostics.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@ end
9696
Diagnostic(2, 19, :error, "try without catch or finally")
9797
Diagnostic(20, 19, :error, "Expected `end`")
9898
]
99+
100+
@test diagnostic("\"\$(x,y)\"") ==
101+
Diagnostic(3, 7, :error, "invalid interpolation syntax")
99102
end
100103

101104
@testset "parser warnings" begin

test/parser.jl

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -869,8 +869,11 @@ tests = [
869869
"\"\"\"\n\$x\n a\"\"\"" => "(string-s x \"\\n\" \" a\")"
870870
"\"a \$(x + y) b\"" => "(string \"a \" (parens (call-i x + y)) \" b\")"
871871
"\"hi\$(\"ho\")\"" => "(string \"hi\" (parens (string \"ho\")))"
872-
"\"\$(x,y)\"" => "(string (error (tuple-p x y)))"
873-
"\"\$(x;y)\"" => "(string (error (block-p x y)))"
872+
"\"\$(x,y)\"" => "(string (parens (error x y)))"
873+
"\"\$(x;y)\"" => "(string (parens (error x y)))"
874+
"\"\$(x for y in z)\"" => "(string (parens (error (generator x (= y z)))))"
875+
"\"\$((x for y in z))\"" => "(string (parens (parens (generator x (= y z)))))"
876+
"\"\$(xs...)\"" => "(string (parens (... xs)))"
874877
"\"a \$foo b\"" => "(string \"a \" foo \" b\")"
875878
"\"\$var\"" => "(string var)"
876879
"\"\$outer\"" => "(string outer)"

0 commit comments

Comments
 (0)