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.



8547
8548
8549
8550
8551
8552
# File 'lib/syntax_tree/node.rb', line 8547

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



8545
8546
8547
# File 'lib/syntax_tree/node.rb', line 8545

def comments
  @comments
end

#predicateObject (readonly)

untyped

the expression to be checked



8542
8543
8544
# File 'lib/syntax_tree/node.rb', line 8542

def predicate
  @predicate
end

#statementObject (readonly)

untyped

the expression to be executed



8539
8540
8541
# File 'lib/syntax_tree/node.rb', line 8539

def statement
  @statement
end

Instance Method Details

#accept(visitor) ⇒ Object



8554
8555
8556
# File 'lib/syntax_tree/node.rb', line 8554

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

#child_nodesObject Also known as: deconstruct



8558
8559
8560
# File 'lib/syntax_tree/node.rb', line 8558

def child_nodes
  [statement, predicate]
end

#deconstruct_keys(keys) ⇒ Object



8564
8565
8566
8567
8568
8569
8570
8571
# File 'lib/syntax_tree/node.rb', line 8564

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

#format(q) ⇒ Object



8573
8574
8575
8576
8577
8578
8579
8580
8581
8582
8583
8584
8585
8586
8587
8588
8589
8590
8591
8592
8593
8594
8595
# File 'lib/syntax_tree/node.rb', line 8573

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