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, #pretty_print, #to_json

Constructor Details

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

Returns a new instance of ENDBlock.



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

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



308
309
310
# File 'lib/syntax_tree/node.rb', line 308

def comments
  @comments
end

#lbraceObject (readonly)

LBrace

the left brace that is seen after the keyword



302
303
304
# File 'lib/syntax_tree/node.rb', line 302

def lbrace
  @lbrace
end

#statementsObject (readonly)

Statements

the expressions to be executed



305
306
307
# File 'lib/syntax_tree/node.rb', line 305

def statements
  @statements
end

Instance Method Details

#===(other) ⇒ Object



361
362
363
364
# File 'lib/syntax_tree/node.rb', line 361

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

#accept(visitor) ⇒ Object



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

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

#child_nodesObject Also known as: deconstruct



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

def child_nodes
  [lbrace, statements]
end

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



325
326
327
328
329
330
331
332
333
334
335
# File 'lib/syntax_tree/node.rb', line 325

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



339
340
341
342
343
344
345
346
# File 'lib/syntax_tree/node.rb', line 339

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

#format(q) ⇒ Object



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

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