Class: Prism::Translation::Parser::Compiler

Inherits:
Compiler show all
Defined in:
lib/prism/translation/parser/compiler.rb

Overview

A visitor that knows how to convert a prism syntax tree into the whitequark/parser gem’s syntax tree.

Defined Under Namespace

Classes: CompilationError

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from BasicVisitor

#visit, #visit_all, #visit_child_nodes

Constructor Details

#initialize(parser, offset_cache, forwarding: [], in_destructure: false, in_pattern: false) ⇒ Compiler

Initialize a new compiler with the given parser, offset cache, and options.



39
40
41
42
43
44
45
46
47
48
# File 'lib/prism/translation/parser/compiler.rb', line 39

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

  @forwarding = forwarding
  @in_destructure = in_destructure
  @in_pattern = in_pattern
end

Instance Attribute Details

#builderObject (readonly)

The Parser::Builders::Default instance that is being used to build the AST.



18
19
20
# File 'lib/prism/translation/parser/compiler.rb', line 18

def builder
  @builder
end

#forwardingObject (readonly)

The types of values that can be forwarded in the current scope.



29
30
31
# File 'lib/prism/translation/parser/compiler.rb', line 29

def forwarding
  @forwarding
end

#in_destructureObject (readonly)

Whether or not the current node is in a destructure.



32
33
34
# File 'lib/prism/translation/parser/compiler.rb', line 32

def in_destructure
  @in_destructure
end

#in_patternObject (readonly)

Whether or not the current node is in a pattern.



35
36
37
# File 'lib/prism/translation/parser/compiler.rb', line 35

def in_pattern
  @in_pattern
end

#offset_cacheObject (readonly)

The offset cache that is used to map between byte and character offsets in the file.



26
27
28
# File 'lib/prism/translation/parser/compiler.rb', line 26

def offset_cache
  @offset_cache
end

#parserObject (readonly)

The Parser::Base instance that is being used to build the AST.



14
15
16
# File 'lib/prism/translation/parser/compiler.rb', line 14

def parser
  @parser
end

#source_bufferObject (readonly)

The Parser::Source::Buffer instance that is holding a reference to the source code.



22
23
24
# File 'lib/prism/translation/parser/compiler.rb', line 22

def source_buffer
  @source_buffer
end

Instance Method Details

#visit_alias_global_variable_node(node) ⇒ Object

alias $foo $bar ^^^^^^^^^^^^^^^



58
59
60
# File 'lib/prism/translation/parser/compiler.rb', line 58

def visit_alias_global_variable_node(node)
  builder.alias(token(node.keyword_loc), visit(node.new_name), visit(node.old_name))
end

#visit_alias_method_node(node) ⇒ Object

alias foo bar ^^^^^^^^^^^^^



52
53
54
# File 'lib/prism/translation/parser/compiler.rb', line 52

def visit_alias_method_node(node)
  builder.alias(token(node.keyword_loc), visit(node.new_name), visit(node.old_name))
end

#visit_alternation_pattern_node(node) ⇒ Object

foo => bar | baz

^^^^^^^^^


64
65
66
# File 'lib/prism/translation/parser/compiler.rb', line 64

def visit_alternation_pattern_node(node)
  builder.match_alt(visit(node.left), token(node.operator_loc), visit(node.right))
end

#visit_and_node(node) ⇒ Object

a and b ^^^^^^^



70
71
72
# File 'lib/prism/translation/parser/compiler.rb', line 70

def visit_and_node(node)
  builder.logical_op(:and, visit(node.left), token(node.operator_loc), visit(node.right))
end

#visit_arguments_node(node) ⇒ Object

foo(bar)

^^^


105
106
107
# File 'lib/prism/translation/parser/compiler.rb', line 105

def visit_arguments_node(node)
  visit_all(node.arguments)
end

#visit_array_node(node) ⇒ Object

^^



76
77
78
# File 'lib/prism/translation/parser/compiler.rb', line 76

def visit_array_node(node)
  builder.array(token(node.opening_loc), visit_all(node.elements), token(node.closing_loc))
end

#visit_array_pattern_node(node) ⇒ Object

foo => [bar]

^^^^^


82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/prism/translation/parser/compiler.rb', line 82

def visit_array_pattern_node(node)
  elements = [*node.requireds]
  elements << node.rest if !node.rest.nil? && !node.rest.is_a?(ImplicitRestNode)
  elements.concat(node.posts)
  visited = visit_all(elements)

  if node.rest.is_a?(ImplicitRestNode)
    visited[-1] = builder.match_with_trailing_comma(visited[-1], token(node.rest.location))
  end

  if node.constant
    if visited.empty?
      builder.const_pattern(visit(node.constant), token(node.opening_loc), builder.array_pattern(token(node.opening_loc), visited, token(node.closing_loc)), token(node.closing_loc))
    else
      builder.const_pattern(visit(node.constant), token(node.opening_loc), builder.array_pattern(nil, visited, nil), token(node.closing_loc))
    end
  else
    builder.array_pattern(token(node.opening_loc), visited, token(node.closing_loc))
  end
end

#visit_assoc_node(node) ⇒ Object

{ a: 1 }

^^^^


111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/prism/translation/parser/compiler.rb', line 111

def visit_assoc_node(node)
  key = node.key

  if in_pattern
    if node.value.is_a?(ImplicitNode)
      if key.is_a?(SymbolNode)
        if key.opening.nil?
          builder.match_hash_var([key.unescaped, srange(key.location)])
        else
          builder.match_hash_var_from_str(token(key.opening_loc), [builder.string_internal([key.unescaped, srange(key.value_loc)])], token(key.closing_loc))
        end
      else
        builder.match_hash_var_from_str(token(key.opening_loc), visit_all(key.parts), token(key.closing_loc))
      end
    elsif key.opening.nil?
      builder.pair_keyword([key.unescaped, srange(key.location)], visit(node.value))
    else
      builder.pair_quoted(token(key.opening_loc), [builder.string_internal([key.unescaped, srange(key.value_loc)])], token(key.closing_loc), visit(node.value))
    end
  elsif node.value.is_a?(ImplicitNode)
    if (value = node.value.value).is_a?(LocalVariableReadNode)
      builder.pair_keyword(
        [key.unescaped, srange(key)],
        builder.ident([value.name, srange(key.value_loc)]).updated(:lvar)
      )
    else
      builder.pair_label([key.unescaped, srange(key.location)])
    end
  elsif node.operator_loc
    builder.pair(visit(key), token(node.operator_loc), visit(node.value))
  elsif key.is_a?(SymbolNode) && key.opening_loc.nil?
    builder.pair_keyword([key.unescaped, srange(key.location)], visit(node.value))
  else
    parts =
      if key.is_a?(SymbolNode)
        [builder.string_internal([key.unescaped, srange(key.value_loc)])]
      else
        visit_all(key.parts)
      end

    builder.pair_quoted(token(key.opening_loc), parts, token(key.closing_loc), visit(node.value))
  end
end

#visit_assoc_splat_node(node) ⇒ Object

def foo(**); bar(**); end

^^

{ **foo }

^^^^^


160
161
162
163
164
165
166
167
168
# File 'lib/prism/translation/parser/compiler.rb', line 160

def visit_assoc_splat_node(node)
  if in_pattern
    builder.match_rest(token(node.operator_loc), token(node.value&.location))
  elsif node.value.nil? && forwarding.include?(:**)
    builder.forwarded_kwrestarg(token(node.operator_loc))
  else
    builder.kwsplat(token(node.operator_loc), visit(node.value))
  end
end

#visit_back_reference_read_node(node) ⇒ Object

$+ ^^



172
173
174
# File 'lib/prism/translation/parser/compiler.rb', line 172

def visit_back_reference_read_node(node)
  builder.back_ref(token(node.location))
end

#visit_begin_node(node) ⇒ Object

begin end ^^^^^^^^^



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/prism/translation/parser/compiler.rb', line 178

def visit_begin_node(node)
  rescue_bodies = []

  if (rescue_clause = node.rescue_clause)
    begin
      find_start_offset = (rescue_clause.reference&.location || rescue_clause.exceptions.last&.location || rescue_clause.keyword_loc).end_offset
      find_end_offset = (rescue_clause.statements&.location&.start_offset || rescue_clause.subsequent&.location&.start_offset || (find_start_offset + 1))

      rescue_bodies << builder.rescue_body(
        token(rescue_clause.keyword_loc),
        rescue_clause.exceptions.any? ? builder.array(nil, visit_all(rescue_clause.exceptions), nil) : nil,
        token(rescue_clause.operator_loc),
        visit(rescue_clause.reference),
        srange_find(find_start_offset, find_end_offset, [";"]),
        visit(rescue_clause.statements)
      )
    end until (rescue_clause = rescue_clause.subsequent).nil?
  end

  begin_body =
    builder.begin_body(
      visit(node.statements),
      rescue_bodies,
      token(node.else_clause&.else_keyword_loc),
      visit(node.else_clause),
      token(node.ensure_clause&.ensure_keyword_loc),
      visit(node.ensure_clause&.statements)
    )

  if node.begin_keyword_loc
    builder.begin_keyword(token(node.begin_keyword_loc), begin_body, token(node.end_keyword_loc))
  else
    begin_body
  end
