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.



5527
5528
5529
5530
5531
5532
5533
# File 'lib/syntax_tree/node.rb', line 5527

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



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

def collection
  @collection
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



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

def comments
  @comments
end

#indexObject (readonly)

MLHS | VarField

the variable declaration being used to

pull values out of the object being enumerated



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

def index
  @index
end

#statementsObject (readonly)

Statements

the statements to be executed



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

def statements
  @statements
end

Instance Method Details

#===(other) ⇒ Object



5587
5588
5589
5590
# File 'lib/syntax_tree/node.rb', line 5587

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

#accept(visitor) ⇒ Object



5535
5536
5537
# File 'lib/syntax_tree/node.rb', line 5535

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

#child_nodesObject Also known as: deconstruct



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

def child_nodes
  [index, collection, statements]
end

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



5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
# File 'lib/syntax_tree/node.rb', line 5543

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



5558
5559
5560
5561
5562
5563
5564
5565
5566
# File 'lib/syntax_tree/node.rb', line 5558

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

#format(q) ⇒ Object



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

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