Skip to content

Handle identifier followed by dual_op across line continuation #14714

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
21 changes: 20 additions & 1 deletion lib/elixir/src/elixir_tokenizer.erl
Original file line number Diff line number Diff line change
Expand Up @@ -723,7 +723,26 @@ unexpected_token([T | Rest], Line, Column, Scope, Tokens) ->
tokenize_eol(Rest, Line, Scope, Tokens) ->
{StrippedRest, Column} = strip_horizontal_space(Rest, Scope#elixir_tokenizer.column),
IndentedScope = Scope#elixir_tokenizer{indentation=Column-1},
tokenize(StrippedRest, Line + 1, Column, IndentedScope, Tokens).
%% If we arrived here via a line continuation (\\\n or \\\r\n), there is no EOL token added.
%% In such case, if the previous token is an identifier and the next non-space token
%% is a dual operator sign (+ or -) followed by a non-marker, we should mirror the
%% same-line space-sensitive behavior and convert the identifier to op_identifier.
%% This makes: foo\\\n-1 and foo\\\n +1 behave like foo -1 and foo +1.
case {Tokens, StrippedRest} of
{[{identifier, _, _} = H | TokensTail], [Sign, NotMarker | T]} ->
case previous_was_eol(Tokens) of
%% Only apply across line continuations (no previous EOL recorded)
nil when ?dual_op(Sign), not(?is_space(NotMarker)), NotMarker =/= Sign, NotMarker =/= $/, NotMarker =/= $> ->
DualOpToken = {dual_op, {Line + 1, Column, nil}, list_to_atom([Sign])},
Rest2 = [NotMarker | T],
tokenize(Rest2, Line + 1, Column + 1, IndentedScope,
[DualOpToken, setelement(1, H, op_identifier) | TokensTail]);
_ ->
tokenize(StrippedRest, Line + 1, Column, IndentedScope, Tokens)
end;
_ ->
tokenize(StrippedRest, Line + 1, Column, IndentedScope, Tokens)
end.

strip_horizontal_space([H | T], Counter) when ?is_horizontal_space(H) ->
strip_horizontal_space(T, Counter + 1);
Expand Down
16 changes: 16 additions & 0 deletions lib/elixir/test/erlang/tokenizer_test.erl
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,22 @@ space_test() ->
{dual_op, {1, 6, nil}, '-'},
{int, {1, 7, 2}, "2"}] = tokenize("foo -2").

op_identifier_line_continuation_test() ->
%% Backslash followed by LF
[{op_identifier, {1, 1, _}, foo},
{dual_op, {2, 1, nil}, '+'},
{int, {2, 2, 1}, "1"}] = tokenize("foo\\\n+1"),
Copy link
Member

Choose a reason for hiding this comment

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

We are no longer adding tests to this file, it is very old. Perhaps we can add them as tests to Code.string_to_quoted? Or do you think it would not capture it?


%% Backslash followed by CRLF
[{op_identifier, {1, 1, _}, foo},
{dual_op, {2, 1, nil}, '+'},
{int, {2, 2, 1}, "1"}] = tokenize("foo\\\r\n+1"),

%% Backslash-LF then a space before '+'
[{op_identifier, {1, 1, _}, foo},
{dual_op, {2, 2, nil}, '+'},
{int, {2, 3, 1}, "1"}] = tokenize("foo\\\n +1").

chars_test() ->
[{char, {1, 1, "?a"}, 97}] = tokenize("?a"),
[{char, {1, 1, "?c"}, 99}] = tokenize("?c"),
Expand Down