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

#construct_keys, #pretty_print, #to_json

Constructor Details

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

Returns a new instance of UntilMod.



9220
9221
9222
9223
9224
9225
# File 'lib/syntax_tree/node.rb', line 9220

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



9218
9219
9220
# File 'lib/syntax_tree/node.rb', line 9218

def comments
  @comments
end

#predicateObject (readonly)

untyped

the expression to be checked



9215
9216
9217
# File 'lib/syntax_tree/node.rb', line 9215

def predicate
  @predicate
end

#statementObject (readonly)

untyped

the expression to be executed



9212
9213
9214
# File 'lib/syntax_tree/node.rb', line 9212

def statement
  @statement
end

Instance Method Details

#accept(visitor) ⇒ Object



9227
9228
9229
# File 'lib/syntax_tree/node.rb', line 9227

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

#child_nodesObject Also known as: deconstruct



9231
9232
9233
# File 'lib/syntax_tree/node.rb', line 9231

def child_nodes
  [statement, predicate]
end

#deconstruct_keys(_keys) ⇒ Object



9237
9238
9239
9240
9241
9242
9243
9244
# File 'lib/syntax_tree/node.rb', line 9237

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

#format(q) ⇒ Object



9246
9247
9248
9249
9250
9251
9252
9253
9254
9255
9256
9257
9258
9259
9260
9261
9262
9263
9264
9265
9266
9267
9268
# File 'lib/syntax_tree/node.rb', line 9246

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