Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 16 additions & 9 deletions lib/prism/translation/ruby_parser.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# frozen_string_literal: true
# :markup: markdown

require "timeout"

begin
require "sexp"
rescue LoadError
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand All @@ -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

Expand All @@ -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
Expand Down
4 changes: 3 additions & 1 deletion rakelib/seattlerb.rake
Original file line number Diff line number Diff line change
Expand Up @@ -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_|<module:)?(.+?)\>?'/, 1]

name = entry[/\d+:in [`'](?:block (?:\(\d+ levels\) )?in )?(?:assert_|test_|<module:)?(.+?)\>?'/, 1]
.sub(/^Test.+#(?:test|assert)_/, '') # 4.x backtrace incl class

COLLECTED[name] << source
super
Expand Down
Loading