Skip to content

Commit 21840ed

Browse files
committed
Do not assume all parser errors can be parsed into erlang terms
1 parent 09c602d commit 21840ed

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

lib/elixir/src/elixir_errors.erl

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ parse_error(Location, File, <<"syntax error before: ">>, Keyword, Input)
357357

358358
%% Produce a human-readable message for errors before a sigil
359359
parse_error(Location, File, <<"syntax error before: ">>, <<"{sigil,", _Rest/binary>> = Full, Input) ->
360-
{sigil, _, Atom, [Content | _], _, _, _} = parse_erl_term(Full),
360+
{ok, {sigil, _, Atom, [Content | _], _, _, _}} = parse_erl_term(Full),
361361
Content2 = case is_binary(Content) of
362362
true -> Content;
363363
false -> <<>>
@@ -378,7 +378,7 @@ parse_error(Location, File, <<"syntax error before: ">>, <<"{sigil,", _Rest/bina
378378
%% Binaries (and interpolation) are wrapped in [<<...>>]
379379
parse_error(Location, File, Error, <<"[", _/binary>> = Full, Input) when is_binary(Error) ->
380380
Term = case parse_erl_term(Full) of
381-
[H | _] when is_binary(H) -> <<$", H/binary, $">>;
381+
{ok, [H | _]} when is_binary(H) -> <<$", H/binary, $">>;
382382
_ -> <<$">>
383383
end,
384384
raise_snippet(Location, File, Input, 'Elixir.SyntaxError', <<Error/binary, Term/binary>>);
@@ -408,9 +408,14 @@ parse_error(Location, File, Error, Token, Input) when is_binary(Error), is_binar
408408
end.
409409

410410
parse_erl_term(Term) ->
411-
{ok, Tokens, _} = erl_scan:string(binary_to_list(Term)),
412-
{ok, Parsed} = erl_parse:parse_term(Tokens ++ [{dot, 1}]),
413-
Parsed.
411+
case erl_scan:string(binary_to_list(Term)) of
412+
{ok, Tokens, _} ->
413+
case erl_parse:parse_term(Tokens ++ [{dot, 1}]) of
414+
{ok, Parsed} -> {ok, Parsed};
415+
_ -> error
416+
end;
417+
_ -> error
418+
end.
414419

415420
raise_reserved(Location, File, Input, Keyword) ->
416421
raise_snippet(Location, File, Input, 'Elixir.SyntaxError',

0 commit comments

Comments
 (0)