Skip to content

Add support for sigils containing integers #13448

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/elixir/pages/getting-started/sigils.md
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,6 @@ iex> ~i(42)n
-42
```

Custom sigils may be either a single lowercase character or several uppercase characters.
Custom sigils may be either a single lowercase character, or an uppercase character followed by more uppercase characters and digits.

Sigils can also be used to do compile-time work with the help of macros. For example, regular expressions in Elixir are compiled into an efficient representation during compilation of the source code, therefore skipping this step at runtime. If you're interested in the subject, you can learn more about macros and check out how sigils are implemented in the `Kernel` module (where the `sigil_*` functions are defined).
5 changes: 4 additions & 1 deletion lib/elixir/src/elixir_import.erl
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,10 @@ is_sigil({Name, 2}) ->
case Letters of
[L] when L >= $a, L =< $z -> true;
[] -> false;
Letters -> lists:all(fun(L) -> L >= $A andalso L =< $Z end, Letters)
[H|T] when H >= $A, H =< $Z ->
lists:all(fun(L) -> (L >= $0 andalso L =< $9)
orelse (L>= $A andalso L =< $Z)
end, T)
end;
_ ->
false
Expand Down
31 changes: 22 additions & 9 deletions lib/elixir/src/elixir_tokenizer.erl
Original file line number Diff line number Diff line change
Expand Up @@ -1559,20 +1559,33 @@ tokenize_sigil([$~ | T], Line, Column, Scope, Tokens) ->
end.

% A one-letter sigil is ok both as upcase as well as downcase.
tokenize_sigil_name([S | T], [], Line, Column, Scope, Tokens) when ?is_upcase(S) orelse ?is_downcase(S) ->
tokenize_sigil_name(T, [S], Line, Column + 1, Scope, Tokens);
tokenize_sigil_name([S | T], [], Line, Column, Scope, Tokens) when ?is_downcase(S) ->
tokenize_lower_sigil_name(T, [S], Line, Column + 1, Scope, Tokens);
tokenize_sigil_name([S | T], [], Line, Column, Scope, Tokens) when ?is_upcase(S) ->
tokenize_upper_sigil_name(T, [S], Line, Column + 1, Scope, Tokens).

tokenize_lower_sigil_name([S | _T] = Original, [_ | _] = NameAcc, _Line, _Column, _Scope, _Tokens) when ?is_downcase(S) ->
SigilName = lists:reverse(NameAcc) ++ Original,
{error, sigil_name_error(), [$~] ++ SigilName};
tokenize_lower_sigil_name(T, NameAcc, Line, Column, Scope, Tokens) ->
{ok, lists:reverse(NameAcc), T, Line, Column, Scope, Tokens}.

% If we have an uppercase letter, we keep tokenizing the name.
tokenize_sigil_name([S | T], NameAcc, Line, Column, Scope, Tokens) when ?is_upcase(S) ->
tokenize_sigil_name(T, [S | NameAcc], Line, Column + 1, Scope, Tokens);
% A digit is allowed but an uppercase letter or digit must proceed it.
tokenize_upper_sigil_name([S | T], NameAcc, Line, Column, Scope, Tokens) when ?is_upcase(S) orelse ?is_digit(S) ->
tokenize_upper_sigil_name(T, [S | NameAcc], Line, Column + 1, Scope, Tokens);
% With a lowercase letter and a non-empty NameAcc we return an error.
tokenize_sigil_name([S | _T] = Original, [_ | _] = NameAcc, _Line, _Column, _Scope, _Tokens) when ?is_downcase(S) ->
Message = "invalid sigil name, it should be either a one-letter lowercase letter or a" ++
" sequence of uppercase letters only, got: ",
{error, Message, [$~] ++ lists:reverse(NameAcc) ++ Original};
tokenize_upper_sigil_name([S | _T] = Original, [_ | _] = NameAcc, _Line, _Column, _Scope, _Tokens) when ?is_downcase(S) ->
SigilName = lists:reverse(NameAcc) ++ Original,
{error, sigil_name_error(), [$~] ++ SigilName};
% We finished the letters, so the name is over.
tokenize_sigil_name(T, NameAcc, Line, Column, Scope, Tokens) ->
tokenize_upper_sigil_name(T, NameAcc, Line, Column, Scope, Tokens) ->
{ok, lists:reverse(NameAcc), T, Line, Column, Scope, Tokens}.

sigil_name_error() ->
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could also be a macro, not sure what the preference here. Likewise, we could just place the error tuple here, but once again, not sure the preference (localization of such things can be valuable).

"invalid sigil name, it should be either a one-letter lowercase letter or a" ++
" uppercase letter optionally followed by uppercase letters and digits, got: ".

tokenize_sigil_contents([H, H, H | T] = Original, [S | _] = SigilName, Line, Column, Scope, Tokens)
when ?is_quote(H) ->
case extract_heredoc_with_interpolation(Line, Column, Scope, ?is_downcase(S), T, H) of
Expand Down
15 changes: 14 additions & 1 deletion lib/elixir/test/elixir/kernel/parser_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -165,13 +165,26 @@ defmodule Kernel.ParserTest do
meta = [delimiter: "\"\"\"", line: 1]
args = {:sigil_MAT, meta, [{:<<>>, [indentation: 0, line: 1], ["1,2,3\n"]}, []]}
assert string_to_quoted.("~MAT\"\"\"\n1,2,3\n\"\"\"") == args

args = {:sigil_FOO1, meta, [{:<<>>, [indentation: 0, line: 1], ["1,2,3\n"]}, []]}
assert string_to_quoted.("~FOO1\"\"\"\n1,2,3\n\"\"\"") == args

args = {:sigil_BAR321, meta, [{:<<>>, [indentation: 0, line: 1], ["1,2,3\n"]}, []]}
assert string_to_quoted.("~BAR321\"\"\"\n1,2,3\n\"\"\"") == args

args = {:sigil_I18N, meta, [{:<<>>, [indentation: 0, line: 1], ["1,2,3\n"]}, []]}
assert string_to_quoted.("~I18N\"\"\"\n1,2,3\n\"\"\"") == args
end

test "invalid multi-letter sigils" do
msg =
~r/invalid sigil name, it should be either a one-letter lowercase letter or a sequence of uppercase letters only/
~r/invalid sigil name, it should be either a one-letter lowercase letter or a uppercase letter optionally followed by uppercase letters and digits/

assert_syntax_error(["nofile:1:1:", msg], "~Regex/foo/")

assert_syntax_error(["nofile:1:1:", msg], "~FOo1{bar]")

assert_syntax_error(["nofile:1:1:", msg], "~foo1{bar]")
end

test "sigil newlines" do
Expand Down