From 3ce822ecd29901bbf7555dcc7d7ca93ffbe6dc5e Mon Sep 17 00:00:00 2001 From: Or Neeman Date: Wed, 4 Mar 2015 20:52:06 -0600 Subject: [PATCH] Produce clearer messages for syntax errors before a sigil Closes #3130 --- lib/elixir/src/elixir_errors.erl | 7 +++++++ lib/elixir/test/elixir/kernel/errors_test.exs | 6 ++++++ 2 files changed, 13 insertions(+) diff --git a/lib/elixir/src/elixir_errors.erl b/lib/elixir/src/elixir_errors.erl index b695ef561c..24c0033d7f 100644 --- a/lib/elixir/src/elixir_errors.erl +++ b/lib/elixir/src/elixir_errors.erl @@ -55,6 +55,13 @@ parse_error(Line, File, Error, <<>>) -> parse_error(Line, File, <<"syntax error before: ">>, <<"'end'">>) -> do_raise(Line, File, 'Elixir.SyntaxError', <<"unexpected token: end">>); +%% Produce a human-readable message for errors before a sigil +parse_error(Line, File, <<"syntax error before: ">>, <<"{sigil,", _Rest/binary>> = Full) -> + {ok, Tokens, _} = erl_scan:string(binary_to_list(Full)), + {ok, {sigil, _, Sigil, [Content], _}} = erl_parse:parse_term(Tokens ++ [{dot, 1}]), + Message = <<"syntax error before: sigil ~", Sigil," with content '", Content/binary, "'">>, + do_raise(Line, File, 'Elixir.SyntaxError', Message); + %% Aliases are wrapped in [''] parse_error(Line, File, Error, <<"['", Token/binary>>) when is_binary(Error) -> Rest = diff --git a/lib/elixir/test/elixir/kernel/errors_test.exs b/lib/elixir/test/elixir/kernel/errors_test.exs index 758e8af5d5..f708c21c94 100644 --- a/lib/elixir/test/elixir/kernel/errors_test.exs +++ b/lib/elixir/test/elixir/kernel/errors_test.exs @@ -106,6 +106,12 @@ defmodule Kernel.ErrorsTest do '+.foo' end + test :syntax_error_before_sigil do + assert_compile_fail SyntaxError, + "nofile:1: syntax error before: sigil ~s with content 'bar baz'", + '~s(foo) ~s(bar baz)' + end + test :compile_error_on_op_ambiguity do msg = "nofile:1: \"a -1\" looks like a function call but there is a variable named \"a\", " <> "please use explicit parentheses or even spaces"