end

#visit_block_argument_node(node) ⇒ Object

foo(&bar)

^^^^


216
217
218
# File 'lib/prism/translation/parser/compiler.rb', line 216

def visit_block_argument_node(node)
  builder.block_pass(token(node.operator_loc), visit(node.expression))
end

#visit_block_local_variable_node(node) ⇒ Object

foo { |; bar| }

^^^


222
223
224
# File 'lib/prism/translation/parser/compiler.rb', line 222

def visit_block_local_variable_node(node)
  builder.shadowarg(token(node.location))
end

#visit_block_node(node) ⇒ Object

A block on a keyword or method call.

Raises:



227
228
229
# File 'lib/prism/translation/parser/compiler.rb', line 227

def visit_block_node(node)
  raise CompilationError, "Cannot directly compile block nodes"
end

#visit_block_parameter_node(node) ⇒ Object

def foo(&bar); end

^^^^


233
234
235
# File 'lib/prism/translation/parser/compiler.rb', line 233

def visit_block_parameter_node(node)
  builder.blockarg(token(node.operator_loc), token(node.name_loc))
end

#visit_block_parameters_node(node) ⇒ Object

A block’s parameters.



238
239
240
# File 'lib/prism/translation/parser/compiler.rb', line 238

def visit_block_parameters_node(node)
  [*visit(node.parameters)].concat(visit_all(node.locals))
end

#visit_break_node(node) ⇒ Object

break ^^^^^

break foo ^^^^^^^^^



247
248
249
# File 'lib/prism/translation/parser/compiler.rb', line 247

def visit_break_node(node)
  builder.keyword_cmd(:break, token(node.keyword_loc), nil, visit(node.arguments) || [], nil)
end

#visit_call_and_write_node(node) ⇒ Object

foo.bar &&= baz ^^^^^^^^^^^^^^^



352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
# File 'lib/prism/translation/parser/compiler.rb', line 352

def visit_call_and_write_node(node)
  call_operator_loc = node.call_operator_loc

  builder.op_assign(
    builder.call_method(
      visit(node.receiver),
      call_operator_loc.nil? ? nil : [{ "." => :dot, "&." => :anddot, "::" => "::" }.fetch(call_operator_loc.slice), srange(call_operator_loc)],
      node.message_loc ? [node.read_name, srange(node.message_loc)] : nil,
      nil,
      [],
      nil
    ),
    [node.operator_loc.slice.chomp("="), srange(node.operator_loc)],
    visit(node.value)
  )
end

#visit_call_node(node) ⇒ Object

foo ^^^

foo.bar ^^^^^^^

foo.bar() {} ^^^^^^^^^^^^



259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
# File 'lib/prism/translation/parser/compiler.rb', line 259

def visit_call_node(node)
  name = node.name
  arguments = node.arguments&.arguments || []
  block = node.block

  if block.is_a?(BlockArgumentNode)
    arguments = [*arguments, block]
    block = nil
  end

  if node.call_operator_loc.nil?
    case name
    when :-@
      case (receiver = node.receiver).type
      when :integer_node, :float_node, :rational_node, :imaginary_node
        return visit(numeric_negate(node.message_loc, receiver))
      end
    when :!
      return visit_block(builder.not_op(token(node.message_loc), token(node.opening_loc), visit(node.receiver), token(node.closing_loc)), block)
    when :=~
      if (receiver = node.receiver).is_a?(RegularExpressionNode)
        return builder.match_op(visit(receiver), token(node.message_loc), visit(node.arguments.arguments.first))
      end
    when :[]
      return visit_block(builder.index(visit(node.receiver), token(node.opening_loc), visit_all(arguments), token(node.closing_loc)), block)
    when :[]=
      if node.message != "[]=" && node.arguments && block.nil? && !node.safe_navigation?
        arguments = node.arguments.arguments[...-1]
        arguments << node.block if node.block

        return visit_block(
          builder.assign(
            builder.index_asgn(
              visit(node.receiver),
              token(node.opening_loc),
              visit_all(arguments),
              token(node.closing_loc),
            ),
            srange_find(node.message_loc.end_offset, node.arguments.arguments.last.location.start_offset, ["="]),
            visit(node.arguments.arguments.last)
          ),
          block
        )
      end
    end
  end

  message_loc = node.message_loc
  call_operator_loc = node.call_operator_loc
  call_operator = [{ "." => :dot, "&." => :anddot, "::" => "::" }.fetch(call_operator_loc.slice), srange(call_operator_loc)] if call_operator_loc

  visit_block(
    if name.end_with?("=") && !message_loc.slice.end_with?("=") && node.arguments && block.nil?
      builder.assign(
        builder.attr_asgn(visit(node.receiver), call_operator, token(message_loc)),
        srange_find(message_loc.end_offset, node.arguments.location.start_offset, ["="]),
        visit(node.arguments.arguments.last)
      )
    else
      builder.call_method(
        visit(node.receiver),
        call_operator,
        message_loc ? [node.name, srange(message_loc)] : nil,
        token(node.opening_loc),
        visit_all(arguments),
        token(node.closing_loc)
      )
    end,
    block
  )
end

#visit_call_operator_write_node(node) ⇒ Object

foo.bar += baz ^^^^^^^^^^^^^^^



333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
# File 'lib/prism/translation/parser/compiler.rb', line 333

def visit_call_operator_write_node(node)
  call_operator_loc = node.call_operator_loc

  builder.op_assign(
    builder.call_method(
      visit(node.receiver),
      call_operator_loc.nil? ? nil : [{ "." => :dot, "&." => :anddot, "::" => "::" }.fetch(call_operator_loc.slice), srange(call_operator_loc)],
      node.message_loc ? [node.read_name, srange(node.message_loc)] : nil,
      nil,
      [],
      nil
    ),
    [node.binary_operator_loc.slice.chomp("="), srange(node.binary_operator_loc)],
    visit(node.value)
  )
end

#visit_call_or_write_node(node) ⇒ Object

foo.bar ||= baz ^^^^^^^^^^^^^^^



371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
# File 'lib/prism/translation/parser/compiler.rb', line 371

def visit_call_or_write_node(node)
  call_operator_loc = node.call_operator_loc

  builder.op_assign(
    builder.call_method(
      visit(node.receiver),
      call_operator_loc.nil? ? nil : [{ "." => :dot, "&." => :anddot, "::" => "::" }.fetch(call_operator_loc.slice), srange(call_operator_loc)],
      node.message_loc ? [node.read_name, srange(node.message_loc)] : nil,
      nil,
      [],
      nil
    ),
    [node.operator_loc.slice.chomp("="), srange(node.operator_loc)],
    visit(node.value)
  )
end

#visit_call_target_node(node) ⇒ Object

foo.bar, = 1 ^^^^^^^



390
391
392
393
394
395
396
397
398
# File 'lib/prism/translation/parser/compiler.rb', line 390

def visit_call_target_node(node)
  call_operator_loc = node.call_operator_loc

  builder.attr_asgn(
    visit(node.receiver),
    call_operator_loc.nil? ? nil : [{ "." => :dot, "&." => :anddot, "::" => "::" }.fetch(call_operator_loc.slice), srange(call_operator_loc)],
    token(node.message_loc)
  )
end

#visit_capture_pattern_node(node) ⇒ Object

foo => bar => baz

^^^^^^^^^^


402
403
404
# File 'lib/prism/translation/parser/compiler.rb', line 402

def visit_capture_pattern_node(node)
  builder.match_as(visit(node.value), token(node.operator_loc), visit(node.target))
end

#visit_case_match_node(node) ⇒ Object

case foo; in bar; end ^^^^^^^^^^^^^^^^^^^^^



421
422
423
424
425
426
427
428
429
430
# File 'lib/prism/translation/parser/compiler.rb', line 421

def visit_case_match_node(node)
  builder.case_match(
    token(node.case_keyword_loc),
    visit(node.predicate),
    visit_all(node.conditions),
    token(node.else_clause&.else_keyword_loc),
    visit(node.else_clause),
    token(node.end_keyword_loc)
  )
end

#visit_case_node(node) ⇒ Object

case foo; when bar; end ^^^^^^^^^^^^^^^^^^^^^^^



408
409
410
411
412
413
414
415
416
417
# File 'lib/prism/translation/parser/compiler.rb', line 408

def visit_case_node(node)
  builder.case(
    token(node.case_keyword_loc),
    visit(node.predicate),
    visit_all(node.conditions),
    token(node.else_clause&.else_keyword_loc),
    visit(node.else_clause),
    token(node.end_keyword_loc)
  )
end

#visit_class_node(node) ⇒ Object

class Foo; end ^^^^^^^^^^^^^^



434
435
436
437
438
439
440
441
442
443
# File 'lib/prism/translation/parser/compiler.rb', line 434

def visit_class_node(node)
  builder.def_class(
    token(node.class_keyword_loc),
    visit(node.constant_path),
    token(node.inheritance_operator_loc),
    visit(node.superclass),
    node.body&.accept(copy_compiler(forwarding: [])),
    token(node.end_keyword_loc)
  )
