Skip to content

Commit

Permalink
Raise clearer error message on number followed by identifiers, closes #…
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim committed Mar 11, 2021
1 parent d8d26e9 commit 6b2cc23
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
15 changes: 13 additions & 2 deletions lib/elixir/src/elixir_tokenizer.erl
Expand Up @@ -493,8 +493,19 @@ tokenize([$: | String] = Original, Line, Column, Scope, Tokens) ->

tokenize([H | T], Line, Column, Scope, Tokens) when ?is_digit(H) ->
case tokenize_number(T, [H], 1, false) of
{error, Reason, Number} ->
{error, {Line, Column, Reason, Number}, T, Tokens};
{error, Reason, Original} ->
{error, {Line, Column, Reason, Original}, T, Tokens};
{[I | _], _Number, Original, _Length} when ?is_upcase(I); ?is_downcase(I); I == $_ ->
Msg =
io_lib:format(
"invalid character ~ts after number ~ts. If you intended to write a number, "
"make sure to add the proper punctuation character after the number (space, comma, etc). "
"If you meant to write an identifier, note that identifiers in Elixir cannot start with numbers. "
"Unexpected token: ",
[[I], Original]
),

{error, {Line, Column, Msg, [I]}, T, Tokens};
{Rest, Number, Original, Length} when is_integer(Number) ->
Token = {int, {Line, Column, Number}, Original},
tokenize(Rest, Line, Column + Length, Scope, [Token | Tokens]);
Expand Down
4 changes: 4 additions & 0 deletions lib/elixir/test/elixir/kernel/errors_test.exs
Expand Up @@ -1138,6 +1138,10 @@ defmodule Kernel.ErrorsTest do
assert_eval_raise SyntaxError, "nofile:1:5: syntax error before: \"12\"", ':ok 12'
assert_eval_raise SyntaxError, "nofile:1:5: syntax error before: \"0b1\"", ':ok 0b1'
assert_eval_raise SyntaxError, "nofile:1:5: syntax error before: \"12.3\"", ':ok 12.3'

assert_eval_raise SyntaxError,
~r"nofile:1:1: invalid character _ after number 123_456",
'123_456_foo'
end

test "invalid \"fn do expr end\"" do
Expand Down

0 comments on commit 6b2cc23

Please sign in to comment.