diff options
author | Kevin Newton <kddnewton@gmail.com> | 2024-03-06 00:12:15 -0500 |
---|---|---|
committer | git <svn-admin@ruby-lang.org> | 2024-03-06 16:42:57 +0000 |
commit | 9018e46cc274dfe8b3a4c4e58b35f1bb84a26460 (patch) | |
tree | 03360e6cd1f64a55a737ee63f9b94b61537a934a | |
parent | 409a49c832fce971cf506228b46d8ca1af35f81d (diff) |
[ruby/prism] Better split between fcall and command for ripper translation
https://github.com/ruby/prism/commit/972fe60aea
-rw-r--r-- | lib/prism/translation/ripper.rb | 52 | ||||
-rw-r--r-- | test/prism/ripper_test.rb | 18 |
2 files changed, 41 insertions, 29 deletions
diff --git a/lib/prism/translation/ripper.rb b/lib/prism/translation/ripper.rb index d96df18010..8cad131e50 100644 --- a/lib/prism/translation/ripper.rb +++ b/lib/prism/translation/ripper.rb @@ -62,11 +62,8 @@ module Prism 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", @@ -110,7 +107,32 @@ module Prism "__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 @@ -599,7 +621,7 @@ module Prism bounds(node.location) on_unary(node.message == "not" ? :not : :!@, receiver) - when :!=, :!~, :=~, :==, :===, :<=>, :>, :>=, :<, :<=, :&, :|, :^, :>>, :<<, :-, :+, :%, :/, :*, :** + when *BINARY_OPERATORS receiver = visit(node.receiver) value = visit(node.arguments.arguments.first) @@ -614,7 +636,7 @@ module Prism 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? @@ -698,7 +720,7 @@ module Prism 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) @@ -709,6 +731,14 @@ module Prism ] 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) @@ -2714,11 +2744,11 @@ module Prism 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) diff --git a/test/prism/ripper_test.rb b/test/prism/ripper_test.rb index 9d5eaf2b91..e7a9505fc7 100644 --- a/test/prism/ripper_test.rb +++ b/test/prism/ripper_test.rb @@ -26,18 +26,14 @@ module Prism 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 @@ -52,7 +48,6 @@ module Prism 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 @@ -82,12 +77,9 @@ module Prism 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 @@ -104,21 +96,15 @@ module Prism 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 @@ -127,13 +113,9 @@ module Prism 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 ] |