Class: SyntaxTree::For

Inherits:
Object
  • Object
show all
Defined in:
lib/syntax_tree.rb

Overview

For represents using a for loop.

for value in list do
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of For.



5799
5800
5801
5802
5803
5804
5805
# File 'lib/syntax_tree.rb', line 5799

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



5788
5789
5790
# File 'lib/syntax_tree.rb', line 5788

def collection
  @collection
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



5797
5798
5799
# File 'lib/syntax_tree.rb', line 5797

def comments
  @comments
end

#indexObject (readonly)

MLHS | VarField

the variable declaration being used to

pull values out of the object being enumerated



5785
5786
5787
# File 'lib/syntax_tree.rb', line 5785

def index
  @index
end

#locationObject (readonly)

Location

the location of this node



5794
5795
5796
# File 'lib/syntax_tree.rb', line 5794

def location
  @location
end

#statementsObject (readonly)

Statements

the statements to be executed



5791
5792
5793
# File 'lib/syntax_tree.rb', line 5791

def statements
  @statements
end

Instance Method Details

#child_nodesObject



5807
5808
5809
# File 'lib/syntax_tree.rb', line 5807

def child_nodes
  [index, collection, statements]
end

#format(q) ⇒ Object



5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
# File 'lib/syntax_tree.rb', line 5811

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

#pretty_print(q) ⇒ Object



5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
# File 'lib/syntax_tree.rb', line 5830

def pretty_print(q)
  q.group(2, "(", ")") do
    q.text("for")

    q.breakable
    q.pp(index)

    q.breakable
    q.pp(collection)

    q.breakable
    q.pp(statements)

    q.pp(Comment::List.new(comments))
  end
end

#to_json(*opts) ⇒ Object



5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
# File 'lib/syntax_tree.rb', line 5847

def to_json(*opts)
  {
    type: :for,
    index: index,
    collection: collection,
    stmts: statements,
    loc: location,
    cmts: comments
  }.to_json(*opts)
end