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

Constructor Details

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

Returns a new instance of Until.



10379
10380
10381
10382
10383
10384
# File 'lib/syntax_tree/node.rb', line 10379

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



10377
10378
10379
# File 'lib/syntax_tree/node.rb', line 10377

def comments
  @comments
end

#predicateObject (readonly)

untyped

the expression to be checked



10371
10372
10373
# File 'lib/syntax_tree/node.rb', line 10371

def predicate
  @predicate
end

#statementsObject (readonly)

Statements

the expressions to be executed



10374
10375
10376
# File 'lib/syntax_tree/node.rb', line 10374

def statements
  @statements
end

Instance Method Details

#child_nodesObject Also known as: deconstruct



10386
10387
10388
# File 'lib/syntax_tree/node.rb', line 10386

def child_nodes
  [predicate, statements]
end

#deconstruct_keys(keys) ⇒ Object



10392
10393
10394
10395
10396
10397
10398
10399
# File 'lib/syntax_tree/node.rb', line 10392

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

#format(q) ⇒ Object



10401
10402
10403
10404
10405
10406
10407
10408
10409
10410
10411
10412
10413
10414
# File 'lib/syntax_tree/node.rb', line 10401

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

#pretty_print(q) ⇒ Object



10416
10417
10418
10419
10420
10421
10422
10423
10424
10425
10426
10427
10428
# File 'lib/syntax_tree/node.rb', line 10416

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

    q.breakable
    q.pp(predicate)

    q.breakable
    q.pp(statements)

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

#to_json(*opts) ⇒ Object



10430
10431
10432
10433
10434
10435
10436
10437
10438
# File 'lib/syntax_tree/node.rb', line 10430

def to_json(*opts)
  {
    type: :until,
    pred: predicate,
    stmts: statements,
    loc: location,
    cmts: comments
  }.to_json(*opts)
end