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

#pretty_print, #to_json

Constructor Details

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



4537
4538
4539
4540
4541
4542
4543
# File 'lib/syntax_tree/node.rb', line 4537

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

Instance Attribute Details

#collectionObject (readonly)

untyped

the object being enumerated in the loop



4529
4530
4531
# File 'lib/syntax_tree/node.rb', line 4529

def collection
  @collection
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



4535
4536
4537
# File 'lib/syntax_tree/node.rb', line 4535

def comments
  @comments
end

#indexObject (readonly)

MLHS | VarField

the variable declaration being used to

pull values out of the object being enumerated



4526
4527
4528
# File 'lib/syntax_tree/node.rb', line 4526

def index
  @index
end

#statementsObject (readonly)

Statements

the statements to be executed



4532
4533
4534
# File 'lib/syntax_tree/node.rb', line 4532

def statements
  @statements
end

Instance Method Details

#accept(visitor) ⇒ Object



4545
4546
4547
# File 'lib/syntax_tree/node.rb', line 4545

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

#child_nodesObject Also known as: deconstruct



4549
4550
4551
# File 'lib/syntax_tree/node.rb', line 4549

def child_nodes
  [index, collection, statements]
end

#deconstruct_keys(keys) ⇒ Object



4555
4556
4557
4558
4559
4560
4561
4562
4563
# File 'lib/syntax_tree/node.rb', line 4555

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

#format(q) ⇒ Object



4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
# File 'lib/syntax_tree/node.rb', line 4565

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: true)
        q.format(statements)
      end
    end

    q.breakable(force: true)
    q.text("end")
  end
end