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.



6151
6152
6153
6154
6155
6156
6157
# File 'lib/syntax_tree.rb', line 6151

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



6140
6141
6142
# File 'lib/syntax_tree.rb', line 6140

def collection
  @collection
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



6149
6150
6151
# File 'lib/syntax_tree.rb', line 6149

def comments
  @comments
end

#indexObject (readonly)

MLHS | VarField

the variable declaration being used to

pull values out of the object being enumerated



6137
6138
6139
# File 'lib/syntax_tree.rb', line 6137

def index
  @index
end

#locationObject (readonly)

Location

the location of this node



6146
6147
6148
# File 'lib/syntax_tree.rb', line 6146

def location
  @location
end

#statementsObject (readonly)

Statements

the statements to be executed



6143
6144
6145
# File 'lib/syntax_tree.rb', line 6143

def statements
  @statements
end

Instance Method Details

#child_nodesObject



6159
6160
6161
# File 'lib/syntax_tree.rb', line 6159

def child_nodes
  [index, collection, statements]
end

#format(q) ⇒ Object



6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
# File 'lib/syntax_tree.rb', line 6163

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



6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
# File 'lib/syntax_tree.rb', line 6182

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



6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
# File 'lib/syntax_tree.rb', line 6199

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