Skip to content

Commit

Permalink
Also disallow bare generators in interpolation syntax
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
c42f committed Jun 9, 2023
1 parent af64b6d commit 7d23151
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
11 changes: 8 additions & 3 deletions src/parser.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3180,11 +3180,16 @@ function parse_string(ps::ParseState, raw::Bool)
# "a $(x + y) b" ==> (string "a " (parens (call-i x + y)) " b")
# "hi$("ho")" ==> (string "hi" (parens (string "ho")))
m = position(ps)
parse_atom(ps)
if peek_behind(ps, skip_parens=false).kind != K"parens"
# "$(x,y)" ==> (string (error (tuple-p x y)))
bump(ps, TRIVIA_FLAG)
opts = parse_brackets(ps, K")") do had_commas, had_splat, num_semis, num_subexprs
return (needs_parameters=false,
simple_interp=!had_commas && !had_splat && num_semis == 0)
end
if !opts.simple_interp || peek_behind(ps).kind == K"generator"
# "$(x,y)" ==> (string (parens (error x y)))
emit(ps, m, K"error", error="invalid interpolation syntax")
end
emit(ps, m, K"parens")
elseif k == K"var"
# var identifiers disabled in strings
# "$var" ==> (string var)
Expand Down
3 changes: 3 additions & 0 deletions test/diagnostics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ end
Diagnostic(2, 19, :error, "try without catch or finally")
Diagnostic(20, 19, :error, "Expected `end`")
]

@test diagnostic("\"\$(x,y)\"") ==
Diagnostic(3, 7, :error, "invalid interpolation syntax")
end

@testset "parser warnings" begin
Expand Down
5 changes: 3 additions & 2 deletions test/parser.jl
Original file line number Diff line number Diff line change
Expand Up @@ -869,8 +869,9 @@ tests = [
"\"\"\"\n\$x\n a\"\"\"" => "(string-s x \"\\n\" \" a\")"
"\"a \$(x + y) b\"" => "(string \"a \" (parens (call-i x + y)) \" b\")"
"\"hi\$(\"ho\")\"" => "(string \"hi\" (parens (string \"ho\")))"
"\"\$(x,y)\"" => "(string (error (tuple-p x y)))"
"\"\$(x;y)\"" => "(string (error (block-p x y)))"
"\"\$(x,y)\"" => "(string (parens (error x y)))"
"\"\$(x;y)\"" => "(string (parens (error x y)))"
"\"\$(x for y in z)\"" => "(string (parens (error (generator x (= y z)))))"
"\"a \$foo b\"" => "(string \"a \" foo \" b\")"
"\"\$var\"" => "(string var)"
"\"\$outer\"" => "(string outer)"
Expand Down

0 comments on commit 7d23151

Please sign in to comment.