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.



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

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



5500
5501
5502
# File 'lib/syntax_tree/node.rb', line 5500

def collection
  @collection
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



5506
5507
5508
# File 'lib/syntax_tree/node.rb', line 5506

def comments
  @comments
end

#indexObject (readonly)

MLHS | VarField

the variable declaration being used to

pull values out of the object being enumerated



5497
5498
5499
# File 'lib/syntax_tree/node.rb', line 5497

def index
  @index
end

#statementsObject (readonly)

Statements

the statements to be executed



5503
5504
5505
# File 'lib/syntax_tree/node.rb', line 5503

def statements
  @statements
end

Instance Method Details

#===(other) ⇒ Object



5568
5569
5570
5571
# File 'lib/syntax_tree/node.rb', line 5568

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

#accept(visitor) ⇒ Object



5516
5517
5518
# File 'lib/syntax_tree/node.rb', line 5516

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

#child_nodesObject Also known as: deconstruct



5520
5521
5522
# File 'lib/syntax_tree/node.rb', line 5520

def child_nodes
  [index, collection, statements]
end

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



5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
# File 'lib/syntax_tree/node.rb', line 5524

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



5539
5540
5541
5542
5543
5544
5545
5546
5547
# File 'lib/syntax_tree/node.rb', line 5539

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

#format(q) ⇒ Object



5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
# File 'lib/syntax_tree/node.rb', line 5549

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