end

#visit_class_variable_and_write_node(node) ⇒ Object

@@foo &&= bar ^^^^^^^^^^^^^



473
474
475
476
477
478
479
# File 'lib/prism/translation/parser/compiler.rb', line 473

def visit_class_variable_and_write_node(node)
  builder.op_assign(
    builder.assignable(builder.cvar(token(node.name_loc))),
    [node.operator_loc.slice.chomp("="), srange(node.operator_loc)],
    visit(node.value)
  )
end

#visit_class_variable_operator_write_node(node) ⇒ Object

@@foo += bar ^^^^^^^^^^^^



463
464
465
466
467
468
469
# File 'lib/prism/translation/parser/compiler.rb', line 463

def visit_class_variable_operator_write_node(node)
  builder.op_assign(
    builder.assignable(builder.cvar(token(node.name_loc))),
    [node.binary_operator_loc.slice.chomp("="), srange(node.binary_operator_loc)],
    visit(node.value)
  )
end

#visit_class_variable_or_write_node(node) ⇒ Object

@@foo ||= bar ^^^^^^^^^^^^^



483
484
485
486
487
488
489
# File 'lib/prism/translation/parser/compiler.rb', line 483

def visit_class_variable_or_write_node(node)
  builder.op_assign(
    builder.assignable(builder.cvar(token(node.name_loc))),
    [node.operator_loc.slice.chomp("="), srange(node.operator_loc)],
    visit(node.value)
  )
end

#visit_class_variable_read_node(node) ⇒ Object

@@foo ^^^^^



447
448
449
# File 'lib/prism/translation/parser/compiler.rb', line 447

def visit_class_variable_read_node(node)
  builder.cvar(token(node.location))
end

#visit_class_variable_target_node(node) ⇒ Object

@@foo, = bar ^^^^^



493
494
495
# File 'lib/prism/translation/parser/compiler.rb', line 493

def visit_class_variable_target_node(node)
  builder.assignable(builder.cvar(token(node.location)))
end

#visit_class_variable_write_node(node) ⇒ Object

@@foo = 1 ^^^^^^^^^



453
454
455
456
457
458
459
# File 'lib/prism/translation/parser/compiler.rb', line 453

def visit_class_variable_write_node(node)
  builder.assign(
    builder.assignable(builder.cvar(token(node.name_loc))),
    token(node.operator_loc),
    visit(node.value)
  )
end

#visit_constant_and_write_node(node) ⇒ Object

Foo &&= bar ^^^^^^^^^^^^



524
525
526
527
528
529
530
# File 'lib/prism/translation/parser/compiler.rb', line 524

def visit_constant_and_write_node(node)
  builder.op_assign(
    builder.assignable(builder.const([node.name, srange(node.name_loc)])),
    [node.operator_loc.slice.chomp("="), srange(node.operator_loc)],
    visit(node.value)
  )
end

#visit_constant_operator_write_node(node) ⇒ Object

Foo += bar ^^^^^^^^^^^



514
515
516
517
518
519
520
# File 'lib/prism/translation/parser/compiler.rb', line 514

def visit_constant_operator_write_node(node)
  builder.op_assign(
    builder.assignable(builder.const([node.name, srange(node.name_loc)])),
    [node.binary_operator_loc.slice.chomp("="), srange(node.binary_operator_loc)],
    visit(node.value)
  )
end

#visit_constant_or_write_node(node) ⇒ Object

Foo ||= bar ^^^^^^^^^^^^



534
535
536
537
538
539
540
# File 'lib/prism/translation/parser/compiler.rb', line 534

def visit_constant_or_write_node(node)
  builder.op_assign(
    builder.assignable(builder.const([node.name, srange(node.name_loc)])),
    [node.operator_loc.slice.chomp("="), srange(node.operator_loc)],
    visit(node.value)
  )
end

#visit_constant_path_and_write_node(node) ⇒ Object

Foo::Bar &&= baz ^^^^^^^^^^^^^^^^



590
591
592
593
594
595
596
# File 'lib/prism/translation/parser/compiler.rb', line 590

def visit_constant_path_and_write_node(node)
  builder.op_assign(
    builder.assignable(visit(node.target)),
    [node.operator_loc.slice.chomp("="), srange(node.operator_loc)],
    visit(node.value)
  )
end

#visit_constant_path_node(node) ⇒ Object

Foo::Bar ^^^^^^^^



550
551
552
553
554
555
556
557
558
559
560
561
562
563
# File 'lib/prism/translation/parser/compiler.rb', line 550

def visit_constant_path_node(node)
  if node.parent.nil?
    builder.const_global(
      token(node.delimiter_loc),
      [node.name, srange(node.name_loc)]
    )
  else
    builder.const_fetch(
      visit(node.parent),
      token(node.delimiter_loc),
      [node.name, srange(node.name_loc)]
    )
  end
end

#visit_constant_path_operator_write_node(node) ⇒ Object

Foo::Bar += baz ^^^^^^^^^^^^^^^



580
581
582
583
584
585
586
# File 'lib/prism/translation/parser/compiler.rb', line 580

def visit_constant_path_operator_write_node(node)
  builder.op_assign(
    builder.assignable(visit(node.target)),
    [node.binary_operator_loc.slice.chomp("="), srange(node.binary_operator_loc)],
    visit(node.value)
  )
end

#visit_constant_path_or_write_node(node) ⇒ Object

Foo::Bar ||= baz ^^^^^^^^^^^^^^^^



600
601
602
603
604
605
606
# File 'lib/prism/translation/parser/compiler.rb', line 600

def visit_constant_path_or_write_node(node)
  builder.op_assign(
    builder.assignable(visit(node.target)),
    [node.operator_loc.slice.chomp("="), srange(node.operator_loc)],
    visit(node.value)
  )
end

#visit_constant_path_target_node(node) ⇒ Object

Foo::Bar, = baz ^^^^^^^^



610
611
612
# File 'lib/prism/translation/parser/compiler.rb', line 610

def visit_constant_path_target_node(node)
  builder.assignable(visit_constant_path_node(node))
end

#visit_constant_path_write_node(node) ⇒ Object

Foo::Bar = 1 ^^^^^^^^^^^^

Foo::Foo, Bar::Bar = 1 ^^^^^^^^ ^^^^^^^^



570
571
572
573
574
575
576
# File 'lib/prism/translation/parser/compiler.rb', line 570

def visit_constant_path_write_node(node)
  builder.assign(
    builder.assignable(visit(node.target)),
    token(node.operator_loc),
    visit(node.value)
  )
end

#visit_constant_read_node(node) ⇒ Object

Foo ^^^



499
500
501
# File 'lib/prism/translation/parser/compiler.rb', line 499

def visit_constant_read_node(node)
  builder.const([node.name, srange(node.location)])
end

#visit_constant_target_node(node) ⇒ Object

Foo, = bar ^^^



544
545
546
# File 'lib/prism/translation/parser/compiler.rb', line 544

def visit_constant_target_node(node)
  builder.assignable(builder.const([node.name, srange(node.location)]))
end

#visit_constant_write_node(node) ⇒ Object

Foo = 1 ^^^^^^^

Foo, Bar = 1 ^^^ ^^^



508
509
510
# File 'lib/prism/translation/parser/compiler.rb', line 508

def visit_constant_write_node(node)
  builder.assign(builder.assignable(builder.const([node.name, srange(node.name_loc)])), token(node.operator_loc), visit(node.value))
end

#visit_def_node(node) ⇒ Object

def foo; end ^^^^^^^^^^^^

def self.foo; end ^^^^^^^^^^^^^^^^^



619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
# File 'lib/prism/translation/parser/compiler.rb', line 619

def visit_def_node(node)
  if node.equal_loc
    if node.receiver
      builder.def_endless_singleton(
        token(node.def_keyword_loc),
        visit(node.receiver.is_a?(ParenthesesNode) ? node.receiver.body : node.receiver),
        token(node.operator_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(forwarding: find_forwarding(node.parameters)))
      )
    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(forwarding: find_forwarding(node.parameters)))
      )
    end
  elsif node.receiver
    builder.def_singleton(
      token(node.def_keyword_loc),
      visit(node.receiver.is_a?(ParenthesesNode) ? node.receiver.body : node.receiver),
      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(forwarding: find_forwarding(node.parameters))),
      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(forwarding: find_forwarding(node.parameters))),
      token(node.end_keyword_loc)
    )
  end
end

#visit_defined_node(node) ⇒ Object

defined? a ^^^^^^^^^^

defined?(a) ^^^^^^^^^^^



666
667
668
669
670
671
672
673
674
# File 'lib/prism/translation/parser/compiler.rb', line 666

def visit_defined_node(node)
  builder.keyword_cmd(
    :defined?,
    token(node.keyword_loc),
    token(node.lparen_loc),
    [visit(node.value)],
    token(node.rparen_loc)
  )
end

#visit_else_node(node) ⇒ Object

if foo then bar else baz end

^^^^^^^^^^^^


678
679
680
# File 'lib/prism/translation/parser/compiler.rb', line 678

def visit_else_node(node)
  visit(node.statements)
end

#visit_embedded_statements_node(node) ⇒ Object

