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.



6114
6115
6116
6117
6118
6119
6120
# File 'lib/syntax_tree.rb', line 6114

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



6103
6104
6105
# File 'lib/syntax_tree.rb', line 6103

def collection
  @collection
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



6112
6113
6114
# File 'lib/syntax_tree.rb', line 6112

def comments
  @comments
end

#indexObject (readonly)

MLHS | VarField

the variable declaration being used to

pull values out of the object being enumerated



6100
6101
6102
# File 'lib/syntax_tree.rb', line 6100

def index
  @index
end

#locationObject (readonly)

Location

the location of this node



6109
6110
6111
# File 'lib/syntax_tree.rb', line 6109

def location
  @location
end

#statementsObject (readonly)

Statements

the statements to be executed



6106
6107
6108
# File 'lib/syntax_tree.rb', line 6106

def statements
  @statements
end

Instance Method Details

#child_nodesObject



6122
6123
6124
# File 'lib/syntax_tree.rb', line 6122

def child_nodes
  [index, collection, statements]
end

#format(q) ⇒ Object



6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
# File 'lib/syntax_tree.rb', line 6126

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



6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
# File 'lib/syntax_tree.rb', line 6145

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



6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
# File 'lib/syntax_tree.rb', line 6162

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