From 6bb8f59b4252a377f800ff10a00a92f6de916a2f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Sun, 20 Nov 2016 11:01:30 +0100 Subject: [PATCH] Improve compiler message on unexpected end of line, closes #5137 --- lib/elixir/src/elixir_errors.erl | 5 +++++ lib/elixir/src/elixir_tokenizer.erl | 2 +- lib/elixir/test/elixir/kernel/errors_test.exs | 12 ++++++------ 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/lib/elixir/src/elixir_errors.erl b/lib/elixir/src/elixir_errors.erl index e102d2e7d4..ea899b1a0f 100644 --- a/lib/elixir/src/elixir_errors.erl +++ b/lib/elixir/src/elixir_errors.erl @@ -67,6 +67,11 @@ parse_error(Line, File, Error, <<>>) -> end, do_raise(Line, File, 'Elixir.TokenMissingError', Message); +%% Show a nicer message for end of line +parse_error(Line, File, <<"syntax error before: ">>, <<"eol">>) -> + do_raise(Line, File, 'Elixir.SyntaxError', + <<"unexpectedly reached end of line. The current expression is invalid or incomplete">>); + %% Show a nicer message for missing end tokens parse_error(Line, File, <<"syntax error before: ">>, <<"'end'">>) -> do_raise(Line, File, 'Elixir.SyntaxError', <<"unexpected token: end">>); diff --git a/lib/elixir/src/elixir_tokenizer.erl b/lib/elixir/src/elixir_tokenizer.erl index a96e7f9dff..2d5b5a0382 100644 --- a/lib/elixir/src/elixir_tokenizer.erl +++ b/lib/elixir/src/elixir_tokenizer.erl @@ -1024,7 +1024,7 @@ keyword('catch') -> block; keyword(_) -> false. invalid_character_error(Char) -> - "invalid character '" ++ [Char] ++ "' in identifier: ". + io_lib:format("invalid character \"~ts\" (codepoint U+~4.16.0B) in token: ", [[Char], Char]). invalid_do_error(Prefix) -> Prefix ++ ". In case you wanted to write a \"do\" expression, " diff --git a/lib/elixir/test/elixir/kernel/errors_test.exs b/lib/elixir/test/elixir/kernel/errors_test.exs index 67651b8241..a8e15a0ac4 100644 --- a/lib/elixir/test/elixir/kernel/errors_test.exs +++ b/lib/elixir/test/elixir/kernel/errors_test.exs @@ -39,12 +39,12 @@ defmodule Kernel.ErrorsTest do end test "invalid identifier" do - msg = fn char, name -> "nofile:1: invalid character '#{char}' in identifier: #{name}" end + msg = fn name -> "nofile:1: invalid character \"@\" (codepoint U+0040) in token: #{name}" end - assert_compile_fail SyntaxError, msg.(:@, "foo@"), 'foo@' - assert_compile_fail SyntaxError, msg.(:@, "foo@"), 'foo@ ' - assert_compile_fail SyntaxError, msg.(:@, "foo@bar"), 'foo@bar' - assert_compile_fail SyntaxError, msg.(:!, "Foo!"), 'Foo!' + assert_compile_fail SyntaxError, msg.("foo@"), 'foo@' + assert_compile_fail SyntaxError, msg.("foo@"), 'foo@ ' + assert_compile_fail SyntaxError, msg.("foo@bar"), 'foo@bar' + assert_compile_fail SyntaxError, msg.("Foo@"), 'Foo@' end test "invalid fn" do @@ -834,7 +834,7 @@ defmodule Kernel.ErrorsTest do test "new line error" do assert_compile_fail SyntaxError, - "nofile:3: syntax error before: eol", + "nofile:3: unexpectedly reached end of line. The current expression is invalid or incomplete", 'if true do\n foo = [],\n baz\nend' end