“foo #bar”

^^^^^^


684
685
686
687
688
689
690
# File 'lib/prism/translation/parser/compiler.rb', line 684

def visit_embedded_statements_node(node)
  builder.begin(
    token(node.opening_loc),
    visit(node.statements),
    token(node.closing_loc)
  )
end

#visit_embedded_variable_node(node) ⇒ Object

“foo #@bar”

^^^^^


694
695
696
# File 'lib/prism/translation/parser/compiler.rb', line 694

def visit_embedded_variable_node(node)
  visit(node.variable)
end

#visit_ensure_node(node) ⇒ Object

begin; foo; ensure; bar; end

^^^^^^^^^^^^

Raises:



700
701
702
# File 'lib/prism/translation/parser/compiler.rb', line 700

def visit_ensure_node(node)
  raise CompilationError, "Cannot directly compile ensure nodes"
end

#visit_false_node(node) ⇒ Object

false ^^^^^



706
707
708
# File 'lib/prism/translation/parser/compiler.rb', line 706

def visit_false_node(node)
  builder.false(token(node.location))
end

#visit_find_pattern_node(node) ⇒ Object

foo => [*, bar, *]

^^^^^^^^^^^


712
713
714
715
716
717
718
719
720
# File 'lib/prism/translation/parser/compiler.rb', line 712

def visit_find_pattern_node(node)
  elements = [node.left, *node.requireds, node.right]

  if node.constant
    builder.const_pattern(visit(node.constant), token(node.opening_loc), builder.find_pattern(nil, visit_all(elements), nil), token(node.closing_loc))
  else
    builder.find_pattern(token(node.opening_loc), visit_all(elements), token(node.closing_loc))
  end
end

#visit_float_node(node) ⇒ Object

1.0 ^^^



724
725
726
# File 'lib/prism/translation/parser/compiler.rb', line 724

def visit_float_node(node)
  visit_numeric(node, builder.float([node.value, srange(node.location)]))
end

#visit_for_node(node) ⇒ Object

for foo in bar do end ^^^^^^^^^^^^^^^^^^^^^



730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
# File 'lib/prism/translation/parser/compiler.rb', line 730

def visit_for_node(node)
  builder.for(
    token(node.for_keyword_loc),
    visit(node.index),
    token(node.in_keyword_loc),
    visit(node.collection),
    if node.do_keyword_loc
      token(node.do_keyword_loc)
    else
      srange_find(node.collection.location.end_offset, (node.statements&.location || node.end_keyword_loc).start_offset, [";"])
    end,
    visit(node.statements),
    token(node.end_keyword_loc)
  )
end

#visit_forwarding_arguments_node(node) ⇒ Object

def foo(…); bar(…); end

^^^


748
749
750
# File 'lib/prism/translation/parser/compiler.rb', line 748

def visit_forwarding_arguments_node(node)
  builder.forwarded_args(token(node.location))
end

#visit_forwarding_parameter_node(node) ⇒ Object

def foo(…); end

^^^


754
755
756
# File 'lib/prism/translation/parser/compiler.rb', line 754

def visit_forwarding_parameter_node(node)
  builder.forward_arg(token(node.location))
end

#visit_forwarding_super_node(node) ⇒ Object

super ^^^^^

super {} ^^^^^^^^



763
764
765
766
767
768
769
770
771
# File 'lib/prism/translation/parser/compiler.rb', line 763

def visit_forwarding_super_node(node)
  visit_block(
    builder.keyword_cmd(
      :zsuper,
      ["super", srange_offsets(node.location.start_offset, node.location.start_offset + 5)]
    ),
    node.block
  )
end

#visit_global_variable_and_write_node(node) ⇒ Object

$foo &&= bar ^^^^^^^^^^^^



801
802
803
804
805
806
807
# File 'lib/prism/translation/parser/compiler.rb', line 801

def visit_global_variable_and_write_node(node)
  builder.op_assign(
    builder.assignable(builder.gvar(token(node.name_loc))),
    [node.operator_loc.slice.chomp("="), srange(node.operator_loc)],
    visit(node.value)
  )
end

#visit_global_variable_operator_write_node(node) ⇒ Object

$foo += bar ^^^^^^^^^^^



791
792
793
794
795
796
797
# File 'lib/prism/translation/parser/compiler.rb', line 791

def visit_global_variable_operator_write_node(node)
  builder.op_assign(
    builder.assignable(builder.gvar(token(node.name_loc))),
    [node.binary_operator_loc.slice.chomp("="), srange(node.binary_operator_loc)],
    visit(node.value)
  )
end

#visit_global_variable_or_write_node(node) ⇒ Object

$foo ||= bar ^^^^^^^^^^^^



811
812
813
814
815
816
817
# File 'lib/prism/translation/parser/compiler.rb', line 811

def visit_global_variable_or_write_node(node)
  builder.op_assign(
    builder.assignable(builder.gvar(token(node.name_loc))),
    [node.operator_loc.slice.chomp("="), srange(node.operator_loc)],
    visit(node.value)
  )
end

#visit_global_variable_read_node(node) ⇒ Object

$foo ^^^^



775
776
777
# File 'lib/prism/translation/parser/compiler.rb', line 775

def visit_global_variable_read_node(node)
  builder.gvar(token(node.location))
end

#visit_global_variable_target_node(node) ⇒ Object

$foo, = bar ^^^^



821
822
823
# File 'lib/prism/translation/parser/compiler.rb', line 821

def visit_global_variable_target_node(node)
  builder.assignable(builder.gvar([node.slice, srange(node.location)]))
end

#visit_global_variable_write_node(node) ⇒ Object

$foo = 1 ^^^^^^^^



781
782
783
784
785
786
787
# File 'lib/prism/translation/parser/compiler.rb', line 781

def visit_global_variable_write_node(node)
  builder.assign(
    builder.assignable(builder.gvar(token(node.name_loc))),
    token(node.operator_loc),
    visit(node.value)
  )
end

#visit_hash_node(node) ⇒ Object

{} ^^



827
828
829
830
831
832
833
# File 'lib/prism/translation/parser/compiler.rb', line 827

def visit_hash_node(node)
  builder.associate(
    token(node.opening_loc),
    visit_all(node.elements),
    token(node.closing_loc)
  )
end

#visit_hash_pattern_node(node) ⇒ Object

foo => {}

^^


837
838
839
840
841
842
843
844
845
# File 'lib/prism/translation/parser/compiler.rb', line 837

def visit_hash_pattern_node(node)
  elements = [*node.elements, *node.rest]

  if node.constant
    builder.const_pattern(visit(node.constant), token(node.opening_loc), builder.hash_pattern(nil, visit_all(elements), nil), token(node.closing_loc))
  else
    builder.hash_pattern(token(node.opening_loc), visit_all(elements), token(node.closing_loc))
  end
end

#visit_if_node(node) ⇒ Object

if foo then bar end ^^^^^^^^^^^^^^^^^^^

bar if foo ^^^^^^^^^^

foo ? bar : baz ^^^^^^^^^^^^^^^



855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
# File 'lib/prism/translation/parser/compiler.rb', line 855

def visit_if_node(node)
  if !node.if_keyword_loc
    builder.ternary(
      visit(node.predicate),
      token(node.then_keyword_loc),
      visit(node.statements),
      token(node.subsequent.else_keyword_loc),
      visit(node.subsequent)
    )
  elsif node.if_keyword_loc.start_offset == node.location.start_offset
    builder.condition(
      token(node.if_keyword_loc),
      visit(node.predicate),
      if node.then_keyword_loc
        token(node.then_keyword_loc)
      else
        srange_find(node.predicate.location.end_offset, (node.statements&.location || node.subsequent&.location || node.end_keyword_loc).start_offset, [";"])
      end,
      visit(node.statements),
      case node.subsequent
      when IfNode
        token(node.subsequent.if_keyword_loc)
      when ElseNode
        token(node.subsequent.else_keyword_loc)
      end,
      visit(node.subsequent),
      if node.if_keyword != "elsif"
        token(node.end_keyword_loc)
      end
    )
  else
    builder.condition_mod(
      visit(node.statements),
      visit(node.subsequent),
      token(node.if_keyword_loc),
      visit(node.predicate)
    )
  end
end

#visit_imaginary_node(node) ⇒ Object

1i ^^



897
898
899
# File 'lib/prism/translation/parser/compiler.rb', line 897

def visit_imaginary_node(node)
  visit_numeric(node, builder.complex([Complex(0, node.numeric.value), srange(node.location)]))
end

#visit_implicit_node(node) ⇒ Object

{ foo: }

^^^^

Raises:



903
904
905
# File 'lib/prism/translation/parser/compiler.rb', line 903

def visit_implicit_node(node)
  raise CompilationError, "Cannot directly compile implicit nodes"
end

#visit_implicit_rest_node(node) ⇒ Object

foo { |bar,| }

^

Raises:



909
910
911
# File 'lib/prism/translation/parser/compiler.rb', line 909

def visit_implicit_rest_node(node)
  raise CompilationError, "Cannot compile implicit rest nodes"
end

#visit_in_node(node) ⇒ Object

