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:, comments: []) ⇒ For

Returns a new instance of For.



4629
4630
4631
4632
4633
4634
4635
# File 'lib/syntax_tree/node.rb', line 4629

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



4621
4622
4623
# File 'lib/syntax_tree/node.rb', line 4621

def collection
  @collection
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



4627
4628
4629
# File 'lib/syntax_tree/node.rb', line 4627

def comments
  @comments
end

#indexObject (readonly)

MLHS | VarField

the variable declaration being used to

pull values out of the object being enumerated



4618
4619
4620
# File 'lib/syntax_tree/node.rb', line 4618

def index
  @index
end

#statementsObject (readonly)

Statements

the statements to be executed



4624
4625
4626
# File 'lib/syntax_tree/node.rb', line 4624

def statements
  @statements
end

Instance Method Details

#accept(visitor) ⇒ Object



4637
4638
4639
# File 'lib/syntax_tree/node.rb', line 4637

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

#child_nodesObject Also known as: deconstruct



4641
4642
4643
# File 'lib/syntax_tree/node.rb', line 4641

def child_nodes
  [index, collection, statements]
end

#deconstruct_keys(_keys) ⇒ Object



4647
4648
4649
4650
4651
4652
4653
4654
4655
# File 'lib/syntax_tree/node.rb', line 4647

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

#format(q) ⇒ Object



4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
# File 'lib/syntax_tree/node.rb', line 4657

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