Skip to content

Commit fb5a703

Browse files
authored
Add error for invalid string interpolation syntax (JuliaLang/JuliaSyntax.jl#306)
Commas and semicolons are not allowed within string interpolation brackets. For example `"$(x, digits=2)"` is reserved syntax.
1 parent ff8f7ca commit fb5a703

File tree

3 files changed

+10
-2
lines changed

3 files changed

+10
-2
lines changed

JuliaSyntax/src/parse_stream.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,7 @@ function peek_full_token(stream::ParseStream, n::Integer=1;
534534
end
535535

536536
"""
537-
peek_behind(ps; skip_trivia=true)
537+
peek_behind(ps; skip_trivia=true, skip_parens=true)
538538
peek_behind(ps, pos::ParseStreamPosition)
539539
540540
Return information about a span which was previously inserted into the output,

JuliaSyntax/src/parser.jl

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2097,7 +2097,8 @@ function parse_function_signature(ps::ParseState, is_function::Bool)
20972097
is_empty_tuple = peek(ps, skip_newlines=true) == K")"
20982098
opts = parse_brackets(ps, K")") do _, _, _, _
20992099
_parsed_call = was_eventually_call(ps)
2100-
_is_anon_func = peek(ps, 2) KSet"( ." && !_parsed_call
2100+
t2 = peek_token(ps, 2)
2101+
_is_anon_func = kind(t2) KSet"( ." && !_parsed_call
21012102
return (needs_parameters = _is_anon_func,
21022103
is_anon_func = _is_anon_func,
21032104
parsed_call = _parsed_call)
@@ -3178,7 +3179,12 @@ function parse_string(ps::ParseState, raw::Bool)
31783179
if k == K"("
31793180
# "a $(x + y) b" ==> (string "a " (parens (call-i x + y)) " b")
31803181
# "hi$("ho")" ==> (string "hi" (parens (string "ho")))
3182+
m = position(ps)
31813183
parse_atom(ps)
3184+
if peek_behind(ps, skip_parens=false).kind != K"parens"
3185+
# "$(x,y)" ==> (string (error (tuple-p x y)))
3186+
emit(ps, m, K"error", error="invalid interpolation syntax")
3187+
end
31823188
elseif k == K"var"
31833189
# var identifiers disabled in strings
31843190
# "$var" ==> (string var)

JuliaSyntax/test/parser.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -869,6 +869,8 @@ 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)))"
872874
"\"a \$foo b\"" => "(string \"a \" foo \" b\")"
873875
"\"\$var\"" => "(string var)"
874876
"\"\$outer\"" => "(string outer)"

0 commit comments

Comments
 (0)