Class: Sexp2Ruby::Node::Iter

Inherits:
Base
  • Object
show all
Defined in:
lib/sexp2ruby/node/iter.rb

Overview

An Iter, AFAICT, is a Call with a block.

Example:

“‘ # ruby derp(foo) { |bar| herp }

# sexp s(

:iter,
s(:call, nil, :a, s(:call, nil, :b)),
s(:args, :c),
s(:call, nil, :d)

) “‘

Constant Summary

Constants inherited from Base

Base::ASSIGN_NODES, Base::LF, Base::LINE_LENGTH

Instance Attribute Summary

Attributes inherited from Base

#processor

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from Sexp2Ruby::Node::Base

Instance Method Details

#to_s(exp) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/sexp2ruby/node/iter.rb', line 21

def to_s(exp)
  call_sexp = exp.shift

  # Process the `Call`. The Sexp is not consumed here (it is cloned)
  # because we will need to refer to it later, when determining which
  # block delimiters to use (brackets vs. do/end).
  iter = process(call_sexp.deep_clone)

  # The block arguments (as opposed to the `Call` arguments)
  args = exp.shift

  # The body of the block.
  body = exp.empty? ? nil : process(exp.shift)

  args = case args
  when 0 then
    ""
  else
    " |#{process(args)[1..-2]}|"
  end

  b, e = if iter == "END"
    [ "{", "}" ]
  else
    [ "do", "end" ]
  end

  iter.sub!(/\(\)$/, '')

  # REFACTOR: ugh
  result = []
  result << "#{iter} {"
  result << args
  result << (body ? " #{body.strip} " : "")
  result << "}"
  result = result.join

  # Can we squeeze the block onto the same line as the call?
  if same_line_bracket_block?(result, iter, call_sexp.deep_clone)
    return result
  end

  # We will not try to squeeze the block onto one line.
  result = []
  result << "#{iter} #{b}"
  result << args
  result << LF
  if body
    result << indent(body.strip)
    result << LF
  end
  result << e
  result.join
end