Class: SyntaxTree::For

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

Overview

For represents using a for loop.

for value in list do
end

Instance Attribute Summary collapse

Attributes inherited from Node

#location

Instance Method Summary collapse

Methods inherited from Node

#construct_keys, #end_char, #pretty_print, #start_char, #to_json, #to_mermaid

Constructor Details

#initialize(index:, collection:, statements:, location:) ⇒ For

Returns a new instance of For.



5477
5478
5479
5480
5481
5482
5483
# File 'lib/syntax_tree/node.rb', line 5477

def initialize(index:, collection:, statements:, location:)
  @index = index
  @collection = collection
  @statements = statements
  @location = location
  @comments = []
end

Instance Attribute Details

#collectionObject (readonly)

Node

the object being enumerated in the loop



5469
5470
5471
# File 'lib/syntax_tree/node.rb', line 5469

def collection
  @collection
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



5475
5476
5477
# File 'lib/syntax_tree/node.rb', line 5475

def comments
  @comments
end

#indexObject (readonly)

MLHS | VarField

the variable declaration being used to

pull values out of the object being enumerated



5466
5467
5468
# File 'lib/syntax_tree/node.rb', line 5466

def index
  @index
end

#statementsObject (readonly)

Statements

the statements to be executed



5472
5473
5474
# File 'lib/syntax_tree/node.rb', line 5472

def statements
  @statements
end

Instance Method Details

#===(other) ⇒ Object



5537
5538
5539
5540
# File 'lib/syntax_tree/node.rb', line 5537

def ===(other)
  other.is_a?(For) && index === other.index &&
    collection === other.collection && statements === other.statements
end

#accept(visitor) ⇒ Object



5485
5486
5487
# File 'lib/syntax_tree/node.rb', line 5485

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

#child_nodesObject Also known as: deconstruct



5489
5490
5491
# File 'lib/syntax_tree/node.rb', line 5489

def child_nodes
  [index, collection, statements]
end

#copy(index: nil, collection: nil, statements: nil, location: nil) ⇒ Object



5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
# File 'lib/syntax_tree/node.rb', line 5493

def copy(index: nil, collection: nil, statements: nil, location: nil)
  node =
    For.new(
      index: index || self.index,
      collection: collection || self.collection,
      statements: statements || self.statements,
      location: location || self.location
    )

  node.comments.concat(comments.map(&:copy))
  node
end

#deconstruct_keys(_keys) ⇒ Object



5508
5509
5510
5511
5512
5513
5514
5515
5516
# File 'lib/syntax_tree/node.rb', line 5508

def deconstruct_keys(_keys)
  {
    index: index,
    collection: collection,
    statements: statements,
    location: location,
    comments: comments
  }
end

#format(q) ⇒ Object



5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
# File 'lib/syntax_tree/node.rb', line 5518

def format(q)
  q.group do
    q.text("for ")
    q.group { q.format(index) }
    q.text(" in ")
    q.format(collection)

    unless statements.empty?
      q.indent do
        q.breakable_force
        q.format(statements)
      end
    end

    q.breakable_force
    q.text("end")
  end
end