| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
module Prism |
| 5 |
module Translation |
| 6 |
class Parser |
| 7 |
|
| 8 |
|
| 9 |
class Builder < ::Parser::Builders::Default |
| 10 |
|
| 11 |
def itarg |
| 12 |
n(:itarg, [:it], nil) |
| 13 |
end |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
def block(method_call, begin_t, args, body, end_t) |
| 23 |
_receiver, _selector, *call_args = *method_call |
| 24 |
|
| 25 |
if method_call.type == :yield |
| 26 |
diagnostic :error, :block_given_to_yield, nil, method_call.loc.keyword, [loc(begin_t)] |
| 27 |
end |
| 28 |
|
| 29 |
last_arg = call_args.last |
| 30 |
if last_arg && (last_arg.type == :block_pass || last_arg.type == :forwarded_args) |
| 31 |
diagnostic :error, :block_and_blockarg, nil, last_arg.loc.expression, [loc(begin_t)] |
| 32 |
end |
| 33 |
|
| 34 |
if args.type == :itarg |
| 35 |
block_type = :itblock |
| 36 |
args = :it |
| 37 |
elsif args.type == :numargs |
| 38 |
block_type = :numblock |
| 39 |
args = args.children[0] |
| 40 |
else |
| 41 |
block_type = :block |
| 42 |
end |
| 43 |
|
| 44 |
if [:send, :csend, :index, :super, :zsuper, :lambda].include?(method_call.type) |
| 45 |
n(block_type, [ method_call, args, body ], |
| 46 |
block_map(method_call.loc.expression, begin_t, end_t)) |
| 47 |
else |
| 48 |
|
| 49 |
|
| 50 |
actual_send, = *method_call |
| 51 |
block = |
| 52 |
n(block_type, [ actual_send, args, body ], |
| 53 |
block_map(actual_send.loc.expression, begin_t, end_t)) |
| 54 |
|
| 55 |
n(method_call.type, [ block ], |
| 56 |
method_call.loc.with_expression(join_exprs(method_call, block))) |
| 57 |
end |
| 58 |
end |
| 59 |
end |
| 60 |
end |
| 61 |
end |
| 62 |
end |