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:, comments: []) ⇒ ENDBlock

Returns a new instance of ENDBlock.



252
253
254
255
256
257
# File 'lib/syntax_tree/node.rb', line 252

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

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



250
251
252
# File 'lib/syntax_tree/node.rb', line 250

def comments
  @comments
end

#lbraceObject (readonly)

LBrace

the left brace that is seen after the keyword



244
245
246
# File 'lib/syntax_tree/node.rb', line 244

def lbrace
  @lbrace
end

#statementsObject (readonly)

Statements

the expressions to be executed



247
248
249
# File 'lib/syntax_tree/node.rb', line 247

def statements
  @statements
end

Instance Method Details

#accept(visitor) ⇒ Object



259
260
261
# File 'lib/syntax_tree/node.rb', line 259

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

#child_nodesObject Also known as: deconstruct



263
264
265
# File 'lib/syntax_tree/node.rb', line 263

def child_nodes
  [lbrace, statements]
end

#deconstruct_keys(_keys) ⇒ Object



269
270
271
272
273
274
275
276
# File 'lib/syntax_tree/node.rb', line 269

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

#format(q) ⇒ Object



278
279
280
281
282
283
284
285
286
287
288
289
# File 'lib/syntax_tree/node.rb', line 278

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