[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)
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)

View File

@ -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

View File

@ -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

View File

@ -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