Debian Codegraph @

lib/prism/translation/parser/builder.rb

raw symbols SCIP
1 # frozen_string_literal: true
2 # :markup: markdown
3
4 module Prism
5 module Translation
6 class Parser
7 # A builder that knows how to convert more modern Ruby syntax
8 # into whitequark/parser gem's syntax tree.
9 class Builder < ::Parser::Builders::Default
10 # It represents the `it` block argument, which is not yet implemented in the Parser gem.
11 def itarg
12 n(:itarg, [:it], nil)
13 end
14
15 # The following three lines have been added to support the `it` block parameter syntax in the source code below.
16 #
17 # if args.type == :itarg
18 # block_type = :itblock
19 # args = :it
20 #
21 # https://github.com/whitequark/parser/blob/v3.3.7.1/lib/parser/builders/default.rb#L1122-L1155
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 # Code like "return foo 1 do end" is reduced in a weird sequence.
49 # Here, method_call is actually (return).
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