From 532ddc1745a3a9aaee4a1bdf3b6c81e5a3df0c81 Mon Sep 17 00:00:00 2001 From: Kevin Newton Date: Wed, 6 Mar 2024 11:34:38 -0500 Subject: [PATCH] [ruby/prism] Fix up linting in ripper translation https://github.com/ruby/prism/commit/5cf5f15ee7 --- lib/prism/translation/ripper.rb | 4 ++- lib/prism/translation/ripper/sexp.rb | 32 +++++++++++++-------- lib/prism/translation/ripper/shim.rb | 2 ++ lib/prism/translation/ruby_parser.rb | 42 ++++++++++++++++++---------- 4 files changed, 52 insertions(+), 28 deletions(-) diff --git a/lib/prism/translation/ripper.rb b/lib/prism/translation/ripper.rb index 849c0b8089..7888f3b8d9 100644 --- a/lib/prism/translation/ripper.rb +++ b/lib/prism/translation/ripper.rb @@ -2814,7 +2814,7 @@ module Prism # ^^^^^^^^ def visit_source_line_node(node) bounds(node.location) - on_var_ref(on_kw("__LINE__")) + on_var_ref(on_kw("__LINE__")) end # foo(*bar) @@ -2943,6 +2943,7 @@ module Prism end end + # Visit a heredoc node that is representing a string. private def visit_heredoc_string_node(node) bounds(node.opening_loc) on_heredoc_beg(node.opening) @@ -2959,6 +2960,7 @@ module Prism result end + # Visit a heredoc node that is representing an xstring. private def visit_heredoc_x_string_node(node) bounds(node.opening_loc) on_heredoc_beg(node.opening) diff --git a/lib/prism/translation/ripper/sexp.rb b/lib/prism/translation/ripper/sexp.rb index 9cb717b9d4..32cc63e47a 100644 --- a/lib/prism/translation/ripper/sexp.rb +++ b/lib/prism/translation/ripper/sexp.rb @@ -8,8 +8,10 @@ module Prism # This class mirrors the ::Ripper::SexpBuilder subclass of ::Ripper that # returns the arrays of [type, *children]. class SexpBuilder < Ripper + # :stopdoc: + attr_reader :error - + private def dedent_element(e, width) @@ -18,7 +20,7 @@ module Prism end e end - + def on_heredoc_dedent(val, width) sub = proc do |cont| cont.map! do |e| @@ -38,7 +40,7 @@ module Prism sub[val] val end - + events = private_instance_methods(false).grep(/\Aon_/) {$'.to_sym} (PARSER_EVENTS - events).each do |event| module_eval(<<-End, __FILE__, __LINE__ + 1) @@ -47,7 +49,7 @@ module Prism end End end - + SCANNER_EVENTS.each do |event| module_eval(<<-End, __FILE__, __LINE__ + 1) def on_#{event}(tok) @@ -55,21 +57,25 @@ module Prism end End end - + def on_error(mesg) @error = mesg end remove_method :on_parse_error alias on_parse_error on_error alias compile_error on_error + + # :startdoc: end # This class mirrors the ::Ripper::SexpBuilderPP subclass of ::Ripper that # returns the same values as ::Ripper::SexpBuilder except with a couple of # niceties that flatten linked lists into arrays. class SexpBuilderPP < SexpBuilder + # :stopdoc: + private - + def on_heredoc_dedent(val, width) val.map! do |e| next e if Symbol === e and /_content\z/ =~ e @@ -82,28 +88,28 @@ module Prism end val end - + def _dispatch_event_new [] end - + def _dispatch_event_push(list, item) list.push item list end - + def on_mlhs_paren(list) [:mlhs, *list] end - + def on_mlhs_add_star(list, star) list.push([:rest_param, star]) end - + def on_mlhs_add_post(list, post) list.concat(post) end - + PARSER_EVENT_TABLE.each do |event, arity| if /_new\z/ =~ event and arity == 0 alias_method "on_#{event}", :_dispatch_event_new @@ -111,6 +117,8 @@ module Prism alias_method "on_#{event}", :_dispatch_event_push end end + + # :startdoc: end end end diff --git a/lib/prism/translation/ripper/shim.rb b/lib/prism/translation/ripper/shim.rb index 195b4c644e..10e21cd16a 100644 --- a/lib/prism/translation/ripper/shim.rb +++ b/lib/prism/translation/ripper/shim.rb @@ -1,3 +1,5 @@ # frozen_string_literal: true +# This writes the prism ripper translation into the Ripper constant so that +# users can transparently use Ripper without any changes. Ripper = Prism::Translation::Ripper diff --git a/lib/prism/translation/ruby_parser.rb b/lib/prism/translation/ruby_parser.rb index 176c777f47..4664061841 100644 --- a/lib/prism/translation/ruby_parser.rb +++ b/lib/prism/translation/ruby_parser.rb @@ -6,7 +6,7 @@ module Prism module Translation # This module is the entry-point for converting a prism syntax tree into the # seattlerb/ruby_parser gem's syntax tree. - module RubyParser + class RubyParser # A prism visitor that builds Sexp objects. class Compiler < ::Prism::Compiler # This is the name of the file that we are compiling. We set it on every @@ -1490,31 +1490,43 @@ module Prism private_constant :Compiler + # Parse the given source and translate it into the seattlerb/ruby_parser + # gem's Sexp format. + def parse(source, filepath = "(string)") + translate(Prism.parse(source), filepath) + end + + # Parse the given file and translate it into the seattlerb/ruby_parser + # gem's Sexp format. + def parse_file(filepath) + translate(Prism.parse_file(filepath), filepath) + end + class << self # Parse the given source and translate it into the seattlerb/ruby_parser # gem's Sexp format. def parse(source, filepath = "(string)") - translate(Prism.parse(source), filepath) + new.parse(source, filepath) end # Parse the given file and translate it into the seattlerb/ruby_parser # gem's Sexp format. def parse_file(filepath) - translate(Prism.parse_file(filepath), filepath) + new.parse_file(filepath) + end + end + + private + + # Translate the given parse result and filepath into the + # seattlerb/ruby_parser gem's Sexp format. + def translate(result, filepath) + if result.failure? + error = result.errors.first + raise ::RubyParser::SyntaxError, "#{filepath}:#{error.location.start_line} :: #{error.message}" end - private - - # Translate the given parse result and filepath into the - # seattlerb/ruby_parser gem's Sexp format. - def translate(result, filepath) - if result.failure? - error = result.errors.first - raise ::RubyParser::SyntaxError, "#{filepath}:#{error.location.start_line} :: #{error.message}" - end - - result.value.accept(Compiler.new(filepath)) - end + result.value.accept(Compiler.new(filepath)) end end end