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, #pretty_print, #to_json

Constructor Details

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



5417
5418
5419
5420
5421
5422
5423
# File 'lib/syntax_tree/node.rb', line 5417

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

Instance Attribute Details

#collectionObject (readonly)

untyped

the object being enumerated in the loop



5409
5410
5411
# File 'lib/syntax_tree/node.rb', line 5409

def collection
  @collection
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



5415
5416
5417
# File 'lib/syntax_tree/node.rb', line 5415

def comments
  @comments
end

#indexObject (readonly)

MLHS | VarField

the variable declaration being used to

pull values out of the object being enumerated



5406
5407
5408
# File 'lib/syntax_tree/node.rb', line 5406

def index
  @index
end

#statementsObject (readonly)

Statements

the statements to be executed



5412
5413
5414
# File 'lib/syntax_tree/node.rb', line 5412

def statements
  @statements
end

Instance Method Details

#===(other) ⇒ Object



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

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

#accept(visitor) ⇒ Object



5425
5426
5427
# File 'lib/syntax_tree/node.rb', line 5425

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

#child_nodesObject Also known as: deconstruct



5429
5430
5431
# File 'lib/syntax_tree/node.rb', line 5429

def child_nodes
  [index, collection, statements]
end

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



5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
# File 'lib/syntax_tree/node.rb', line 5433

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



5448
5449
5450
5451
5452
5453
5454
5455
5456
# File 'lib/syntax_tree/node.rb', line 5448

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

#format(q) ⇒ Object



5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
# File 'lib/syntax_tree/node.rb', line 5458

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