case foo; in bar; end ^^^^^^^^^^^^^^^^^^^^^



915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
# File 'lib/prism/translation/parser/compiler.rb', line 915

def visit_in_node(node)
  pattern = nil
  guard = nil

  case node.pattern
  when IfNode
    pattern = within_pattern { |compiler| node.pattern.statements.accept(compiler) }
    guard = builder.if_guard(token(node.pattern.if_keyword_loc), visit(node.pattern.predicate))
  when UnlessNode
    pattern = within_pattern { |compiler| node.pattern.statements.accept(compiler) }
    guard = builder.unless_guard(token(node.pattern.keyword_loc), visit(node.pattern.predicate))
  else
    pattern = within_pattern { |compiler| node.pattern.accept(compiler) }
  end

  builder.in_pattern(
    token(node.in_loc),
    pattern,
    guard,
    srange_find(node.pattern.location.end_offset, node.statements&.location&.start_offset, [";", "then"]),
    visit(node.statements)
  )
end

#visit_index_and_write_node(node) ⇒ Object

foo &&= baz ^^^^^^^^^^^^^^^^



959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
# File 'lib/prism/translation/parser/compiler.rb', line 959

def visit_index_and_write_node(node)
  arguments = node.arguments&.arguments || []
  arguments << node.block if node.block

  builder.op_assign(
    builder.index(
      visit(node.receiver),
      token(node.opening_loc),
      visit_all(arguments),
      token(node.closing_loc)
    ),
    [node.operator_loc.slice.chomp("="), srange(node.operator_loc)],
    visit(node.value)
  )
end

#visit_index_operator_write_node(node) ⇒ Object

foo += baz ^^^^^^^^^^^^^^^



941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
# File 'lib/prism/translation/parser/compiler.rb', line 941

def visit_index_operator_write_node(node)
  arguments = node.arguments&.arguments || []
  arguments << node.block if node.block

  builder.op_assign(
    builder.index(
      visit(node.receiver),
      token(node.opening_loc),
      visit_all(arguments),
      token(node.closing_loc)
    ),
    [node.binary_operator_loc.slice.chomp("="), srange(node.binary_operator_loc)],
    visit(node.value)
  )
end

#visit_index_or_write_node(node) ⇒ Object

foo ||= baz ^^^^^^^^^^^^^^^^



977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
# File 'lib/prism/translation/parser/compiler.rb', line 977

def visit_index_or_write_node(node)
  arguments = node.arguments&.arguments || []
  arguments << node.block if node.block

  builder.op_assign(
    builder.index(
      visit(node.receiver),
      token(node.opening_loc),
      visit_all(arguments),
      token(node.closing_loc)
    ),
    [node.operator_loc.slice.chomp("="), srange(node.operator_loc)],
    visit(node.value)
  )
end

#visit_index_target_node(node) ⇒ Object

foo, = 1 ^^^^^^^^



995
996
997
998
999
1000
1001
1002
# File 'lib/prism/translation/parser/compiler.rb', line 995

def visit_index_target_node(node)
  builder.index_asgn(
    visit(node.receiver),
    token(node.opening_loc),
    visit_all(node.arguments.arguments),
    token(node.closing_loc),
  )
end

#visit_instance_variable_and_write_node(node) ⇒ Object

^^^^^^^^^^^^



1032
1033
1034
1035
1036
1037
1038
# File 'lib/prism/translation/parser/compiler.rb', line 1032

def visit_instance_variable_and_write_node(node)
  builder.op_assign(
    builder.assignable(builder.ivar(token(node.name_loc))),
    [node.operator_loc.slice.chomp("="), srange(node.operator_loc)],
    visit(node.value)
  )
end

#visit_instance_variable_operator_write_node(node) ⇒ Object

^^^^^^^^^^^



1022
1023
1024
1025
1026
1027
1028
# File 'lib/prism/translation/parser/compiler.rb', line 1022

def visit_instance_variable_operator_write_node(node)
  builder.op_assign(
    builder.assignable(builder.ivar(token(node.name_loc))),
    [node.binary_operator_loc.slice.chomp("="), srange(node.binary_operator_loc)],
    visit(node.value)
  )
end

#visit_instance_variable_or_write_node(node) ⇒ Object

^^^^^^^^^^^^



1042
1043
1044
1045
1046
1047
1048
# File 'lib/prism/translation/parser/compiler.rb', line 1042

def visit_instance_variable_or_write_node(node)
  builder.op_assign(
    builder.assignable(builder.ivar(token(node.name_loc))),
    [node.operator_loc.slice.chomp("="), srange(node.operator_loc)],
    visit(node.value)
  )
end

#visit_instance_variable_read_node(node) ⇒ Object

^^^^



1006
1007
1008
# File 'lib/prism/translation/parser/compiler.rb', line 1006

def visit_instance_variable_read_node(node)
  builder.ivar(token(node.location))
end

#visit_instance_variable_target_node(node) ⇒ Object

@foo, = bar ^^^^



1052
1053
1054
# File 'lib/prism/translation/parser/compiler.rb', line 1052

def visit_instance_variable_target_node(node)
  builder.assignable(builder.ivar(token(node.location)))
end

#visit_instance_variable_write_node(node) ⇒ Object

^^^^^^^^



1012
1013
1014
1015
1016
1017
1018
# File 'lib/prism/translation/parser/compiler.rb', line 1012

def visit_instance_variable_write_node(node)
  builder.assign(
    builder.assignable(builder.ivar(token(node.name_loc))),
    token(node.operator_loc),
    visit(node.value)
  )
end

#visit_integer_node(node) ⇒ Object

1 ^



1058
1059
1060
# File 'lib/prism/translation/parser/compiler.rb', line 1058

def visit_integer_node(node)
  visit_numeric(node, builder.integer([node.value, srange(node.location)]))
end

#visit_interpolated_regular_expression_node(node) ⇒ Object Also known as: visit_interpolated_match_last_line_node

/foo #bar/ ^^^^^^^^^^^^



1064
1065
1066
1067
1068
1069
1070
1071
# File 'lib/prism/translation/parser/compiler.rb', line 1064

def visit_interpolated_regular_expression_node(node)
  builder.regexp_compose(
    token(node.opening_loc),
    visit_all(node.parts),
    [node.closing[0], srange_offsets(node.closing_loc.start_offset, node.closing_loc.start_offset + 1)],
    builder.regexp_options([node.closing[1..], srange_offsets(node.closing_loc.start_offset + 1, node.closing_loc.end_offset)])
  )
end

#visit_interpolated_string_node(node) ⇒ Object

“foo #bar” ^^^^^^^^^^^^



1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
# File 'lib/prism/translation/parser/compiler.rb', line 1079

def visit_interpolated_string_node(node)
  if node.heredoc?
    return visit_heredoc(node) { |children, closing| builder.string_compose(token(node.opening_loc), children, closing) }
  end

  parts = if node.parts.one? { |part| part.type == :string_node }
    node.parts.flat_map do |node|
      if node.type == :string_node && node.unescaped.lines.count >= 2
        start_offset = node.content_loc.start_offset

        node.unescaped.lines.map do |line|
          end_offset = start_offset + line.length
          offsets = srange_offsets(start_offset, end_offset)
          start_offset = end_offset

          builder.string_internal([line, offsets])
        end
      else
        visit(node)
      end
    end
  else
    visit_all(node.parts)
  end

  builder.string_compose(
    token(node.opening_loc),
    parts,
    token(node.closing_loc)
  )
end

#visit_interpolated_symbol_node(node) ⇒ Object

:“foo #bar” ^^^^^^^^^^^^^



1113
1114
1115
1116
1117
1118
1119
# File 'lib/prism/translation/parser/compiler.rb', line 1113

def visit_interpolated_symbol_node(node)
  builder.symbol_compose(
    token(node.opening_loc),
    visit_all(node.parts),
    token(node.closing_loc)
  )
end

#visit_interpolated_x_string_node(node) ⇒ Object

‘foo #bar` ^^^^^^^^^^^^



1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
# File 'lib/prism/translation/parser/compiler.rb', line 1123

def visit_interpolated_x_string_node(node)
  if node.heredoc?
    visit_heredoc(node) { |children, closing| builder.xstring_compose(token(node.opening_loc), children, closing) }
  else
    builder.xstring_compose(
      token(node.opening_loc),
      visit_all(node.parts),
      token(node.closing_loc)
    )
  end
end

#visit_it_local_variable_read_node(node) ⇒ Object

-> { it }

^^


1137
1138
1139
# File 'lib/prism/translation/parser/compiler.rb', line 1137

def visit_it_local_variable_read_node(node)
  builder.ident([:it, srange(node.location)]).updated(:lvar)
end

#visit_it_parameters_node(node) ⇒ Object

-> { it } ^^^^^^^^^



1143
1144
1145
# File 'lib/prism/translation/parser/compiler.rb', line 1143

def visit_it_parameters_node(node)
  builder.args(nil, [], nil, false)
end

#visit_keyword_hash_node(node) ⇒ Object

foo(bar: baz)

^^^^^^^^


1149
1150
1151
# File 'lib/prism/translation/parser/compiler.rb', line 1149

