Class: SyntaxTree::UntilMod

Inherits:
Node
  • Object
show all
Defined in:
lib/syntax_tree/node.rb

Overview

UntilMod represents the modifier form of a until loop.

expression until predicate

Instance Attribute Summary collapse

Attributes inherited from Node

#location

Instance Method Summary collapse

Methods inherited from Node

#pretty_print, #to_json

Constructor Details

#initialize(statement:, predicate:, location:, comments: []) ⇒ UntilMod

Returns a new instance of UntilMod.



9107
9108
9109
9110
9111
9112
# File 'lib/syntax_tree/node.rb', line 9107

def initialize(statement:, predicate:, location:, comments: [])
  @statement = statement
  @predicate = predicate
  @location = location
  @comments = comments
end

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



9105
9106
9107
# File 'lib/syntax_tree/node.rb', line 9105

def comments
  @comments
end

#predicateObject (readonly)

untyped

the expression to be checked



9102
9103
9104
# File 'lib/syntax_tree/node.rb', line 9102

def predicate
  @predicate
end

#statementObject (readonly)

untyped

the expression to be executed



9099
9100
9101
# File 'lib/syntax_tree/node.rb', line 9099

def statement
  @statement
end

Instance Method Details

#accept(visitor) ⇒ Object



9114
9115
9116
# File 'lib/syntax_tree/node.rb', line 9114

def accept(visitor)
  visitor.visit_until_mod(self)
end

#child_nodesObject Also known as: deconstruct



9118
9119
9120
# File 'lib/syntax_tree/node.rb', line 9118

def child_nodes
  [statement, predicate]
end

#deconstruct_keys(keys) ⇒ Object



9124
9125
9126
9127
9128
9129
9130
9131
# File 'lib/syntax_tree/node.rb', line 9124

def deconstruct_keys(keys)
  {
    statement: statement,
    predicate: predicate,
    location: location,
    comments: comments
  }
end

#format(q) ⇒ Object



9133
9134
9135
9136
9137
9138
9139
9140
9141
9142
9143
9144
9145
9146
9147
9148
9149
9150
9151
9152
9153
9154
9155
# File 'lib/syntax_tree/node.rb', line 9133

def format(q)
  # If we're in the modifier form and we're modifying a `begin`, then this
  # is a special case where we need to explicitly use the modifier form
  # because otherwise the semantic meaning changes. This looks like:
  #
  #     begin
  #       foo
  #     end until bar
  #
  # Also, if the statement of the modifier includes an assignment, then we
  # can't know for certain that it won't impact the predicate, so we need to
  # force it to stay as it is. This looks like:
  #
  #     foo = bar until foo
  #
  if statement.is_a?(Begin) || ContainsAssignment.call(statement)
    q.format(statement)
    q.text(" until ")
    q.format(predicate)
  else
    LoopFormatter.new("until", self, statement).format(q)
  end
end