Class: SyntaxTree::ENDBlock

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

Overview

ENDBlock represents the use of the END keyword, which hooks into the lifecycle of the interpreter. Whatever is inside the block will get executed when the program ends.

END {
}

Interestingly, the END keyword doesn’t allow the do and end keywords for the block. Only braces are permitted.

Instance Attribute Summary collapse

Attributes inherited from Node

#location

Instance Method Summary collapse

Methods inherited from Node

#construct_keys, #end_char, #pretty_print, #start_char, #to_json, #to_mermaid

Constructor Details

#initialize(lbrace:, statements:, location:) ⇒ ENDBlock

Returns a new instance of ENDBlock.



320
321
322
323
324
325
# File 'lib/syntax_tree/node.rb', line 320

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

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



318
319
320
# File 'lib/syntax_tree/node.rb', line 318

def comments
  @comments
end

#lbraceObject (readonly)

LBrace

the left brace that is seen after the keyword



312
313
314
# File 'lib/syntax_tree/node.rb', line 312

def lbrace
  @lbrace
end

#statementsObject (readonly)

Statements

the expressions to be executed



315
316
317
# File 'lib/syntax_tree/node.rb', line 315

def statements
  @statements
end

Instance Method Details

#===(other) ⇒ Object



371
372
373
374
# File 'lib/syntax_tree/node.rb', line 371

def ===(other)
  other.is_a?(ENDBlock) && lbrace === other.lbrace &&
    statements === other.statements
end

#accept(visitor) ⇒ Object



327
328
329
# File 'lib/syntax_tree/node.rb', line 327

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

#child_nodesObject Also known as: deconstruct



331
332
333
# File 'lib/syntax_tree/node.rb', line 331

def child_nodes
  [lbrace, statements]
end

#copy(lbrace: nil, statements: nil, location: nil) ⇒ Object



335
336
337
338
339
340
341
342
343
344
345
# File 'lib/syntax_tree/node.rb', line 335

def copy(lbrace: nil, statements: nil, location: nil)
  node =
    ENDBlock.new(
      lbrace: lbrace || self.lbrace,
      statements: statements || self.statements,
      location: location || self.location
    )

  node.comments.concat(comments.map(&:copy))
  node
end

#deconstruct_keys(_keys) ⇒ Object



349
350
351
352
353
354
355
356
# File 'lib/syntax_tree/node.rb', line 349

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

#format(q) ⇒ Object



358
359
360
361
362
363
364
365
366
367
368
369
# File 'lib/syntax_tree/node.rb', line 358

def format(q)
  q.group do
    q.text("END ")
    q.format(lbrace)
    q.indent do
      q.breakable_space
      q.format(statements)
    end
    q.breakable_space
    q.text("}")
  end
end