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.



9381
9382
9383
9384
9385
9386
# File 'lib/syntax_tree/node.rb', line 9381

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



9379
9380
9381
# File 'lib/syntax_tree/node.rb', line 9379

def comments
  @comments
end

#predicateObject (readonly)

untyped

the expression to be checked



9376
9377
9378
# File 'lib/syntax_tree/node.rb', line 9376

def predicate
  @predicate
end

#statementObject (readonly)

untyped

the expression to be executed



9373
9374
9375
# File 'lib/syntax_tree/node.rb', line 9373

def statement
  @statement
end

Instance Method Details

#accept(visitor) ⇒ Object



9388
9389
9390
# File 'lib/syntax_tree/node.rb', line 9388

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

#child_nodesObject Also known as: deconstruct



9392
9393
9394
# File 'lib/syntax_tree/node.rb', line 9392

def child_nodes
  [statement, predicate]
end

#deconstruct_keys(_keys) ⇒ Object



9398
9399
9400
9401
9402
9403
9404
9405
# File 'lib/syntax_tree/node.rb', line 9398

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

#format(q) ⇒ Object



9407
9408
9409
9410
9411
9412
9413
9414
9415
9416
9417
9418
9419
9420
9421
9422
9423
9424
9425
9426
9427
9428
9429
# File 'lib/syntax_tree/node.rb', line 9407

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