Skip to content

Commit 788d11b

Browse files
committed
Fix delimiter metadata for single quote atoms and remote calls
1 parent ee90126 commit 788d11b

File tree

3 files changed

+27
-9
lines changed

3 files changed

+27
-9
lines changed

lib/elixir/src/elixir_parser.yrl

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -284,9 +284,9 @@ access_expr -> list_heredoc : build_list_heredoc('$1').
284284
access_expr -> bitstring : '$1'.
285285
access_expr -> sigil : build_sigil('$1').
286286
access_expr -> atom : handle_literal(?exprs('$1'), '$1').
287-
access_expr -> atom_quoted : handle_literal(?exprs('$1'), '$1', delimiter(<<$">>)).
288-
access_expr -> atom_safe : build_quoted_atom('$1', true, delimiter(<<$">>)).
289-
access_expr -> atom_unsafe : build_quoted_atom('$1', false, delimiter(<<$">>)).
287+
access_expr -> atom_quoted : handle_literal(?exprs('$1'), '$1', atom_delimiter('$1')).
288+
access_expr -> atom_safe : build_quoted_atom('$1', true, atom_delimiter('$1')).
289+
access_expr -> atom_unsafe : build_quoted_atom('$1', false, atom_delimiter('$1')).
290290
access_expr -> dot_alias : '$1'.
291291
access_expr -> parens_call : '$1'.
292292

@@ -1033,6 +1033,12 @@ build_quoted_atom({_, Location, Args}, Safe, ExtraMeta) ->
10331033
binary_to_atom_op(true) -> binary_to_existing_atom;
10341034
binary_to_atom_op(false) -> binary_to_atom.
10351035

1036+
atom_delimiter({_Kind, {_Line, _Column, Delimiter}, _Args}) ->
1037+
case ?token_metadata() of
1038+
true -> [{delimiter, <<Delimiter>>}];
1039+
false -> []
1040+
end.
1041+
10361042
charlist_parts(Parts) ->
10371043
[charlist_part(Part) || Part <- Parts].
10381044
charlist_part(Binary) when is_binary(Binary) ->

lib/elixir/src/elixir_tokenizer.erl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,7 @@ tokenize([$:, H | T] = Original, Line, Column, Scope, Tokens) when ?is_quote(H)
511511
{ok, [Part]} when is_binary(Part) ->
512512
case unsafe_to_atom(Part, Line, Column, Scope) of
513513
{ok, Atom} ->
514-
Token = {atom_quoted, {Line, Column, nil}, Atom},
514+
Token = {atom_quoted, {Line, Column, H}, Atom},
515515
tokenize(Rest, NewLine, NewColumn, NewScope, [Token | Tokens]);
516516

517517
{error, Reason} ->
@@ -523,7 +523,7 @@ tokenize([$:, H | T] = Original, Line, Column, Scope, Tokens) when ?is_quote(H)
523523
true -> atom_safe;
524524
false -> atom_unsafe
525525
end,
526-
Token = {Key, {Line, Column, nil}, Unescaped},
526+
Token = {Key, {Line, Column, H}, Unescaped},
527527
tokenize(Rest, NewLine, NewColumn, NewScope, [Token | Tokens]);
528528

529529
{error, Reason} ->
@@ -919,7 +919,7 @@ handle_dot([$., H | T] = Original, Line, Column, DotInfo, Scope, Tokens) when ?i
919919
case unsafe_to_atom(UnescapedPart, Line, Column, NewScope) of
920920
{ok, Atom} ->
921921
Token = check_call_identifier(Line, Column, Part, Atom, Rest),
922-
DotInfo1 = setelement(3, DotInfo, $"),
922+
DotInfo1 = setelement(3, DotInfo, H),
923923
TokensSoFar = add_token_with_eol({'.', DotInfo1}, Tokens),
924924
tokenize(Rest, NewLine, NewColumn, NewScope, [Token | TokensSoFar]);
925925

lib/elixir/test/elixir/kernel/parser_test.exs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,17 +128,29 @@ defmodule Kernel.ParserTest do
128128
end
129129

130130
test "handles graphemes inside quoted identifiers" do
131+
string_to_quoted =
132+
fn code ->
133+
Code.string_to_quoted!(code,
134+
token_metadata: true,
135+
literal_encoder: &{:ok, {:__block__, &2, [&1]}}
136+
)
137+
end
138+
131139
assert {
132140
{:., _, [{:foo, _, nil}, :"➡️"]},
133141
[no_parens: true, delimiter: ~S["], line: 1],
134142
[]
135-
} = Code.string_to_quoted!(~S|foo."➡️"|, token_metadata: true)
143+
} = string_to_quoted.(~S|foo."➡️"|)
136144

137145
assert {
138146
{:., _, [{:foo, _, nil}, :"➡️"]},
139-
[closing: [line: 1], delimiter: ~S["], line: 1],
147+
[no_parens: true, delimiter: ~S['], line: 1],
140148
[]
141-
} = Code.string_to_quoted!(~S|foo."➡️"()|, token_metadata: true)
149+
} = string_to_quoted.(~S|foo.'➡️'|)
150+
151+
assert {:__block__, [delimiter: ~S["], line: 1], [:"➡️"]} = string_to_quoted.(~S|:"➡️"|)
152+
153+
assert {:__block__, [delimiter: ~S['], line: 1], [:"➡️"]} = string_to_quoted.(~S|:'➡️'|)
142154
end
143155
end
144156

0 commit comments

Comments
 (0)