Skip to content

Commit

Permalink
Better split between fcall and command for ripper translation
Browse files Browse the repository at this point in the history
  • Loading branch information
kddnewton committed Mar 6, 2024
1 parent fe10b5f commit 972fe60
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 29 deletions.
52 changes: 41 additions & 11 deletions lib/prism/translation/ripper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,8 @@ def _dispatch_event_push(list, item) # :nodoc:
end
end

# In an alias statement Ripper will emit @kw instead of @ident if the
# object being aliased is a Ruby keyword. For instance, in the line
# "alias :foo :if", the :if is treated as a lexer keyword. So we need to
# know what symbols are also keywords.
RUBY_KEYWORDS = [
# A list of all of the Ruby keywords.
KEYWORDS = [
"alias",
"and",
"begin",
Expand Down Expand Up @@ -110,7 +107,32 @@ def _dispatch_event_push(list, item) # :nodoc:
"__LINE__"
]

private_constant :RUBY_KEYWORDS
# A list of all of the Ruby binary operators.
BINARY_OPERATORS = [
:!=,
:!~,
:=~,
:==,
:===,
:<=>,
:>,
:>=,
:<,
:<=,
:&,
:|,
:^,
:>>,
:<<,
:-,
:+,
:%,
:/,
:*,
:**
]

private_constant :KEYWORDS, :BINARY_OPERATORS

# The source that is being parsed.
attr_reader :source
Expand Down Expand Up @@ -599,7 +621,7 @@ def visit_call_node(node)

bounds(node.location)
on_unary(node.message == "not" ? :not : :!@, receiver)
when :!=, :!~, :=~, :==, :===, :<=>, :>, :>=, :<, :<=, :&, :|, :^, :>>, :<<, :-, :+, :%, :/, :*, :**
when *BINARY_OPERATORS
receiver = visit(node.receiver)
value = visit(node.arguments.arguments.first)

Expand All @@ -614,7 +636,7 @@ def visit_call_node(node)
else
arguments, block = visit_call_node_arguments(node.arguments, node.block, trailing_comma?(node.arguments&.location || node.location, node.closing_loc || node.location))
call =
if node.opening_loc.nil? && (arguments&.any? || block.nil?)
if node.opening_loc.nil? && arguments&.any?
bounds(node.location)
on_command(message, arguments)
elsif !node.opening_loc.nil?
Expand Down Expand Up @@ -698,7 +720,7 @@ def visit_call_node(node)
elsif arguments.any?
args = visit_arguments(arguments)

if block_node.is_a?(BlockArgumentNode) || arguments.last.is_a?(ForwardingArgumentsNode) || trailing_comma
if block_node.is_a?(BlockArgumentNode) || arguments.last.is_a?(ForwardingArgumentsNode) || command?(arguments.last) || trailing_comma
args
else
bounds(arguments.first.location)
Expand All @@ -709,6 +731,14 @@ def visit_call_node(node)
]
end

# Returns true if the given node is a command node.
private def command?(node)
node.is_a?(CallNode) &&
node.opening_loc.nil? &&
(!node.arguments.nil? || node.block.is_a?(BlockArgumentNode)) &&
!BINARY_OPERATORS.include?(node.name)
end

# foo.bar += baz
# ^^^^^^^^^^^^^^^
def visit_call_operator_write_node(node)
Expand Down Expand Up @@ -2714,11 +2744,11 @@ def visit_token(token)
on_period(token)
when "`"
on_backtick(token)
when *RUBY_KEYWORDS
when *KEYWORDS
on_kw(token)
when /^_/
on_ident(token)
when /^[[:upper:]]/
when /^[[:upper:]]\w*$/
on_const(token)
when /^@@/
on_cvar(token)
Expand Down
18 changes: 0 additions & 18 deletions test/prism/ripper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,14 @@ class RipperTest < TestCase

skips = incorrect | %w[
arrays.txt
blocks.txt
case.txt
command_method_call.txt
constants.txt
dos_endings.txt
embdoc_no_newline_at_end.txt
hashes.txt
heredocs_leading_whitespace.txt
heredocs_nested.txt
heredocs_with_ignored_newlines.txt
if.txt
method_calls.txt
modules.txt
multi_write.txt
patterns.txt
Expand All @@ -52,7 +48,6 @@ class RipperTest < TestCase
seattlerb/block_next.txt
seattlerb/block_return.txt
seattlerb/bug_hash_interp_array.txt
seattlerb/call_args_command.txt
seattlerb/call_array_lambda_block_call.txt
seattlerb/defn_oneliner_eq2.txt
seattlerb/defs_oneliner_eq2.txt
Expand Down Expand Up @@ -82,12 +77,9 @@ class RipperTest < TestCase
seattlerb/stabby_block_iter_call_no_target_with_arg.txt
seattlerb/str_lit_concat_bad_encodings.txt
seattlerb/yield_call_assocs.txt
single_method_call_with_bang.txt
spanning_heredoc.txt
strings.txt
ternary_operator.txt
tilde_heredocs.txt
unless.txt
unparser/corpus/literal/assignment.txt
unparser/corpus/literal/block.txt
unparser/corpus/literal/class.txt
Expand All @@ -104,21 +96,15 @@ class RipperTest < TestCase
unparser/corpus/literal/since/27.txt
unparser/corpus/literal/while.txt
unparser/corpus/semantic/dstr.txt
unparser/corpus/semantic/literal.txt
unparser/corpus/semantic/while.txt
until.txt
variables.txt
while.txt
whitequark/args_cmd.txt
whitequark/asgn_mrhs.txt
whitequark/bug_480.txt
whitequark/dedenting_heredoc.txt
whitequark/dedenting_interpolating_heredoc_fake_line_continuation.txt
whitequark/dedenting_non_interpolating_heredoc_line_continuation.txt
whitequark/def.txt
whitequark/empty_stmt.txt
whitequark/if_elsif.txt
whitequark/kwbegin_compstmt.txt
whitequark/masgn_attr.txt
whitequark/masgn_nested.txt
whitequark/masgn_splat.txt
Expand All @@ -127,13 +113,9 @@ class RipperTest < TestCase
whitequark/parser_drops_truncated_parts_of_squiggly_heredoc.txt
whitequark/parser_slash_slash_n_escaping_in_literals.txt
whitequark/ruby_bug_11107.txt
whitequark/ruby_bug_11873.txt
whitequark/ruby_bug_11873_a.txt
whitequark/ruby_bug_11990.txt
whitequark/ruby_bug_15789.txt
whitequark/send_block_chain_cmd.txt
whitequark/send_index_cmd.txt
whitequark/send_self.txt
whitequark/slash_newline_in_heredocs.txt
whitequark/string_concat.txt
]
Expand Down

0 comments on commit 972fe60

Please sign in to comment.