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.



4766
4767
4768
4769
4770
4771
4772
# File 'lib/syntax_tree/node.rb', line 4766

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



4758
4759
4760
# File 'lib/syntax_tree/node.rb', line 4758

def collection
  @collection
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



4764
4765
4766
# File 'lib/syntax_tree/node.rb', line 4764

def comments
  @comments
end

#indexObject (readonly)

MLHS | VarField

the variable declaration being used to

pull values out of the object being enumerated



4755
4756
4757
# File 'lib/syntax_tree/node.rb', line 4755

def index
  @index
end

#statementsObject (readonly)

Statements

the statements to be executed



4761
4762
4763
# File 'lib/syntax_tree/node.rb', line 4761

def statements
  @statements
end

Instance Method Details

#accept(visitor) ⇒ Object



4774
4775
4776
# File 'lib/syntax_tree/node.rb', line 4774

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

#child_nodesObject Also known as: deconstruct



4778
4779
4780
# File 'lib/syntax_tree/node.rb', line 4778

def child_nodes
  [index, collection, statements]
end

#deconstruct_keys(_keys) ⇒ Object



4784
4785
4786
4787
4788
4789
4790
4791
4792
# File 'lib/syntax_tree/node.rb', line 4784

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

#format(q) ⇒ Object



4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
# File 'lib/syntax_tree/node.rb', line 4794

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