def visit_keyword_hash_node(node)
  builder.associate(nil, visit_all(node.elements), nil)
end

#visit_keyword_rest_parameter_node(node) ⇒ Object

def foo(**bar); end

^^^^^

def foo(**); end

^^


1158
1159
1160
1161
1162
1163
# File 'lib/prism/translation/parser/compiler.rb', line 1158

def visit_keyword_rest_parameter_node(node)
  builder.kwrestarg(
    token(node.operator_loc),
    node.name ? [node.name, srange(node.name_loc)] : nil
  )
end

#visit_lambda_node(node) ⇒ Object

-> {} ^^^^^



1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
# File 'lib/prism/translation/parser/compiler.rb', line 1167

def visit_lambda_node(node)
  parameters = node.parameters
  implicit_parameters = parameters.is_a?(NumberedParametersNode) || parameters.is_a?(ItParametersNode)

  builder.block(
    builder.call_lambda(token(node.operator_loc)),
    [node.opening, srange(node.opening_loc)],
    if parameters.nil?
      builder.args(nil, [], nil, false)
    elsif implicit_parameters
      visit(node.parameters)
    else
      builder.args(
        token(node.parameters.opening_loc),
        visit(node.parameters),
        token(node.parameters.closing_loc),
        false
      )
    end,
    node.body&.accept(copy_compiler(forwarding: implicit_parameters ? [] : find_forwarding(parameters&.parameters))),
    [node.closing, srange(node.closing_loc)]
  )
end

#visit_local_variable_and_write_node(node) ⇒ Object

foo &&= bar ^^^^^^^^^^^



1219
1220
1221
1222
1223
1224
1225
# File 'lib/prism/translation/parser/compiler.rb', line 1219

def visit_local_variable_and_write_node(node)
  builder.op_assign(
    builder.assignable(builder.ident(token(node.name_loc))),
    [node.operator_loc.slice.chomp("="), srange(node.operator_loc)],
    visit(node.value)
  )
end

#visit_local_variable_operator_write_node(node) ⇒ Object

foo += bar ^^^^^^^^^^



1209
1210
1211
1212
1213
1214
1215
# File 'lib/prism/translation/parser/compiler.rb', line 1209

def visit_local_variable_operator_write_node(node)
  builder.op_assign(
    builder.assignable(builder.ident(token(node.name_loc))),
    [node.binary_operator_loc.slice.chomp("="), srange(node.binary_operator_loc)],
    visit(node.value)
  )
end

#visit_local_variable_or_write_node(node) ⇒ Object

foo ||= bar ^^^^^^^^^^^



1229
1230
1231
1232
1233
1234
1235
# File 'lib/prism/translation/parser/compiler.rb', line 1229

def visit_local_variable_or_write_node(node)
  builder.op_assign(
    builder.assignable(builder.ident(token(node.name_loc))),
    [node.operator_loc.slice.chomp("="), srange(node.operator_loc)],
    visit(node.value)
  )
end

#visit_local_variable_read_node(node) ⇒ Object

foo ^^^



1193
1194
1195
# File 'lib/prism/translation/parser/compiler.rb', line 1193

def visit_local_variable_read_node(node)
  builder.ident([node.name, srange(node.location)]).updated(:lvar)
end

#visit_local_variable_target_node(node) ⇒ Object

foo, = bar ^^^



1239
1240
1241
1242
1243
1244
1245
# File 'lib/prism/translation/parser/compiler.rb', line 1239

def visit_local_variable_target_node(node)
  if in_pattern
    builder.assignable(builder.match_var([node.name, srange(node.location)]))
  else
    builder.assignable(builder.ident(token(node.location)))
  end
end

#visit_local_variable_write_node(node) ⇒ Object

foo = 1 ^^^^^^^



1199
1200
1201
1202
1203
1204
1205
# File 'lib/prism/translation/parser/compiler.rb', line 1199

def visit_local_variable_write_node(node)
  builder.assign(
    builder.assignable(builder.ident(token(node.name_loc))),
    token(node.operator_loc),
    visit(node.value)
  )
end

#visit_match_predicate_node(node) ⇒ Object

foo in bar ^^^^^^^^^^



1249
1250
1251
1252
1253
1254
1255
# File 'lib/prism/translation/parser/compiler.rb', line 1249

def visit_match_predicate_node(node)
  builder.match_pattern_p(
    visit(node.value),
    token(node.operator_loc),
    within_pattern { |compiler| node.pattern.accept(compiler) }
  )
end

#visit_match_required_node(node) ⇒ Object

foo => bar ^^^^^^^^^^



1259
1260
1261
1262
1263
1264
1265
# File 'lib/prism/translation/parser/compiler.rb', line 1259

def visit_match_required_node(node)
  builder.match_pattern(
    visit(node.value),
    token(node.operator_loc),
    within_pattern { |compiler| node.pattern.accept(compiler) }
  )
end

#visit_match_write_node(node) ⇒ Object

/(?<foo>foo)/ =~ bar ^^^^^^^^^^^^^^^^^^^^



1269
1270
1271
1272
1273
1274
1275
# File 'lib/prism/translation/parser/compiler.rb', line 1269

def visit_match_write_node(node)
  builder.match_op(
    visit(node.call.receiver),
    token(node.call.message_loc),
    visit(node.call.arguments.arguments.first)
  )
end

#visit_missing_node(node) ⇒ Object

A node that is missing from the syntax tree. This is only used in the case of a syntax error. The parser gem doesn’t have such a concept, so we invent our own here.



1280
1281
1282
# File 'lib/prism/translation/parser/compiler.rb', line 1280

def visit_missing_node(node)
  ::AST::Node.new(:missing, [], location: ::Parser::Source::Map.new(srange(node.location)))
end

#visit_module_node(node) ⇒ Object

module Foo; end ^^^^^^^^^^^^^^^



1286
1287
1288
1289
1290
1291
1292
1293
# File 'lib/prism/translation/parser/compiler.rb', line 1286

def visit_module_node(node)
  builder.def_module(
    token(node.module_keyword_loc),
    visit(node.constant_path),
    node.body&.accept(copy_compiler(forwarding: [])),
    token(node.end_keyword_loc)
  )
end

#visit_multi_target_node(node) ⇒ Object

foo, bar = baz ^^^^^^^^



1297
1298
1299
1300
1301
1302
1303
# File 'lib/prism/translation/parser/compiler.rb', line 1297

def visit_multi_target_node(node)
  builder.multi_lhs(
    token(node.lparen_loc),
    visit_all(multi_target_elements(node)),
    token(node.rparen_loc)
  )
end

#visit_multi_write_node(node) ⇒ Object

foo, bar = baz ^^^^^^^^^^^^^^



1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
# File 'lib/prism/translation/parser/compiler.rb', line 1307

def visit_multi_write_node(node)
  elements = multi_target_elements(node)

  if elements.length == 1 && elements.first.is_a?(MultiTargetNode)
    elements = multi_target_elements(elements.first)
  end

  builder.multi_assign(
    builder.multi_lhs(
      token(node.lparen_loc),
      visit_all(elements),
      token(node.rparen_loc)
    ),
    token(node.operator_loc),
    visit(node.value)
  )
end

#visit_next_node(node) ⇒ Object

next ^^^^

next foo ^^^^^^^^



1330
1331
1332
1333
1334
1335
1336
1337
1338
# File 'lib/prism/translation/parser/compiler.rb', line 1330

def visit_next_node(node)
  builder.keyword_cmd(
    :next,
    token(node.keyword_loc),
    nil,
    visit(node.arguments) || [],
    nil
  )
end

#visit_nil_node(node) ⇒ Object

nil ^^^



1342
1343
1344
# File 'lib/prism/translation/parser/compiler.rb', line 1342

def visit_nil_node(node)
  builder.nil(token(node.location))
end

#visit_no_keywords_parameter_node(node) ⇒ Object

def foo(**nil); end

^^^^^


1348
1349
1350
1351
1352
1353
1354
# File 'lib/prism/translation/parser/compiler.rb', line 1348

def visit_no_keywords_parameter_node(node)
  if in_pattern
    builder.match_nil_pattern(token(node.operator_loc), token(node.keyword_loc))
  else
    builder.kwnilarg(token(node.operator_loc), token(node.keyword_loc))
  end
end

#visit_numbered_parameters_node(node) ⇒ Object

-> { _1 + _2 } ^^^^^^^^^^^^^^



1358
1359
1360
# File 'lib/prism/translation/parser/compiler.rb', line 1358

def visit_numbered_parameters_node(node)
  builder.numargs(node.maximum)
end

#visit_numbered_reference_read_node(node) ⇒ Object

$1 ^^



1364
1365
1366
# File 'lib/prism/translation/parser/compiler.rb', line 1364

def visit_numbered_reference_read_node(node)
  builder.nth_ref([node.number, srange(node.location)])
end

#visit_optional_keyword_parameter_node(node) ⇒ Object

def foo(bar: baz); end

^^^^^^^^


1370
1371
1372
# File 'lib/prism/translation/parser/compiler.rb', line 1370

def visit_optional_keyword_parameter_node(node)
  builder.kwoptarg([node.name, srange(node.name_loc)], visit(node.value))
