[ruby/prism] Fix up linting in ripper translation

https://github.com/ruby/prism/commit/5cf5f15ee7
This commit is contained in:
Kevin Newton 2024-03-06 11:34:38 -05:00 committed by git
parent 1966b5c8c6
commit 532ddc1745
4 changed files with 52 additions and 28 deletions

View File

@ -2814,7 +2814,7 @@ module Prism
# ^^^^^^^^ # ^^^^^^^^
def visit_source_line_node(node) def visit_source_line_node(node)
bounds(node.location) bounds(node.location)
on_var_ref(on_kw("__LINE__")) on_var_ref(on_kw("__LINE__"))
end end
# foo(*bar) # foo(*bar)
@ -2943,6 +2943,7 @@ module Prism
end end
end end
# Visit a heredoc node that is representing a string.
private def visit_heredoc_string_node(node) private def visit_heredoc_string_node(node)
bounds(node.opening_loc) bounds(node.opening_loc)
on_heredoc_beg(node.opening) on_heredoc_beg(node.opening)
@ -2959,6 +2960,7 @@ module Prism
result result
end end
# Visit a heredoc node that is representing an xstring.
private def visit_heredoc_x_string_node(node) private def visit_heredoc_x_string_node(node)
bounds(node.opening_loc) bounds(node.opening_loc)
on_heredoc_beg(node.opening) on_heredoc_beg(node.opening)

View File

@ -8,8 +8,10 @@ module Prism
# This class mirrors the ::Ripper::SexpBuilder subclass of ::Ripper that # This class mirrors the ::Ripper::SexpBuilder subclass of ::Ripper that
# returns the arrays of [type, *children]. # returns the arrays of [type, *children].
class SexpBuilder < Ripper class SexpBuilder < Ripper
# :stopdoc:
attr_reader :error attr_reader :error
private private
def dedent_element(e, width) def dedent_element(e, width)
@ -18,7 +20,7 @@ module Prism
end end
e e
end end
def on_heredoc_dedent(val, width) def on_heredoc_dedent(val, width)
sub = proc do |cont| sub = proc do |cont|
cont.map! do |e| cont.map! do |e|
@ -38,7 +40,7 @@ module Prism
sub[val] sub[val]
val val
end end
events = private_instance_methods(false).grep(/\Aon_/) {$'.to_sym} events = private_instance_methods(false).grep(/\Aon_/) {$'.to_sym}
(PARSER_EVENTS - events).each do |event| (PARSER_EVENTS - events).each do |event|
module_eval(<<-End, __FILE__, __LINE__ + 1) module_eval(<<-End, __FILE__, __LINE__ + 1)
@ -47,7 +49,7 @@ module Prism
end end
End End
end end
SCANNER_EVENTS.each do |event| SCANNER_EVENTS.each do |event|
module_eval(<<-End, __FILE__, __LINE__ + 1) module_eval(<<-End, __FILE__, __LINE__ + 1)
def on_#{event}(tok) def on_#{event}(tok)
@ -55,21 +57,25 @@ module Prism
end end
End End
end end
def on_error(mesg) def on_error(mesg)
@error = mesg @error = mesg
end end
remove_method :on_parse_error remove_method :on_parse_error
alias on_parse_error on_error alias on_parse_error on_error
alias compile_error on_error alias compile_error on_error
# :startdoc:
end end
# This class mirrors the ::Ripper::SexpBuilderPP subclass of ::Ripper that # This class mirrors the ::Ripper::SexpBuilderPP subclass of ::Ripper that
# returns the same values as ::Ripper::SexpBuilder except with a couple of # returns the same values as ::Ripper::SexpBuilder except with a couple of
# niceties that flatten linked lists into arrays. # niceties that flatten linked lists into arrays.
class SexpBuilderPP < SexpBuilder class SexpBuilderPP < SexpBuilder
# :stopdoc:
private private
def on_heredoc_dedent(val, width) def on_heredoc_dedent(val, width)
val.map! do |e| val.map! do |e|
next e if Symbol === e and /_content\z/ =~ e next e if Symbol === e and /_content\z/ =~ e
@ -82,28 +88,28 @@ module Prism
end end
val val
end end
def _dispatch_event_new def _dispatch_event_new
[] []
end end
def _dispatch_event_push(list, item) def _dispatch_event_push(list, item)
list.push item list.push item
list list
end end
def on_mlhs_paren(list) def on_mlhs_paren(list)
[:mlhs, *list] [:mlhs, *list]
end end
def on_mlhs_add_star(list, star) def on_mlhs_add_star(list, star)
list.push([:rest_param, star]) list.push([:rest_param, star])
end end
def on_mlhs_add_post(list, post) def on_mlhs_add_post(list, post)
list.concat(post) list.concat(post)
end end
PARSER_EVENT_TABLE.each do |event, arity| PARSER_EVENT_TABLE.each do |event, arity|
if /_new\z/ =~ event and arity == 0 if /_new\z/ =~ event and arity == 0
alias_method "on_#{event}", :_dispatch_event_new alias_method "on_#{event}", :_dispatch_event_new
@ -111,6 +117,8 @@ module Prism
alias_method "on_#{event}", :_dispatch_event_push alias_method "on_#{event}", :_dispatch_event_push
end end
end end
# :startdoc:
end end
end end
end end

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true # 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 Ripper = Prism::Translation::Ripper

View File

@ -6,7 +6,7 @@ module Prism
module Translation module Translation
# This module is the entry-point for converting a prism syntax tree into the # This module is the entry-point for converting a prism syntax tree into the
# seattlerb/ruby_parser gem's syntax tree. # seattlerb/ruby_parser gem's syntax tree.
module RubyParser class RubyParser
# A prism visitor that builds Sexp objects. # A prism visitor that builds Sexp objects.
class Compiler < ::Prism::Compiler class Compiler < ::Prism::Compiler
# This is the name of the file that we are compiling. We set it on every # 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 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 class << self
# Parse the given source and translate it into the seattlerb/ruby_parser # Parse the given source and translate it into the seattlerb/ruby_parser
# gem's Sexp format. # gem's Sexp format.
def parse(source, filepath = "(string)") def parse(source, filepath = "(string)")
translate(Prism.parse(source), filepath) new.parse(source, filepath)
end end
# Parse the given file and translate it into the seattlerb/ruby_parser # Parse the given file and translate it into the seattlerb/ruby_parser
# gem's Sexp format. # gem's Sexp format.
def parse_file(filepath) 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 end
private result.value.accept(Compiler.new(filepath))
# 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
end end
end end
end end