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.



5530
5531
5532
5533
5534
5535
5536
# File 'lib/syntax_tree/node.rb', line 5530

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



5522
5523
5524
# File 'lib/syntax_tree/node.rb', line 5522

def collection
  @collection
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



5528
5529
5530
# File 'lib/syntax_tree/node.rb', line 5528

def comments
  @comments
end

#indexObject (readonly)

MLHS | VarField

the variable declaration being used to

pull values out of the object being enumerated



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

def index
  @index
end

#statementsObject (readonly)

Statements

the statements to be executed



5525
5526
5527
# File 'lib/syntax_tree/node.rb', line 5525

def statements
  @statements
end

Instance Method Details

#===(other) ⇒ Object



5590
5591
5592
5593
# File 'lib/syntax_tree/node.rb', line 5590

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

#accept(visitor) ⇒ Object



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

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

#child_nodesObject Also known as: deconstruct



5542
5543
5544
# File 'lib/syntax_tree/node.rb', line 5542

def child_nodes
  [index, collection, statements]
end

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



5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
# File 'lib/syntax_tree/node.rb', line 5546

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



5561
5562
5563
5564
5565
5566
5567
5568
5569
# File 'lib/syntax_tree/node.rb', line 5561

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

#format(q) ⇒ Object



5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
# File 'lib/syntax_tree/node.rb', line 5571

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