end

#visit_optional_parameter_node(node) ⇒ Object

def foo(bar = 1); end

^^^^^^^


1376
1377
1378
# File 'lib/prism/translation/parser/compiler.rb', line 1376

def visit_optional_parameter_node(node)
  builder.optarg(token(node.name_loc), token(node.operator_loc), visit(node.value))
end

#visit_or_node(node) ⇒ Object

a or b ^^^^^^



1382
1383
1384
# File 'lib/prism/translation/parser/compiler.rb', line 1382

def visit_or_node(node)
  builder.logical_op(:or, visit(node.left), token(node.operator_loc), visit(node.right))
end

#visit_parameters_node(node) ⇒ Object

def foo(bar, *baz); end

^^^^^^^^^


1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
# File 'lib/prism/translation/parser/compiler.rb', line 1388

def visit_parameters_node(node)
  params = []

  if node.requireds.any?
    node.requireds.each do |required|
      params <<
        if required.is_a?(RequiredParameterNode)
          visit(required)
        else
          required.accept(copy_compiler(in_destructure: true))
        end
    end
  end

  params.concat(visit_all(node.optionals)) if node.optionals.any?
  params << visit(node.rest) if !node.rest.nil? && !node.rest.is_a?(ImplicitRestNode)

  if node.posts.any?
    node.posts.each do |post|
      params <<
        if post.is_a?(RequiredParameterNode)
          visit(post)
        else
          post.accept(copy_compiler(in_destructure: true))
        end
    end
  end

  params.concat(visit_all(node.keywords)) if node.keywords.any?
  params << visit(node.keyword_rest) if !node.keyword_rest.nil?
  params << visit(node.block) if !node.block.nil?
  params
end

#visit_parentheses_node(node) ⇒ Object

() ^^

(1) ^^^



1427
1428
1429
1430
1431
1432
1433
# File 'lib/prism/translation/parser/compiler.rb', line 1427

def visit_parentheses_node(node)
  builder.begin(
    token(node.opening_loc),
    visit(node.body),
    token(node.closing_loc)
  )
end

#visit_pinned_expression_node(node) ⇒ Object

foo => ^(bar)

^^^^^^


1437
1438
1439
1440
# File 'lib/prism/translation/parser/compiler.rb', line 1437

def visit_pinned_expression_node(node)
  expression = builder.begin(token(node.lparen_loc), visit(node.expression), token(node.rparen_loc))
  builder.pin(token(node.operator_loc), expression)
end

#visit_pinned_variable_node(node) ⇒ Object

foo = 1 and bar => ^foo

^^^^


1444
1445
1446
# File 'lib/prism/translation/parser/compiler.rb', line 1444

def visit_pinned_variable_node(node)
  builder.pin(token(node.operator_loc), visit(node.variable))
end

#visit_post_execution_node(node) ⇒ Object

END {}



1449
1450
1451
1452
1453
1454
1455
1456
# File 'lib/prism/translation/parser/compiler.rb', line 1449

def visit_post_execution_node(node)
  builder.postexe(
    token(node.keyword_loc),
    token(node.opening_loc),
    visit(node.statements),
    token(node.closing_loc)
  )
end

#visit_pre_execution_node(node) ⇒ Object

BEGIN {}



1459
1460
1461
1462
1463
1464
1465
1466
# File 'lib/prism/translation/parser/compiler.rb', line 1459

def visit_pre_execution_node(node)
  builder.preexe(
    token(node.keyword_loc),
    token(node.opening_loc),
    visit(node.statements),
    token(node.closing_loc)
  )
end

#visit_program_node(node) ⇒ Object

The top-level program node.



1469
1470
1471
# File 'lib/prism/translation/parser/compiler.rb', line 1469

def visit_program_node(node)
  visit(node.statements)
end

#visit_range_node(node) ⇒ Object Also known as: visit_flip_flop_node

0..5 ^^^^



1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
# File 'lib/prism/translation/parser/compiler.rb', line 1475

def visit_range_node(node)
  if node.exclude_end?
    builder.range_exclusive(
      visit(node.left),
      token(node.operator_loc),
      visit(node.right)
    )
  else
    builder.range_inclusive(
      visit(node.left),
      token(node.operator_loc),
      visit(node.right)
    )
  end
end

#visit_rational_node(node) ⇒ Object

1r ^^



1497
1498
1499
# File 'lib/prism/translation/parser/compiler.rb', line 1497

def visit_rational_node(node)
  visit_numeric(node, builder.rational([node.value, srange(node.location)]))
end

#visit_redo_node(node) ⇒ Object

redo ^^^^



1503
1504
1505
# File 'lib/prism/translation/parser/compiler.rb', line 1503

def visit_redo_node(node)
  builder.keyword_cmd(:redo, token(node.location))
end

#visit_regular_expression_node(node) ⇒ Object Also known as: visit_match_last_line_node

/foo/ ^^^^^



1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
# File 'lib/prism/translation/parser/compiler.rb', line 1509

def visit_regular_expression_node(node)
  content = node.content
  parts =
    if content.include?("\n")
      offset = node.content_loc.start_offset
      content.lines.map do |line|
        builder.string_internal([line, srange_offsets(offset, offset += line.bytesize)])
      end
    else
      [builder.string_internal(token(node.content_loc))]
    end

  builder.regexp_compose(
    token(node.opening_loc),
    parts,
    [node.closing[0], srange_offsets(node.closing_loc.start_offset, node.closing_loc.start_offset + 1)],
    builder.regexp_options([node.closing[1..], srange_offsets(node.closing_loc.start_offset + 1, node.closing_loc.end_offset)])
  )
end

#visit_required_keyword_parameter_node(node) ⇒ Object

def foo(bar:); end

^^^^


1535
1536
1537
# File 'lib/prism/translation/parser/compiler.rb', line 1535

def visit_required_keyword_parameter_node(node)
  builder.kwarg([node.name, srange(node.name_loc)])
end

#visit_required_parameter_node(node) ⇒ Object

def foo(bar); end

^^^


1541
1542
1543
# File 'lib/prism/translation/parser/compiler.rb', line 1541

def visit_required_parameter_node(node)
  builder.arg(token(node.location))
end

#visit_rescue_modifier_node(node) ⇒ Object

foo rescue bar ^^^^^^^^^^^^^^



1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
# File 'lib/prism/translation/parser/compiler.rb', line 1547

def visit_rescue_modifier_node(node)
  builder.begin_body(
    visit(node.expression),
    [
      builder.rescue_body(
        token(node.keyword_loc),
        nil,
        nil,
        nil,
        nil,
        visit(node.rescue_expression)
      )
    ]
  )
end

#visit_rescue_node(node) ⇒ Object

begin; rescue; end

^^^^^^^

Raises:



1565
1566
1567
# File 'lib/prism/translation/parser/compiler.rb', line 1565

def visit_rescue_node(node)
  raise CompilationError, "Cannot directly compile rescue nodes"
end

#visit_rest_parameter_node(node) ⇒ Object

def foo(*bar); end

^^^^

def foo(*); end

^


1574
1575
1576
# File 'lib/prism/translation/parser/compiler.rb', line 1574

def visit_rest_parameter_node(node)
  builder.restarg(token(node.operator_loc), token(node.name_loc))
end

#visit_retry_node(node) ⇒ Object

retry ^^^^^



1580
1581
1582
# File 'lib/prism/translation/parser/compiler.rb', line 1580

def visit_retry_node(node)
  builder.keyword_cmd(:retry, token(node.location))
end

#visit_return_node(node) ⇒ Object

return ^^^^^^

return 1 ^^^^^^^^



1589
1590
1591
1592
1593
1594
1595
1596
1597
# File 'lib/prism/translation/parser/compiler.rb', line 1589

def visit_return_node(node)
  builder.keyword_cmd(
    :return,
    token(node.keyword_loc),
    nil,
    visit(node.arguments) || [],
    nil
  )
end

#visit_self_node(node) ⇒ Object

self ^^^^



1601
1602
1603
# File 'lib/prism/translation/parser/compiler.rb', line 1601

def visit_self_node(node)
  builder.self(token(node.location))
end

#visit_shareable_constant_node(node) ⇒ Object

A shareable constant.



1606
1607
1608
# File 'lib/prism/translation/parser/compiler.rb', line 1606

def visit_shareable_constant_node(node)
  visit(node.write)
end

#visit_singleton_class_node(node) ⇒ Object

class << self; end ^^^^^^^^^^^^^^^^^^



1612
1613
1614
1615
1616
1617
1618
1619
1620
# File 'lib/prism/translation/parser/compiler.rb', line 1612

def visit_singleton_class_node(node)
  builder.def_sclass(
    token(node.class_keyword_loc),
    token(node.operator_loc),
    visit(node.expression),
    node.body&.accept(copy_compiler(forwarding: [])),
    token(node.end_keyword_loc)
  )
end

#visit_source_encoding_node(node) ⇒ Object

__ENCODING__ ^^^^^^^^^^^^



1624
1625
1626
# File 'lib/prism/translation/parser/compiler.rb', line 1624

def visit_source_encoding_node(node)
  builder.accessible(builder.__ENCODING__(token(node.location)))
