diff --git a/lib/prism/translation/ruby_parser.rb b/lib/prism/translation/ruby_parser.rb index 42bc5ee658..505d6c21f8 100644 --- a/lib/prism/translation/ruby_parser.rb +++ b/lib/prism/translation/ruby_parser.rb @@ -1,6 +1,8 @@ # frozen_string_literal: true # :markup: markdown +require "timeout" + begin require "sexp" rescue LoadError @@ -1560,7 +1562,7 @@ def attach_comments(sexp, node) # Create a new compiler with the given options. def copy_compiler(in_def: self.in_def, in_pattern: self.in_pattern) - Compiler.new(file, in_def: in_def, in_pattern: in_pattern) + self.class.new(file, in_def: in_def, in_pattern: in_pattern) end # Create a new Sexp object from the given prism node and arguments. @@ -1622,18 +1624,23 @@ def visit_write_value(node) end end - private_constant :Compiler + attr_accessor :scopes # :nodoc: + + def initialize scopes:nil # :nodoc: + super() + self.scopes = [scopes] if scopes + end # 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: filepath, partial_script: true), filepath) + translate(Prism.parse(source, filepath: filepath, partial_script: true, scopes: scopes), 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, partial_script: true), filepath) + translate(Prism.parse_file(filepath, partial_script: true, scopes: scopes), filepath) end # Parse the give file and translate it into the @@ -1647,14 +1654,14 @@ def process(ruby, file = "(string)", timeout = nil) class << self # Parse the given source and translate it into the seattlerb/ruby_parser # gem's Sexp format. - def parse(source, filepath = "(string)") - new.parse(source, filepath) + def parse(source, filepath = "(string)", scopes: nil) + new(scopes: scopes).parse(source, filepath) end # Parse the given file and translate it into the seattlerb/ruby_parser # gem's Sexp format. - def parse_file(filepath) - new.parse_file(filepath) + def parse_file(filepath, scopes: nil) + new(scopes: scopes).parse_file(filepath) end end @@ -1669,7 +1676,7 @@ def translate(result, filepath) end result.attach_comments! - result.value.accept(Compiler.new(filepath)) + result.value.accept(self.class::Compiler.new(filepath)) end end end diff --git a/rakelib/seattlerb.rake b/rakelib/seattlerb.rake index 217d64ef4b..44708757a2 100644 --- a/rakelib/seattlerb.rake +++ b/rakelib/seattlerb.rake @@ -65,7 +65,9 @@ namespace :seattlerb do def assert_parse(source, _) entry = caller.find { _1.include?("test_ruby_parser.rb") } - name = entry[/\d+:in `(?:block (?:\(\d+ levels\) )?in )?(?:assert_|test_|?'/, 1] + + name = entry[/\d+:in [`'](?:block (?:\(\d+ levels\) )?in )?(?:assert_|test_|?'/, 1] + .sub(/^Test.+#(?:test|assert)_/, '') # 4.x backtrace incl class COLLECTED[name] << source super