Class: SyntaxTree::Until

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

Overview

Until represents an until loop.

until predicate
end

Instance Attribute Summary collapse

Attributes inherited from Node

#location

Instance Method Summary collapse

Methods inherited from Node

#construct_keys, #pretty_print, #to_json

Constructor Details

#initialize(predicate:, statements:, location:, comments: []) ⇒ Until

Returns a new instance of Until.



9275
9276
9277
9278
9279
9280
# File 'lib/syntax_tree/node.rb', line 9275

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

Instance Attribute Details

#comments ⇒ Object (readonly)

[Array[ Comment | EmbDoc ]] the comments attached to this node



9273
9274
9275
# File 'lib/syntax_tree/node.rb', line 9273

def comments
  @comments
end

#predicate ⇒ Object (readonly)

[untyped] the expression to be checked



9267
9268
9269
# File 'lib/syntax_tree/node.rb', line 9267

def predicate
  @predicate
end

#statements ⇒ Object (readonly)

[Statements] the expressions to be executed



9270
9271
9272
# File 'lib/syntax_tree/node.rb', line 9270

def statements
  @statements
end

Instance Method Details

#accept(visitor) ⇒ Object



9282
9283
9284
# File 'lib/syntax_tree/node.rb', line 9282

def accept(visitor)
  visitor.visit_until(self)
end

#child_nodes ⇒ Object Also known as: deconstruct



9286
9287
9288
# File 'lib/syntax_tree/node.rb', line 9286

def child_nodes
  [predicate, statements]
end

#deconstruct_keys(_keys) ⇒ Object



9292
9293
9294
9295
9296
9297
9298
9299
# File 'lib/syntax_tree/node.rb', line 9292

def deconstruct_keys(_keys)
  {
    predicate: predicate,
    statements: statements,
    location: location,
    comments: comments
  }
end

#format(q) ⇒ Object



9301
9302
9303
9304
9305
9306
9307
9308
9309
9310
9311
9312
9313
9314
# File 'lib/syntax_tree/node.rb', line 9301

def format(q)
  if statements.empty?
    keyword = "until "

    q.group do
      q.text(keyword)
      q.nest(keyword.length) { q.format(predicate) }
      q.breakable(force: true)
      q.text("end")
    end
  else
    LoopFormatter.new("until", self, statements).format(q)
  end
end