Skip to content

Commit

Permalink
Fix up CI
Browse files Browse the repository at this point in the history
  • Loading branch information
kddnewton committed Jan 30, 2024
1 parent ff68c09 commit 224ea85
Showing 1 changed file with 35 additions and 18 deletions.
53 changes: 35 additions & 18 deletions lib/prism/translation/parser/compiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ class CompilationError < StandardError
# offsets in the file.
attr_reader :offset_cache

# The locals in the current scope.
attr_reader :locals
# The types of values that can be forwarded in the current scope.
attr_reader :forwarding

# Whether or not the current node is in a destructure.
attr_reader :in_destructure
Expand All @@ -36,13 +36,13 @@ class CompilationError < StandardError

# Initialize a new compiler with the given parser, offset cache, and
# options.
def initialize(parser, offset_cache, locals: nil, in_destructure: false, in_pattern: false)
def initialize(parser, offset_cache, forwarding: [], in_destructure: false, in_pattern: false)
@parser = parser
@builder = parser.builder
@source_buffer = parser.source_buffer
@offset_cache = offset_cache

@locals = locals
@forwarding = forwarding
@in_destructure = in_destructure
@in_pattern = in_pattern
end
Expand Down Expand Up @@ -135,7 +135,7 @@ def visit_assoc_node(node)
# { **foo }
# ^^^^^
def visit_assoc_splat_node(node)
if node.value.nil? && locals.include?(:**)
if node.value.nil? && forwarding.include?(:**)
builder.forwarded_kwrestarg(token(node.operator_loc))
else
builder.kwsplat(token(node.operator_loc), visit(node.value))
Expand Down Expand Up @@ -372,7 +372,7 @@ def visit_class_node(node)
visit(node.constant_path),
token(node.inheritance_operator_loc),
visit(node.superclass),
node.body&.accept(copy_compiler(locals: node.locals)),
node.body&.accept(copy_compiler(forwarding: [])),
token(node.end_keyword_loc)
)
end
Expand Down Expand Up @@ -519,6 +519,8 @@ def visit_constant_path_target_node(node)
# def self.foo; end
# ^^^^^^^^^^^^^^^^^
def visit_def_node(node)
forwarding = find_forwarding(node.parameters)

if node.equal_loc
if node.receiver
builder.def_endless_singleton(
Expand All @@ -528,15 +530,15 @@ def visit_def_node(node)
token(node.name_loc),
builder.args(token(node.lparen_loc), visit(node.parameters) || [], token(node.rparen_loc), false),
token(node.equal_loc),
node.body&.accept(copy_compiler(locals: node.locals))
node.body&.accept(copy_compiler(forwarding: forwarding))
)
else
builder.def_endless_method(
token(node.def_keyword_loc),
token(node.name_loc),
builder.args(token(node.lparen_loc), visit(node.parameters) || [], token(node.rparen_loc), false),
token(node.equal_loc),
node.body&.accept(copy_compiler(locals: node.locals))
node.body&.accept(copy_compiler(forwarding: forwarding))
)
end
elsif node.receiver
Expand All @@ -546,15 +548,15 @@ def visit_def_node(node)
token(node.operator_loc),
token(node.name_loc),
builder.args(token(node.lparen_loc), visit(node.parameters) || [], token(node.rparen_loc), false),
node.body&.accept(copy_compiler(locals: node.locals)),
node.body&.accept(copy_compiler(forwarding: forwarding)),
token(node.end_keyword_loc)
)
else
builder.def_method(
token(node.def_keyword_loc),
token(node.name_loc),
builder.args(token(node.lparen_loc), visit(node.parameters) || [], token(node.rparen_loc), false),
node.body&.accept(copy_compiler(locals: node.locals)),
node.body&.accept(copy_compiler(forwarding: forwarding)),
token(node.end_keyword_loc)
)
end
Expand Down Expand Up @@ -1008,7 +1010,7 @@ def visit_lambda_node(node)
else
builder.args(nil, [], nil, false)
end,
node.body&.accept(copy_compiler(locals: node.locals)),
node.body&.accept(copy_compiler(forwarding: find_forwarding(node.parameters&.parameters))),
[node.closing, srange(node.closing_loc)]
)
end
Expand Down Expand Up @@ -1103,7 +1105,7 @@ def visit_module_node(node)
builder.def_module(
token(node.module_keyword_loc),
visit(node.constant_path),
node.body&.accept(copy_compiler(locals: node.locals)),
node.body&.accept(copy_compiler(forwarding: [])),
token(node.end_keyword_loc)
)
end
Expand Down Expand Up @@ -1284,7 +1286,7 @@ def visit_pre_execution_node(node)

# The top-level program node.
def visit_program_node(node)
node.statements.accept(copy_compiler(locals: node.locals))
visit(node.statements)
end

# 0..5
Expand Down Expand Up @@ -1415,7 +1417,7 @@ def visit_singleton_class_node(node)
token(node.class_keyword_loc),
token(node.operator_loc),
visit(node.expression),
node.body&.accept(copy_compiler(locals: node.locals)),
node.body&.accept(copy_compiler(forwarding: [])),
token(node.end_keyword_loc)
)
end
Expand Down Expand Up @@ -1447,7 +1449,7 @@ def visit_source_line_node(node)
# def foo(*); bar(*); end
# ^
def visit_splat_node(node)
if node.expression.nil? && locals.include?(:*)
if node.expression.nil? && forwarding.include?(:*)
builder.forwarded_restarg(token(node.operator_loc))
elsif in_destructure
builder.restarg(token(node.operator_loc), token(node.expression&.location))
Expand Down Expand Up @@ -1658,8 +1660,23 @@ def visit_yield_node(node)

# Initialize a new compiler with the given option overrides, used to
# visit a subtree with the given options.
def copy_compiler(locals: self.locals, in_destructure: self.in_destructure, in_pattern: self.in_pattern)
Compiler.new(parser, offset_cache, locals: locals, in_destructure: in_destructure, in_pattern: in_pattern)
def copy_compiler(forwarding: self.forwarding, in_destructure: self.in_destructure, in_pattern: self.in_pattern)
Compiler.new(parser, offset_cache, forwarding: forwarding, in_destructure: in_destructure, in_pattern: in_pattern)
end

# When *, **, &, or ... are used as an argument in a method call, we
# check if they were allowed by the current context. To determine that
# we build this lookup table.
def find_forwarding(node)
return [] if node.nil?

forwarding = []
forwarding << :* if node.rest.is_a?(RestParameterNode) && node.rest.name.nil?
forwarding << :** if node.keyword_rest.is_a?(KeywordRestParameterNode) && node.keyword_rest.name.nil?
forwarding << :& if !node.block.nil? && node.block.name.nil?
forwarding |= [:&, :"..."] if node.keyword_rest.is_a?(ForwardingParameterNode)

forwarding
end

# Blocks can have a special set of parameters that automatically expand
Expand Down Expand Up @@ -1732,7 +1749,7 @@ def visit_block(call, block)
else
builder.args(nil, [], nil, false)
end,
visit(block.body),
block.body&.accept(copy_compiler(forwarding: find_forwarding(block.parameters&.parameters))),
token(block.closing_loc)
)
else
Expand Down

0 comments on commit 224ea85

Please sign in to comment.