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.



13240
13241
13242
13243
13244
13245
# File 'lib/syntax_tree.rb', line 13240

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



13238
13239
13240
# File 'lib/syntax_tree.rb', line 13238

def comments
  @comments
end

#locationObject (readonly)

Location

the location of this node



13235
13236
13237
# File 'lib/syntax_tree.rb', line 13235

def location
  @location
end

#predicateObject (readonly)

untyped

the expression to be checked



13232
13233
13234
# File 'lib/syntax_tree.rb', line 13232

def predicate
  @predicate
end

#statementObject (readonly)

untyped

the expression to be executed



13229
13230
13231
# File 'lib/syntax_tree.rb', line 13229

def statement
  @statement
end

Instance Method Details

#child_nodesObject



13247
13248
13249
# File 'lib/syntax_tree.rb', line 13247

def child_nodes
  [statement, predicate]
end

#format(q) ⇒ Object



13251
13252
13253
13254
13255
13256
13257
13258
13259
13260
13261
13262
13263
13264
13265
13266
13267
13268
13269
13270
13271
13272
13273
# File 'lib/syntax_tree.rb', line 13251

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
  #
  # Also, if the statement of the modifier includes an assignment, then we
  # can't know for certain that it won't impact the predicate, so we need to
  # force it to stay as it is. This looks like:
  #
  #     foo = bar while foo
  #
  if statement.is_a?(Begin) || ContainsAssignment.call(statement)
    q.format(statement)
    q.text(" while ")
    q.format(predicate)
  else
    LoopFormatter.new("while", self, statement).format(q)
  end
end

#pretty_print(q) ⇒ Object



13275
13276
13277
13278
13279
13280
13281
13282
13283
13284
13285
13286
13287
# File 'lib/syntax_tree.rb', line 13275

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



13289
13290
13291
13292
13293
13294
13295
13296
13297
# File 'lib/syntax_tree.rb', line 13289

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