Skip to content

Commit 566f946

Browse files
Earlopainmatzbot
authored andcommitted
[ruby/prism] Fix parser translator tSPACE tokens for percent arrays
Tests worked around this but the incompatibility is not hard to fix. This fixes 17 token incompatibilies in tests here that were previously passing ruby/prism@101962526d
1 parent 14b9098 commit 566f946

File tree

2 files changed

+9
-8
lines changed

2 files changed

+9
-8
lines changed

lib/prism/translation/parser/lexer.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,15 @@ def to_a
457457
location = range(token.location.start_offset, token.location.start_offset + 1)
458458
end
459459

460-
quote_stack.pop
460+
if percent_array?(quote_stack.pop)
461+
prev_token = lexed[index - 2][0] if index - 2 >= 0
462+
empty = %i[PERCENT_LOWER_I PERCENT_LOWER_W PERCENT_UPPER_I PERCENT_UPPER_W].include?(prev_token&.type)
463+
ends_with_whitespace = prev_token&.type == :WORDS_SEP
464+
# parser always emits a space token after content in a percent array, even if no actual whitespace is present.
465+
if !empty && !ends_with_whitespace
466+
tokens << [:tSPACE, [nil, range(token.location.start_offset, token.location.start_offset)]]
467+
end
468+
end
461469
when :tSYMBEG
462470
if (next_token = lexed[index][0]) && next_token.type != :STRING_CONTENT && next_token.type != :EMBEXPR_BEGIN && next_token.type != :EMBVAR && next_token.type != :STRING_END
463471
next_location = token.location.join(next_token.location)

test/prism/ruby/parser_test.rb

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -219,13 +219,6 @@ def assert_equal_tokens(expected_tokens, actual_tokens)
219219
expected_index += 1
220220
actual_index += 1
221221

222-
# The parser gem always has a space before a string end in list
223-
# literals, but we don't. So we'll skip over the space.
224-
if expected_token[0] == :tSPACE && actual_token[0] == :tSTRING_END
225-
expected_index += 1
226-
next
227-
end
228-
229222
# There are a lot of tokens that have very specific meaning according
230223
# to the context of the parser. We don't expose that information in
231224
# prism, so we need to normalize these tokens a bit.

0 commit comments

Comments
 (0)