end

#visit_source_file_node(node) ⇒ Object

__FILE__ ^^^^^^^^



1630
1631
1632
# File 'lib/prism/translation/parser/compiler.rb', line 1630

def visit_source_file_node(node)
  builder.accessible(builder.__FILE__(token(node.location)))
end

#visit_source_line_node(node) ⇒ Object

__LINE__ ^^^^^^^^



1636
1637
1638
# File 'lib/prism/translation/parser/compiler.rb', line 1636

def visit_source_line_node(node)
  builder.accessible(builder.__LINE__(token(node.location)))
end

#visit_splat_node(node) ⇒ Object

foo(*bar)

^^^^

def foo((bar, *baz)); end

^^^^

def foo(*); bar(*); end

^


1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
# File 'lib/prism/translation/parser/compiler.rb', line 1648

def visit_splat_node(node)
  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))
  elsif in_pattern
    builder.match_rest(token(node.operator_loc), token(node.expression&.location))
  else
    builder.splat(token(node.operator_loc), visit(node.expression))
  end
end

#visit_statements_node(node) ⇒ Object

A list of statements.



1661
1662
1663
# File 'lib/prism/translation/parser/compiler.rb', line 1661

def visit_statements_node(node)
  builder.compstmt(visit_all(node.body))
end

#visit_string_node(node) ⇒ Object

“foo” ^^^^^



1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
# File 'lib/prism/translation/parser/compiler.rb', line 1667

def visit_string_node(node)
  if node.heredoc?
    visit_heredoc(node.to_interpolated) { |children, closing| builder.string_compose(token(node.opening_loc), children, closing) }
  elsif node.opening == "?"
    builder.character([node.unescaped, srange(node.location)])
  elsif node.opening&.start_with?("%") && node.unescaped.empty?
    builder.string_compose(token(node.opening_loc), [], token(node.closing_loc))
  else
    content_lines = node.content.lines
    unescaped_lines = node.unescaped.lines

    parts =
      if content_lines.length <= 1 || unescaped_lines.length <= 1
        [builder.string_internal([node.unescaped, srange(node.content_loc)])]
      elsif content_lines.length != unescaped_lines.length
        # This occurs when we have line continuations in the string. We
        # need to come back and fix this, but for now this stops the
        # code from breaking when we encounter it because of trying to
        # transpose arrays of different lengths.
        [builder.string_internal([node.unescaped, srange(node.content_loc)])]
      else
        start_offset = node.content_loc.start_offset

        [content_lines, unescaped_lines].transpose.map do |content_line, unescaped_line|
          end_offset = start_offset + content_line.length
          offsets = srange_offsets(start_offset, end_offset)
          start_offset = end_offset

          builder.string_internal([unescaped_line, offsets])
        end
      end

    builder.string_compose(
      token(node.opening_loc),
      parts,
      token(node.closing_loc)
    )
  end
end

#visit_super_node(node) ⇒ Object

super(foo) ^^^^^^^^^^



1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
# File 'lib/prism/translation/parser/compiler.rb', line 1709

def visit_super_node(node)
  arguments = node.arguments&.arguments || []
  block = node.block

  if block.is_a?(BlockArgumentNode)
    arguments = [*arguments, block]
    block = nil
  end

  visit_block(
    builder.keyword_cmd(
      :super,
      token(node.keyword_loc),
      token(node.lparen_loc),
      visit_all(arguments),
      token(node.rparen_loc)
    ),
    block
  )
end

#visit_symbol_node(node) ⇒ Object

:foo ^^^^



1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
# File 'lib/prism/translation/parser/compiler.rb', line 1732

def visit_symbol_node(node)
  if node.closing_loc.nil?
    if node.opening_loc.nil?
      builder.symbol_internal([node.unescaped, srange(node.location)])
    else
      builder.symbol([node.unescaped, srange(node.location)])
    end
  else
    parts = if node.value.lines.one?
      [builder.string_internal([node.unescaped, srange(node.value_loc)])]
    else
      start_offset = node.value_loc.start_offset

      node.value.lines.map do |line|
        end_offset = start_offset + line.length
        offsets = srange_offsets(start_offset, end_offset)
        start_offset = end_offset

        builder.string_internal([line, offsets])
      end
    end

    builder.symbol_compose(
      token(node.opening_loc),
      parts,
      token(node.closing_loc)
    )
  end
end

#visit_true_node(node) ⇒ Object

true ^^^^



1764
1765
1766
# File 'lib/prism/translation/parser/compiler.rb', line 1764

def visit_true_node(node)
  builder.true(token(node.location))
end

#visit_undef_node(node) ⇒ Object

undef foo ^^^^^^^^^



1770
1771
1772
# File 'lib/prism/translation/parser/compiler.rb', line 1770

def visit_undef_node(node)
  builder.undef_method(token(node.keyword_loc), visit_all(node.names))
end

#visit_unless_node(node) ⇒ Object

unless foo; bar end ^^^^^^^^^^^^^^^^^^^

bar unless foo ^^^^^^^^^^^^^^



1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
# File 'lib/prism/translation/parser/compiler.rb', line 1779

def visit_unless_node(node)
  if node.keyword_loc.start_offset == node.location.start_offset
    builder.condition(
      token(node.keyword_loc),
      visit(node.predicate),
      if node.then_keyword_loc
        token(node.then_keyword_loc)
      else
        srange_find(node.predicate.location.end_offset, (node.statements&.location || node.else_clause&.location || node.end_keyword_loc).start_offset, [";"])
      end,
      visit(node.else_clause),
      token(node.else_clause&.else_keyword_loc),
      visit(node.statements),
      token(node.end_keyword_loc)
    )
  else
    builder.condition_mod(
      visit(node.else_clause),
      visit(node.statements),
      token(node.keyword_loc),
      visit(node.predicate)
    )
  end
end

#visit_until_node(node) ⇒ Object

until foo; bar end ^^^^^^^^^^^^^^^^^^

bar until foo ^^^^^^^^^^^^^



1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
# File 'lib/prism/translation/parser/compiler.rb', line 1809

def visit_until_node(node)
  if node.location.start_offset == node.keyword_loc.start_offset
    builder.loop(
      :until,
      token(node.keyword_loc),
      visit(node.predicate),
      srange_find(node.predicate.location.end_offset, (node.statements&.location || node.closing_loc).start_offset, [";", "do"]),
      visit(node.statements),
      token(node.closing_loc)
    )
  else
    builder.loop_mod(
      :until,
      visit(node.statements),
      token(node.keyword_loc),
      visit(node.predicate)
    )
  end
end

#visit_when_node(node) ⇒ Object

case foo; when bar; end

^^^^^^^^^^^^^


1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
# File 'lib/prism/translation/parser/compiler.rb', line 1831

def visit_when_node(node)
  builder.when(
    token(node.keyword_loc),
    visit_all(node.conditions),
    if node.then_keyword_loc
      token(node.then_keyword_loc)
    else
      srange_find(node.conditions.last.location.end_offset, node.statements&.location&.start_offset, [";"])
    end,
    visit(node.statements)
  )
end

#visit_while_node(node) ⇒ Object

while foo; bar end ^^^^^^^^^^^^^^^^^^

bar while foo ^^^^^^^^^^^^^



1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
# File 'lib/prism/translation/parser/compiler.rb', line 1849

def visit_while_node(node)
  if node.location.start_offset == node.keyword_loc.start_offset
    builder.loop(
      :while,
      token(node.keyword_loc),
      visit(node.predicate),
      srange_find(node.predicate.location.end_offset, (node.statements&.location || node.closing_loc).start_offset, [";", "do"]),
      visit(node.statements),
      token(node.closing_loc)
    )
  else
    builder.loop_mod(
      :while,
      visit(node.statements),
      token(node.keyword_loc),
      visit(node.predicate)
    )
  end
end

#visit_x_string_node(node) ⇒ Object

‘foo` ^^^^^



1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
# File 'lib/prism/translation/parser/compiler.rb', line 1871

def visit_x_string_node(node)
  if node.heredoc?
    visit_heredoc(node.to_interpolated) { |children, closing| builder.xstring_compose(token(node.opening_loc), children, closing) }
  else
    parts = if node.unescaped.lines.one?
      [builder.string_internal([node.unescaped, srange(node.content_loc)])]
    else
      start_offset = node.content_loc.start_offset

      node.unescaped.lines.map do |line|
        end_offset = start_offset + line.length
        offsets = srange_offsets(start_offset, end_offset)
        start_offset = end_offset

        builder.string_internal([line, offsets])
      end
    end

    builder.xstring_compose(
      token(node.opening_loc),
      parts,
      token(node.closing_loc)
    )
  end
end

#visit_yield_node(node) ⇒ Object

yield ^^^^^

yield 1 ^^^^^^^



1902
1903
1904
1905
1906
1907
1908
1909
1910
# File 'lib/prism/translation/parser/compiler.rb', line 1902

def visit_yield_node(node)
  builder.keyword_cmd(
    :yield,
    token(node.keyword_loc),
    token(node.lparen_loc),
    visit(node.arguments) || [],
    token(node.rparen_loc)
  )
end