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.



9328
9329
9330
9331
9332
9333
# File 'lib/syntax_tree/node.rb', line 9328

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

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



9326
9327
9328
# File 'lib/syntax_tree/node.rb', line 9326

def comments
  @comments
end

#predicateObject (readonly)

untyped

the expression to be checked



9320
9321
9322
# File 'lib/syntax_tree/node.rb', line 9320

def predicate
  @predicate
end

#statementsObject (readonly)

Statements

the expressions to be executed



9323
9324
9325
# File 'lib/syntax_tree/node.rb', line 9323

def statements
  @statements
end

Instance Method Details

#accept(visitor) ⇒ Object



9335
9336
9337
# File 'lib/syntax_tree/node.rb', line 9335

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

#child_nodesObject Also known as: deconstruct



9339
9340
9341
# File 'lib/syntax_tree/node.rb', line 9339

def child_nodes
  [predicate, statements]
end

#deconstruct_keys(_keys) ⇒ Object



9345
9346
9347
9348
9349
9350
9351
9352
# File 'lib/syntax_tree/node.rb', line 9345

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

#format(q) ⇒ Object



9354
9355
9356
9357
9358
9359
9360
9361
9362
9363
9364
9365
9366
9367
# File 'lib/syntax_tree/node.rb', line 9354

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