Class: SyntaxTree::WhileMod

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

Overview

WhileMod represents the modifier form of a while loop.

expression while predicate

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(statement:, predicate:, location:, comments: []) ⇒ WhileMod

Returns a new instance of WhileMod.



13154
13155
13156
13157
13158
13159
# File 'lib/syntax_tree.rb', line 13154

def initialize(statement:, predicate:, location:, comments: [])
  @statement = statement
  @predicate = predicate
  @location = location
  @comments = comments
end

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



13152
13153
13154
# File 'lib/syntax_tree.rb', line 13152

def comments
  @comments
end

#locationObject (readonly)

Location

the location of this node



13149
13150
13151
# File 'lib/syntax_tree.rb', line 13149

def location
  @location
end

#predicateObject (readonly)

untyped

the expression to be checked



13146
13147
13148
# File 'lib/syntax_tree.rb', line 13146

def predicate
  @predicate
end

#statementObject (readonly)

untyped

the expression to be executed



13143
13144
13145
# File 'lib/syntax_tree.rb', line 13143

def statement
  @statement
end

Instance Method Details

#child_nodesObject



13161
13162
13163
# File 'lib/syntax_tree.rb', line 13161

def child_nodes
  [statement, predicate]
end

#format(q) ⇒ Object



13165
13166
13167
13168
13169
13170
13171
13172
13173
13174
13175
13176
13177
13178
13179
13180
13181
13182
# File 'lib/syntax_tree.rb', line 13165

def format(q)
  # If we're in the modifier form and we're modifying a `begin`, then this
  # is a special case where we need to explicitly use the modifier form
  # because otherwise the semantic meaning changes. This looks like:
  #
  #     begin
  #       foo
  #     end while bar
  #
  # The above is effectively a `do...while` loop.
  if statement.is_a?(Begin)
    q.format(statement)
    q.text(" while ")
    q.format(predicate)
  else
    LoopFormatter.new("while", self, statement).format(q)
  end
end

#pretty_print(q) ⇒ Object



13184
13185
13186
13187
13188
13189
13190
13191
13192
13193
13194
13195
13196
# File 'lib/syntax_tree.rb', line 13184

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

    q.breakable
    q.pp(statement)

    q.breakable
    q.pp(predicate)

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

#to_json(*opts) ⇒ Object



13198
13199
13200
13201
13202
13203
13204
13205
13206
# File 'lib/syntax_tree.rb', line 13198

def to_json(*opts)
  {
    type: :while_mod,
    stmt: statement,
    pred: predicate,
    loc: location,
    cmts: comments
  }.to_json(*opts)
end